diff --git a/server/mvcc/kvstore.go b/server/mvcc/kvstore.go index 9b37e7104..e1f74760e 100644 --- a/server/mvcc/kvstore.go +++ b/server/mvcc/kvstore.go @@ -232,19 +232,9 @@ func (s *store) checkPrevCompactionCompleted() bool { tx := s.b.ReadTx() tx.Lock() defer tx.Unlock() - _, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0) - scheduledCompact := int64(0) - if len(scheduledCompactBytes) != 0 { - scheduledCompact = bytesToRev(scheduledCompactBytes[0]).main - } - - _, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0) - finishedCompact := int64(0) - if len(finishedCompactBytes) != 0 { - finishedCompact = bytesToRev(finishedCompactBytes[0]).main - - } - return scheduledCompact == finishedCompact + scheduledCompact, scheduledCompactFound := UnsafeReadScheduledCompact(tx) + finishedCompact, finishedCompactFound := UnsafeReadFinishedCompact(tx) + return scheduledCompact == finishedCompact && scheduledCompactFound == finishedCompactFound } func (s *store) compact(trace *traceutil.Trace, rev, prevCompactRev int64, prevCompactionCompleted bool) (<-chan struct{}, error) { @@ -343,10 +333,10 @@ func (s *store) restore() error { tx := s.b.ReadTx() tx.Lock() - _, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0) - if len(finishedCompactBytes) != 0 { + finishedCompact, found := UnsafeReadFinishedCompact(tx) + if found { s.revMu.Lock() - s.compactMainRev = bytesToRev(finishedCompactBytes[0]).main + s.compactMainRev = finishedCompact s.lg.Info( "restored last compact revision", @@ -356,11 +346,7 @@ func (s *store) restore() error { ) s.revMu.Unlock() } - _, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0) - scheduledCompact := int64(0) - if len(scheduledCompactBytes) != 0 { - scheduledCompact = bytesToRev(scheduledCompactBytes[0]).main - } + scheduledCompact, _ := UnsafeReadScheduledCompact(tx) // index keys concurrently as they're loaded in from tx keysGauge.Set(0) diff --git a/server/mvcc/util.go b/server/mvcc/util.go index db79bffa6..ea3111267 100644 --- a/server/mvcc/util.go +++ b/server/mvcc/util.go @@ -41,3 +41,19 @@ func UnsafeSetScheduledCompact(tx backend.BatchTx, value int64) { revToBytes(revision{main: value}, rbytes) tx.UnsafePut(buckets.Meta, scheduledCompactKeyName, rbytes) } + +func UnsafeReadFinishedCompact(tx backend.ReadTx) (int64, bool) { + _, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0) + if len(finishedCompactBytes) != 0 { + return bytesToRev(finishedCompactBytes[0]).main, true + } + return 0, false +} + +func UnsafeReadScheduledCompact(tx backend.ReadTx) (int64, bool) { + _, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0) + if len(scheduledCompactBytes) != 0 { + return bytesToRev(scheduledCompactBytes[0]).main, true + } + return 0, false +}