Skip to content

Commit

Permalink
sort by chunk first (tikv#1456)
Browse files Browse the repository at this point in the history
Signed-off-by: Ping Yu <[email protected]>
  • Loading branch information
pingyu authored Sep 2, 2024
1 parent 8cc71f4 commit 53f61c5
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions txnkv/transaction/txn_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ func (cs *txnChunkSlice) groupToBatches(c *locate.RegionCache, bo *retry.Backoff
batches = append(batches, *batch)
}
sort.Slice(batches, func(i, j int) bool {
// Sort by both chunks and region, to make sure that primary key is in the first batch:
// Sort by chunks first, and then by region, to make sure that primary key is in the first batch:
// 1. Different batches may contain the same chunks.
// 2. Different batches may have regions with same start key (if region merge happens during grouping).
cmp := bytes.Compare(batches[i].region.StartKey, batches[j].region.StartKey)
// 3. Bigger batches may have regions with smaller start key (if region merge happens during grouping).
cmp := bytes.Compare(batches[i].Smallest(), batches[j].Smallest())
if cmp == 0 {
return bytes.Compare(batches[i].Smallest(), batches[j].Smallest()) < 0
return bytes.Compare(batches[i].region.StartKey, batches[j].region.StartKey) < 0
}
return cmp < 0
})
Expand Down

0 comments on commit 53f61c5

Please sign in to comment.