Skip to content

Commit

Permalink
Merge pull request #17013 from serathius/deduplicate-shouldApplyV3
Browse files Browse the repository at this point in the history
Move duplicated shouldApplyV3 logic up into apply method
  • Loading branch information
serathius authored Nov 24, 2023
2 parents c975f24 + 7fdb330 commit 6db5e00
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,30 +1771,30 @@ func (s *EtcdServer) apply(
s.lg.Debug("Applying entries", zap.Int("num-entries", len(es)))
for i := range es {
e := es[i]
index := s.consistIndex.ConsistentIndex()
s.lg.Debug("Applying entry",
zap.Uint64("index", e.Index),
zap.Uint64("term", e.Term),
zap.Stringer("type", e.Type))
zap.Uint64("consistent-index", index),
zap.Uint64("entry-index", e.Index),
zap.Uint64("entry-term", e.Term),
zap.Stringer("entry-type", e.Type))

// We need to toApply all WAL entries on top of v2store
// and only 'unapplied' (e.Index>backend.ConsistentIndex) on the backend.
shouldApplyV3 := membership.ApplyV2storeOnly
if e.Index > index {
shouldApplyV3 = membership.ApplyBoth
// set the consistent index of current executing entry
s.consistIndex.SetConsistentApplyingIndex(e.Index, e.Term)
}
switch e.Type {
case raftpb.EntryNormal:
// gofail: var beforeApplyOneEntryNormal struct{}
s.applyEntryNormal(&e)
s.applyEntryNormal(&e, shouldApplyV3)
s.setAppliedIndex(e.Index)
s.setTerm(e.Term)

case raftpb.EntryConfChange:
// gofail: var beforeApplyOneConfChange struct{}

// We need to toApply all WAL entries on top of v2store
// and only 'unapplied' (e.Index>backend.ConsistentIndex) on the backend.
shouldApplyV3 := membership.ApplyV2storeOnly

// set the consistent index of current executing entry
if e.Index > s.consistIndex.ConsistentIndex() {
s.consistIndex.SetConsistentApplyingIndex(e.Index, e.Term)
shouldApplyV3 = membership.ApplyBoth
}

var cc raftpb.ConfChange
pbutil.MustUnmarshal(&cc, e.Data)
removedSelf, err := s.applyConfChange(cc, confState, shouldApplyV3)
Expand All @@ -1816,14 +1816,9 @@ func (s *EtcdServer) apply(
}

// applyEntryNormal applies an EntryNormal type raftpb request to the EtcdServer
func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) {
shouldApplyV3 := membership.ApplyV2storeOnly
func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry, shouldApplyV3 membership.ShouldApplyV3) {
var ar *apply.Result
index := s.consistIndex.ConsistentIndex()
if e.Index > index {
// set the consistent index of current executing entry
s.consistIndex.SetConsistentApplyingIndex(e.Index, e.Term)
shouldApplyV3 = membership.ApplyBoth
if shouldApplyV3 {
defer func() {
// The txPostLockInsideApplyHook will not get called in some cases,
// in which we should move the consistent index forward directly.
Expand All @@ -1833,10 +1828,6 @@ func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) {
}
}()
}
s.lg.Debug("toApply entry normal",
zap.Uint64("consistent-index", index),
zap.Uint64("entry-index", e.Index),
zap.Bool("should-applyV3", bool(shouldApplyV3)))

// raft state machine may generate noop entry when leader confirmation.
// skip it in advance to avoid some potential bug in the future
Expand Down

0 comments on commit 6db5e00

Please sign in to comment.