Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etcdutl: use map to count unique user keys in snapshot status #19344

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

jxustc
Copy link
Contributor

@jxustc jxustc commented Feb 5, 2025

The new implementation:

  • Uses map to track unique keys for accurate counting
  • Excludes internal built-in keys from total count
  • Improves code maintainability

Although this approach uses additional memory for the map, the trade-off is acceptable since:

  • Status() is not in hot path
  • Correctness takes priority over performance optimization
  • Simpler code is easier to maintain

Fixes #19253

Please read https://github.com/etcd-io/etcd/blob/main/CONTRIBUTING.md#contribution-flow.

@k8s-ci-robot
Copy link

Hi @jxustc. 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.

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.

@ahrtr
Copy link
Member

ahrtr commented Feb 5, 2025

Thanks for the PR, but I am afraid that the fix isn't accurate. When a key has already been removed from user perspective, but it is still in the db file with tombstone flag set. We shouldn't count such keys.

Refer to https://etcd.io/docs/v3.5/learning/data_model/

@jxustc
Copy link
Contributor Author

jxustc commented Feb 6, 2025

Thanks for the PR, but I am afraid that the fix isn't accurate. When a key has already been removed from user perspective, but it is still in the db file with tombstone flag set. We shouldn't count such keys.

Refer to https://etcd.io/docs/v3.5/learning/data_model/

Thanks for the feedback. I hadn't considered the tombstone case. I will update the implementation to exclude deleted keys.

@k8s-ci-robot k8s-ci-robot added size/L and removed size/S labels Feb 6, 2025
@jxustc jxustc force-pushed the fix/etcdutl-key-count branch from e67feac to 775af72 Compare February 6, 2025 07:11
Comment on lines 182 to 190
key := string(kv.Key)
// Version == 0 indicates logical deletion.
// refer to https://etcd.io/docs/v3.5/learning/data_model/
isActive := kv.Version > 0
if curr, exists := keyStates[key]; !exists || rev.Main > curr.rev {
keyStates[key] = struct {
rev int64
active bool
}{rev.Main, isActive}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Pls use isTombstone to check whether it's a tombstone. We can expose the function.
    func isTombstone(b []byte) bool {
  • No need to compare revision. They are guaranteed to be increasing.

@jxustc jxustc force-pushed the fix/etcdutl-key-count branch 3 times, most recently from 122d5e1 to 049178d Compare February 7, 2025 03:29
@@ -118,6 +118,106 @@ func TestSnapshotStatusNegativeRevisionSub(t *testing.T) {
require.ErrorContains(t, err, "negative revision")
}

// TestSnapshotStatusDuplicateKeys tests if TotalKey in status remains unchanged when inserting the same key multiple times
func TestSnapshotStatusDuplicateKeys(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the test cases should be able to be consolidated into one table-driven sub tests. But not a big problem.

@ahrtr
Copy link
Member

ahrtr commented Feb 7, 2025

/ok-to-test

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.93%. Comparing base (d62cdee) to head (2d37700).
Report is 25 commits behind head on main.

Additional details and impacted files
Files with missing lines Coverage Δ
etcdutl/snapshot/v3_snapshot.go 58.30% <100.00%> (+0.86%) ⬆️
server/storage/mvcc/revision.go 91.83% <100.00%> (+0.34%) ⬆️

... and 32 files with indirect coverage changes

@@            Coverage Diff             @@
##             main   #19344      +/-   ##
==========================================
+ Coverage   68.84%   68.93%   +0.09%     
==========================================
  Files         420      420              
  Lines       35716    35731      +15     
==========================================
+ Hits        24587    24630      +43     
+ Misses       9695     9680      -15     
+ Partials     1434     1421      -13     

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 d62cdee...2d37700. Read the comment docs.

@jxustc
Copy link
Contributor Author

jxustc commented Feb 7, 2025

Thanks for the review. You make a good point about table-driven tests. I'm happy to refactor them. I also noticed some e2e test failures due to the changed TotalKey semantics - I'll fix those as well.

The new implementation:
- Uses map to track unique keys for accurate counting
- Excludes internal built-in keys from total count
- Improves code maintainability

Although this approach uses additional memory for the map, the trade-off
is acceptable since:
- Status() is not in hot path
- Correctness takes priority over performance optimization
- Simpler code is easier to maintain

Fixes etcd-io#19253

Signed-off-by: Xiang Ji <[email protected]>
@jxustc jxustc force-pushed the fix/etcdutl-key-count branch from f7751dd to 2d37700 Compare February 7, 2025 11:57
Copy link
Member

@ahrtr ahrtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahrtr
Copy link
Member

ahrtr commented Feb 7, 2025

Please also update 3.6 changelog https://github.com/etcd-io/etcd/blob/main/CHANGELOG/CHANGELOG-3.6.md#etcdutl-v3

It could be in a in a separate PR

@ahrtr
Copy link
Member

ahrtr commented Feb 7, 2025

cc @fuweid @serathius

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ahrtr, fuweid, jxustc

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

The pull request process is described here

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

@ahrtr ahrtr merged commit a747fde into etcd-io:main Feb 7, 2025
38 checks passed
@ahrtr
Copy link
Member

ahrtr commented Feb 7, 2025

Please also update 3.6 changelog https://github.com/etcd-io/etcd/blob/main/CHANGELOG/CHANGELOG-3.6.md#etcdutl-v3

Done in #19361

@jxustc
Copy link
Contributor Author

jxustc commented Feb 8, 2025

Please also update 3.6 changelog https://github.com/etcd-io/etcd/blob/main/CHANGELOG/CHANGELOG-3.6.md#etcdutl-v3

Done in #19361

Thx

@jxustc jxustc deleted the fix/etcdutl-key-count branch February 8, 2025 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

etcdutl Snapshot Status: TotalKey Calculation Inquiry
4 participants