Skip to content

Commit

Permalink
etcdserver/*, wal/*:Add comments, clean up error messages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbetz committed May 14, 2020
1 parent bd16071 commit b68eea2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
5 changes: 3 additions & 2 deletions etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ func (r *raftNode) start(rh *raftReadyHandler) {
// Force WAL to fsync its hard state before Release() releases
// old data from the WAL. Otherwise could get an error like:
// panic: tocommit(107) is out of range [lastIndex(84)]. Was the raft log corrupted, truncated, or lost?
// See https://github.com/etcd-io/etcd/issues/10219 for more details.
if err := r.storage.Sync(); err != nil {
log.Fatal(err)
r.lg.Fatal("failed to sync Raft snapshot", zap.Error(err))
}

// etcdserver now claim the snapshot has been persisted onto the disk
Expand All @@ -263,7 +264,7 @@ func (r *raftNode) start(rh *raftReadyHandler) {
// gofail: var raftAfterApplySnap struct{}

if err := r.storage.Release(rd.Snapshot); err != nil {
log.Fatal(err)
r.lg.Fatal("failed to release Raft wal", zap.Error(err))
}
// gofail: var raftAfterWALRelease struct{}
}
Expand Down
2 changes: 1 addition & 1 deletion etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ func TestTriggerSnap(t *testing.T) {
// each operation is recorded as a Save
// (SnapshotCount+1) * Puts + SaveSnap = (SnapshotCount+1) * Save + SaveSnap + Release
if len(gaction) != wcnt {
fmt.Println("gaction", gaction)
t.Logf("gaction: %v", gaction)
t.Fatalf("len(action) = %d, want %d", len(gaction), wcnt)
}

Expand Down
2 changes: 1 addition & 1 deletion wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ func ValidSnapshotEntries(lg *zap.Logger, walDir string) ([]walpb.Snapshot, erro
n++
}
}
snaps = snaps[:n]
snaps = snaps[:n:n]

return snaps, nil
}
Expand Down
17 changes: 6 additions & 11 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,9 +1011,11 @@ func TestValidSnapshotEntries(t *testing.T) {
defer os.RemoveAll(p)
snap0 := walpb.Snapshot{Index: 0, Term: 0}
snap1 := walpb.Snapshot{Index: 1, Term: 1}
state1 := raftpb.HardState{Commit: 1, Term: 1}
snap2 := walpb.Snapshot{Index: 2, Term: 1}
snap3 := walpb.Snapshot{Index: 3, Term: 2}
snap4 := walpb.Snapshot{Index: 4, Term: 2}
state2 := raftpb.HardState{Commit: 3, Term: 2}
snap4 := walpb.Snapshot{Index: 4, Term: 2} // will be orphaned since the last committed entry will be snap3
func() {
w, err := Create(zap.NewExample(), p, nil)
if err != nil {
Expand All @@ -1025,8 +1027,7 @@ func TestValidSnapshotEntries(t *testing.T) {
if err = w.SaveSnapshot(snap1); err != nil {
t.Fatal(err)
}
state := raftpb.HardState{Commit: 1, Term: 1}
if err = w.Save(state, nil); err != nil {
if err = w.Save(state1, nil); err != nil {
t.Fatal(err)
}
if err = w.SaveSnapshot(snap2); err != nil {
Expand All @@ -1035,7 +1036,6 @@ func TestValidSnapshotEntries(t *testing.T) {
if err = w.SaveSnapshot(snap3); err != nil {
t.Fatal(err)
}
state2 := raftpb.HardState{Commit: 3, Term: 2}
if err = w.Save(state2, nil); err != nil {
t.Fatal(err)
}
Expand All @@ -1048,12 +1048,7 @@ func TestValidSnapshotEntries(t *testing.T) {
t.Fatal(err)
}
expected := []walpb.Snapshot{snap0, snap1, snap2, snap3}
if len(walSnaps) != len(expected) {
t.Fatalf("expected 4 walSnaps, got %d", len(expected))
}
for i := 0; i < len(expected); i++ {
if walSnaps[i].Index != expected[i].Index || walSnaps[i].Term != expected[i].Term {
t.Errorf("expected walSnaps %+v at index %d, got %+v", expected[i], i, walSnaps[i])
}
if !reflect.DeepEqual(walSnaps, expected) {
t.Errorf("expected walSnaps %+v, got %+v", expected, walSnaps)
}
}

0 comments on commit b68eea2

Please sign in to comment.