Skip to content

etcdserver: optimize range request with KEY ASCEND sorting#21326

Open
kairosci wants to merge 3 commits intoetcd-io:mainfrom
kairosci:fix-range-limit-optimization
Open

etcdserver: optimize range request with KEY ASCEND sorting#21326
kairosci wants to merge 3 commits intoetcd-io:mainfrom
kairosci:fix-range-limit-optimization

Conversation

@kairosci
Copy link
Copy Markdown
Contributor

@kairosci kairosci commented Feb 17, 2026

This PR optimizes range request handling by passing the limit to the backend when sorting by key in ascending order, as the backend already returns keys in this order. This avoids fetching all keys and then truncating in memory.

goos: windows
goarch: amd64
pkg: go.etcd.io/etcd/server/v3/etcdserver/txn
cpu: Intel(R) Core(TM) 5 120U

                                            │        old.txt        │              new.txt               │
                                            │         sec/op        │   sec/op      vs base              │
--------------------------------------------------------------------------------------------------------
RangeLimitOptimization/KeyAscendLimit100_10k/Old       3.056m ± 0%       26.93μ ± 0%   -99.12% (p=0.000)
RangeLimitOptimization/KeyAscendLimit100_100k/Old     16.639m ± 0%       27.11μ ± 0%   -99.84% (p=0.000)
RangeLimitOptimization/KeyAscendLimit100_1m/Old     168.512m ± 0%       25.22μ ± 0%   -99.99% (p=0.000)
RangeLimitOptimization/KeyAscendLimit1000_10k/Old     2.412m ± 0%      256.44μ ± 0%   -89.37% (p=0.000)
geomean                                        14.274m              49.52μ         -99.65%

Closes #20745

@k8s-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: kairosci
Once this PR has been reviewed and has the lgtm label, please assign siyuanfoundation for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link
Copy Markdown

