From 933712066f7dcbe8039b301ad1c169e77722a1f4 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Sun, 25 Sep 2022 15:16:02 -0400 Subject: [PATCH] raft: rename raftLog.nextEnts to raftLog.nextCommittedEnts Also rename hasNextEnts to hasNextCommittedEnts. Pure refactor. Signed-off-by: Nathan VanBenschoten --- raft/log.go | 12 ++++++------ raft/log_test.go | 8 ++++---- raft/node.go | 2 +- raft/node_test.go | 4 ++-- raft/raft_paper_test.go | 10 +++++----- raft/rawnode.go | 2 +- raft/rawnode_test.go | 4 ++-- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/raft/log.go b/raft/log.go index 82cf54aa2786..2fbf8e3d2b8d 100644 --- a/raft/log.go +++ b/raft/log.go @@ -40,7 +40,7 @@ type raftLog struct { logger Logger // maxNextEntsSize is the maximum number aggregate byte size of the messages - // returned from calls to nextEnts. + // returned from calls to nextCommittedEnts. maxNextEntsSize uint64 } @@ -177,10 +177,10 @@ func (l *raftLog) unstableEntries() []pb.Entry { return l.unstable.entries } -// nextEnts returns all the available entries for execution. +// nextCommittedEnts returns all the available entries for execution. // If applied is smaller than the index of snapshot, it returns all committed // entries after the index of snapshot. -func (l *raftLog) nextEnts() (ents []pb.Entry) { +func (l *raftLog) nextCommittedEnts() (ents []pb.Entry) { off := max(l.applied+1, l.firstIndex()) if l.committed+1 > off { ents, err := l.slice(off, l.committed+1, l.maxNextEntsSize) @@ -192,9 +192,9 @@ func (l *raftLog) nextEnts() (ents []pb.Entry) { return nil } -// hasNextEnts returns if there is any available entries for execution. This -// is a fast check without heavy raftLog.slice() in raftLog.nextEnts(). -func (l *raftLog) hasNextEnts() bool { +// hasNextCommittedEnts returns if there is any available entries for execution. +// This is a fast check without heavy raftLog.slice() in nextCommittedEnts(). +func (l *raftLog) hasNextCommittedEnts() bool { off := max(l.applied+1, l.firstIndex()) return l.committed+1 > off } diff --git a/raft/log_test.go b/raft/log_test.go index fe89039bca26..adeaee3ea95f 100644 --- a/raft/log_test.go +++ b/raft/log_test.go @@ -339,7 +339,7 @@ func TestCompactionSideEffects(t *testing.T) { } } -func TestHasNextEnts(t *testing.T) { +func TestHasNextCommittedEnts(t *testing.T) { snap := pb.Snapshot{ Metadata: pb.SnapshotMetadata{Term: 1, Index: 3}, } @@ -365,14 +365,14 @@ func TestHasNextEnts(t *testing.T) { raftLog.maybeCommit(5, 1) raftLog.appliedTo(tt.applied) - hasNext := raftLog.hasNextEnts() + hasNext := raftLog.hasNextCommittedEnts() if hasNext != tt.hasNext { t.Errorf("#%d: hasNext = %v, want %v", i, hasNext, tt.hasNext) } } } -func TestNextEnts(t *testing.T) { +func TestNextCommittedEnts(t *testing.T) { snap := pb.Snapshot{ Metadata: pb.SnapshotMetadata{Term: 1, Index: 3}, } @@ -398,7 +398,7 @@ func TestNextEnts(t *testing.T) { raftLog.maybeCommit(5, 1) raftLog.appliedTo(tt.applied) - nents := raftLog.nextEnts() + nents := raftLog.nextCommittedEnts() if !reflect.DeepEqual(nents, tt.wents) { t.Errorf("#%d: nents = %+v, want %+v", i, nents, tt.wents) } diff --git a/raft/node.go b/raft/node.go index 381000621ac7..71a95cc8cda8 100644 --- a/raft/node.go +++ b/raft/node.go @@ -568,7 +568,7 @@ func (n *node) ReadIndex(ctx context.Context, rctx []byte) error { func newReady(r *raft, prevSoftSt *SoftState, prevHardSt pb.HardState) Ready { rd := Ready{ Entries: r.raftLog.unstableEntries(), - CommittedEntries: r.raftLog.nextEnts(), + CommittedEntries: r.raftLog.nextCommittedEnts(), Messages: r.msgs, } if softSt := r.softState(); !softSt.equal(prevSoftSt) { diff --git a/raft/node_test.go b/raft/node_test.go index be7461fa7ac6..0b61dd9f43f8 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -952,8 +952,8 @@ func (s *ignoreSizeHintMemStorage) Entries(lo, hi uint64, maxSize uint64) ([]raf // internal one. The original bug was the following: // // - node learns that index 11 (or 100, doesn't matter) is committed -// - nextEnts returns index 1..10 in CommittedEntries due to size limiting. However, -// index 10 already exceeds maxBytes, due to a user-provided impl of Entries. +// - nextCommittedEnts returns index 1..10 in CommittedEntries due to size limiting. +// However, index 10 already exceeds maxBytes, due to a user-provided impl of Entries. // - Commit index gets bumped to 10 // - the node persists the HardState, but crashes before applying the entries // - upon restart, the storage returns the same entries, but `slice` takes a different code path diff --git a/raft/raft_paper_test.go b/raft/raft_paper_test.go index 71bf35cf2a5f..44536c241ab6 100644 --- a/raft/raft_paper_test.go +++ b/raft/raft_paper_test.go @@ -450,8 +450,8 @@ func TestLeaderCommitEntry(t *testing.T) { t.Errorf("committed = %d, want %d", g, li+1) } wents := []pb.Entry{{Index: li + 1, Term: 1, Data: []byte("some data")}} - if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) { - t.Errorf("nextEnts = %+v, want %+v", g, wents) + if g := r.raftLog.nextCommittedEnts(); !reflect.DeepEqual(g, wents) { + t.Errorf("nextCommittedEnts = %+v, want %+v", g, wents) } msgs := r.readMessages() sort.Sort(messageSlice(msgs)) @@ -538,7 +538,7 @@ func TestLeaderCommitPrecedingEntries(t *testing.T) { li := uint64(len(tt)) wents := append(tt, pb.Entry{Term: 3, Index: li + 1}, pb.Entry{Term: 3, Index: li + 2, Data: []byte("some data")}) - if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) { + if g := r.raftLog.nextCommittedEnts(); !reflect.DeepEqual(g, wents) { t.Errorf("#%d: ents = %+v, want %+v", i, g, wents) } } @@ -590,8 +590,8 @@ func TestFollowerCommitEntry(t *testing.T) { t.Errorf("#%d: committed = %d, want %d", i, g, tt.commit) } wents := tt.ents[:int(tt.commit)] - if g := r.raftLog.nextEnts(); !reflect.DeepEqual(g, wents) { - t.Errorf("#%d: nextEnts = %v, want %v", i, g, wents) + if g := r.raftLog.nextCommittedEnts(); !reflect.DeepEqual(g, wents) { + t.Errorf("#%d: nextCommittedEnts = %v, want %v", i, g, wents) } } } diff --git a/raft/rawnode.go b/raft/rawnode.go index abe1f9634173..c433ac34fc75 100644 --- a/raft/rawnode.go +++ b/raft/rawnode.go @@ -163,7 +163,7 @@ func (rn *RawNode) HasReady() bool { if r.raftLog.hasPendingSnapshot() { return true } - if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextEnts() { + if len(r.msgs) > 0 || len(r.raftLog.unstableEntries()) > 0 || r.raftLog.hasNextCommittedEnts() { return true } if len(r.readStates) != 0 { diff --git a/raft/rawnode_test.go b/raft/rawnode_test.go index 0bda8a80f5e3..aa48f5675546 100644 --- a/raft/rawnode_test.go +++ b/raft/rawnode_test.go @@ -885,8 +885,8 @@ func TestRawNodeStatus(t *testing.T) { // Raft group would forget to apply entries: // // - node learns that index 11 is committed -// - nextEnts returns index 1..10 in CommittedEntries (but index 10 already -// exceeds maxBytes), which isn't noticed internally by Raft +// - nextCommittedEnts returns index 1..10 in CommittedEntries (but index 10 +// already exceeds maxBytes), which isn't noticed internally by Raft // - Commit index gets bumped to 10 // - the node persists the HardState, but crashes before applying the entries // - upon restart, the storage returns the same entries, but `slice` takes a