Initiatively release session in etcd when service close (#19825)

Signed-off-by: wayblink <anyang.wang@zilliz.com>

Signed-off-by: wayblink <anyang.wang@zilliz.com>
pull/19810/head
wayblink 2022-10-18 19:13:26 +08:00 committed by GitHub
parent 6f5ff61ac3
commit 4cc57adc5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -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)

View File

@ -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")