From 53f61c5a21004b0ae176f868c303bf267b6f0f46 Mon Sep 17 00:00:00 2001 From: Ping Yu Date: Mon, 2 Sep 2024 15:27:57 +0800 Subject: [PATCH] sort by chunk first (#1456) Signed-off-by: Ping Yu --- txnkv/transaction/txn_file.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/txnkv/transaction/txn_file.go b/txnkv/transaction/txn_file.go index 7cfe9654e..11bab81f1 100644 --- a/txnkv/transaction/txn_file.go +++ b/txnkv/transaction/txn_file.go @@ -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 })