-
Notifications
You must be signed in to change notification settings - Fork 4.1k
kvstorage: add WAG truncation infrastructure #167533
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright 2026 The Cockroach Authors. | ||
| // | ||
| // Use of this software is governed by the CockroachDB Software License | ||
| // included in the /LICENSE file. | ||
|
|
||
| package kvstorage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/cockroachdb/cockroach/pkg/keys" | ||
| "github.com/cockroachdb/cockroach/pkg/kv/kvpb" | ||
| "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/wag" | ||
| "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/wag/wagpb" | ||
| "github.com/cockroachdb/cockroach/pkg/roachpb" | ||
| "github.com/cockroachdb/cockroach/pkg/storage" | ||
| "github.com/cockroachdb/errors" | ||
| ) | ||
|
|
||
| // SideloadClearer truncates sideloaded files for a given range up to and | ||
| // including the specified raft log index. Files beyond this index may belong | ||
| // to a newer replica and must be preserved. | ||
| // The SideloadClearer must also sync the sideloaded storage after truncation to | ||
| // avoid leaking the files in case of a node crash. | ||
| type SideloadClearer func(ctx context.Context, rangeID roachpb.RangeID, lastIndex kvpb.RaftIndex) error | ||
|
|
||
| // clearReplicaRaftLog clears raft log entries at or below the given index for | ||
| // a destroyed or subsumed replica. | ||
| func clearReplicaRaftLog( | ||
| ctx context.Context, raft Raft, rangeID roachpb.RangeID, lastIndex kvpb.RaftIndex, | ||
| ) error { | ||
| return storage.ClearRangeWithHeuristic( | ||
| ctx, raft.RO, raft.WO, | ||
| keys.RaftLogPrefix(rangeID), /* start */ | ||
| keys.RaftLogKey(rangeID, lastIndex+1), /* end */ | ||
| ClearRangeThresholdPointKeys(), | ||
| ) | ||
| } | ||
|
|
||
| // truncateAppliedWAGNodeAndClearRaftState checks the first WAG node and | ||
| // deletes it if all of its events have been applied to the state engine. For | ||
| // nodes containing EventDestroy or EventSubsume events, it also clears the | ||
| // corresponding raft log prefix from the engine and the sideloaded entries | ||
| // storage. | ||
| // | ||
| // The caller must provide a stateRO reader with GuaranteedDurability so that | ||
| // only state confirmed flushed to persistent storage is visible. This ensures | ||
| // we never delete a WAG node whose mutations aren't flushed yet. | ||
| // | ||
| // Returns a boolean indicating whether a node was successfully truncated or | ||
| // not. If the return value is false, it means that either there are no WAG | ||
| // nodes left, or that the WAG node has not been applied to the state engine. | ||
| // Also, an error is returned if the WAG node could not be fetched or deleted. | ||
| // TODO(ibrahim): Support deleting multiple WAG nodes within the same batch. | ||
| func truncateAppliedWAGNodeAndClearRaftState( | ||
|
pav-kv marked this conversation as resolved.
|
||
| ctx context.Context, raft Raft, stateRO StateRO, clearSideloaded SideloadClearer, | ||
| ) (bool, error) { | ||
| var iter wag.Iterator | ||
| for index, node := range iter.Iter(ctx, raft.RO) { | ||
| // TODO(ibrahim): Right now, the canApplyWAGNode function returns a list of | ||
| // raftCatchUpTargets that are not needed for the purposes of truncation, | ||
| // consider refactoring the function to return only the needed info. | ||
| replayAction, err := canApplyWAGNode(ctx, node, stateRO) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if replayAction.apply { | ||
| // If an event needs to be applied, the WAG node cannot be deleted yet. | ||
| return false, nil | ||
| } | ||
| if err := wag.Delete(raft.WO, index); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| // Clean up the raft log prefix of a destroyed/subsumed replica. | ||
| for _, event := range node.Events { | ||
| if event.Type != wagpb.EventDestroy && event.Type != wagpb.EventSubsume { | ||
| continue | ||
| } | ||
| if err := clearReplicaRaftLog(ctx, raft, event.Addr.RangeID, event.Addr.Index); err != nil { | ||
| return false, errors.Wrapf(err, "clearing raft log for r%d", event.Addr.RangeID) | ||
| } | ||
| if clearSideloaded != nil { | ||
| // In general, we shouldn't delete sideloaded files before committing the | ||
| // batch that deletes the Raft log entries. However, in this case, we | ||
| // know that the destroy/subsume event has already been flushed to disk, | ||
| // and the replica is destroyed. We will not need to reference the | ||
| // sideloaded files anymore. If we were to delay deleting them till | ||
| // after the batch is committed, we risk leaking the sideloaded files | ||
| // if a node crash happens after the batch is committed (and synced) and | ||
| // before the sideloaded files are deleted. | ||
| if err := clearSideloaded(ctx, event.Addr.RangeID, event.Addr.Index); err != nil { | ||
| return false, errors.Wrapf(err, "clearing sideloaded files for r%d", event.Addr.RangeID) | ||
| } | ||
| } | ||
| } | ||
| return true, nil | ||
| } | ||
| return false, iter.Error() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.