Skip to content

Commit

Permalink
Merge pull request #165 from pav-kv/maybe-update
Browse files Browse the repository at this point in the history
tracker: clean-up Match, Next and MaybeUpdate
  • Loading branch information
ahrtr committed Feb 21, 2024
2 parents 83d8dec + 457bb8e commit d475d7e
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion confchange/confchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func (c Changer) initProgress(cfg *tracker.Config, trk tracker.ProgressMap, id u
// at all (and will thus likely need a snapshot), though the app may
// have applied a snapshot out of band before adding the replica (thus
// making the first index the better choice).
Next: c.LastIndex,
Match: 0,
Next: max(c.LastIndex, 1), // invariant: Match < Next
Inflights: tracker.NewInflights(c.Tracker.MaxInflight, c.Tracker.MaxInflightBytes),
IsLearner: isLearner,
// When a node is first added, we should mark it as recently active.
Expand Down
6 changes: 3 additions & 3 deletions confchange/testdata/joint_autoleave.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

# Autoleave is reflected in the config.
enter-joint autoleave=true
v2 v3
----
voters=(1 2 3)&&(1) autoleave
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
3: StateProbe match=0 next=1

Expand All @@ -24,6 +24,6 @@ config is already joint
leave-joint
----
voters=(1 2 3)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
3: StateProbe match=0 next=1
6 changes: 3 additions & 3 deletions confchange/testdata/joint_idempotency.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

enter-joint
r1 r2 r9 v2 v3 v4 v2 v3 v4 l2 l2 r4 r4 l1 l1
----
voters=(3)&&(1) learners=(2) learners_next=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1 learner
3: StateProbe match=0 next=1

leave-joint
----
voters=(3) learners=(1 2)
1: StateProbe match=0 next=0 learner
1: StateProbe match=0 next=1 learner
2: StateProbe match=0 next=1 learner
3: StateProbe match=0 next=1
6 changes: 3 additions & 3 deletions confchange/testdata/joint_learners_next.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

enter-joint
v2 l1
----
voters=(2)&&(1) learners_next=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1

leave-joint
----
voters=(2) learners=(1)
1: StateProbe match=0 next=0 learner
1: StateProbe match=0 next=1 learner
2: StateProbe match=0 next=1
10 changes: 5 additions & 5 deletions confchange/testdata/simple_idempotency.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

simple
v2
----
voters=(1 2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=2

simple
l1
----
voters=(2) learners=(1)
1: StateProbe match=0 next=0 learner
1: StateProbe match=0 next=1 learner
2: StateProbe match=0 next=2

simple
l1
----
voters=(2) learners=(1)
1: StateProbe match=0 next=0 learner
1: StateProbe match=0 next=1 learner
2: StateProbe match=0 next=2

simple
Expand Down
14 changes: 7 additions & 7 deletions confchange/testdata/simple_promote_demote.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

simple
v2
----
voters=(1 2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1

simple
v3
----
voters=(1 2 3)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
3: StateProbe match=0 next=2

Expand All @@ -27,7 +27,7 @@ simple
l1 v1
----
voters=(1 2 3)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
3: StateProbe match=0 next=2

Expand All @@ -36,7 +36,7 @@ simple
l2
----
voters=(1 3) learners=(2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1 learner
3: StateProbe match=0 next=2

Expand All @@ -46,7 +46,7 @@ simple
v2 l2
----
voters=(1 3) learners=(2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1 learner
3: StateProbe match=0 next=2

Expand All @@ -55,6 +55,6 @@ simple
v2
----
voters=(1 2 3)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
3: StateProbe match=0 next=2
6 changes: 3 additions & 3 deletions confchange/testdata/update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ simple
v1
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1

simple
v2 u1
----
voters=(1 2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1

simple
u1 u2 u3 u1 u2 u3
----
voters=(1 2)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
2: StateProbe match=0 next=1
2 changes: 1 addition & 1 deletion confchange/testdata/zero.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ simple
v1 r0 v0 l0
----
voters=(1)
1: StateProbe match=0 next=0
1: StateProbe match=0 next=1
3 changes: 0 additions & 3 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1902,9 +1902,6 @@ func (r *raft) restore(s pb.Snapshot) bool {

assertConfStatesEquivalent(r.logger, cs, r.switchToConfig(cfg, trk))

pr := r.trk.Progress[r.id]
pr.MaybeUpdate(pr.Next - 1) // TODO(tbg): this is untested and likely unneeded

last := r.raftLog.lastEntryID()
r.logger.Infof("%x [commit: %d, lastindex: %d, lastterm: %d] restored snapshot [index: %d, term: %d]",
r.id, r.raftLog.committed, last.index, last.term, id.index, id.term)
Expand Down
24 changes: 15 additions & 9 deletions tracker/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ import (
// strewn around `*raft.raft`. Additionally, some fields are only used when in a
// certain State. All of this isn't ideal.
type Progress struct {
Match, Next uint64
// Match is the index up to which the follower's log is known to match the
// leader's.
Match uint64
// Next is the log index of the next entry to send to this follower. All
// entries with indices in (Match, Next) interval are already in flight.
//
// Invariant: 0 <= Match < Next.
Next uint64

// State defines how the leader should interact with the follower.
//
// When in StateProbe, leader sends at most one replication message
Expand Down Expand Up @@ -166,14 +174,13 @@ func (pr *Progress) UpdateOnEntriesSend(entries int, bytes uint64) {
// index acked by it. The method returns false if the given n index comes from
// an outdated message. Otherwise it updates the progress and returns true.
func (pr *Progress) MaybeUpdate(n uint64) bool {
var updated bool
if pr.Match < n {
pr.Match = n
updated = true
pr.MsgAppFlowPaused = false
if n <= pr.Match {
return false
}
pr.Next = max(pr.Next, n+1)
return updated
pr.Match = n
pr.Next = max(pr.Next, n+1) // invariant: Match < Next
pr.MsgAppFlowPaused = false
return true
}

// MaybeDecrTo adjusts the Progress to the receipt of a MsgApp rejection. The
Expand Down Expand Up @@ -208,7 +215,6 @@ func (pr *Progress) MaybeDecrTo(rejected, matchHint uint64) bool {
return false
}

// Next index shall always be larger than match index.
pr.Next = max(min(rejected, matchHint+1), pr.Match+1)
pr.MsgAppFlowPaused = false
return true
Expand Down

0 comments on commit d475d7e

Please sign in to comment.