Skip to content

Commit eb9bfaa

Browse files
author
caojiamingalan
committed
Follow up #16068 (comment)
Add a UnsafeReadScheduledCompact and UnsafeReadFinishedCompact Signed-off-by: caojiamingalan <[email protected]>
1 parent 8f4b6c9 commit eb9bfaa

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

server/mvcc/kvstore.go

+7-21
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,9 @@ func (s *store) checkPrevCompactionCompleted() bool {
232232
tx := s.b.ReadTx()
233233
tx.Lock()
234234
defer tx.Unlock()
235-
_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0)
236-
scheduledCompact := int64(0)
237-
if len(scheduledCompactBytes) != 0 {
238-
scheduledCompact = bytesToRev(scheduledCompactBytes[0]).main
239-
}
240-
241-
_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0)
242-
finishedCompact := int64(0)
243-
if len(finishedCompactBytes) != 0 {
244-
finishedCompact = bytesToRev(finishedCompactBytes[0]).main
245-
246-
}
247-
return scheduledCompact == finishedCompact
235+
scheduledCompact, scheduledCompactFound := UnsafeReadScheduledCompact(tx)
236+
finishedCompact, finishedCompactFound := UnsafeReadFinishedCompact(tx)
237+
return scheduledCompact == finishedCompact && scheduledCompactFound == finishedCompactFound
248238
}
249239

250240
func (s *store) compact(trace *traceutil.Trace, rev, prevCompactRev int64, prevCompactionCompleted bool) (<-chan struct{}, error) {
@@ -343,10 +333,10 @@ func (s *store) restore() error {
343333
tx := s.b.ReadTx()
344334
tx.Lock()
345335

346-
_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0)
347-
if len(finishedCompactBytes) != 0 {
336+
finishedCompact, found := UnsafeReadFinishedCompact(tx)
337+
if found {
348338
s.revMu.Lock()
349-
s.compactMainRev = bytesToRev(finishedCompactBytes[0]).main
339+
s.compactMainRev = finishedCompact
350340

351341
s.lg.Info(
352342
"restored last compact revision",
@@ -356,11 +346,7 @@ func (s *store) restore() error {
356346
)
357347
s.revMu.Unlock()
358348
}
359-
_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0)
360-
scheduledCompact := int64(0)
361-
if len(scheduledCompactBytes) != 0 {
362-
scheduledCompact = bytesToRev(scheduledCompactBytes[0]).main
363-
}
349+
scheduledCompact, _ := UnsafeReadScheduledCompact(tx)
364350

365351
// index keys concurrently as they're loaded in from tx
366352
keysGauge.Set(0)

server/mvcc/util.go

+16
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ func UnsafeSetScheduledCompact(tx backend.BatchTx, value int64) {
4141
revToBytes(revision{main: value}, rbytes)
4242
tx.UnsafePut(buckets.Meta, scheduledCompactKeyName, rbytes)
4343
}
44+
45+
func UnsafeReadFinishedCompact(tx backend.ReadTx) (int64, bool) {
46+
_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0)
47+
if len(finishedCompactBytes) != 0 {
48+
return bytesToRev(finishedCompactBytes[0]).main, true
49+
}
50+
return 0, false
51+
}
52+
53+
func UnsafeReadScheduledCompact(tx backend.ReadTx) (int64, bool) {
54+
_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0)
55+
if len(scheduledCompactBytes) != 0 {
56+
return bytesToRev(scheduledCompactBytes[0]).main, true
57+
}
58+
return 0, false
59+
}

0 commit comments

Comments
 (0)