From 4cc57adc5dfa8dfaef3020c670c49fb5afdcbdc0 Mon Sep 17 00:00:00 2001 From: wayblink Date: Tue, 18 Oct 2022 19:13:26 +0800 Subject: [PATCH] Initiatively release session in etcd when service close (#19825) Signed-off-by: wayblink Signed-off-by: wayblink --- internal/util/sessionutil/session_util.go | 10 +++++++++- internal/util/sessionutil/session_util_test.go | 5 ----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/util/sessionutil/session_util.go b/internal/util/sessionutil/session_util.go index dc01750afd..b7d7154a7a 100644 --- a/internal/util/sessionutil/session_util.go +++ b/internal/util/sessionutil/session_util.go @@ -347,7 +347,15 @@ func (s *Session) registerService() (<-chan *clientv3.LeaseKeepAliveResponse, er log.Debug("put session key into etcd", zap.String("key", completeKey), zap.String("value", string(sessionJSON))) keepAliveCtx, keepAliveCancel := context.WithCancel(context.Background()) - s.keepAliveCancel = keepAliveCancel + s.keepAliveCancel = func() { + // delete the session key to make roll update faster + // ignore the resp and error handle, just delete + _, _ = s.etcdCli.Delete(keepAliveCtx, completeKey) + if s.enableActiveStandBy && !s.isStandby.Load().(bool) { + _, _ = s.etcdCli.Delete(keepAliveCtx, s.activeKey) + } + keepAliveCancel() + } ch, err = s.etcdCli.KeepAlive(keepAliveCtx, resp.ID) if err != nil { fmt.Printf("got error during keeping alive with etcd, err: %s\n", err) diff --git a/internal/util/sessionutil/session_util_test.go b/internal/util/sessionutil/session_util_test.go index 6e3c5c1575..d34c71aa31 100644 --- a/internal/util/sessionutil/session_util_test.go +++ b/internal/util/sessionutil/session_util_test.go @@ -651,9 +651,6 @@ func TestSessionProcessActiveStandBy(t *testing.T) { flag = true signal <- struct{}{} s1.keepAliveCancel() - // directly delete the primary key to make this UT fast, - // or the session2 has to wait for session1 release after ttl(60s) - etcdCli.Delete(ctx1, s1.activeKey) }) assert.False(t, s1.isStandby.Load().(bool)) @@ -670,8 +667,6 @@ func TestSessionProcessActiveStandBy(t *testing.T) { }) assert.True(t, s2.isStandby.Load().(bool)) - // sleep 20 seconds, the standby service will start watching primary key - time.Sleep(time.Second * 20) //assert.True(t, s2.watchingPrimaryKeyLock) // stop session 1, session 2 will take over primary service log.Debug("Stop session 1, session 2 will take over primary service")