From 5c8aa08e2cfe479fc1178589751f559664007ec1 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Tue, 6 Sep 2022 12:48:06 +0800 Subject: [PATCH] move consistent_index forward when executing alarmList operation Cherry pick https://github.com/etcd-io/etcd/pull/14419 to 3.5. Signed-off-by: Benjamin Wang --- server/etcdserver/server.go | 5 ++--- tests/integration/v3_alarm_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/server/etcdserver/server.go b/server/etcdserver/server.go index 33bad9f6ce2..87148eab414 100644 --- a/server/etcdserver/server.go +++ b/server/etcdserver/server.go @@ -2175,7 +2175,6 @@ func (s *EtcdServer) apply( // applyEntryNormal apples an EntryNormal type raftpb request to the EtcdServer func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) { shouldApplyV3 := membership.ApplyV2storeOnly - applyV3Performed := false var ar *applyResult index := s.consistIndex.ConsistentIndex() if e.Index > index { @@ -2185,7 +2184,8 @@ func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) { defer func() { // The txPostLockInsideApplyHook will not get called in some cases, // in which we should move the consistent index forward directly. - if !applyV3Performed || (ar != nil && ar.err != nil) { + newIndex := s.consistIndex.ConsistentIndex() + if newIndex < e.Index { s.consistIndex.SetConsistentIndex(e.Index, e.Term) } }() @@ -2235,7 +2235,6 @@ func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) { if !needResult && raftReq.Txn != nil { removeNeedlessRangeReqs(raftReq.Txn) } - applyV3Performed = true ar = s.applyV3.Apply(&raftReq, shouldApplyV3) } diff --git a/tests/integration/v3_alarm_test.go b/tests/integration/v3_alarm_test.go index 3d66eb78eab..d51fb53bb44 100644 --- a/tests/integration/v3_alarm_test.go +++ b/tests/integration/v3_alarm_test.go @@ -332,6 +332,32 @@ func TestV3CorruptAlarmWithLeaseCorrupted(t *testing.T) { } } +// Refer to https://github.com/etcd-io/etcd/issues/14382 +func TestAlarmlistOnMemberRestart(t *testing.T) { + BeforeTest(t) + clus := NewClusterV3(t, &ClusterConfig{ + CorruptCheckTime: time.Second, + Size: 1, + SnapshotCount: 5, + }) + defer clus.Terminate(t) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + for i := 0; i < 6; i++ { + req := &pb.AlarmRequest{Action: pb.AlarmRequest_GET} + if _, err := clus.Members[0].s.Alarm(ctx, req); err != nil { + t.Fatalf("unexpected error, %v", err) + } + } + + clus.Members[0].Stop(t) + if err := clus.Members[0].Restart(t); err != nil { + t.Fatalf("failed to start member, %v", err) + } +} + func leaseIdToBytes(n int64) []byte { bytes := make([]byte, 8) binary.BigEndian.PutUint64(bytes, uint64(n))