-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-28440][changelog] Fix premature discard of state handles during checkpoint restore #27599
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
Open
nateab
wants to merge
2
commits into
apache:master
Choose a base branch
from
nateab:fix/FLINK-28440-changelog-handle-pinning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ scalastyle-output.xml | |
| .bloop | ||
| .cursor | ||
| .claude | ||
| .worktrees | ||
| .metadata | ||
| .settings | ||
| .project | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import org.apache.flink.changelog.fs.StateChangeUploadScheduler.UploadTask; | ||
| import org.apache.flink.runtime.state.KeyGroupRange; | ||
| import org.apache.flink.runtime.state.LocalRecoveryConfig; | ||
| import org.apache.flink.runtime.state.PhysicalStateHandleID; | ||
| import org.apache.flink.runtime.state.SnapshotResult; | ||
| import org.apache.flink.runtime.state.StreamStateHandle; | ||
| import org.apache.flink.runtime.state.changelog.ChangelogStateHandleStreamImpl; | ||
|
|
@@ -43,9 +44,11 @@ | |
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NavigableMap; | ||
| import java.util.Set; | ||
| import java.util.TreeMap; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
@@ -140,6 +143,12 @@ class FsStateChangelogWriter implements StateChangelogWriter<ChangelogStateHandl | |
|
|
||
| private boolean closed; | ||
|
|
||
| /** | ||
| * Handles pinned (retained) per checkpoint to prevent premature discard during truncation. Keyed | ||
| * by checkpointId, value is the list of handles that were retained for that checkpoint. | ||
| */ | ||
| private final TreeMap<Long, List<StreamStateHandle>> checkpointPinnedHandles = new TreeMap<>(); | ||
|
|
||
| private final MailboxExecutor mailboxExecutor; | ||
|
|
||
| private final TaskChangelogRegistry changelogRegistry; | ||
|
|
@@ -253,12 +262,13 @@ private CompletableFuture<SnapshotResult<ChangelogStateHandleStreamImpl>> persis | |
| if (range.size() == readyToReturn.size()) { | ||
| checkState(toUpload.isEmpty()); | ||
| return CompletableFuture.completedFuture( | ||
| buildSnapshotResult(keyGroupRange, readyToReturn, 0L)); | ||
| buildSnapshotResult(keyGroupRange, readyToReturn, 0L, checkpointId)); | ||
| } else { | ||
| CompletableFuture<SnapshotResult<ChangelogStateHandleStreamImpl>> future = | ||
| new CompletableFuture<>(); | ||
| uploadCompletionListeners.add( | ||
| new UploadCompletionListener(keyGroupRange, range, readyToReturn, future)); | ||
| new UploadCompletionListener( | ||
| keyGroupRange, range, readyToReturn, future, checkpointId)); | ||
| if (!toUpload.isEmpty()) { | ||
| UploadTask uploadTask = | ||
| new UploadTask( | ||
|
|
@@ -335,6 +345,10 @@ public void close() { | |
| activeChangeSetSize = 0; | ||
| notUploaded.clear(); | ||
| uploaded.clear(); | ||
| for (List<StreamStateHandle> handles : checkpointPinnedHandles.values()) { | ||
| handles.forEach(changelogRegistry::release); | ||
| } | ||
| checkpointPinnedHandles.clear(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -380,6 +394,21 @@ public void confirm(SequenceNumber from, SequenceNumber to, long checkpointId) { | |
| from, | ||
| to, | ||
| activeSequenceNumber); | ||
| // Unpin handles for this and all older checkpoints. | ||
| // Using stopTracking (not release) because JM has confirmed ownership. | ||
| NavigableMap<Long, List<StreamStateHandle>> toUnpin = | ||
| checkpointPinnedHandles.headMap(checkpointId, true); | ||
| if (!toUnpin.isEmpty()) { | ||
| LOG.debug( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:move the |
||
| "Unpinning handles for {} checkpoint(s) up to {}", | ||
| toUnpin.size(), | ||
| checkpointId); | ||
| } | ||
| for (List<StreamStateHandle> handles : toUnpin.values()) { | ||
| handles.forEach(changelogRegistry::stopTracking); | ||
| } | ||
| toUnpin.clear(); | ||
|
|
||
| // it is possible that "uploaded" has already been truncated (after checkpoint subsumption) | ||
| // so do not check that "uploaded" contains the specified range | ||
| LOG.debug("Confirm [{}, {})", from, to); | ||
|
|
@@ -400,14 +429,27 @@ public void confirm(SequenceNumber from, SequenceNumber to, long checkpointId) { | |
|
|
||
| @Override | ||
| public void reset(SequenceNumber from, SequenceNumber to, long checkpointId) { | ||
| // Release pinned handles for the aborted checkpoint only. | ||
| List<StreamStateHandle> pinned = checkpointPinnedHandles.remove(checkpointId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious should we be checking SequenceNumber from or SequenceNumber to here? |
||
| if (pinned != null) { | ||
| LOG.debug( | ||
| "Releasing {} pinned handles for aborted checkpoint {}", | ||
| pinned.size(), | ||
| checkpointId); | ||
| pinned.forEach(changelogRegistry::release); | ||
| } | ||
|
|
||
| // delete all accumulated local dstl files when abort | ||
| localChangelogRegistry.discardUpToCheckpoint(checkpointId + 1); | ||
| } | ||
|
|
||
| private SnapshotResult<ChangelogStateHandleStreamImpl> buildSnapshotResult( | ||
| KeyGroupRange keyGroupRange, | ||
| NavigableMap<SequenceNumber, UploadResult> results, | ||
| long incrementalSize) { | ||
| long incrementalSize, | ||
| long checkpointId) { | ||
| pinHandlesForCheckpoint(results, checkpointId); | ||
|
|
||
| List<Tuple2<StreamStateHandle, Long>> tuples = new ArrayList<>(); | ||
| long size = 0; | ||
| for (UploadResult uploadResult : results.values()) { | ||
|
|
@@ -452,6 +494,33 @@ private SnapshotResult<ChangelogStateHandleStreamImpl> buildSnapshotResult( | |
| return SnapshotResult.of(jmChangelogStateHandle); | ||
| } | ||
|
|
||
| private void pinHandlesForCheckpoint( | ||
| NavigableMap<SequenceNumber, UploadResult> results, long checkpointId) { | ||
| if (checkpointId == DUMMY_PERSIST_CHECKPOINT) { | ||
| return; | ||
| } | ||
| Set<PhysicalStateHandleID> seen = new HashSet<>(); | ||
| List<StreamStateHandle> pinned = new ArrayList<>(); | ||
| for (UploadResult result : results.values()) { | ||
| StreamStateHandle handle = result.getStreamStateHandle(); | ||
| if (seen.add(handle.getStreamStateHandleID())) { | ||
| changelogRegistry.retain(handle); | ||
| pinned.add(handle); | ||
| } | ||
| StreamStateHandle localHandle = result.getLocalStreamHandleStateHandle(); | ||
| if (localHandle != null && seen.add(localHandle.getStreamStateHandleID())) { | ||
| changelogRegistry.retain(localHandle); | ||
| pinned.add(localHandle); | ||
| } | ||
| } | ||
| if (!pinned.isEmpty()) { | ||
| LOG.debug("Pinned {} handles for checkpoint {}", pinned.size(), checkpointId); | ||
| checkpointPinnedHandles | ||
| .computeIfAbsent(checkpointId, k -> new ArrayList<>()) | ||
| .addAll(pinned); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| SequenceNumber lastAppendedSqnUnsafe() { | ||
| return activeSequenceNumber; | ||
|
|
@@ -482,19 +551,22 @@ private final class UploadCompletionListener { | |
| completionFuture; | ||
| private final KeyGroupRange keyGroupRange; | ||
| private final SequenceNumberRange changeRange; | ||
| private final long checkpointId; | ||
|
|
||
| private UploadCompletionListener( | ||
| KeyGroupRange keyGroupRange, | ||
| SequenceNumberRange changeRange, | ||
| Map<SequenceNumber, UploadResult> uploaded, | ||
| CompletableFuture<SnapshotResult<ChangelogStateHandleStreamImpl>> | ||
| completionFuture) { | ||
| completionFuture, | ||
| long checkpointId) { | ||
| checkArgument( | ||
| !changeRange.isEmpty(), "Empty change range not allowed: %s", changeRange); | ||
| this.uploaded = new TreeMap<>(uploaded); | ||
| this.completionFuture = completionFuture; | ||
| this.keyGroupRange = keyGroupRange; | ||
| this.changeRange = changeRange; | ||
| this.checkpointId = checkpointId; | ||
| } | ||
|
|
||
| public boolean onSuccess(List<UploadResult> uploadResults) { | ||
|
|
@@ -505,7 +577,11 @@ public boolean onSuccess(List<UploadResult> uploadResults) { | |
| incrementalSize += uploadResult.getSize(); | ||
| if (uploaded.size() == changeRange.size()) { | ||
| completionFuture.complete( | ||
| buildSnapshotResult(keyGroupRange, uploaded, incrementalSize)); | ||
| buildSnapshotResult( | ||
| keyGroupRange, | ||
| uploaded, | ||
| incrementalSize, | ||
| checkpointId)); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious - do we need the values to have order? If not can we use a Set.