2017-12-18 21:16:23 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-03-15 12:42:43 +00:00
|
|
|
"fmt"
|
2017-12-18 21:16:23 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/raft"
|
2022-09-01 18:27:10 +00:00
|
|
|
autopilot "github.com/hashicorp/raft-autopilot"
|
2017-12-18 21:16:23 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2022-09-07 13:35:01 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-09-01 18:27:10 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-12-18 21:16:23 +00:00
|
|
|
)
|
|
|
|
|
2022-09-01 18:27:10 +00:00
|
|
|
var _ autopilot.ApplicationIntegration = (*AutopilotDelegate)(nil)
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// wantPeers determines whether the server has the given
|
|
|
|
// number of voting raft peers.
|
|
|
|
func wantPeers(s *Server, peers int) error {
|
|
|
|
future := s.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-01 18:27:10 +00:00
|
|
|
var n int
|
|
|
|
for _, server := range future.Configuration().Servers {
|
|
|
|
if server.Suffrage == raft.Voter {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
if got, want := n, peers; got != want {
|
2021-08-18 18:29:00 +00:00
|
|
|
return fmt.Errorf("server %v: got %d peers want %d\n\tservers: %#+v", s.config.NodeName, got, want, future.Configuration().Servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutopilot_CleanupDeadServer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
conf := func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2017-12-18 21:16:23 +00:00
|
|
|
c.BootstrapExpect = 3
|
2022-09-07 13:35:01 +00:00
|
|
|
c.RaftConfig.ProtocolVersion = raft.ProtocolVersion(3)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
|
|
defer cleanupS1()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
|
|
defer cleanupS2()
|
|
|
|
|
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
|
|
defer cleanupS3()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
servers := []*Server{s1, s2, s3}
|
2021-09-28 23:38:56 +00:00
|
|
|
TestJoin(t, servers...)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for initial stable cluster")
|
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, conf)
|
|
|
|
defer cleanupS4()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Kill a non-leader server
|
2021-09-28 23:38:56 +00:00
|
|
|
killedIdx := 0
|
|
|
|
for i, s := range servers {
|
|
|
|
if !s.IsLeader() {
|
|
|
|
killedIdx = i
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("killing a server (index %d)", killedIdx)
|
2021-09-28 23:38:56 +00:00
|
|
|
s.Shutdown()
|
|
|
|
break
|
|
|
|
}
|
2021-08-18 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for server loss to be detected")
|
|
|
|
testutil.WaitForResultUntil(10*time.Second, func() (bool, error) {
|
2021-09-28 23:38:56 +00:00
|
|
|
for i, s := range servers {
|
|
|
|
alive := 0
|
|
|
|
if i == killedIdx {
|
|
|
|
// Skip shutdown server
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, m := range s.Members() {
|
|
|
|
if m.Status == serf.StatusAlive {
|
|
|
|
alive++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if alive != 2 {
|
2022-09-07 13:35:01 +00:00
|
|
|
return false, fmt.Errorf("expected 2 alive servers but found %v", alive)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 13:35:01 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) { must.NoError(t, err) })
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Join the new server
|
2021-09-28 23:38:56 +00:00
|
|
|
servers[killedIdx] = s4
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("adding server s4")
|
2021-09-28 23:38:56 +00:00
|
|
|
TestJoin(t, servers...)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for dead server to be removed")
|
2021-08-18 18:29:00 +00:00
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutopilot_CleanupDeadServerPeriodic(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
conf := func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 5
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
|
|
defer cleanupS1()
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
|
|
defer cleanupS2()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
|
|
defer cleanupS3()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, conf)
|
|
|
|
defer cleanupS4()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s5, cleanupS5 := TestServer(t, conf)
|
|
|
|
defer cleanupS5()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
servers := []*Server{s1, s2, s3, s4, s5}
|
2021-06-08 17:47:04 +00:00
|
|
|
TestJoin(t, servers...)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for initial stable cluster")
|
|
|
|
waitForStableLeadership(t, servers)
|
|
|
|
|
|
|
|
t.Logf("killing a non-leader server")
|
2021-08-18 18:29:00 +00:00
|
|
|
if leader := waitForStableLeadership(t, servers); leader == s4 {
|
|
|
|
s1, s4 = s4, s1
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
s4.Shutdown()
|
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for dead peer to be removed")
|
2017-12-18 21:16:23 +00:00
|
|
|
servers = []*Server{s1, s2, s3, s5}
|
2022-09-07 13:35:01 +00:00
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutopilot_RollingUpdate(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
conf := func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 3
|
2017-12-18 21:16:23 +00:00
|
|
|
c.RaftConfig.ProtocolVersion = 3
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
|
|
defer cleanupS1()
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
|
|
defer cleanupS2()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
|
|
defer cleanupS3()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
servers := []*Server{s1, s2, s3}
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
2022-09-07 13:35:01 +00:00
|
|
|
|
|
|
|
t.Logf("waiting for initial stable cluster")
|
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Add one more server like we are doing a rolling update.
|
2020-03-30 01:08:54 +00:00
|
|
|
t.Logf("adding server s4")
|
2019-12-04 00:15:11 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, conf)
|
|
|
|
defer cleanupS4()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s4)
|
2020-03-30 01:08:54 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
// Wait for s4 to stabilize and get promoted to a voter
|
|
|
|
t.Logf("waiting for s4 to stabilize and be promoted")
|
2017-12-18 21:16:23 +00:00
|
|
|
servers = append(servers, s4)
|
2022-09-07 13:35:01 +00:00
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Now kill one of the "old" nodes like we are doing a rolling update.
|
2020-03-30 01:08:54 +00:00
|
|
|
t.Logf("shutting down server s3")
|
2017-12-18 21:16:23 +00:00
|
|
|
s3.Shutdown()
|
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
// Wait for s3 to be removed and the cluster to stablize.
|
|
|
|
t.Logf("waiting for cluster to stabilize")
|
2017-12-18 21:16:23 +00:00
|
|
|
servers = []*Server{s1, s2, s4}
|
2022-09-07 13:35:01 +00:00
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:09:36 +00:00
|
|
|
func TestAutopilot_MultiRegion(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
conf := func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
|
|
|
c.BootstrapExpect = 3
|
|
|
|
}
|
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
|
|
defer cleanupS1()
|
|
|
|
|
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
|
|
defer cleanupS2()
|
|
|
|
|
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
|
|
defer cleanupS3()
|
|
|
|
|
|
|
|
// federated regions should not be considered raft peers or show up in the
|
|
|
|
// known servers list
|
|
|
|
s4, cleanupS4 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 0
|
|
|
|
c.Region = "other"
|
|
|
|
})
|
|
|
|
defer cleanupS4()
|
|
|
|
|
|
|
|
servers := []*Server{s1, s2, s3}
|
|
|
|
TestJoin(t, s1, s2, s3, s4)
|
|
|
|
|
|
|
|
t.Logf("waiting for initial stable cluster")
|
|
|
|
waitForStableLeadership(t, servers)
|
|
|
|
|
|
|
|
apDelegate := &AutopilotDelegate{s3}
|
|
|
|
known := apDelegate.KnownServers()
|
|
|
|
must.Eq(t, 3, len(known))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
func TestAutopilot_CleanupStaleRaftServer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
conf := func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 3
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, conf)
|
|
|
|
defer cleanupS1()
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, conf)
|
|
|
|
defer cleanupS2()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s3, cleanupS3 := TestServer(t, conf)
|
|
|
|
defer cleanupS3()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2020-03-02 15:29:24 +00:00
|
|
|
s4, cleanupS4 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 0
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS4()
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
servers := []*Server{s1, s2, s3}
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2, s3)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for initial stable cluster")
|
2019-09-04 12:49:33 +00:00
|
|
|
leader := waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("adding server s4 to peers directly")
|
2018-01-30 03:53:34 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", s4.config.RPCAddr.Port)
|
2019-09-04 12:49:33 +00:00
|
|
|
future := leader.raft.AddVoter(raft.ServerID(s4.config.NodeID), raft.ServerAddress(addr), 0, 0)
|
2018-01-30 03:53:34 +00:00
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
t.Logf("waiting for 4th server to be removed")
|
|
|
|
waitForStableLeadership(t, servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutopilot_PromoteNonVoter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2017-12-18 21:16:23 +00:00
|
|
|
c.RaftConfig.ProtocolVersion = 3
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
2017-12-18 21:16:23 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2022-09-07 13:35:01 +00:00
|
|
|
c.NumSchedulers = 0 // reduces test log noise
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 0
|
2017-12-18 21:16:23 +00:00
|
|
|
c.RaftConfig.ProtocolVersion = 3
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-01-12 01:00:30 +00:00
|
|
|
TestJoin(t, s1, s2)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2022-09-07 13:35:01 +00:00
|
|
|
// Note: we can't reliably detect that the server is initially a non-voter,
|
|
|
|
// because it can transition too quickly for the test setup to detect,
|
|
|
|
// especially in low-resource environments like CI. We'll assume that
|
|
|
|
// happens correctly here and only test that it transitions to become a
|
|
|
|
// voter.
|
|
|
|
testutil.WaitForResultUntil(10*time.Second, func() (bool, error) {
|
2017-12-18 21:16:23 +00:00
|
|
|
future := s1.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
2022-09-07 13:35:01 +00:00
|
|
|
return false, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
servers := future.Configuration().Servers
|
|
|
|
if len(servers) != 2 {
|
2022-09-07 13:35:01 +00:00
|
|
|
return false, fmt.Errorf("expected 2 servers, got: %v", servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
if servers[1].Suffrage != raft.Voter {
|
2022-09-07 13:35:01 +00:00
|
|
|
return false, fmt.Errorf("expected server to be voter: %v", servers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2022-09-07 13:35:01 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) { must.NoError(t, err) })
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|