2017-07-06 10:40:54 +00:00
|
|
|
package router
|
2016-02-24 18:55:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-02-25 16:05:15 +00:00
|
|
|
"fmt"
|
2016-03-29 09:37:35 +00:00
|
|
|
"math/rand"
|
2017-06-15 13:16:16 +00:00
|
|
|
"net"
|
2016-02-24 18:55:04 +00:00
|
|
|
"testing"
|
2016-02-25 16:05:15 +00:00
|
|
|
"time"
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-10-15 15:42:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-06 08:42:09 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-15 15:42:40 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2016-02-24 18:55:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
localLogBuffer *bytes.Buffer
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
localLogBuffer = new(bytes.Buffer)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func GetBufferedLogger() hclog.Logger {
|
|
|
|
localLogBuffer = new(bytes.Buffer)
|
|
|
|
return hclog.New(&hclog.LoggerOptions{
|
|
|
|
Level: 0,
|
|
|
|
Output: localLogBuffer,
|
|
|
|
})
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 02:28:13 +00:00
|
|
|
type fauxConnPool struct {
|
2016-03-29 09:37:35 +00:00
|
|
|
// failPct between 0.0 and 1.0 == pct of time a Ping should fail
|
|
|
|
failPct float64
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 07:48:34 +00:00
|
|
|
func (cp *fauxConnPool) Ping(string, string, net.Addr) (bool, error) {
|
2016-03-29 09:37:35 +00:00
|
|
|
var success bool
|
|
|
|
successProb := rand.Float64()
|
|
|
|
if successProb > cp.failPct {
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
return success, nil
|
2016-03-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2016-02-24 23:04:04 +00:00
|
|
|
type fauxSerf struct {
|
2016-02-25 16:05:15 +00:00
|
|
|
numNodes int
|
2016-02-24 23:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fauxSerf) NumNodes() int {
|
2016-02-25 16:05:15 +00:00
|
|
|
return s.numNodes
|
2016-02-24 23:04:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
func testManager() (m *Manager) {
|
2016-02-24 18:55:04 +00:00
|
|
|
logger := GetBufferedLogger()
|
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m = New(logger, shutdownCh, &fauxSerf{numNodes: 16384}, &fauxConnPool{}, "", noopRebalancer)
|
2016-03-29 22:58:15 +00:00
|
|
|
return m
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:16:44 +00:00
|
|
|
func noopRebalancer() {}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
func testManagerFailProb(failPct float64) (m *Manager) {
|
2016-03-29 09:37:35 +00:00
|
|
|
logger := GetBufferedLogger()
|
|
|
|
shutdownCh := make(chan struct{})
|
2020-09-14 20:16:44 +00:00
|
|
|
m = New(logger, shutdownCh, &fauxSerf{}, &fauxConnPool{failPct: failPct}, "", noopRebalancer)
|
2016-03-29 22:58:15 +00:00
|
|
|
return m
|
2016-03-29 09:37:35 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
// func (l *serverList) cycleServer() (servers []*metadata.Server) {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestManagerInternal_cycleServer(t *testing.T) {
|
|
|
|
m := testManager()
|
2016-03-29 23:17:16 +00:00
|
|
|
l := m.getServerList()
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
server0 := &metadata.Server{Name: "server1"}
|
|
|
|
server1 := &metadata.Server{Name: "server2"}
|
|
|
|
server2 := &metadata.Server{Name: "server3"}
|
2016-03-29 23:17:16 +00:00
|
|
|
l.servers = append(l.servers, server0, server1, server2)
|
|
|
|
m.saveServerList(l)
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
l = m.getServerList()
|
|
|
|
if len(l.servers) != 3 {
|
|
|
|
t.Fatalf("server length incorrect: %d/3", len(l.servers))
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
2016-03-29 23:17:16 +00:00
|
|
|
if l.servers[0] != server0 &&
|
|
|
|
l.servers[1] != server1 &&
|
|
|
|
l.servers[2] != server2 {
|
2016-02-24 18:55:04 +00:00
|
|
|
t.Fatalf("initial server ordering not correct")
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
l.servers = l.cycleServer()
|
|
|
|
if len(l.servers) != 3 {
|
|
|
|
t.Fatalf("server length incorrect: %d/3", len(l.servers))
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
2016-03-29 23:17:16 +00:00
|
|
|
if l.servers[0] != server1 &&
|
|
|
|
l.servers[1] != server2 &&
|
|
|
|
l.servers[2] != server0 {
|
2016-02-24 18:55:04 +00:00
|
|
|
t.Fatalf("server ordering after one cycle not correct")
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
l.servers = l.cycleServer()
|
|
|
|
if len(l.servers) != 3 {
|
|
|
|
t.Fatalf("server length incorrect: %d/3", len(l.servers))
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
2016-03-29 23:17:16 +00:00
|
|
|
if l.servers[0] != server2 &&
|
|
|
|
l.servers[1] != server0 &&
|
|
|
|
l.servers[2] != server1 {
|
2016-02-24 18:55:04 +00:00
|
|
|
t.Fatalf("server ordering after two cycles not correct")
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
l.servers = l.cycleServer()
|
|
|
|
if len(l.servers) != 3 {
|
|
|
|
t.Fatalf("server length incorrect: %d/3", len(l.servers))
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
2016-03-29 23:17:16 +00:00
|
|
|
if l.servers[0] != server0 &&
|
|
|
|
l.servers[1] != server1 &&
|
|
|
|
l.servers[2] != server2 {
|
2016-02-24 18:55:04 +00:00
|
|
|
t.Fatalf("server ordering after three cycles not correct")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
// func (m *Manager) getServerList() serverList {
|
|
|
|
func TestManagerInternal_getServerList(t *testing.T) {
|
2016-03-29 22:58:15 +00:00
|
|
|
m := testManager()
|
2016-03-29 23:17:16 +00:00
|
|
|
l := m.getServerList()
|
|
|
|
if l.servers == nil {
|
|
|
|
t.Fatalf("serverList.servers nil")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
if len(l.servers) != 0 {
|
|
|
|
t.Fatalf("serverList.servers length not zero")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestManagerInternal_New(t *testing.T) {
|
|
|
|
m := testManager()
|
|
|
|
if m == nil {
|
|
|
|
t.Fatalf("Manager nil")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.clusterInfo == nil {
|
|
|
|
t.Fatalf("Manager.clusterInfo nil")
|
2016-02-25 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.logger == nil {
|
|
|
|
t.Fatalf("Manager.logger nil")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
if m.shutdownCh == nil {
|
|
|
|
t.Fatalf("Manager.shutdownCh nil")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
// func (m *Manager) reconcileServerList(l *serverList) bool {
|
2016-03-29 22:58:15 +00:00
|
|
|
func TestManagerInternal_reconcileServerList(t *testing.T) {
|
2016-03-29 09:37:35 +00:00
|
|
|
tests := []int{0, 1, 2, 3, 4, 5, 10, 100}
|
|
|
|
for _, n := range tests {
|
|
|
|
ok, err := test_reconcileServerList(n)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Expected %d to pass: %v", n, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_reconcileServerList(maxServers int) (bool, error) {
|
|
|
|
// Build a server list, reconcile, verify the missing servers are
|
|
|
|
// missing, the added have been added, and the original server is
|
|
|
|
// present.
|
|
|
|
const failPct = 0.5
|
2016-03-29 22:58:15 +00:00
|
|
|
m := testManagerFailProb(failPct)
|
2016-03-29 09:37:35 +00:00
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
var failedServers, healthyServers []*metadata.Server
|
2016-03-29 09:37:35 +00:00
|
|
|
for i := 0; i < maxServers; i++ {
|
|
|
|
nodeName := fmt.Sprintf("s%02d", i)
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
node := &metadata.Server{Name: nodeName}
|
2016-03-29 22:58:15 +00:00
|
|
|
// Add 66% of servers to Manager
|
2016-03-29 09:37:35 +00:00
|
|
|
if rand.Float64() > 0.33 {
|
2016-03-29 22:58:15 +00:00
|
|
|
m.AddServer(node)
|
2016-03-29 09:37:35 +00:00
|
|
|
|
|
|
|
// Of healthy servers, (ab)use connPoolPinger to
|
|
|
|
// failPct of the servers for the reconcile. This
|
|
|
|
// allows for the selected server to no longer be
|
|
|
|
// healthy for the reconcile below.
|
2020-05-28 07:48:34 +00:00
|
|
|
if ok, _ := m.connPoolPinger.Ping(node.Datacenter, node.ShortName, node.Addr); ok {
|
2016-03-29 09:37:35 +00:00
|
|
|
// Will still be present
|
|
|
|
healthyServers = append(healthyServers, node)
|
|
|
|
} else {
|
|
|
|
// Will be missing
|
|
|
|
failedServers = append(failedServers, node)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Will be added from the call to reconcile
|
|
|
|
healthyServers = append(healthyServers, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// Randomize Manager's server list
|
|
|
|
m.RebalanceServers()
|
|
|
|
selectedServer := m.FindServer()
|
2016-03-29 09:37:35 +00:00
|
|
|
|
|
|
|
var selectedServerFailed bool
|
|
|
|
for _, s := range failedServers {
|
|
|
|
if selectedServer.Key().Equal(s.Key()) {
|
|
|
|
selectedServerFailed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// Update Manager's server list to be "healthy" based on Serf.
|
2016-03-29 09:37:35 +00:00
|
|
|
// Reconcile this with origServers, which is shuffled and has a live
|
|
|
|
// connection, but possibly out of date.
|
2016-03-29 23:17:16 +00:00
|
|
|
origServers := m.getServerList()
|
|
|
|
m.saveServerList(serverList{servers: healthyServers})
|
2016-03-29 09:37:35 +00:00
|
|
|
|
|
|
|
// This should always succeed with non-zero server lists
|
2016-03-29 22:58:15 +00:00
|
|
|
if !selectedServerFailed && !m.reconcileServerList(&origServers) &&
|
2016-03-29 23:17:16 +00:00
|
|
|
len(m.getServerList().servers) != 0 &&
|
2016-03-29 09:37:35 +00:00
|
|
|
len(origServers.servers) != 0 {
|
|
|
|
// If the random gods are unfavorable and we end up with zero
|
|
|
|
// length lists, expect things to fail and retry the test.
|
|
|
|
return false, fmt.Errorf("Expected reconcile to succeed: %v %d %d",
|
|
|
|
selectedServerFailed,
|
2016-03-29 23:17:16 +00:00
|
|
|
len(m.getServerList().servers),
|
2016-03-29 09:37:35 +00:00
|
|
|
len(origServers.servers))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have zero-length server lists, test succeeded in degenerate
|
|
|
|
// case.
|
2016-03-29 23:17:16 +00:00
|
|
|
if len(m.getServerList().servers) == 0 &&
|
2016-03-29 09:37:35 +00:00
|
|
|
len(origServers.servers) == 0 {
|
|
|
|
// Failed as expected w/ zero length list
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
resultingServerMap := make(map[metadata.Key]bool)
|
2016-03-29 23:17:16 +00:00
|
|
|
for _, s := range m.getServerList().servers {
|
2016-03-29 09:37:35 +00:00
|
|
|
resultingServerMap[*s.Key()] = true
|
|
|
|
}
|
|
|
|
|
2016-03-29 22:58:15 +00:00
|
|
|
// Test to make sure no failed servers are in the Manager's
|
2016-03-29 23:17:16 +00:00
|
|
|
// list. Error if there are any failedServers in l.servers
|
2016-03-29 09:37:35 +00:00
|
|
|
for _, s := range failedServers {
|
|
|
|
_, ok := resultingServerMap[*s.Key()]
|
|
|
|
if ok {
|
|
|
|
return false, fmt.Errorf("Found failed server %v in merged list %v", s, resultingServerMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test to make sure all healthy servers are in the healthy list.
|
2016-03-29 23:17:16 +00:00
|
|
|
if len(healthyServers) != len(m.getServerList().servers) {
|
2016-03-29 09:37:35 +00:00
|
|
|
return false, fmt.Errorf("Expected healthy map and servers to match: %d/%d", len(healthyServers), len(healthyServers))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test to make sure all healthy servers are in the resultingServerMap list.
|
|
|
|
for _, s := range healthyServers {
|
|
|
|
_, ok := resultingServerMap[*s.Key()]
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("Server %v missing from healthy map after merged lists", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:51:57 +00:00
|
|
|
func TestRebalanceDelayer(t *testing.T) {
|
2020-10-15 15:42:40 +00:00
|
|
|
type testCase struct {
|
|
|
|
servers int
|
2020-10-15 16:04:11 +00:00
|
|
|
nodes int
|
2020-10-15 15:42:40 +00:00
|
|
|
expected time.Duration
|
2016-02-25 16:05:15 +00:00
|
|
|
}
|
2020-10-15 15:42:40 +00:00
|
|
|
|
|
|
|
testCases := []testCase{
|
2020-10-15 16:04:11 +00:00
|
|
|
{servers: 0, nodes: 1},
|
|
|
|
{servers: 0, nodes: 100},
|
|
|
|
{servers: 0, nodes: 65535},
|
|
|
|
{servers: 0, nodes: 1000000},
|
|
|
|
|
|
|
|
{servers: 1, nodes: 100},
|
|
|
|
{servers: 1, nodes: 1024},
|
|
|
|
{servers: 1, nodes: 8192},
|
|
|
|
{servers: 1, nodes: 11520},
|
2020-10-15 18:43:29 +00:00
|
|
|
{servers: 1, nodes: 11521, expected: 3*time.Minute + 15625*time.Microsecond},
|
2020-10-15 16:04:11 +00:00
|
|
|
{servers: 1, nodes: 16384, expected: 4*time.Minute + 16*time.Second},
|
|
|
|
{servers: 1, nodes: 65535, expected: 17*time.Minute + 3984375000},
|
|
|
|
{servers: 1, nodes: 1000000, expected: 4*time.Hour + 20*time.Minute + 25*time.Second},
|
|
|
|
|
|
|
|
{servers: 2, nodes: 100},
|
|
|
|
{servers: 2, nodes: 16384},
|
2020-10-15 18:43:29 +00:00
|
|
|
{servers: 2, nodes: 23040},
|
|
|
|
{servers: 2, nodes: 23041, expected: 3*time.Minute + 7812500},
|
2020-10-15 16:04:11 +00:00
|
|
|
{servers: 2, nodes: 65535, expected: 8*time.Minute + 31992187500},
|
|
|
|
{servers: 2, nodes: 1000000, expected: 2*time.Hour + 10*time.Minute + 12500*time.Millisecond},
|
|
|
|
|
|
|
|
{servers: 3, nodes: 0},
|
|
|
|
{servers: 3, nodes: 100},
|
|
|
|
{servers: 3, nodes: 1024},
|
|
|
|
{servers: 3, nodes: 16384},
|
2020-10-15 18:43:29 +00:00
|
|
|
{servers: 3, nodes: 34560},
|
|
|
|
{servers: 3, nodes: 34561, expected: 3*time.Minute + 5208333},
|
2020-10-15 16:04:11 +00:00
|
|
|
{servers: 3, nodes: 65535, expected: 5*time.Minute + 41328125000},
|
|
|
|
{servers: 3, nodes: 1000000, expected: 86*time.Minute + 48333333333},
|
|
|
|
|
|
|
|
{servers: 5, nodes: 0},
|
|
|
|
{servers: 5, nodes: 1024},
|
|
|
|
{servers: 5, nodes: 16384},
|
|
|
|
{servers: 5, nodes: 32768},
|
2020-10-15 18:43:29 +00:00
|
|
|
{servers: 5, nodes: 57600},
|
2020-10-15 16:04:11 +00:00
|
|
|
{servers: 5, nodes: 65535, expected: 3*time.Minute + 24796875000},
|
|
|
|
{servers: 5, nodes: 1000000, expected: 52*time.Minute + 5*time.Second},
|
|
|
|
|
|
|
|
{servers: 7, nodes: 65535},
|
|
|
|
{servers: 7, nodes: 80500},
|
|
|
|
{servers: 7, nodes: 131070, expected: 4*time.Minute + 52566964285},
|
|
|
|
|
|
|
|
{servers: 11, nodes: 1000000, expected: 23*time.Minute + 40454545454},
|
|
|
|
{servers: 19, nodes: 1000000, expected: 13*time.Minute + 42368421052},
|
2016-02-25 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:42:40 +00:00
|
|
|
for _, tc := range testCases {
|
2020-10-15 17:51:57 +00:00
|
|
|
delay := delayer.Delay(tc.servers, tc.nodes)
|
2016-03-27 07:04:52 +00:00
|
|
|
|
2020-10-15 15:42:40 +00:00
|
|
|
if tc.expected != 0 {
|
2020-10-15 16:04:11 +00:00
|
|
|
assert.Equal(t, tc.expected, delay, "nodes=%d, servers=%d", tc.nodes, tc.servers)
|
2020-10-15 15:42:40 +00:00
|
|
|
continue
|
2016-02-25 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:04:11 +00:00
|
|
|
min := 2 * time.Minute
|
|
|
|
max := 3 * time.Minute
|
|
|
|
if delay < min {
|
|
|
|
t.Errorf("nodes=%d, servers=%d, expected >%v, actual %v", tc.nodes, tc.servers, min, delay)
|
2020-10-15 15:42:40 +00:00
|
|
|
}
|
2020-10-15 16:04:11 +00:00
|
|
|
if delay > max {
|
|
|
|
t.Errorf("nodes=%d, servers=%d, expected <%v, actual %v", tc.nodes, tc.servers, max, delay)
|
2016-02-25 16:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
// func (m *Manager) saveServerList(l serverList) {
|
|
|
|
func TestManagerInternal_saveServerList(t *testing.T) {
|
2016-03-29 22:58:15 +00:00
|
|
|
m := testManager()
|
2016-02-24 18:55:04 +00:00
|
|
|
|
|
|
|
// Initial condition
|
|
|
|
func() {
|
2016-03-29 23:17:16 +00:00
|
|
|
l := m.getServerList()
|
|
|
|
if len(l.servers) != 0 {
|
|
|
|
t.Fatalf("Manager.saveServerList failed to load init config")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
newServer := new(metadata.Server)
|
2016-03-29 23:17:16 +00:00
|
|
|
l.servers = append(l.servers, newServer)
|
|
|
|
m.saveServerList(l)
|
2016-02-24 18:55:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Test that save works
|
|
|
|
func() {
|
2016-03-29 23:17:16 +00:00
|
|
|
l1 := m.getServerList()
|
|
|
|
t1NumServers := len(l1.servers)
|
2016-02-24 18:55:04 +00:00
|
|
|
if t1NumServers != 1 {
|
2016-03-29 23:17:16 +00:00
|
|
|
t.Fatalf("Manager.saveServerList failed to save mutated config")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Verify mutation w/o a save doesn't alter the original
|
|
|
|
func() {
|
2017-07-06 10:48:37 +00:00
|
|
|
newServer := new(metadata.Server)
|
2016-03-29 23:17:16 +00:00
|
|
|
l := m.getServerList()
|
|
|
|
l.servers = append(l.servers, newServer)
|
2016-02-24 18:55:04 +00:00
|
|
|
|
2016-03-29 23:17:16 +00:00
|
|
|
l_orig := m.getServerList()
|
|
|
|
origNumServers := len(l_orig.servers)
|
|
|
|
if origNumServers >= len(l.servers) {
|
|
|
|
t.Fatalf("Manager.saveServerList unsaved config overwrote original")
|
2016-02-24 18:55:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-08-06 08:42:09 +00:00
|
|
|
|
|
|
|
func TestManager_healthyServer(t *testing.T) {
|
|
|
|
t.Run("checking itself", func(t *testing.T) {
|
|
|
|
m := testManager()
|
|
|
|
m.serverName = "s1"
|
|
|
|
server := metadata.Server{Name: m.serverName}
|
|
|
|
require.True(t, m.healthyServer(&server))
|
|
|
|
})
|
|
|
|
t.Run("checking another server with successful ping", func(t *testing.T) {
|
|
|
|
m := testManager()
|
|
|
|
server := metadata.Server{Name: "s1"}
|
|
|
|
require.True(t, m.healthyServer(&server))
|
|
|
|
})
|
|
|
|
t.Run("checking another server with failed ping", func(t *testing.T) {
|
|
|
|
m := testManagerFailProb(1)
|
|
|
|
server := metadata.Server{Name: "s1"}
|
|
|
|
require.False(t, m.healthyServer(&server))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManager_Rebalance(t *testing.T) {
|
|
|
|
t.Run("single server cluster checking itself", func(t *testing.T) {
|
|
|
|
m := testManager()
|
|
|
|
m.serverName = "s1"
|
|
|
|
m.AddServer(&metadata.Server{Name: m.serverName})
|
|
|
|
m.RebalanceServers()
|
|
|
|
require.False(t, m.IsOffline())
|
|
|
|
})
|
|
|
|
t.Run("multi server cluster is unhealthy when pings always fail", func(t *testing.T) {
|
|
|
|
m := testManagerFailProb(1)
|
|
|
|
m.AddServer(&metadata.Server{Name: "s1"})
|
|
|
|
m.AddServer(&metadata.Server{Name: "s2"})
|
|
|
|
m.AddServer(&metadata.Server{Name: "s3"})
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
m.RebalanceServers()
|
|
|
|
require.True(t, m.IsOffline())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("multi server cluster checking itself remains healthy despite pings always fail", func(t *testing.T) {
|
|
|
|
m := testManagerFailProb(1)
|
|
|
|
m.serverName = "s1"
|
|
|
|
m.AddServer(&metadata.Server{Name: m.serverName})
|
|
|
|
m.AddServer(&metadata.Server{Name: "s2"})
|
|
|
|
m.AddServer(&metadata.Server{Name: "s3"})
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
m.RebalanceServers()
|
|
|
|
require.False(t, m.IsOffline())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|