Skip to content

Commit d2ecbc2

Browse files
committed
open: Start assigning sequence numbers from 1
This updates initial sequence number behaviour to match that in RocksDB; which also starts assigning sequence numbers from 1 instead of 0.
1 parent f7c9c37 commit d2ecbc2

9 files changed

Lines changed: 63 additions & 57 deletions

File tree

error_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ func TestRequireReadError(t *testing.T) {
355355
require.NoError(t, d.Flush())
356356
expectLSM(`
357357
0:
358-
000007:[a1#3,SET-a2#72057594037927935,RANGEDEL]
358+
000007:[a1#4,SET-a2#72057594037927935,RANGEDEL]
359359
6:
360-
000005:[a1#0,SET-a2#1,SET]
360+
000005:[a1#1,SET-a2#2,SET]
361361
`, d, t)
362362

363363
// Now perform foreground ops with error injection enabled.
@@ -437,9 +437,9 @@ func TestCorruptReadError(t *testing.T) {
437437
require.NoError(t, d.Flush())
438438
expectLSM(`
439439
0:
440-
000007:[a1#3,SET-a2#72057594037927935,RANGEDEL]
440+
000007:[a1#4,SET-a2#72057594037927935,RANGEDEL]
441441
6:
442-
000005:[a1#0,SET-a2#1,SET]
442+
000005:[a1#1,SET-a2#2,SET]
443443
`, d, t)
444444

445445
// Now perform foreground ops with corruption injection enabled.

open.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func Open(dirname string, opts *Options) (db *DB, _ error) {
105105
d.mu.compact.cond.L = &d.mu.Mutex
106106
d.mu.compact.inProgress = make(map[*compaction]struct{})
107107
d.mu.snapshots.init()
108+
// logSeqNum is the next sequence number that will be assigned. Start
109+
// assigning sequence numbers from 1 to match rocksdb.
110+
d.mu.versions.logSeqNum = 1
108111

109112
d.timeNow = time.Now
110113

range_del_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ func TestRangeDelCompactionTruncation(t *testing.T) {
158158
}
159159
expectLSM(`
160160
1:
161-
000008:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
162-
000009:[b#2,RANGEDEL-d#72057594037927935,RANGEDEL]
161+
000008:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
162+
000009:[b#3,RANGEDEL-d#72057594037927935,RANGEDEL]
163163
`)
164164

165165
// Compact again to move one of the tables to L2.
@@ -168,9 +168,9 @@ func TestRangeDelCompactionTruncation(t *testing.T) {
168168
}
169169
expectLSM(`
170170
1:
171-
000008:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
171+
000008:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
172172
2:
173-
000009:[b#2,RANGEDEL-d#72057594037927935,RANGEDEL]
173+
000009:[b#3,RANGEDEL-d#72057594037927935,RANGEDEL]
174174
`)
175175

176176
// Write "b" and "c" to a new table.
@@ -185,11 +185,11 @@ func TestRangeDelCompactionTruncation(t *testing.T) {
185185
}
186186
expectLSM(`
187187
0:
188-
000011:[b#3,SET-c#4,SET]
188+
000011:[b#4,SET-c#5,SET]
189189
1:
190-
000008:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
190+
000008:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
191191
2:
192-
000009:[b#2,RANGEDEL-d#72057594037927935,RANGEDEL]
192+
000009:[b#3,RANGEDEL-d#72057594037927935,RANGEDEL]
193193
`)
194194

195195
// "b" is still visible at this point as it should be.
@@ -225,11 +225,11 @@ func TestRangeDelCompactionTruncation(t *testing.T) {
225225
}
226226
expectLSM(`
227227
1:
228-
000012:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
228+
000012:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
229229
3:
230-
000017:[b#3,SET-b#3,SET]
231-
000018:[b#2,RANGEDEL-c#72057594037927935,RANGEDEL]
232-
000019:[c#4,SET-d#72057594037927935,RANGEDEL]
230+
000017:[b#4,SET-b#4,SET]
231+
000018:[b#3,RANGEDEL-c#72057594037927935,RANGEDEL]
232+
000019:[c#5,SET-d#72057594037927935,RANGEDEL]
233233
`)
234234

235235
// The L1 table still contains a tombstone from [a,d) which will improperly
@@ -306,8 +306,8 @@ func TestRangeDelCompactionTruncation2(t *testing.T) {
306306
}
307307
expectLSM(`
308308
6:
309-
000008:[a#2,RANGEDEL-b#1,SET]
310-
000009:[b#0,RANGEDEL-d#72057594037927935,RANGEDEL]
309+
000008:[a#3,RANGEDEL-b#2,SET]
310+
000009:[b#1,RANGEDEL-d#72057594037927935,RANGEDEL]
311311
`)
312312

313313
if err := d.Set([]byte("c"), bytes.Repeat([]byte("d"), 100), nil); err != nil {
@@ -319,9 +319,9 @@ func TestRangeDelCompactionTruncation2(t *testing.T) {
319319
}
320320
expectLSM(`
321321
6:
322-
000012:[a#2,RANGEDEL-b#1,SET]
323-
000013:[b#0,RANGEDEL-c#72057594037927935,RANGEDEL]
324-
000014:[c#3,SET-d#72057594037927935,RANGEDEL]
322+
000012:[a#3,RANGEDEL-b#2,SET]
323+
000013:[b#1,RANGEDEL-c#72057594037927935,RANGEDEL]
324+
000014:[c#4,SET-d#72057594037927935,RANGEDEL]
325325
`)
326326
}
327327

@@ -399,8 +399,8 @@ func TestRangeDelCompactionTruncation3(t *testing.T) {
399399
}
400400
expectLSM(`
401401
3:
402-
000012:[a#2,RANGEDEL-b#1,SET]
403-
000013:[b#0,RANGEDEL-d#72057594037927935,RANGEDEL]
402+
000012:[a#3,RANGEDEL-b#2,SET]
403+
000013:[b#1,RANGEDEL-d#72057594037927935,RANGEDEL]
404404
`)
405405

406406
if err := d.Set([]byte("c"), bytes.Repeat([]byte("d"), 100), nil); err != nil {
@@ -412,19 +412,19 @@ func TestRangeDelCompactionTruncation3(t *testing.T) {
412412
}
413413
expectLSM(`
414414
4:
415-
000020:[a#2,RANGEDEL-b#1,SET]
416-
000021:[b#0,RANGEDEL-c#72057594037927935,RANGEDEL]
417-
000022:[c#3,SET-d#72057594037927935,RANGEDEL]
415+
000020:[a#3,RANGEDEL-b#2,SET]
416+
000021:[b#1,RANGEDEL-c#72057594037927935,RANGEDEL]
417+
000022:[c#4,SET-d#72057594037927935,RANGEDEL]
418418
`)
419419

420420
if err := d.Compact([]byte("c"), []byte("c")); err != nil {
421421
t.Fatal(err)
422422
}
423423
expectLSM(`
424424
5:
425-
000023:[a#2,RANGEDEL-b#1,SET]
426-
000024:[b#0,RANGEDEL-c#72057594037927935,RANGEDEL]
427-
000025:[c#3,SET-d#72057594037927935,RANGEDEL]
425+
000023:[a#3,RANGEDEL-b#2,SET]
426+
000024:[b#1,RANGEDEL-c#72057594037927935,RANGEDEL]
427+
000025:[c#4,SET-d#72057594037927935,RANGEDEL]
428428
`)
429429

430430
if _, _, err := d.Get([]byte("b")); err != ErrNotFound {
@@ -436,10 +436,10 @@ func TestRangeDelCompactionTruncation3(t *testing.T) {
436436
}
437437
expectLSM(`
438438
5:
439-
000025:[c#3,SET-d#72057594037927935,RANGEDEL]
439+
000025:[c#4,SET-d#72057594037927935,RANGEDEL]
440440
6:
441-
000026:[a#2,RANGEDEL-b#1,SET]
442-
000027:[b#0,RANGEDEL-c#72057594037927935,RANGEDEL]
441+
000026:[a#3,RANGEDEL-b#2,SET]
442+
000027:[b#1,RANGEDEL-c#72057594037927935,RANGEDEL]
443443
`)
444444

445445
if v, _, err := d.Get([]byte("b")); err != ErrNotFound {

testdata/ingest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ ingest ext15
479479
lsm
480480
----
481481
6:
482-
000004:[a#1,RANGEDEL-b#72057594037927935,RANGEDEL]
482+
000004:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
483483

484484
reset
485485
----
@@ -498,7 +498,7 @@ ingest ext16
498498
lsm
499499
----
500500
6:
501-
000004:[a#1,RANGEDEL-b#72057594037927935,RANGEDEL]
501+
000004:[a#2,RANGEDEL-b#72057594037927935,RANGEDEL]
502502

503503
reset
504504
----

testdata/level_checker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,4 @@ Level 1
355355

356356
check merger=fail-merger
357357
----
358-
merge processing error on key a#9,SINGLEDEL in L1: fileNum=000033: finish failed
358+
merge processing error on key a#9,SINGLEDEL in L1: fileNum=000033: finish failed

testdata/manual_compaction

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set b 2
66
compact a-b
77
----
88
6:
9-
000005:[a#0,SET-b#1,SET]
9+
000005:[a#1,SET-b#2,SET]
1010

1111
batch
1212
set c 3
@@ -16,8 +16,8 @@ set d 4
1616
compact c-d
1717
----
1818
6:
19-
000005:[a#0,SET-b#1,SET]
20-
000007:[c#2,SET-d#3,SET]
19+
000005:[a#1,SET-b#2,SET]
20+
000007:[c#3,SET-d#4,SET]
2121

2222
batch
2323
set b 5
@@ -263,7 +263,7 @@ set z 1
263263
compact a-z
264264
----
265265
6:
266-
000005:[a#0,SET-z#4,SET]
266+
000005:[a#1,SET-z#5,SET]
267267

268268
build ext1
269269
set a 2
@@ -277,10 +277,10 @@ del-range c z
277277
ingest ext1 ext2
278278
----
279279
0:
280-
000006:[a#5,SET-a#5,SET]
281-
000007:[b#6,SET-z#72057594037927935,RANGEDEL]
280+
000006:[a#6,SET-a#6,SET]
281+
000007:[b#7,SET-z#72057594037927935,RANGEDEL]
282282
6:
283-
000005:[a#0,SET-z#4,SET]
283+
000005:[a#1,SET-z#5,SET]
284284

285285
iter
286286
first

testdata/manual_flush

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set b 2
77
flush
88
----
99
0:
10-
000005:[a#0,SET-b#1,SET]
10+
000005:[a#1,SET-b#2,SET]
1111

1212
reset
1313
----
@@ -22,7 +22,7 @@ del b
2222
flush
2323
----
2424
0:
25-
000005:[a#2,DEL-b#3,DEL]
25+
000005:[a#3,DEL-b#4,DEL]
2626

2727
batch
2828
set a 3
@@ -32,8 +32,8 @@ set a 3
3232
flush
3333
----
3434
0:
35-
000005:[a#2,DEL-b#3,DEL]
36-
000007:[a#4,SET-a#4,SET]
35+
000005:[a#3,DEL-b#4,DEL]
36+
000007:[a#5,SET-a#5,SET]
3737

3838
batch
3939
set c 4
@@ -43,9 +43,9 @@ set c 4
4343
flush
4444
----
4545
0:
46-
000005:[a#2,DEL-b#3,DEL]
47-
000007:[a#4,SET-a#4,SET]
48-
000009:[c#5,SET-c#5,SET]
46+
000005:[a#3,DEL-b#4,DEL]
47+
000007:[a#5,SET-a#5,SET]
48+
000009:[c#6,SET-c#6,SET]
4949

5050
reset
5151
----
@@ -59,7 +59,7 @@ del-range a c
5959
flush
6060
----
6161
0:
62-
000005:[a#2,RANGEDEL-c#72057594037927935,RANGEDEL]
62+
000005:[a#3,RANGEDEL-c#72057594037927935,RANGEDEL]
6363

6464
reset
6565
----
@@ -72,4 +72,4 @@ set b 2
7272
async-flush
7373
----
7474
0:
75-
000005:[a#0,SET-b#1,SET]
75+
000005:[a#1,SET-b#2,SET]

testdata/metrics

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ iter-new a
88
flush
99
----
1010
0:
11-
000005:[a#0,SET-a#0,SET]
11+
000005:[a#1,SET-a#1,SET]
1212

1313
# iter b references both a memtable and sstable 5.
1414

@@ -44,8 +44,8 @@ set b 2
4444
flush
4545
----
4646
0:
47-
000005:[a#0,SET-a#0,SET]
48-
000007:[b#1,SET-b#1,SET]
47+
000005:[a#1,SET-a#1,SET]
48+
000007:[b#2,SET-b#2,SET]
4949

5050
# iter c references both a memtable and sstables 5 and 7.
5151

version_set.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (vs *versionSet) load(dirname string, opts *Options, mu *sync.Mutex) error
225225
// this behaviour mimics that in RocksDB; the first sequence number
226226
// assigned is one greater than the one present in the manifest
227227
// (assuming no WALs contain higher sequence numbers than the
228-
// manifest's LastSequence). Increment LastSeqNum by 1 to get the
228+
// manifest's LastSeqNum). Increment LastSeqNum by 1 to get the
229229
// next sequence number that will be assigned.
230230
vs.logSeqNum = ve.LastSeqNum + 1
231231
}
@@ -339,9 +339,12 @@ func (vs *versionSet) logAndApply(
339339
// in an unflushed memtable. logSeqNum is the _next_ sequence number that
340340
// will be assigned, so subtract that by 1 to get the upper bound on the
341341
// last assigned sequence number.
342-
ve.LastSeqNum = atomic.LoadUint64(&vs.logSeqNum) - 1
343-
if ve.LastSeqNum < 0 {
344-
ve.LastSeqNum = 0
342+
logSeqNum := atomic.LoadUint64(&vs.logSeqNum)
343+
ve.LastSeqNum = logSeqNum - 1
344+
if logSeqNum == 0 {
345+
// logSeqNum is initialized to 1 in Open() if there are no previous WAL
346+
// or manifest records, so this case should never happen.
347+
vs.opts.Logger.Fatalf("logSeqNum must be a positive integer: %d", logSeqNum)
345348
}
346349

347350
currentVersion := vs.currentVersion()

0 commit comments

Comments
 (0)