-
Notifications
You must be signed in to change notification settings - Fork 10
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 behaviour on write failures #32
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
package segment | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
@@ -117,3 +118,116 @@ func TestConcurrentReadersAndWriter(t *testing.T) { | |
require.Greater(t, int(atomic.LoadUint64(&numReads)), 1000) | ||
require.Greater(t, int(atomic.LoadUint64(&sealedMaxIndex)), 1000) | ||
} | ||
|
||
func TestWriterRecoversFromWriteFailure(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. My understanding is that the following test cases would fail the first 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 actually wrote the tests to fail not the first It turns out (and I'd forgotten) that we buffering the whole batch in memory and only actually write to the file once per 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. Yes I understand that as only one flush is done by batch. The test case that I'm referring to is: 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. Maybe I'm still missing something but I think that is what the I pulled that out of the table because I wanted to assert both versions for every other case so it made more sense to just call 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 missed the double t.Run call at the end. Yes that's exactly what I had in mind! |
||
cases := []struct { | ||
name string | ||
setupFailure func(f *testWritableFile, batch []types.LogEntry) | ||
fixFailure func(batch []types.LogEntry) | ||
}{ | ||
{ | ||
name: "fwrite failure", | ||
setupFailure: func(f *testWritableFile, batch []types.LogEntry) { | ||
f.failNextWrite() | ||
}, | ||
}, | ||
{ | ||
name: "fsync failure", | ||
setupFailure: func(f *testWritableFile, batch []types.LogEntry) { | ||
f.failNextSync() | ||
}, | ||
}, | ||
{ | ||
name: "log append failure", | ||
setupFailure: func(f *testWritableFile, batch []types.LogEntry) { | ||
// Should cause monotonicity check to fail but only on last log after | ||
// other logs have been written and internal state updated. | ||
batch[len(batch)-1].Index = 123456 | ||
}, | ||
fixFailure: func(batch []types.LogEntry) { | ||
batch[len(batch)-1].Index = batch[len(batch)-2].Index + 1 | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
testFn := func(t *testing.T, empty bool) { | ||
vfs := newTestVFS() | ||
|
||
f := NewFiler("test", vfs) | ||
|
||
seg0 := testSegment(1) | ||
|
||
w, err := f.Create(seg0) | ||
require.NoError(t, err) | ||
defer w.Close() | ||
|
||
batch := make([]types.LogEntry, 5) | ||
for i := range batch { | ||
batch[i].Index = uint64(i + 1) | ||
batch[i].Data = []byte(fmt.Sprintf("val-%d", i+1)) | ||
} | ||
maxIdx := len(batch) | ||
expectFirstIdx := 0 | ||
expectLastIdx := 0 | ||
|
||
if !empty { | ||
require.NoError(t, w.Append(batch)) | ||
expectFirstIdx = 1 | ||
expectLastIdx = maxIdx | ||
for i := range batch { | ||
batch[i].Index = uint64(i + maxIdx + 1) | ||
batch[i].Data = []byte(fmt.Sprintf("val-%d", i+maxIdx+1)) | ||
} | ||
} | ||
|
||
tf := testFileFor(t, w) | ||
|
||
tc.setupFailure(tf, batch) | ||
|
||
require.Error(t, w.Append(batch)) | ||
assertExpectedLogs(t, w, expectFirstIdx, expectLastIdx) | ||
|
||
if tc.fixFailure != nil { | ||
tc.fixFailure(batch) | ||
} | ||
|
||
// Now retry that write, it should work! | ||
expectFirstIdx = 1 | ||
expectLastIdx = int(batch[4].Index) | ||
require.NoError(t, w.Append(batch)) | ||
assertExpectedLogs(t, w, expectFirstIdx, expectLastIdx) | ||
|
||
// Also, re-open the file "from disk" to make sure what has been written | ||
// is correct and recoverable! | ||
w2, err := f.RecoverTail(seg0) | ||
require.NoError(t, err) | ||
assertExpectedLogs(t, w2, expectFirstIdx, expectLastIdx) | ||
w2.Close() | ||
} | ||
|
||
t.Run(tc.name+" empty", func(t *testing.T) { | ||
testFn(t, true) | ||
}) | ||
t.Run(tc.name+" non-empty", func(t *testing.T) { | ||
testFn(t, false) | ||
}) | ||
} | ||
} | ||
|
||
func assertExpectedLogs(t *testing.T, w types.SegmentWriter, first, last int) { | ||
t.Helper() | ||
|
||
require.Equal(t, uint64(last), w.LastIndex()) | ||
if last == 0 { | ||
return | ||
} | ||
for idx := first; idx <= last; idx++ { | ||
buf, err := w.GetLog(uint64(idx)) | ||
require.NoError(t, err) | ||
require.Equal(t, fmt.Sprintf("val-%d", idx), string(buf.Bs)) | ||
buf.Close() | ||
} | ||
} |
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.
offsets being atomic here, I'm not sure this is still safe. As we could be overwriting other updates of offsets between the start of the append and when defer runs.
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.
This library relies heavily on the fact that
Append
may only be called serially and the caller must ensure that. That's why we don't have locks around the writer state for example. I think with that documented constraint there is no way for this to be racey but I might still be wrong so please correct me if you see anything!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.
Looking closer I think it's safe too 😅.
The reason is that we rely on commitIdx from the reader perspective to know if it's safe to read, so even if the offset is incremented it's safe from the reader perspective.
Also we only increment the commitIdx after the flush to disk is successful.