2014-12-10 21:43:15 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2017-11-29 02:01:17 +00:00
|
|
|
"fmt"
|
2014-12-10 21:43:15 +00:00
|
|
|
"os"
|
2014-12-13 05:42:59 +00:00
|
|
|
"strings"
|
2014-12-10 21:43:15 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-11-29 02:01:17 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2014-12-10 21:43:15 +00:00
|
|
|
)
|
|
|
|
|
2017-11-29 02:01:17 +00:00
|
|
|
func generateUUID() (ret string) {
|
|
|
|
var err error
|
|
|
|
if ret, err = uuid.GenerateUUID(); err != nil {
|
|
|
|
panic(fmt.Sprintf("Unable to generate a UUID, %v", err))
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
func TestInitializeSessionTimers(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-13 05:42:59 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-13 03:56:17 +00:00
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(100, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the session timers
|
|
|
|
err := s1.initializeSessionTimers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have a timer
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get(session.ID) == nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("missing session timer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetSessionTimer_Fault(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
// Should not exist
|
2015-10-20 00:09:02 +00:00
|
|
|
err := s1.resetSessionTimer(generateUUID(), nil)
|
2014-12-13 05:42:59 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "not found") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a session
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-13 03:56:17 +00:00
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(100, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the session timer
|
|
|
|
err = s1.resetSessionTimer(session.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have a timer
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get(session.ID) == nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("missing session timer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetSessionTimer_NoTTL(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
// Create a session
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-13 03:56:17 +00:00
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "0000s",
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(100, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the session timer
|
|
|
|
err := s1.resetSessionTimer(session.ID, session)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we have a timer
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get(session.ID) != nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("should not have session timer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetSessionTimer_InvalidTTL(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
// Create a session
|
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the session timer
|
|
|
|
err := s1.resetSessionTimer(session.ID, session)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Invalid Session TTL") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetSessionTimerLocked(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-11-05 04:53:22 +00:00
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
s1.createSessionTimer("foo", 5*time.Millisecond)
|
|
|
|
if s1.sessionTimers.Get("foo") == nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("missing timer")
|
|
|
|
}
|
|
|
|
|
2019-02-21 21:27:17 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if s1.sessionTimers.Get("foo") != nil {
|
|
|
|
r.Fatal("timer should be gone")
|
|
|
|
}
|
|
|
|
})
|
2014-12-13 05:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetSessionTimerLocked_Renew(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
ttl := 100 * time.Millisecond
|
2014-12-13 05:42:59 +00:00
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
// create the timer
|
|
|
|
s1.createSessionTimer("foo", ttl)
|
|
|
|
if s1.sessionTimers.Get("foo") == nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("missing timer")
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
// wait until it is "expired" but at this point
|
|
|
|
// the session still exists.
|
|
|
|
time.Sleep(ttl)
|
|
|
|
if s1.sessionTimers.Get("foo") == nil {
|
|
|
|
t.Fatal("missing timer")
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
// renew the session which will reset the TTL to 2*ttl
|
|
|
|
// since that is the current SessionTTLMultiplier
|
|
|
|
s1.createSessionTimer("foo", ttl)
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
// Watch for invalidation
|
2017-06-27 13:25:25 +00:00
|
|
|
renew := time.Now()
|
|
|
|
deadline := renew.Add(2 * structs.SessionTTLMultiplier * ttl)
|
|
|
|
for {
|
|
|
|
now := time.Now()
|
|
|
|
if now.After(deadline) {
|
|
|
|
t.Fatal("should have expired by now")
|
|
|
|
}
|
|
|
|
|
|
|
|
// timer still exists
|
|
|
|
if s1.sessionTimers.Get("foo") != nil {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
continue
|
2014-12-13 05:42:59 +00:00
|
|
|
}
|
2017-06-27 13:25:25 +00:00
|
|
|
|
|
|
|
// timer gone
|
|
|
|
if now.Sub(renew) < ttl {
|
|
|
|
t.Fatalf("early invalidate")
|
|
|
|
}
|
|
|
|
break
|
2014-12-13 05:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidateSession(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
// Create a session
|
2015-10-13 05:21:39 +00:00
|
|
|
state := s1.fsm.State()
|
2015-10-13 03:56:17 +00:00
|
|
|
if err := state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
session := &structs.Session{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
}
|
|
|
|
if err := state.SessionCreate(100, session); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should cause a destroy
|
|
|
|
s1.invalidateSession(session.ID)
|
|
|
|
|
|
|
|
// Check it is gone
|
2017-01-24 18:08:14 +00:00
|
|
|
_, sess, err := state.SessionGet(nil, session.ID)
|
2014-12-13 05:42:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatalf("should destroy session")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClearSessionTimer(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
s1.createSessionTimer("foo", 5*time.Millisecond)
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
err := s1.clearSessionTimer("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
if s1.sessionTimers.Get("foo") != nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("timer should be gone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClearAllSessionTimers(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-13 05:42:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
s1.createSessionTimer("foo", 10*time.Millisecond)
|
|
|
|
s1.createSessionTimer("bar", 10*time.Millisecond)
|
|
|
|
s1.createSessionTimer("baz", 10*time.Millisecond)
|
2014-12-13 05:42:59 +00:00
|
|
|
|
|
|
|
err := s1.clearAllSessionTimers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:25:25 +00:00
|
|
|
// sessionTimers is guarded by the lock
|
|
|
|
if s1.sessionTimers.Len() != 0 {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("timers should be gone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServer_SessionTTL_Failover(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-12-10 21:43:15 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2018-12-12 20:09:42 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
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
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, s2, s1)
|
|
|
|
joinLAN(t, s3, s1)
|
2017-05-05 07:23:28 +00:00
|
|
|
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 3)) })
|
2014-12-10 21:43:15 +00:00
|
|
|
|
|
|
|
// Find the leader
|
|
|
|
var leader *Server
|
|
|
|
for _, s := range servers {
|
2014-12-13 05:42:59 +00:00
|
|
|
// Check that s.sessionTimers is empty
|
2017-06-27 13:25:25 +00:00
|
|
|
if s.sessionTimers.Len() != 0 {
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Fatalf("should have no sessionTimers")
|
|
|
|
}
|
2014-12-13 05:42:59 +00:00
|
|
|
// Find the leader too
|
2014-12-10 21:43:15 +00:00
|
|
|
if s.IsLeader() {
|
|
|
|
leader = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if leader == nil {
|
|
|
|
t.Fatalf("Should have a leader")
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, leader)
|
|
|
|
defer codec.Close()
|
2014-12-10 21:43:15 +00:00
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
// Register a node
|
|
|
|
node := structs.RegisterRequest{
|
|
|
|
Datacenter: s1.config.Datacenter,
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := s1.RPC("Catalog.Register", &node, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-12-10 21:43:15 +00:00
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
// Create a TTL session
|
2014-12-10 21:43:15 +00:00
|
|
|
arg := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
|
|
|
Node: "foo",
|
|
|
|
TTL: "10s",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var id1 string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Session.Apply", &arg, &id1); err != nil {
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
// Check that sessionTimers has the session ID
|
2017-06-27 13:25:25 +00:00
|
|
|
if leader.sessionTimers.Get(id1) == nil {
|
2014-12-13 05:42:59 +00:00
|
|
|
t.Fatalf("missing session timer")
|
2014-12-10 21:43:15 +00:00
|
|
|
}
|
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
// Shutdown the leader!
|
2014-12-10 21:43:15 +00:00
|
|
|
leader.Shutdown()
|
|
|
|
|
2014-12-13 05:42:59 +00:00
|
|
|
// sessionTimers should be cleared on leader shutdown
|
2017-06-27 13:25:25 +00:00
|
|
|
if leader.sessionTimers.Len() != 0 {
|
2014-12-10 21:43:15 +00:00
|
|
|
t.Fatalf("session timers should be empty on the shutdown leader")
|
|
|
|
}
|
|
|
|
// Find the new leader
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2015-01-10 01:18:11 +00:00
|
|
|
leader = nil
|
|
|
|
for _, s := range servers {
|
|
|
|
if s.IsLeader() {
|
|
|
|
leader = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if leader == nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal("Should have a new leader")
|
2014-12-10 21:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 01:18:11 +00:00
|
|
|
// Ensure session timer is restored
|
2017-06-27 13:25:25 +00:00
|
|
|
if leader.sessionTimers.Get(id1) == nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal("missing session timer")
|
2015-01-10 01:18:11 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2014-12-10 21:43:15 +00:00
|
|
|
}
|