2014-01-10 01:46:33 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-05-16 22:49:47 +00:00
|
|
|
"errors"
|
2014-01-10 01:46:33 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-05-09 01:24:13 +00:00
|
|
|
"time"
|
2014-10-14 05:14:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/consul/testutil"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2014-10-14 05:14:43 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2014-01-10 01:46:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLeader_RegisterMember(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-10 01:46:33 +00:00
|
|
|
|
2014-01-10 02:02:44 +00:00
|
|
|
// Client should be registered
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2014-05-09 01:05:38 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node != nil, nil
|
2014-05-09 01:05:38 +00:00
|
|
|
}, func(err error) {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("client not registered")
|
2014-05-09 01:05:38 +00:00
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
|
|
|
|
// Should have a check
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := state.NodeChecks(nil, c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-10 01:46:33 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("client missing check")
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if checks[0].CheckID != SerfCheckID {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("bad check: %v", checks[0])
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if checks[0].Name != SerfCheckName {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("bad check: %v", checks[0])
|
|
|
|
}
|
|
|
|
if checks[0].Status != structs.HealthPassing {
|
|
|
|
t.Fatalf("bad check: %v", checks[0])
|
|
|
|
}
|
2014-01-10 02:02:44 +00:00
|
|
|
|
|
|
|
// Server should be registered
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(s1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
2014-01-10 02:02:44 +00:00
|
|
|
t.Fatalf("server not registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service should be registered
|
2017-01-20 07:36:50 +00:00
|
|
|
_, services, err := state.NodeServices(nil, s1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-10 02:02:44 +00:00
|
|
|
if _, ok := services.Services["consul"]; !ok {
|
|
|
|
t.Fatalf("consul service not registered: %v", services)
|
|
|
|
}
|
2014-01-10 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeader_FailedMember(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-10 01:46:33 +00:00
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail the member
|
|
|
|
c1.Shutdown()
|
|
|
|
|
|
|
|
// Should be registered
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2014-05-09 00:14:39 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node != nil, nil
|
2014-05-09 00:14:39 +00:00
|
|
|
}, func(err error) {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("client not registered")
|
2014-05-09 00:14:39 +00:00
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
|
|
|
|
// Should have a check
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := state.NodeChecks(nil, c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-10 01:46:33 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("client missing check")
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if checks[0].CheckID != SerfCheckID {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("bad check: %v", checks[0])
|
|
|
|
}
|
2014-01-16 03:24:16 +00:00
|
|
|
if checks[0].Name != SerfCheckName {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("bad check: %v", checks[0])
|
|
|
|
}
|
2014-05-09 00:04:52 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err = state.NodeChecks(nil, c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-09 00:04:52 +00:00
|
|
|
return checks[0].Status == structs.HealthCritical, errors.New(checks[0].Status)
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("check status is %v, should be critical", err)
|
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeader_LeftMember(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2014-01-10 01:46:33 +00:00
|
|
|
|
|
|
|
// Should be registered
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node != nil, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client should be registered")
|
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
|
|
|
|
// Node should leave
|
|
|
|
c1.Leave()
|
|
|
|
c1.Shutdown()
|
|
|
|
|
|
|
|
// Should be deregistered
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node == nil, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client should not be registered")
|
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 19:51:49 +00:00
|
|
|
func TestLeader_ReapMember(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2014-03-20 19:51:49 +00:00
|
|
|
|
|
|
|
// Should be registered
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node != nil, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client should be registered")
|
|
|
|
})
|
2014-03-20 19:51:49 +00:00
|
|
|
|
|
|
|
// Simulate a node reaping
|
|
|
|
mems := s1.LANMembers()
|
|
|
|
var c1mem serf.Member
|
|
|
|
for _, m := range mems {
|
|
|
|
if m.Name == c1.config.NodeName {
|
|
|
|
c1mem = m
|
|
|
|
c1mem.Status = StatusReap
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s1.reconcileCh <- c1mem
|
|
|
|
|
|
|
|
// Should be deregistered
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node == nil, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client should not be registered")
|
|
|
|
})
|
2014-03-20 19:51:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 22:51:03 +00:00
|
|
|
func TestLeader_Reconcile_ReapMember(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-04-03 22:51:03 +00:00
|
|
|
|
|
|
|
// Register a non-existing member
|
|
|
|
dead := structs.RegisterRequest{
|
|
|
|
Datacenter: s1.config.Datacenter,
|
|
|
|
Node: "no-longer-around",
|
|
|
|
Address: "127.1.1.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: "no-longer-around",
|
|
|
|
CheckID: SerfCheckID,
|
|
|
|
Name: SerfCheckName,
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := s1.RPC("Catalog.Register", &dead, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force a reconciliation
|
|
|
|
if err := s1.reconcile(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node should be gone
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode("no-longer-around")
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node != nil {
|
2014-04-03 22:51:03 +00:00
|
|
|
t.Fatalf("client registered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 01:46:33 +00:00
|
|
|
func TestLeader_Reconcile(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Join before we have a leader, this should cause a reconcile!
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not be registered
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node != nil {
|
2014-01-10 01:46:33 +00:00
|
|
|
t.Fatalf("client registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be registered
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err = state.GetNode(c1.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node != nil, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("client should be registered")
|
|
|
|
})
|
2014-01-10 01:46:33 +00:00
|
|
|
}
|
2014-01-10 20:55:55 +00:00
|
|
|
|
|
|
|
func TestLeader_LeftServer(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
dir3, s3 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
servers := []*Server{s1, s2, s3}
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := s3.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
return peers == 3, nil
|
2014-05-09 00:04:52 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 3 peers")
|
|
|
|
})
|
2014-01-10 20:55:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 03:10:17 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
// Kill any server
|
|
|
|
servers[0].Shutdown()
|
2014-01-10 20:55:55 +00:00
|
|
|
|
2015-01-10 03:10:17 +00:00
|
|
|
// Force remove the non-leader (transition to left state)
|
|
|
|
if err := servers[1].RemoveFailedNode(servers[0].config.NodeName); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-01-10 20:55:55 +00:00
|
|
|
|
2015-01-10 03:10:17 +00:00
|
|
|
for _, s := range servers[1:] {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
return peers == 2, errors.New(fmt.Sprintf("%d", peers))
|
2015-01-10 03:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
2014-01-10 20:55:55 +00:00
|
|
|
}
|
2014-01-20 23:56:29 +00:00
|
|
|
|
2014-10-14 05:14:43 +00:00
|
|
|
func TestLeader_LeftLeader(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
dir3, s3 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
servers := []*Server{s1, s2, s3}
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := s3.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
return peers == 3, nil
|
2014-10-14 05:14:43 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 3 peers")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill the leader!
|
|
|
|
var leader *Server
|
|
|
|
for _, s := range servers {
|
|
|
|
if s.IsLeader() {
|
|
|
|
leader = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 15:02:23 +00:00
|
|
|
if leader == nil {
|
|
|
|
t.Fatalf("Should have a leader")
|
|
|
|
}
|
2014-10-14 05:14:43 +00:00
|
|
|
leader.Leave()
|
|
|
|
leader.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
var remain *Server
|
|
|
|
for _, s := range servers {
|
|
|
|
if s == leader {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
remain = s
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
return peers == 2, errors.New(fmt.Sprintf("%d", peers))
|
2014-10-14 05:14:43 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 2 peers: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the old leader is deregistered
|
2015-10-13 05:21:39 +00:00
|
|
|
state := remain.fsm.State()
|
2014-10-18 00:40:03 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := state.GetNode(leader.config.NodeName)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return node == nil, nil
|
2014-10-18 00:40:03 +00:00
|
|
|
}, func(err error) {
|
2014-10-14 05:14:43 +00:00
|
|
|
t.Fatalf("leader should be deregistered")
|
2014-10-18 00:40:03 +00:00
|
|
|
})
|
2014-10-14 05:14:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 23:56:29 +00:00
|
|
|
func TestLeader_MultiBootstrap(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, s2 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
servers := []*Server{s1, s2}
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
2014-05-09 00:04:52 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
peers := s.serfLAN.Members()
|
|
|
|
return len(peers) == 2, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 2 peers")
|
|
|
|
})
|
2014-01-20 23:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we don't have multiple raft peers
|
|
|
|
for _, s := range servers {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
if peers != 1 {
|
2014-01-20 23:56:29 +00:00
|
|
|
t.Fatalf("should only have 1 raft peer!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 22:37:49 +00:00
|
|
|
|
|
|
|
func TestLeader_TombstoneGC_Reset(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
dir3, s3 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
servers := []*Server{s1, s2, s3}
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := s3.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2016-07-28 19:11:28 +00:00
|
|
|
peers, _ := s.numPeers()
|
|
|
|
return peers == 3, nil
|
2014-12-15 22:37:49 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have 3 peers")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var leader *Server
|
|
|
|
for _, s := range servers {
|
|
|
|
if s.IsLeader() {
|
|
|
|
leader = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if leader == nil {
|
|
|
|
t.Fatalf("Should have a leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the leader has a pending GC expiration
|
|
|
|
if !leader.tombstoneGC.PendingExpiration() {
|
|
|
|
t.Fatalf("should have pending expiration")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill the leader
|
|
|
|
leader.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// Wait for a new leader
|
|
|
|
leader = nil
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
for _, s := range servers {
|
|
|
|
if s.IsLeader() {
|
|
|
|
leader = s
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have leader")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check that the new leader has a pending GC expiration
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
return leader.tombstoneGC.PendingExpiration(), nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have pending expiration")
|
|
|
|
})
|
|
|
|
}
|
2014-12-19 00:02:08 +00:00
|
|
|
|
|
|
|
func TestLeader_ReapTombstones(t *testing.T) {
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.TombstoneTTL = 50 * time.Millisecond
|
|
|
|
c.TombstoneTTLGranularity = 10 * time.Millisecond
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
2014-12-19 00:02:08 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-19 00:02:08 +00:00
|
|
|
|
|
|
|
// Create a KV entry
|
|
|
|
arg := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "test",
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out bool
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-12-19 00:02:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
// Delete the KV entry (tombstoned).
|
2014-12-19 00:02:08 +00:00
|
|
|
arg.Op = structs.KVSDelete
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
|
2014-12-19 00:02:08 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:48:35 +00:00
|
|
|
// Make sure there's a tombstone.
|
|
|
|
state := s1.fsm.State()
|
|
|
|
func() {
|
|
|
|
snap := state.Snapshot()
|
|
|
|
defer snap.Close()
|
2015-10-19 22:51:11 +00:00
|
|
|
stones, err := snap.Tombstones()
|
2015-10-13 18:48:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if stones.Next() == nil {
|
2015-10-19 21:56:22 +00:00
|
|
|
t.Fatalf("missing tombstones")
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if stones.Next() != nil {
|
2015-10-19 21:56:22 +00:00
|
|
|
t.Fatalf("unexpected extra tombstones")
|
2015-10-13 18:48:35 +00:00
|
|
|
}
|
|
|
|
}()
|
2014-12-19 00:02:08 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
// Check that the new leader has a pending GC expiration by
|
2015-10-13 18:48:35 +00:00
|
|
|
// watching for the tombstone to get removed.
|
2014-12-19 00:02:08 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-10-13 18:48:35 +00:00
|
|
|
snap := state.Snapshot()
|
|
|
|
defer snap.Close()
|
2015-10-19 22:51:11 +00:00
|
|
|
stones, err := snap.Tombstones()
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
2015-10-13 18:48:35 +00:00
|
|
|
return false, err
|
2015-10-12 07:42:09 +00:00
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
return stones.Next() == nil, nil
|
2014-12-19 00:02:08 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|