-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[Flink] Bound SDF self-checkpoint residual state on the portable runner #39191
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
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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,15 @@ | |
| import org.apache.beam.runners.core.StateInternalsFactory; | ||
| import org.apache.beam.runners.core.StateNamespace; | ||
| import org.apache.beam.runners.core.StateNamespaces; | ||
| import org.apache.beam.runners.core.StateTag; | ||
| import org.apache.beam.runners.core.StateTags; | ||
| import org.apache.beam.runners.core.TimerInternals; | ||
| import org.apache.beam.runners.core.TimerInternalsFactory; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
| import org.apache.beam.sdk.fn.IdGenerator; | ||
| import org.apache.beam.sdk.fn.IdGenerators; | ||
| import org.apache.beam.sdk.state.MapState; | ||
| import org.apache.beam.sdk.state.TimeDomain; | ||
| import org.apache.beam.sdk.transforms.windowing.BoundedWindow; | ||
| import org.apache.beam.sdk.util.CoderUtils; | ||
|
|
@@ -48,7 +51,7 @@ public class BundleCheckpointHandlers { | |
| /** | ||
| * A {@link BundleCheckpointHandler} which uses {@link | ||
| * org.apache.beam.runners.core.TimerInternals.TimerData} and {@link | ||
| * org.apache.beam.sdk.state.ValueState} to reschedule {@link DelayedBundleApplication}. | ||
| * org.apache.beam.sdk.state.MapState} to reschedule {@link DelayedBundleApplication}. | ||
| */ | ||
| public static class StateAndTimerBundleCheckpointHandler<T> implements BundleCheckpointHandler { | ||
|
|
||
|
|
@@ -82,6 +85,20 @@ private static String constructSdfCheckpointId(String id, int index) { | |
| return SDF_PREFIX + ":" + id + ":" + index; | ||
| } | ||
|
|
||
| /** The state id under which all SDF self-checkpoint residuals for a key/window are stored. */ | ||
|
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. Just a note, it appears BundleCheckpointHandlers is only used by Flink runner, thus this bug only affects Flink runner as well |
||
| public static final String SDF_RESIDUAL_STATE = "sdf_checkpoint_residuals"; | ||
|
|
||
| /** | ||
| * The single, stable state tag holding every SDF self-checkpoint residual for a key/window, | ||
| * keyed by the per-residual checkpoint id. Storing all residuals under one stable descriptor, | ||
| * instead of a fresh {@link StateTags#value} per residual, keeps a polling SDF's keyed state | ||
| * bounded. See https://github.com/apache/beam/issues/27648. | ||
| */ | ||
| public static <T> StateTag<MapState<String, WindowedValue<T>>> residualStateTag( | ||
| Coder<WindowedValue<T>> residualCoder) { | ||
| return StateTags.map(SDF_RESIDUAL_STATE, StringUtf8Coder.of(), residualCoder); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCheckpoint(ProcessBundleResponse response) { | ||
| String id = idGenerator.getId(); | ||
|
|
@@ -126,8 +143,9 @@ public void onCheckpoint(ProcessBundleResponse response) { | |
| Instant.ofEpochMilli(outputTimestamp), | ||
| TimeDomain.PROCESSING_TIME); | ||
| stateInternals | ||
| .state(stateNamespace, StateTags.value(tag, residualCoder)) | ||
| .write( | ||
| .state(stateNamespace, residualStateTag(residualCoder)) | ||
| .put( | ||
| tag, | ||
| WindowedValues.of( | ||
| stateValue.getValue(), | ||
| stateValue.getTimestamp(), | ||
|
|
||
125 changes: 125 additions & 0 deletions
125
...c/test/java/org/apache/beam/runners/fnexecution/control/BundleCheckpointHandlersTest.java
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,125 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.beam.runners.fnexecution.control; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import org.apache.beam.model.fnexecution.v1.BeamFnApi.BundleApplication; | ||
| import org.apache.beam.model.fnexecution.v1.BeamFnApi.DelayedBundleApplication; | ||
| import org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleResponse; | ||
| import org.apache.beam.runners.core.InMemoryStateInternals; | ||
| import org.apache.beam.runners.core.InMemoryTimerInternals; | ||
| import org.apache.beam.runners.core.StateInternals; | ||
| import org.apache.beam.runners.core.StateInternalsFactory; | ||
| import org.apache.beam.runners.core.StateNamespace; | ||
| import org.apache.beam.runners.core.StateTag; | ||
| import org.apache.beam.runners.core.TimerInternalsFactory; | ||
| import org.apache.beam.runners.fnexecution.control.BundleCheckpointHandlers.StateAndTimerBundleCheckpointHandler; | ||
| import org.apache.beam.sdk.coders.Coder; | ||
| import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
| import org.apache.beam.sdk.state.State; | ||
| import org.apache.beam.sdk.state.StateContext; | ||
| import org.apache.beam.sdk.transforms.windowing.GlobalWindow; | ||
| import org.apache.beam.sdk.util.CoderUtils; | ||
| import org.apache.beam.sdk.values.WindowedValue; | ||
| import org.apache.beam.sdk.values.WindowedValues; | ||
| import org.apache.beam.vendor.grpc.v1p69p0.com.google.protobuf.ByteString; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| /** Tests for {@link BundleCheckpointHandlers}. */ | ||
| @RunWith(JUnit4.class) | ||
| public class BundleCheckpointHandlersTest { | ||
|
|
||
| private static final Coder<WindowedValue<String>> RESIDUAL_CODER = | ||
| WindowedValues.getFullCoder(StringUtf8Coder.of(), GlobalWindow.Coder.INSTANCE); | ||
|
|
||
| /** | ||
| * Regression test for https://github.com/apache/beam/issues/27648. | ||
| * | ||
| * <p>A polling SDF self-checkpoints on every poll via {@code tracker.defer_remainder()}. Each | ||
| * self-checkpoint used to store its residual under a brand new, unique state tag | ||
| * ("sdf_checkpoint:<id>:<index>"), so every poll registered another keyed-state | ||
| * descriptor and the job leaked heap without bound. All residuals for a key/window must instead | ||
| * live under a single, stable state descriptor. | ||
| */ | ||
| @Test | ||
| public void repeatedSelfCheckpointsUseBoundedStateDescriptors() throws Exception { | ||
| Set<String> stateDescriptorIds = new HashSet<>(); | ||
| StateInternalsFactory<String> stateInternalsFactory = | ||
| key -> new RecordingStateInternals(InMemoryStateInternals.forKey(key), stateDescriptorIds); | ||
| TimerInternalsFactory<String> timerInternalsFactory = key -> new InMemoryTimerInternals(); | ||
|
|
||
| StateAndTimerBundleCheckpointHandler<String> handler = | ||
| new StateAndTimerBundleCheckpointHandler<>( | ||
| timerInternalsFactory, | ||
| stateInternalsFactory, | ||
| RESIDUAL_CODER, | ||
| GlobalWindow.Coder.INSTANCE); | ||
|
|
||
| int selfCheckpoints = 100; | ||
| for (int i = 0; i < selfCheckpoints; i++) { | ||
| handler.onCheckpoint(residualResponse("key")); | ||
| } | ||
|
|
||
| // One stable descriptor for all residuals, no matter how many times the SDF checkpointed. | ||
| assertThat(stateDescriptorIds, hasSize(1)); | ||
| } | ||
|
|
||
| private static ProcessBundleResponse residualResponse(String key) throws Exception { | ||
| // The residual element's value is used as the SDF key. | ||
| byte[] encodedElement = | ||
| CoderUtils.encodeToByteArray(RESIDUAL_CODER, WindowedValues.valueInGlobalWindow(key)); | ||
| return ProcessBundleResponse.newBuilder() | ||
| .addResidualRoots( | ||
| DelayedBundleApplication.newBuilder() | ||
| .setApplication( | ||
| BundleApplication.newBuilder() | ||
| .setElement(ByteString.copyFrom(encodedElement)) | ||
| .build()) | ||
| .build()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** A {@link StateInternals} that records every state descriptor id it is asked for. */ | ||
| private static class RecordingStateInternals implements StateInternals { | ||
| private final StateInternals delegate; | ||
| private final Set<String> descriptorIds; | ||
|
|
||
| RecordingStateInternals(StateInternals delegate, Set<String> descriptorIds) { | ||
| this.delegate = delegate; | ||
| this.descriptorIds = descriptorIds; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getKey() { | ||
| return delegate.getKey(); | ||
| } | ||
|
|
||
| @Override | ||
| public <T extends State> T state( | ||
| StateNamespace namespace, StateTag<T> address, StateContext<?> c) { | ||
| descriptorIds.add(address.getId()); | ||
| return delegate.state(namespace, address, c); | ||
| } | ||
| } | ||
| } |
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.
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.
2.75.0 has already been cut and this fix will go to 2.76.0