-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix inconsistent state between WAL and saved Snapshot #3584
base: release-2.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ func TestOpenWAL(t *testing.T) { | |
for i := 0; i < 10; i++ { | ||
store.Store( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this test demonstrates the problem you describe. Can you reproduce the problem in a unit test? |
||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 10)}}, | ||
raftpb.HardState{}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
|
@@ -155,7 +155,7 @@ func TestTakeSnapshot(t *testing.T) { | |
for i := 0; i < 10; i++ { | ||
store.Store( | ||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 100)}}, | ||
raftpb.HardState{}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
|
@@ -216,7 +216,7 @@ func TestTakeSnapshot(t *testing.T) { | |
for i := 0; i < 10; i++ { | ||
store.Store( | ||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 100)}}, | ||
raftpb.HardState{}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
|
@@ -282,7 +282,7 @@ func TestTakeSnapshot(t *testing.T) { | |
for i := 0; i < 10; i++ { | ||
store.Store( | ||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 100)}}, | ||
raftpb.HardState{}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
|
@@ -369,7 +369,7 @@ func TestApplyOutOfDateSnapshot(t *testing.T) { | |
for i := 0; i < 10; i++ { | ||
store.Store( | ||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 100)}}, | ||
raftpb.HardState{}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
|
@@ -395,3 +395,82 @@ func TestApplyOutOfDateSnapshot(t *testing.T) { | |
assertFileCount(t, 12, 1) | ||
}) | ||
} | ||
|
||
func TestAbortWhenWritingSnapshot(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I add the unit test to reproduce the problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks but I meant a unit test that uses an instance of a Fabric Raft chain, in We need to be sure that a Fabric Raft chain instance can encounter the bespoken problem, so that:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem can be reproduced with the following steps:
|
||
t.Run("Abort when writing snapshot", func(t *testing.T) { | ||
setup(t) | ||
defer clean(t) | ||
|
||
// set SegmentSizeBytes to a small value so that | ||
// every entry persisted to wal would result in | ||
// a new wal being created. | ||
oldSegmentSizeBytes := wal.SegmentSizeBytes | ||
wal.SegmentSizeBytes = 10 | ||
defer func() { | ||
wal.SegmentSizeBytes = oldSegmentSizeBytes | ||
}() | ||
|
||
// create 5 new entry | ||
for i := 0; i < 5; i++ { | ||
store.Store( | ||
[]raftpb.Entry{{Index: uint64(i), Data: make([]byte, 100)}}, | ||
raftpb.HardState{Commit: uint64(i)}, | ||
raftpb.Snapshot{}, | ||
) | ||
} | ||
assertFileCount(t, 6, 0) | ||
|
||
// Assume an orderer missed some records due to exceptions and receives a new snapshot from other orderers. | ||
commit := 10 | ||
store.Store( | ||
[]raftpb.Entry{}, | ||
raftpb.HardState{Commit: uint64(commit)}, | ||
raftpb.Snapshot{ | ||
Metadata: raftpb.SnapshotMetadata{ | ||
Index: uint64(commit), | ||
}, | ||
Data: make([]byte, 100), | ||
}, | ||
) | ||
err = store.Close() | ||
assert.NoError(t, err) | ||
|
||
// In old logic, it will use rs.wal.Save(hardstate, entries) to save the state firstly, so we remove the snapshot files. | ||
// sd, err := os.Open(snapDir) | ||
// assert.NoError(t, err) | ||
// defer sd.Close() | ||
// names, err := sd.Readdirnames(-1) | ||
// assert.NoError(t, err) | ||
// sort.Sort(sort.Reverse(sort.StringSlice(names))) | ||
// os.Remove(filepath.Join(snapDir, names[0])) | ||
// wd, err := os.Open(walDir) | ||
// assert.NoError(t, err) | ||
// defer wd.Close() | ||
// names, err = wd.Readdirnames(-1) | ||
// assert.NoError(t, err) | ||
// sort.Sort(sort.Reverse(sort.StringSlice(names))) | ||
// os.Remove(filepath.Join(walDir, names[0])) | ||
|
||
// But in the new logic, it will use rs.saveSnap(snapshot) to save the snapshot firstly, so we remove the WAL files. | ||
wd, err := os.Open(walDir) | ||
assert.NoError(t, err) | ||
defer wd.Close() | ||
names, err := wd.Readdirnames(-1) | ||
assert.NoError(t, err) | ||
sort.Sort(sort.Reverse(sort.StringSlice(names))) | ||
os.Remove(filepath.Join(walDir, names[0])) | ||
|
||
// Then restart the orderer. | ||
ram := raft.NewMemoryStorage() | ||
store, err = CreateStorage(logger, walDir, snapDir, ram) | ||
assert.NoError(t, err) | ||
|
||
// Check the state from go.etcd.io/etcd/raft/raft.go | ||
// func (r *raft) loadState(state pb.HardState) | ||
hd, _, err := store.ram.InitialState() | ||
assert.NoError(t, err) | ||
lastIndex, err := store.ram.LastIndex() | ||
assert.NoError(t, err) | ||
assert.False(t, hd.Commit > lastIndex) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, we need to swap the order of writing the snapshot entries and snapshot file(needs change in saveSnap) as it is done in etcdserver https://github.com/etcd-io/etcd/blob/6c2f5dc78af6b6970d48cecaac515c58a91efca8/server/storage/storage.go#L66