Skip to content

Commit e672ef4

Browse files
committed
minor improvements from comments
Signed-off-by: ekexium <[email protected]>
1 parent e906cd5 commit e672ef4

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

internal/unionstore/arena/arena.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func (cp *MemDBCheckpoint) IsSamePosition(other *MemDBCheckpoint) bool {
226226
return cp.blocks == other.blocks && cp.offsetInBlock == other.offsetInBlock
227227
}
228228

229+
// LessThan compares two checkpoints.
229230
func (cp *MemDBCheckpoint) LessThan(cp2 *MemDBCheckpoint) bool {
230231
if cp == nil || cp2 == nil {
231232
logutil.BgLogger().Panic("unexpected nil checkpoint", zap.Any("cp", cp), zap.Any("cp2", cp2))

internal/unionstore/art/art.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ type ART struct {
5454
missCount atomic.Uint64
5555

5656
// The counter of every write operation, used to invalidate iterators that were created before the write operation.
57-
SeqNo int
58-
// increased by 1 when an operation that may affect the content returned by "snapshot iter" (i.e. stage[0]) happens.
57+
WriteSeqNo int
58+
// Increased by 1 when an operation that may affect the content returned by "snapshot iter" (i.e. stage[0]) happens.
5959
// It's used to invalidate snapshot iterators.
6060
// invariant: no concurrent access to it
6161
SnapshotSeqNo int
@@ -122,7 +122,7 @@ func (t *ART) Set(key artKey, value []byte, ops ...kv.FlagsOp) error {
122122
}
123123
}
124124

125-
t.SeqNo++
125+
t.WriteSeqNo++
126126
if len(t.stages) == 0 {
127127
t.dirty = true
128128
}
@@ -487,7 +487,7 @@ func (t *ART) RevertToCheckpoint(cp *arena.MemDBCheckpoint) {
487487
t.allocator.vlogAllocator.RevertToCheckpoint(t, cp)
488488
t.allocator.vlogAllocator.Truncate(cp)
489489
t.allocator.vlogAllocator.OnMemChange()
490-
t.SeqNo++
490+
t.WriteSeqNo++
491491
if len(t.stages) == 0 || t.stages[0].LessThan(cp) {
492492
t.SnapshotSeqNo++
493493
}
@@ -510,7 +510,7 @@ func (t *ART) Release(h int) {
510510
if h != len(t.stages) {
511511
panic("cannot release staging buffer")
512512
}
513-
t.SeqNo++
513+
t.WriteSeqNo++
514514
if h == 1 {
515515
t.SnapshotSeqNo++
516516
tail := t.checkpoint()
@@ -533,7 +533,7 @@ func (t *ART) Cleanup(h int) {
533533
panic(fmt.Sprintf("cannot cleanup staging buffer, h=%v, len(tree.stages)=%v", h, len(t.stages)))
534534
}
535535

536-
t.SeqNo++
536+
t.WriteSeqNo++
537537
if h == 1 {
538538
t.SnapshotSeqNo++
539539
}
@@ -562,7 +562,7 @@ func (t *ART) Reset() {
562562
t.allocator.vlogAllocator.Reset()
563563
t.lastTraversedNode.Store(arena.NullU64Addr)
564564
t.SnapshotSeqNo++
565-
t.SeqNo++
565+
t.WriteSeqNo++
566566
}
567567

568568
// DiscardValues releases the memory used by all values.

internal/unionstore/art/art_iterator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (t *ART) iter(lowerBound, upperBound []byte, reverse, includeFlags bool) (*
5959
// this avoids the initial value of currAddr equals to endAddr.
6060
currAddr: arena.BadAddr,
6161
endAddr: arena.NullAddr,
62-
seqNo: t.SeqNo,
62+
seqNo: t.WriteSeqNo,
6363
}
6464
it.init(lowerBound, upperBound)
6565
if !it.valid {
@@ -88,11 +88,11 @@ type Iterator struct {
8888
}
8989

9090
func (it *Iterator) checkSeqNo() {
91-
if it.seqNo != it.tree.SeqNo && !it.ignoreSeqNo {
91+
if it.seqNo != it.tree.WriteSeqNo && !it.ignoreSeqNo {
9292
logutil.BgLogger().Panic(
9393
"seqNo mismatch",
9494
zap.Int("it seqNo", it.seqNo),
95-
zap.Int("art seqNo", it.tree.SeqNo),
95+
zap.Int("art seqNo", it.tree.WriteSeqNo),
9696
zap.Stack("stack"),
9797
)
9898
}

internal/unionstore/art/art_snapshot.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"github.com/tikv/client-go/v2/internal/unionstore/arena"
2222
)
2323

24-
func (t *ART) getSnapshot() arena.MemDBCheckpoint {
24+
// GetSnapshot returns the "snapshot" for snapshotGetter or snapshotIterator, which is usually the snapshot
25+
// of stage[0]
26+
func (t *ART) GetSnapshot() arena.MemDBCheckpoint {
2527
if len(t.stages) > 0 {
2628
return t.stages[0]
2729
}
@@ -32,7 +34,7 @@ func (t *ART) getSnapshot() arena.MemDBCheckpoint {
3234
func (t *ART) SnapshotGetter() *SnapGetter {
3335
return &SnapGetter{
3436
tree: t,
35-
cp: t.getSnapshot(),
37+
cp: t.GetSnapshot(),
3638
}
3739
}
3840

@@ -52,7 +54,7 @@ func (t *ART) newSnapshotIterator(start, end []byte, desc bool) *SnapIter {
5254
inner.ignoreSeqNo = true
5355
it := &SnapIter{
5456
Iterator: inner,
55-
cp: t.getSnapshot(),
57+
cp: t.GetSnapshot(),
5658
}
5759
it.tree.allocator.snapshotInc()
5860
for !it.setValue() && it.Valid() {

internal/unionstore/memdb_art.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package unionstore
1717
import (
1818
"context"
1919
"fmt"
20+
"go.uber.org/zap"
2021
"sync"
2122

2223
"github.com/tikv/client-go/v2/internal/logutil"
@@ -210,6 +211,9 @@ type snapshotBatchedIter struct {
210211
pos int
211212
batchSize int
212213
nextKey []byte
214+
215+
// only used to check if the snapshot ever changes between batches. It is not supposed to change.
216+
snapshot MemDBCheckpoint
213217
}
214218

215219
func (db *artDBWithContext) BatchedSnapshotIter(lower, upper []byte, reverse bool) Iterator {
@@ -225,14 +229,29 @@ func (db *artDBWithContext) BatchedSnapshotIter(lower, upper []byte, reverse boo
225229
batchSize: 32,
226230
}
227231

228-
iter.fillBatch()
232+
iter.snapshot = db.GetSnapshot()
233+
err := iter.fillBatch()
234+
if err != nil {
235+
logutil.BgLogger().Error("failed to fill batch for snapshotBatchedIter", zap.Error(err))
236+
}
229237
return iter
230238
}
231239

232240
func (it *snapshotBatchedIter) fillBatch() error {
233241
if it.snapshotTruncateSeqNo != it.db.SnapshotSeqNo {
234-
return errors.New(fmt.Sprintf("invalid iter: truncation happened, iter's=%d, db's=%d",
235-
it.snapshotTruncateSeqNo, it.db.SnapshotSeqNo))
242+
return errors.Errorf(
243+
"invalid iter: truncation happened, iter's=%d, db's=%d",
244+
it.snapshotTruncateSeqNo,
245+
it.db.SnapshotSeqNo,
246+
)
247+
}
248+
249+
if it.db.GetSnapshot() != it.snapshot {
250+
return errors.Errorf(
251+
"snapshot changed between batches, expected=%v, actual=%v",
252+
it.snapshot,
253+
it.db.GetSnapshot(),
254+
)
236255
}
237256

238257
it.db.RLock()

0 commit comments

Comments
 (0)