Skip to content

Commit

Permalink
wal: correctly set nextTx on Truncate (#866)
Browse files Browse the repository at this point in the history
On Truncate(k), if k was logged to the WAL the behavior is unchanged. However,
if k had not yet been logged to the WAL, the WAL performed a full reset of the
WAL and set the next expected txn to k+1. This is incorrect, since the
semantics of truncate are that k becomes the first entry in the WAL after
truncation.
  • Loading branch information
asubiotto authored May 20, 2024
1 parent 9856188 commit 13880aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (w *FileWAL) process() {
// last index are now 0. The underlying WAL will allow a
// record with any index to be written, however we only
// want to allow the next index to be logged.
w.protected.nextTx = truncateTx + 1
w.protected.nextTx = truncateTx
// Remove any records that have not yet been written and
// are now below the nextTx.
for w.protected.queue.Len() > 0 {
Expand Down
11 changes: 7 additions & 4 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,25 @@ func TestWALTruncate(t *testing.T) {
// be observed on replay.
require.NoError(t, w.Log(1, logRecord("should-not-be-logged")))

// The only valid record to log is the one after truncateIdx.
require.NoError(t, w.Log(truncateIdx+1, logRecord("should-be-logged")))
// The only valid record to log is truncateIdx. Note that Truncate
// semantics are that Truncate truncates up to but not including the
// truncateIdx. In other words, truncateIdx becomes the first entry in
// the WAL.
require.NoError(t, w.Log(truncateIdx, logRecord("should-be-logged")))

// Wait for record to be logged.
require.Eventually(t, func() bool {
first, _ := w.FirstIndex()
last, _ := w.LastIndex()
return first == truncateIdx+1 && last == truncateIdx+1
return first == truncateIdx && last == truncateIdx
}, time.Second, 10*time.Millisecond)

numRecords := 0
require.NoError(
t,
w.Replay(0, func(tx uint64, r *walpb.Record) error {
numRecords++
require.Equal(t, uint64(truncateIdx+1), tx)
require.Equal(t, uint64(truncateIdx), tx)
require.Equal(t, []byte("should-be-logged"), r.Entry.GetWrite().Data)
return nil
}),
Expand Down

0 comments on commit 13880aa

Please sign in to comment.