Skip to content

Commit d45b333

Browse files
committed
spanset: comment mergeSpans
Epic: none Release note: none
1 parent 9b3327a commit d45b333

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pkg/kv/kvserver/spanset/merge.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ import "slices"
1010
// mergeSpans sorts the given spans and merges ones with overlapping spans and
1111
// equal access timestamps. The implementation is a modified roachpb.MergeSpans.
1212
//
13+
// The input and output spans represent the same subset of {keys}x{timestamps}
14+
// space. The resulting spans are ordered by key. It is minimal (impossible to
15+
// "merge" further without losing precision) only if there are no keys present
16+
// under multiple timestamps.
17+
//
18+
// When there are keys present under multiple timestamps, the resulting merged
19+
// set is not guaranteed to be minimal. For example, [{a-b}@10, b@20, {b-c}@10]
20+
// is returned as is, even though we could merge it as [{a-c}@10, b@20]. This is
21+
// due to an arbitrary choice of sorting it by key.
22+
//
23+
// TODO(pav-kv): we could sort by (timestamp, key), which makes it possible to
24+
// merge spans with the same timestamp and guarantee minimality. After which, we
25+
// can optionally sort by key again. This is still more expensive than one sort,
26+
// so consider if the users are fine with the set sorted by (timestamp, key).
27+
//
28+
// The priority for this function is to be simple and free from assumptions
29+
// about how to "resolve" the situation of having the same key under multiple
30+
// timestamps. E.g. it does not assume that higher/lower timestamp "wins", or
31+
// what the semantics of an empty timestamp are. Considerations like that are
32+
// left to the consumer of this span set, e.g. the latch manager.
33+
//
1334
// The input spans are not safe for re-use.
1435
func mergeSpans(latches []Span) []Span {
1536
if len(latches) == 0 {

pkg/kv/kvserver/spanset/spanset.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,13 @@ func (s *SpanSet) Merge(s2 *SpanSet) {
212212
s.SortAndDedup()
213213
}
214214

215-
// SortAndDedup sorts the spans in the SpanSet and removes any duplicates.
215+
// SortAndDedup sorts the spans in the SpanSet by key and removes duplicate or
216+
// overlapping / redundant spans. The SpanSet represents the same subset of the
217+
// {keys}x{timestamps} space before and after this call, but is not guaranteed
218+
// to have a minimal number of spans in a general case (see mergeSpans comment).
219+
//
220+
// TODO(pav-kv): does this make CheckAllowed falsely fail in some cases? Maybe
221+
// it's fine: importantly, it should not falsely succeed.
216222
func (s *SpanSet) SortAndDedup() {
217223
for sa := SpanAccess(0); sa < NumSpanAccess; sa++ {
218224
for ss := SpanScope(0); ss < NumSpanScope; ss++ {

0 commit comments

Comments
 (0)