Hi @kairosci. 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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@kairosci kairosci force-pushed the fix-range-limit-optimization branch from b24d5be to 781fc18 Compare February 17, 2026 18:18
@kairosci kairosci changed the title etcdserver: optimize range request with KEY ASCEND sorting (Fixes #20745) etcdserver: optimize range request with KEY ASCEND sorting Mar 4, 2026
@kairosci kairosci force-pushed the fix-range-limit-optimization branch from ee975e2 to 0e82e30 Compare March 13, 2026 14:30
@kairosci kairosci requested a review from serathius March 13, 2026 14:35
@kairosci
Copy link
Copy Markdown
Contributor Author

Addressed the review feedback:

Replaced the TestRangeLimit unit test with integration tests that test the full Range function:

  • TestRangeWithSortAndLimit: Tests correctness with various sort orders, targets, and limits
  • TestRangeKeyAscendOptimizationCorrectness: Validates the optimized path produces identical results to non-optimized paths
  • BenchmarkRange: Benchmarks demonstrating ~40x performance improvement with KEY ASCEND sorting

The tests no longer depend on the internal rangeLimit function.

@kairosci
Copy link
Copy Markdown
Contributor Author

@serathius could you take another look? Thank you!

@serathius
Copy link
Copy Markdown
Member

/ok-to-test

Nont can you do a first round of review?
/cc @nwnt

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.27%. Comparing base (23085b4) to head (6ef7e66).
⚠️ Report is 203 commits behind head on main.

⚠️ Current head 6ef7e66 differs from pull request most recent head 871d697

Please upload reports for the commit 871d697 to get more accurate results.

Additional details and impacted files
Files with missing lines Coverage Δ
server/etcdserver/txn/range.go 91.30% <100.00%> (-0.23%) ⬇️

... and 75 files with indirect coverage changes

@@            Coverage Diff             @@
##             main   #21326      +/-   ##
==========================================
+ Coverage   65.55%   68.27%   +2.72%     
==========================================
  Files         428      429       +1     
  Lines       35262    35390     +128     
==========================================
+ Hits        23115    24164    +1049     
+ Misses      10737     9811     -926     
- Partials     1410     1415       +5     

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23085b4...871d697. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@serathius
Copy link
Copy Markdown
Member

Please fix

../server/etcdserver/txn/range_test.go:194:4: formatter: use assert.Equalf (testifylint)
			assert.Equal(t, tt.wantKeys, gotKeys, "returned keys mismatch")
			^
../server/etcdserver/txn/range_test.go:195:4: formatter: use assert.Equalf (testifylint)
			assert.Equal(t, tt.wantMore, resp.More, "More flag mismatch")
			^
../server/etcdserver/txn/range_test.go:196:4: formatter: use assert.Equalf (testifylint)
			assert.Equal(t, tt.wantCount, resp.Count, "Count mismatch")
			^
../server/etcdserver/txn/range_test.go:251:4: len: use assert.Len (testifylint)
			assert.Equal(t, len(respNoSort.Kvs), len(respAscend.Kvs), "number of keys mismatch")
			^
../server/etcdserver/txn/range_test.go:253:5: formatter: use assert.Equalf (testifylint)
				assert.Equal(t, string(respNoSort.Kvs[i].Key), string(respAscend.Kvs[i].Key),
				^
../server/etcdserver/txn/range_test.go:256:4: formatter: use assert.Equalf (testifylint)
			assert.Equal(t, respNoSort.More, respAscend.More, "More flag mismatch")
			^
../server/etcdserver/txn/range_test.go:257:4: formatter: use assert.Equalf (testifylint)
			assert.Equal(t, respNoSort.Count, respAscend.Count, "Count mismatch")
			^
7 issues:

@kairosci kairosci force-pushed the fix-range-limit-optimization branch 2 times, most recently from 2ad5361 to 7eee58c Compare March 18, 2026 10:47
@kairosci kairosci requested a review from nwnt April 4, 2026 23:09
@serathius
Copy link
Copy Markdown
Member

Please squash commits

@kairosci kairosci force-pushed the fix-range-limit-optimization branch from 1aa2192 to a06b3f4 Compare April 6, 2026 17:42
@kairosci kairosci marked this pull request as draft April 6, 2026 17:52
@k8s-ci-robot k8s-ci-robot added size/L and removed size/M labels Apr 7, 2026
@kairosci kairosci force-pushed the fix-range-limit-optimization branch from 4257430 to 1b02505 Compare April 7, 2026 17:06
@kairosci kairosci force-pushed the fix-range-limit-optimization branch 2 times, most recently from 050154e to bf50b8b Compare April 7, 2026 17:08
@kairosci kairosci requested a review from serathius April 7, 2026 17:09
@kairosci
Copy link
Copy Markdown
Contributor Author

kairosci commented Apr 7, 2026

/test pull-etcd-robustness-amd64

@kairosci
Copy link
Copy Markdown
Contributor Author

kairosci commented Apr 7, 2026

@serathius So I've rebased and removed the tests, leaving only server/etcdserver/txn/range.go in this PR. Let me know if there are any other changes to make; it should be fine.

@serathius
Copy link
Copy Markdown
Member

Please utilize the added range benchmark to show improvement that this PR brings. See https://www.youtube.com/watch?v=jiXnzkAzy30 for how to benchmark code.

@kairosci kairosci closed this Apr 7, 2026
@kairosci kairosci deleted the fix-range-limit-optimization branch April 7, 2026 20:07
@kairosci kairosci restored the fix-range-limit-optimization branch April 7, 2026 20:10
@kairosci kairosci reopened this Apr 7, 2026
@kairosci kairosci force-pushed the fix-range-limit-optimization branch from 738c473 to 5ba140f Compare April 7, 2026 20:18
@kairosci kairosci force-pushed the fix-range-limit-optimization branch 2 times, most recently from 727a2bb to dae1c74 Compare April 7, 2026 20:20
Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
@serathius
Copy link
Copy Markdown
Member

I analyzed the benchmarks using Daniel Martí’s approach.

Can you just paste the results like in #21486

…Shows 4.5x-17x speedup on real workloads.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
@k8s-ci-robot
Copy link
Copy Markdown

@kairosci: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-etcd-verify 6ef7e66 link true /test pull-etcd-verify
pull-etcd-unit-test-386 6ef7e66 link true /test pull-etcd-unit-test-386
pull-etcd-unit-test-amd64 6ef7e66 link true /test pull-etcd-unit-test-amd64
pull-etcd-unit-test-arm64 6ef7e66 link true /test pull-etcd-unit-test-arm64
pull-etcd-integration-1-cpu-amd64 6ef7e66 link true /test pull-etcd-integration-1-cpu-amd64
pull-etcd-coverage-report 6ef7e66 link true /test pull-etcd-coverage-report

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Details

Instructions 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. I understand the commands that are listed here.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/testing github_actions Pull requests that update GitHub Actions code ok-to-test size/S

Development

Successfully merging this pull request may close these issues.

Range requests with limit ignore limit when sorting by key ascending

4 participants