server/storage/backend: dedupe intra-batch duplicate keys in writeback before merge#21987
server/storage/backend: dedupe intra-batch duplicate keys in writeback before merge#21987wilmerdooley wants to merge 1 commit into
Conversation
…k before merge Signed-off-by: wilmerdooley <wilmerdooley1@gmail.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: wilmerdooley The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @wilmerdooley. Thanks for your PR. I'm waiting for a etcd-io member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
When the read buffer already holds a bucket,
txWriteBuffer.writebackfolds the write buffer in viabucketBuffer.merge. The non-sequential path was sorting and assuming no duplicate keys, butput/putInternal/addappend unconditionally, so a key overwritten twice in one batch (e.g.[(k,OLD),(k,NEW)]) survived the merge. A subsequent single-key read withendKey == nilforceslimit=1, andbucketBuffer.Range/sort.Searchreturns the smallest-index match, returning the staleOLDvalue from the in-memory buffer.This change makes the non-sequential branch in
writebackcallwb.dedupe()beforerb.merge(wb), mirroring the sibling delete branch and honoring the newest-wins contract thatTestDedupepins.bucketBuffer.mergeitself is intentionally left unchanged: its only caller (writeback) now guarantees the source is duplicate-free for non-seq buckets, and adding dedupe inside merge would double-dedupe non-seq buckets while needlessly sorting the hot sequentialKey/revision bucket on every merge.Resolves #21986
Changes:
server/storage/backend/tx_buffer.go: intxWriteBuffer.writeback, replace the non-sequentialsort.Sort(wb)(gated onwb.used > 1) withwb.dedupe()for the read-buffer-already-has-bucket branch.server/storage/backend/tx_buffer_test.go: addTestWritebackDedupesIntraBatchDuplicateForNonSeqBucket, a regression test that exercises the bug through the realwritebackpath on a non-sequential bucket. It puts the same key twice in one batch, primes the read buffer with a smaller key so the no-overlap merge fast-path fires, callstxw.writeback(txr), and asserts that the subsequenttxr.Range(bucket, key, nil, 1)returnsNEW. The test fails on the previoussort.Sortlogic and passes after the fix.This PR was written in part with the assistance of generative AI; all changes were authored and reviewed by me.