Skip to content

Conversation

silentred
Copy link
Contributor

@silentred silentred commented Aug 30, 2025

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

for #20271 , related to #20553

This PR also can pass #20503 , but with a shorter lock range.

In this PR, I moved OpenSnapshotBackend logic into backend instance. So that the defrag and ReopenFromSnapshotFile could be controlled by locks(batchTx, mu, readTx) in backend.
In applySnapshot process, there is no need to close old backend, because db file is replaced in place inside old backend.

s.consistIndex.SetBackend(newbe) has to be moved up, because it would fail to verity previous and current cindex in the unsafeUpdateConsistentIndex function if it is not.

I think defrag would not change logic data, so it is safe to happen during any part of restoring, e.g. kv restore, lessor restore, alarm restore.

This is not final PR, just a provement of thoughts.

cc @fuweid @ahrtr

Signed-off-by: shenmu.wy <[email protected]>
@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot
Copy link

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

Signed-off-by: shenmu.wy <[email protected]>
@silentred silentred changed the title [NotMerge] another fix of 20271 with short lock range [NotMerge] another fix of 20271 Aug 30, 2025
@ahrtr
Copy link
Member

ahrtr commented Sep 1, 2025

Ideally, we should protect the applySnapshot process starting FROM renaming the snapshot file,

newbe, err := serverstorage.OpenSnapshotBackend(s.Cfg, s.snapshotter, toApply.snapshot, s.beHooks)

if err := os.Rename(snapPath, cfg.BackendPath()); err != nil {

TO setting the new backend,

s.be = newbe

The problem is that during the above period, there are two different backend instances, but both of which point to the same db file.

  • Note that normally the old backend instance still reads from the old db file even after the renaming, until you restart etcdserver or perform a defragment right after the renaming or setting the new backend (s.be = newbe).
  • Normal operations (e.g. read, write) against two backend instances can't prevent each other.

We should block any write to both backends during the above process. Otherwise, any write to the old instance will be lost once we set the new backend (s.be = newbe).

Please let me know if you want to continue to work on this PR. thx

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.

3 participants