2018-01-09 23:26:53 +00:00
|
|
|
package servers_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/servers"
|
2018-04-04 00:27:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-04-05 17:40:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-09 23:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fauxAddr struct {
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fa *fauxAddr) String() string { return fa.Addr }
|
|
|
|
func (fa *fauxAddr) Network() string { return fa.Addr }
|
|
|
|
|
|
|
|
type fauxConnPool struct {
|
|
|
|
// failPct between 0.0 and 1.0 == pct of time a Ping should fail
|
|
|
|
failPct float64
|
|
|
|
}
|
|
|
|
|
2018-01-25 02:00:21 +00:00
|
|
|
func (cp *fauxConnPool) Ping(net.Addr) error {
|
2018-01-09 23:26:53 +00:00
|
|
|
successProb := rand.Float64()
|
|
|
|
if successProb > cp.failPct {
|
2018-01-25 02:00:21 +00:00
|
|
|
return nil
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
2018-01-25 02:00:21 +00:00
|
|
|
return fmt.Errorf("bad server")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
func testManager(t *testing.T) (m *servers.Manager) {
|
|
|
|
logger := testlog.Logger(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
m = servers.New(logger, shutdownCh, &fauxConnPool{})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
func testManagerFailProb(t *testing.T, failPct float64) (m *servers.Manager) {
|
|
|
|
logger := testlog.Logger(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
m = servers.New(logger, shutdownCh, &fauxConnPool{failPct: failPct})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_SetServers(t *testing.T) {
|
2018-04-05 17:40:17 +00:00
|
|
|
require := require.New(t)
|
2018-04-04 00:27:34 +00:00
|
|
|
m := testManager(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
var num int
|
|
|
|
num = m.NumServers()
|
|
|
|
if num != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := &servers.Server{Addr: &fauxAddr{"server1"}}
|
|
|
|
s2 := &servers.Server{Addr: &fauxAddr{"server2"}}
|
2018-04-05 17:40:17 +00:00
|
|
|
require.True(m.SetServers([]*servers.Server{s1, s2}))
|
|
|
|
require.False(m.SetServers([]*servers.Server{s1, s2}))
|
|
|
|
require.False(m.SetServers([]*servers.Server{s2, s1}))
|
|
|
|
require.Equal(2, m.NumServers())
|
|
|
|
require.Len(m.GetServers(), 2)
|
|
|
|
|
|
|
|
require.True(m.SetServers([]*servers.Server{s1}))
|
|
|
|
require.Equal(1, m.NumServers())
|
|
|
|
require.Len(m.GetServers(), 1)
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_FindServer(t *testing.T) {
|
2018-04-04 00:27:34 +00:00
|
|
|
m := testManager(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
|
|
|
|
if m.FindServer() != nil {
|
|
|
|
t.Fatalf("Expected nil return")
|
|
|
|
}
|
|
|
|
|
2018-01-25 02:00:21 +00:00
|
|
|
var srvs []*servers.Server
|
|
|
|
srvs = append(srvs, &servers.Server{Addr: &fauxAddr{"s1"}})
|
|
|
|
m.SetServers(srvs)
|
2018-01-09 23:26:53 +00:00
|
|
|
if m.NumServers() != 1 {
|
|
|
|
t.Fatalf("Expected one server")
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := m.FindServer()
|
|
|
|
if s1 == nil {
|
|
|
|
t.Fatalf("Expected non-nil server")
|
|
|
|
}
|
|
|
|
if s1.String() != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server")
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 = m.FindServer()
|
|
|
|
if s1 == nil || s1.String() != "s1" {
|
|
|
|
t.Fatalf("Expected s1 server (still)")
|
|
|
|
}
|
|
|
|
|
2018-01-25 02:00:21 +00:00
|
|
|
srvs = append(srvs, &servers.Server{Addr: &fauxAddr{"s2"}})
|
|
|
|
m.SetServers(srvs)
|
2018-01-09 23:26:53 +00:00
|
|
|
if m.NumServers() != 2 {
|
|
|
|
t.Fatalf("Expected two servers")
|
|
|
|
}
|
|
|
|
s1 = m.FindServer()
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
for _, srv := range srvs {
|
|
|
|
m.NotifyFailedServer(srv)
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
s2 := m.FindServer()
|
|
|
|
if s1.Equal(s2) {
|
|
|
|
t.Fatalf("Expected different server")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_New(t *testing.T) {
|
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
m := servers.New(logger, shutdownCh, &fauxConnPool{})
|
|
|
|
if m == nil {
|
|
|
|
t.Fatalf("Manager nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_NotifyFailedServer(t *testing.T) {
|
2018-04-04 00:27:34 +00:00
|
|
|
m := testManager(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
|
|
|
|
if m.NumServers() != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
s1 := &servers.Server{Addr: &fauxAddr{"s1"}}
|
|
|
|
s2 := &servers.Server{Addr: &fauxAddr{"s2"}}
|
|
|
|
|
|
|
|
// Try notifying for a server that is not managed by Manager
|
|
|
|
m.NotifyFailedServer(s1)
|
|
|
|
if m.NumServers() != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
2018-01-25 02:00:21 +00:00
|
|
|
m.SetServers([]*servers.Server{s1})
|
2018-01-09 23:26:53 +00:00
|
|
|
|
|
|
|
// Test again w/ a server not in the list
|
|
|
|
m.NotifyFailedServer(s2)
|
|
|
|
if m.NumServers() != 1 {
|
|
|
|
t.Fatalf("Expected one server")
|
|
|
|
}
|
|
|
|
|
2018-01-25 02:00:21 +00:00
|
|
|
m.SetServers([]*servers.Server{s1, s2})
|
2018-01-09 23:26:53 +00:00
|
|
|
if m.NumServers() != 2 {
|
|
|
|
t.Fatalf("Expected two servers")
|
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
// Grab a server
|
|
|
|
first := m.FindServer()
|
|
|
|
|
|
|
|
// Find the other server
|
|
|
|
second := s1
|
|
|
|
if first.Equal(s1) {
|
|
|
|
second = s2
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
// Fail the other server
|
|
|
|
m.NotifyFailedServer(second)
|
|
|
|
next := m.FindServer()
|
|
|
|
if !next.Equal(first) {
|
|
|
|
t.Fatalf("Expected first server (still)")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
// Fail the first
|
|
|
|
m.NotifyFailedServer(first)
|
|
|
|
next = m.FindServer()
|
|
|
|
if !next.Equal(second) {
|
|
|
|
t.Fatalf("Expected second server")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 00:27:34 +00:00
|
|
|
// Fail the second
|
|
|
|
m.NotifyFailedServer(second)
|
|
|
|
next = m.FindServer()
|
|
|
|
if !next.Equal(first) {
|
|
|
|
t.Fatalf("Expected first server")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_NumServers(t *testing.T) {
|
2018-04-04 00:27:34 +00:00
|
|
|
m := testManager(t)
|
2018-01-09 23:26:53 +00:00
|
|
|
var num int
|
|
|
|
num = m.NumServers()
|
|
|
|
if num != 0 {
|
|
|
|
t.Fatalf("Expected zero servers to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &servers.Server{Addr: &fauxAddr{"server1"}}
|
2018-01-25 02:00:21 +00:00
|
|
|
m.SetServers([]*servers.Server{s})
|
2018-01-09 23:26:53 +00:00
|
|
|
num = m.NumServers()
|
|
|
|
if num != 1 {
|
2018-01-25 02:00:21 +00:00
|
|
|
t.Fatalf("Expected one server after SetServers")
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServers_RebalanceServers(t *testing.T) {
|
|
|
|
const failPct = 0.5
|
2018-04-04 00:27:34 +00:00
|
|
|
m := testManagerFailProb(t, failPct)
|
2018-01-09 23:26:53 +00:00
|
|
|
const maxServers = 100
|
|
|
|
const numShuffleTests = 100
|
|
|
|
const uniquePassRate = 0.5
|
|
|
|
|
|
|
|
// Make a huge list of nodes.
|
2018-01-25 02:00:21 +00:00
|
|
|
var srvs []*servers.Server
|
2018-01-09 23:26:53 +00:00
|
|
|
for i := 0; i < maxServers; i++ {
|
|
|
|
nodeName := fmt.Sprintf("s%02d", i)
|
2018-01-25 02:00:21 +00:00
|
|
|
srvs = append(srvs, &servers.Server{Addr: &fauxAddr{nodeName}})
|
2018-01-09 23:26:53 +00:00
|
|
|
}
|
2018-01-25 02:00:21 +00:00
|
|
|
m.SetServers(srvs)
|
2018-01-09 23:26:53 +00:00
|
|
|
|
|
|
|
// Keep track of how many unique shuffles we get.
|
|
|
|
uniques := make(map[string]struct{}, maxServers)
|
|
|
|
for i := 0; i < numShuffleTests; i++ {
|
|
|
|
m.RebalanceServers()
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
for j := 0; j < maxServers; j++ {
|
|
|
|
server := m.FindServer()
|
|
|
|
m.NotifyFailedServer(server)
|
|
|
|
names = append(names, server.String())
|
|
|
|
}
|
|
|
|
key := strings.Join(names, "|")
|
|
|
|
uniques[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to allow for the fact that there won't always be a unique
|
|
|
|
// shuffle each pass, so we just look for smell here without the test
|
|
|
|
// being flaky.
|
|
|
|
if len(uniques) < int(maxServers*uniquePassRate) {
|
|
|
|
t.Fatalf("unique shuffle ratio too low: %d/%d", len(uniques), maxServers)
|
|
|
|
}
|
|
|
|
}
|