Skip to content

Commit

Permalink
kvserver/gc: partially migrate to MVCCRangeKeyStack
Browse files Browse the repository at this point in the history
Some parts require invasive changes to MVCC stats helpers. These will
shortly be consolidated with other MVCC stats logic elsewhere, so the
existing logic is retained for now by using `AsRangeKeyValues()`.

Release note: None
  • Loading branch information
erikgrinaker committed Jul 29, 2022
1 parent 4800f7c commit 0d37b5a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
1 change: 1 addition & 0 deletions pkg/kv/kvserver/gc/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ func processReplicatedRangeTombstones(
if !ok {
break
}
// TODO(erikgrinaker): Rewrite to use MVCCRangeKeyStack.
rangeKeys := iter.RangeKeys().AsRangeKeyValues()

if idx := sort.Search(len(rangeKeys), func(i int) bool {
Expand Down
13 changes: 5 additions & 8 deletions pkg/kv/kvserver/gc/gc_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package gc

import (
"sort"
"strings"

"github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
Expand Down Expand Up @@ -193,15 +192,13 @@ func (it *gcIterator) currentRangeTS() hlc.Timestamp {
if rangeTombstoneStartKey.Equal(it.cachedRangeTombstoneKey) {
return it.cachedRangeTombstoneTS
}
it.cachedRangeTombstoneKey = append(it.cachedRangeTombstoneKey[:0], rangeTombstoneStartKey...)

it.cachedRangeTombstoneTS = hlc.Timestamp{}
rangeKeys := it.it.RangeKeys().AsRangeKeyValues()
if idx := sort.Search(len(rangeKeys), func(i int) bool {
return rangeKeys[i].RangeKey.Timestamp.LessEq(it.threshold)
}); idx < len(rangeKeys) {
it.cachedRangeTombstoneTS = rangeKeys[idx].RangeKey.Timestamp
if v, ok := it.it.RangeKeys().FirstBelow(it.threshold); ok {
it.cachedRangeTombstoneTS = v.Timestamp
} else {
it.cachedRangeTombstoneTS = hlc.Timestamp{}
}
it.cachedRangeTombstoneKey = append(it.cachedRangeTombstoneKey[:0], rangeTombstoneStartKey...)
return it.cachedRangeTombstoneTS
}

Expand Down
15 changes: 5 additions & 10 deletions pkg/kv/kvserver/gc/gc_random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,12 @@ func getExpectationsGenerator(
// so we will add them to history of current key for analysis.
// Bare range tombstones are ignored.
if r {
for _, r := range it.RangeKeys().AsRangeKeyValues() {
for _, r := range it.RangeKeys().AsRangeKeys() {
history = append(history, historyItem{
MVCCKeyValue: storage.MVCCKeyValue{
Key: storage.MVCCKey{
Key: r.RangeKey.StartKey,
Timestamp: r.RangeKey.Timestamp,
Key: r.StartKey,
Timestamp: r.Timestamp,
},
Value: nil,
},
Expand Down Expand Up @@ -622,13 +622,8 @@ func rangeFragmentsFromIt(t *testing.T, it storage.MVCCIterator) [][]storage.MVC
if !ok {
break
}
_, r := it.HasPointAndRange()
if r {
fragments := make([]storage.MVCCRangeKeyValue, len(it.RangeKeys().AsRangeKeyValues()))
for i, r := range it.RangeKeys().AsRangeKeyValues() {
fragments[i] = r.Clone()
}
result = append(result, fragments)
if _, hasRange := it.HasPointAndRange(); hasRange {
result = append(result, it.RangeKeys().Clone().AsRangeKeyValues())
}
it.NextKey()
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/kv/kvserver/gc/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,20 +1084,20 @@ func engineData(t *testing.T, r storage.Reader, desc roachpb.RangeDescriptor) []
_, r := rangeIt.HasPointAndRange()
if r {
span := rangeIt.RangeBounds()
newKeys := rangeIt.RangeKeys().AsRangeKeyValues()
newKeys := rangeIt.RangeKeys().AsRangeKeys()
if lastEnd.Equal(span.Key) {
// Try merging keys by timestamp.
var newPartial []storage.MVCCRangeKey
i, j := 0, 0
for i < len(newKeys) && j < len(partialRangeKeys) {
switch newKeys[i].RangeKey.Timestamp.Compare(partialRangeKeys[j].Timestamp) {
switch newKeys[i].Timestamp.Compare(partialRangeKeys[j].Timestamp) {
case 1:
newPartial = append(newPartial, newKeys[i].RangeKey.Clone())
newPartial = append(newPartial, newKeys[i].Clone())
i++
case 0:
newPartial = append(newPartial, storage.MVCCRangeKey{
StartKey: partialRangeKeys[j].StartKey,
EndKey: newKeys[i].RangeKey.EndKey.Clone(),
EndKey: newKeys[i].EndKey.Clone(),
Timestamp: partialRangeKeys[j].Timestamp,
})
i++
Expand All @@ -1108,7 +1108,7 @@ func engineData(t *testing.T, r storage.Reader, desc roachpb.RangeDescriptor) []
}
}
for ; i < len(newKeys); i++ {
newPartial = append(newPartial, newKeys[i].RangeKey.Clone())
newPartial = append(newPartial, newKeys[i].Clone())
}
for ; j < len(partialRangeKeys); j++ {
newPartial = append(newPartial, partialRangeKeys[j].Clone())
Expand All @@ -1118,7 +1118,7 @@ func engineData(t *testing.T, r storage.Reader, desc roachpb.RangeDescriptor) []
result = append(result, makeRangeCells(partialRangeKeys)...)
partialRangeKeys = make([]storage.MVCCRangeKey, len(newKeys))
for i, rk := range newKeys {
partialRangeKeys[i] = rk.RangeKey.Clone()
partialRangeKeys[i] = rk.Clone()
}
}
lastEnd = span.EndKey.Clone()
Expand Down
18 changes: 8 additions & 10 deletions pkg/storage/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4599,18 +4599,15 @@ func MVCCGarbageCollect(
if ms != nil {
// We need to iterate ranges only to compute GCBytesAge if we are updating
// stats.
//
// TODO(erikgrinaker): Rewrite to use MVCCRangeKeyStack.
if _, hasRange := iter.HasPointAndRange(); hasRange && !lastRangeTombstoneStart.Equal(iter.RangeBounds().Key) {
rangeKeys := iter.RangeKeys().AsRangeKeyValues()
newLen := len(rangeKeys)
if cap(rangeTombstoneTss) < newLen {
rangeTombstoneTss = make([]hlc.Timestamp, newLen)
} else {
rangeTombstoneTss = rangeTombstoneTss[:newLen]
}
for i, rkv := range rangeKeys {
rangeTombstoneTss[i] = rkv.RangeKey.Timestamp
rangeKeys := iter.RangeKeys()
lastRangeTombstoneStart = append(lastRangeTombstoneStart[:0], rangeKeys.Bounds.Key...)
rangeTombstoneTss = rangeTombstoneTss[:0]
for _, v := range rangeKeys.Versions {
rangeTombstoneTss = append(rangeTombstoneTss, v.Timestamp)
}
lastRangeTombstoneStart = append(lastRangeTombstoneStart[:0], rangeKeys[0].RangeKey.StartKey...)
} else if !hasRange {
lastRangeTombstoneStart = lastRangeTombstoneStart[:0]
rangeTombstoneTss = rangeTombstoneTss[:0]
Expand Down Expand Up @@ -4816,6 +4813,7 @@ func MVCCGarbageCollectRangeKeys(
break
}

// TODO(erikgrinaker): Rewrite to use MVCCRangeKeyStack.
bounds := iter.RangeBounds()
unsafeRangeKeys := iter.RangeKeys().AsRangeKeyValues()

Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/mvcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5763,9 +5763,9 @@ func TestMVCCGarbageCollectRanges(t *testing.T) {
if !ok {
break
}
for _, rkv := range it.RangeKeys().AsRangeKeyValues() {
require.Less(t, expectIndex, len(d.after), "not enough expectations; at unexpected range:", rkv.RangeKey.String())
require.EqualValues(t, d.after[expectIndex], rkv.RangeKey, "range key is not equal")
for _, rk := range it.RangeKeys().AsRangeKeys() {
require.Less(t, expectIndex, len(d.after), "not enough expectations; at unexpected range: %s", rk)
require.EqualValues(t, d.after[expectIndex], rk, "range key is not equal")
expectIndex++
}
}
Expand Down

0 comments on commit 0d37b5a

Please sign in to comment.