@@ -80,7 +80,7 @@ type WAL struct {
80
80
metadata []byte // metadata recorded at the head of each WAL
81
81
state raftpb.HardState // hardstate recorded at the head of WAL
82
82
83
- start walpb. Snapshot // snapshot to start reading
83
+ start Position // Position to start reading
84
84
decoder Decoder // decoder to Decode records
85
85
readClose func () error // closer for Decode reader
86
86
@@ -94,6 +94,10 @@ type WAL struct {
94
94
fp * filePipeline
95
95
}
96
96
97
+ type Position struct {
98
+ Term , Index uint64
99
+ }
100
+
97
101
// Create creates a WAL ready for appending records. The given metadata is
98
102
// recorded at the head of each WAL file, and can be retrieved with ReadAll
99
103
// after the file is Open.
@@ -258,12 +262,12 @@ func createNewWALFile[T *os.File | *fileutil.LockedFile](path string, forceNew b
258
262
return any (file ).(T ), nil
259
263
}
260
264
261
- func (w * WAL ) Reopen (lg * zap.Logger , snap walpb. Snapshot ) (* WAL , error ) {
265
+ func (w * WAL ) Reopen (lg * zap.Logger , pos Position ) (* WAL , error ) {
262
266
err := w .Close ()
263
267
if err != nil {
264
268
lg .Panic ("failed to close WAL during reopen" , zap .Error (err ))
265
269
}
266
- return Open (lg , w .dir , snap )
270
+ return Open (lg , w .dir , pos )
267
271
}
268
272
269
273
func (w * WAL ) SetUnsafeNoFsync () {
@@ -324,7 +328,7 @@ func (w *WAL) renameWALUnlock(tmpdirpath string) (*WAL, error) {
324
328
}
325
329
326
330
// reopen and relock
327
- newWAL , oerr := Open (w .lg , w .dir , walpb. Snapshot { })
331
+ newWAL , oerr := Open (w .lg , w .dir , Position { Index : 0 , Term : 0 })
328
332
if oerr != nil {
329
333
return nil , oerr
330
334
}
@@ -335,14 +339,12 @@ func (w *WAL) renameWALUnlock(tmpdirpath string) (*WAL, error) {
335
339
return newWAL , nil
336
340
}
337
341
338
- // Open opens the WAL at the given snap.
339
- // The snap SHOULD have been previously saved to the WAL, or the following
340
- // ReadAll will fail.
342
+ // Open opens the WAL at the given position.
341
343
// The returned WAL is ready to read and the first record will be the one after
342
- // the given snap . The WAL cannot be appended to before reading out all of its
344
+ // the given position . The WAL cannot be appended to before reading out all of its
343
345
// previous records.
344
- func Open (lg * zap.Logger , dirpath string , snap walpb. Snapshot ) (* WAL , error ) {
345
- w , err := openAtIndex (lg , dirpath , snap , true )
346
+ func Open (lg * zap.Logger , dirpath string , pos Position ) (* WAL , error ) {
347
+ w , err := openAtPosition (lg , dirpath , pos , true )
346
348
if err != nil {
347
349
return nil , fmt .Errorf ("openAtIndex failed: %w" , err )
348
350
}
@@ -354,15 +356,15 @@ func Open(lg *zap.Logger, dirpath string, snap walpb.Snapshot) (*WAL, error) {
354
356
355
357
// OpenForRead only opens the wal files for read.
356
358
// Write on a read only wal panics.
357
- func OpenForRead (lg * zap.Logger , dirpath string , snap walpb. Snapshot ) (* WAL , error ) {
358
- return openAtIndex (lg , dirpath , snap , false )
359
+ func OpenForRead (lg * zap.Logger , dirpath string , pos Position ) (* WAL , error ) {
360
+ return openAtPosition (lg , dirpath , pos , false )
359
361
}
360
362
361
- func openAtIndex (lg * zap.Logger , dirpath string , snap walpb. Snapshot , write bool ) (* WAL , error ) {
363
+ func openAtPosition (lg * zap.Logger , dirpath string , pos Position , write bool ) (* WAL , error ) {
362
364
if lg == nil {
363
365
lg = zap .NewNop ()
364
366
}
365
- names , nameIndex , err := selectWALFiles (lg , dirpath , snap )
367
+ names , nameIndex , err := selectWALFiles (lg , dirpath , pos )
366
368
if err != nil {
367
369
return nil , fmt .Errorf ("[openAtIndex] selectWALFiles failed: %w" , err )
368
370
}
@@ -376,7 +378,7 @@ func openAtIndex(lg *zap.Logger, dirpath string, snap walpb.Snapshot, write bool
376
378
w := & WAL {
377
379
lg : lg ,
378
380
dir : dirpath ,
379
- start : snap ,
381
+ start : pos ,
380
382
decoder : NewDecoder (rs ... ),
381
383
readClose : closer ,
382
384
locks : ls ,
@@ -396,15 +398,15 @@ func openAtIndex(lg *zap.Logger, dirpath string, snap walpb.Snapshot, write bool
396
398
return w , nil
397
399
}
398
400
399
- func selectWALFiles (lg * zap.Logger , dirpath string , snap walpb. Snapshot ) ([]string , int , error ) {
401
+ func selectWALFiles (lg * zap.Logger , dirpath string , pos Position ) ([]string , int , error ) {
400
402
names , err := readWALNames (lg , dirpath )
401
403
if err != nil {
402
404
return nil , - 1 , fmt .Errorf ("readWALNames failed: %w" , err )
403
405
}
404
406
405
- nameIndex , ok := searchIndex (lg , names , snap .Index )
407
+ nameIndex , ok := searchIndex (lg , names , pos .Index )
406
408
if ! ok {
407
- return nil , - 1 , fmt .Errorf ("wal: file not found which matches the snapshot index '%d'" , snap .Index )
409
+ return nil , - 1 , fmt .Errorf ("wal: file not found which matches the index '%d'" , pos .Index )
408
410
}
409
411
410
412
if ! isValidSeq (lg , names [nameIndex :]) {
@@ -453,9 +455,8 @@ func openWALFiles(lg *zap.Logger, dirpath string, names []string, nameIndex int,
453
455
// If opened in write mode, it must read out all records until EOF. Or an error
454
456
// will be returned.
455
457
// If opened in read mode, it will try to read all records if possible.
456
- // If it cannot read out the expected snap, it will return ErrSnapshotNotFound.
457
- // If loaded snap doesn't match with the expected one, it will return
458
- // all the records and error ErrSnapshotMismatch.
458
+ // If the position matches snapshot and term doesn't match, it will
459
+ // return error ErrSnapshotMismatch.
459
460
// TODO: detect not-last-snap error.
460
461
// TODO: maybe loose the checking of match.
461
462
// After ReadAll, the WAL will be ready for appending new records.
@@ -477,7 +478,6 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
477
478
}
478
479
decoder := w .decoder
479
480
480
- var match bool
481
481
for err = decoder .Decode (rec ); err == nil ; err = decoder .Decode (rec ) {
482
482
switch rec .Type {
483
483
case EntryType :
@@ -526,7 +526,6 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
526
526
state .Reset ()
527
527
return nil , state , nil , ErrSnapshotMismatch
528
528
}
529
- match = true
530
529
}
531
530
532
531
default :
@@ -565,16 +564,13 @@ func (w *WAL) ReadAll() (metadata []byte, state raftpb.HardState, ents []raftpb.
565
564
}
566
565
567
566
err = nil
568
- if ! match {
569
- err = ErrSnapshotNotFound
570
- }
571
567
572
568
// close decoder, disable reading
573
569
if w .readClose != nil {
574
570
w .readClose ()
575
571
w .readClose = nil
576
572
}
577
- w .start = walpb. Snapshot {}
573
+ w .start = Position {}
578
574
579
575
w .metadata = metadata
580
576
@@ -658,10 +654,9 @@ func ValidSnapshotEntries(lg *zap.Logger, walDir string) ([]walpb.Snapshot, erro
658
654
// It creates a new decoder to read through the records of the given WAL.
659
655
// It does not conflict with any open WAL, but it is recommended not to
660
656
// call this function after opening the WAL for writing.
661
- // If it cannot read out the expected snap, it will return ErrSnapshotNotFound.
662
- // If the loaded snap doesn't match with the expected one, it will
657
+ // If the position matches snapshot and term doesn't match, it will
663
658
// return error ErrSnapshotMismatch.
664
- func Verify (lg * zap.Logger , walDir string , snap walpb. Snapshot ) (* raftpb.HardState , error ) {
659
+ func Verify (lg * zap.Logger , walDir string , pos Position ) (* raftpb.HardState , error ) {
665
660
var metadata []byte
666
661
var err error
667
662
var match bool
@@ -672,7 +667,7 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) (*raftpb.HardSta
672
667
if lg == nil {
673
668
lg = zap .NewNop ()
674
669
}
675
- names , nameIndex , err := selectWALFiles (lg , walDir , snap )
670
+ names , nameIndex , err := selectWALFiles (lg , walDir , pos )
676
671
if err != nil {
677
672
return nil , err
678
673
}
@@ -710,8 +705,8 @@ func Verify(lg *zap.Logger, walDir string, snap walpb.Snapshot) (*raftpb.HardSta
710
705
case SnapshotType :
711
706
var loadedSnap walpb.Snapshot
712
707
pbutil .MustUnmarshal (& loadedSnap , rec .Data )
713
- if loadedSnap .Index == snap .Index {
714
- if loadedSnap .Term != snap .Term {
708
+ if loadedSnap .Index == pos .Index {
709
+ if loadedSnap .Term != pos .Term {
715
710
return nil , ErrSnapshotMismatch
716
711
}
717
712
match = true
0 commit comments