-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Side Input improvements #38363
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
reuvenlax
wants to merge
16
commits into
apache:master
Choose a base branch
from
reuvenlax:ontimer_sideinput
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
Side Input improvements #38363
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f4fa989
foo
reuvenlax 56eb603
in progress changes with onTimer buffering
reuvenlax 629a879
side-input improvements
reuvenlax a754665
remove extra code
reuvenlax d27c4f8
remove extra code
reuvenlax bcfc6bf
spotless
reuvenlax b24e2cb
spotless
reuvenlax 3dc0b8b
spotless
reuvenlax f83aa35
spotless
reuvenlax 525ad2f
some more refactoring
reuvenlax 383855d
add test
reuvenlax 6e93bef
more fixes
reuvenlax 963ec93
create lazy iterator
reuvenlax 82847c7
small optimization
reuvenlax 92f753f
exclude test from Prism
reuvenlax a96e9ff
avoid element copying
reuvenlax 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
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -59,6 +59,7 @@ | |||||||||||||||||||||||||||||
| import org.apache.beam.sdk.values.WindowingStrategy; | ||||||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; | ||||||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; | ||||||||||||||||||||||||||||||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; | ||||||||||||||||||||||||||||||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||||||||||||||||||||||||||||||
| import org.joda.time.Duration; | ||||||||||||||||||||||||||||||
| import org.joda.time.Instant; | ||||||||||||||||||||||||||||||
|
|
@@ -76,7 +77,7 @@ | |||||||||||||||||||||||||||||
| "rawtypes", // TODO(https://github.com/apache/beam/issues/20447) | ||||||||||||||||||||||||||||||
| "nullness" // TODO(https://github.com/apache/beam/issues/20497) | ||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||
| public class SimpleParDoFn<InputT, OutputT> implements ParDoFn { | ||||||||||||||||||||||||||||||
| public class SimpleParDoFn<InputT, OutputT, W extends BoundedWindow> implements ParDoFn { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // TODO: Remove once Distributions has shipped. | ||||||||||||||||||||||||||||||
| @VisibleForTesting | ||||||||||||||||||||||||||||||
|
|
@@ -112,6 +113,8 @@ public class SimpleParDoFn<InputT, OutputT> implements ParDoFn { | |||||||||||||||||||||||||||||
| // GroupAlsoByWindowViaWindowSetDoFn | ||||||||||||||||||||||||||||||
| private @Nullable DoFnSignature fnSignature; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private @Nullable StreamingSideInputProcessor<InputT, W> sideInputProcessor; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** Creates a {@link SimpleParDoFn} using basic information about the step being executed. */ | ||||||||||||||||||||||||||||||
| SimpleParDoFn( | ||||||||||||||||||||||||||||||
| PipelineOptions options, | ||||||||||||||||||||||||||||||
|
|
@@ -317,8 +320,31 @@ public <TagT> void output(TupleTag<TagT> tag, WindowedValue<TagT> output) { | |||||||||||||||||||||||||||||
| outputManager, | ||||||||||||||||||||||||||||||
| doFnSchemaInformation, | ||||||||||||||||||||||||||||||
| sideInputMapping); | ||||||||||||||||||||||||||||||
| if (hasStreamingSideInput) { | ||||||||||||||||||||||||||||||
| sideInputProcessor = | ||||||||||||||||||||||||||||||
| new StreamingSideInputProcessor<>( | ||||||||||||||||||||||||||||||
| new StreamingSideInputFetcher<InputT, W>( | ||||||||||||||||||||||||||||||
| fnInfo.getSideInputViews(), | ||||||||||||||||||||||||||||||
| fnInfo.getInputCoder(), | ||||||||||||||||||||||||||||||
| (WindowingStrategy<?, W>) fnInfo.getWindowingStrategy(), | ||||||||||||||||||||||||||||||
| (StreamingModeExecutionContext.StreamingModeStepContext) userStepContext)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| fnRunner.startBundle(); | ||||||||||||||||||||||||||||||
| if (sideInputProcessor != null) { | ||||||||||||||||||||||||||||||
| boolean hasState = fnSignature != null && !fnSignature.stateDeclarations().isEmpty(); | ||||||||||||||||||||||||||||||
| Iterable<WindowedValue<InputT>> unblockedElements = sideInputProcessor.tryUnblockElements(); | ||||||||||||||||||||||||||||||
| for (WindowedValue<InputT> unblockedElement : unblockedElements) { | ||||||||||||||||||||||||||||||
| fnRunner.processElement(unblockedElement); | ||||||||||||||||||||||||||||||
| if (hasState) { | ||||||||||||||||||||||||||||||
| // These elements are now processed. Register cleanup timers for all the unblocked | ||||||||||||||||||||||||||||||
| // windows. | ||||||||||||||||||||||||||||||
| registerStateCleanup( | ||||||||||||||||||||||||||||||
| (WindowingStrategy<?, W>) getDoFnInfo().getWindowingStrategy(), | ||||||||||||||||||||||||||||||
| (Collection<W>) unblockedElement.getWindows()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||
|
|
@@ -334,14 +360,28 @@ public void processElement(Object untypedElem) throws Exception { | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| WindowedValue<InputT> elem = (WindowedValue<InputT>) untypedElem; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (fnSignature != null && fnSignature.stateDeclarations().size() > 0) { | ||||||||||||||||||||||||||||||
| boolean hasState = fnSignature != null && !fnSignature.stateDeclarations().isEmpty(); | ||||||||||||||||||||||||||||||
| outputsPerElementTracker.onProcessElement(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Collection<W> windowsProcessed; | ||||||||||||||||||||||||||||||
| if (sideInputProcessor != null) { | ||||||||||||||||||||||||||||||
| windowsProcessed = Lists.newArrayList(); | ||||||||||||||||||||||||||||||
| Iterable<WindowedValue<InputT>> elementsToProcess = | ||||||||||||||||||||||||||||||
| sideInputProcessor.handleProcessElement(elem); | ||||||||||||||||||||||||||||||
| for (WindowedValue<InputT> toProcess : elementsToProcess) { | ||||||||||||||||||||||||||||||
| fnRunner.processElement(toProcess); | ||||||||||||||||||||||||||||||
| windowsProcessed.addAll((Collection<W>) toProcess.getWindows()); | ||||||||||||||||||||||||||||||
|
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. The
Suggested change
|
||||||||||||||||||||||||||||||
| // If the element was blocked, don't register a cleanup timer. The timer will be registered | ||||||||||||||||||||||||||||||
| // when the window is unblocked ensuring that it is not processed until the element is. | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| fnRunner.processElement(elem); | ||||||||||||||||||||||||||||||
| windowsProcessed = (Collection<W>) elem.getWindows(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if (hasState) { | ||||||||||||||||||||||||||||||
| registerStateCleanup( | ||||||||||||||||||||||||||||||
| (WindowingStrategy<?, BoundedWindow>) getDoFnInfo().getWindowingStrategy(), | ||||||||||||||||||||||||||||||
| (Collection<BoundedWindow>) elem.getWindows()); | ||||||||||||||||||||||||||||||
| (WindowingStrategy<?, W>) getDoFnInfo().getWindowingStrategy(), windowsProcessed); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| outputsPerElementTracker.onProcessElement(); | ||||||||||||||||||||||||||||||
| fnRunner.processElement(elem); | ||||||||||||||||||||||||||||||
| outputsPerElementTracker.onProcessElementSuccess(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -367,6 +407,9 @@ private void processUserTimer(TimerData timer) throws Exception { | |||||||||||||||||||||||||||||
| if (fnSignature.timerDeclarations().containsKey(timer.getTimerId()) | ||||||||||||||||||||||||||||||
| || fnSignature.timerFamilyDeclarations().containsKey(timer.getTimerFamilyId())) { | ||||||||||||||||||||||||||||||
| BoundedWindow window = ((WindowNamespace) timer.getNamespace()).getWindow(); | ||||||||||||||||||||||||||||||
| if (sideInputProcessor != null) { | ||||||||||||||||||||||||||||||
| sideInputProcessor.handleProcessTimer(timer); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| fnRunner.onTimer( | ||||||||||||||||||||||||||||||
| timer.getTimerId(), | ||||||||||||||||||||||||||||||
| timer.getTimerFamilyId(), | ||||||||||||||||||||||||||||||
|
|
@@ -380,7 +423,6 @@ private void processUserTimer(TimerData timer) throws Exception { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private void processSystemTimer(TimerData timer) throws Exception { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Timer owned by this class, for cleaning up state in expired windows | ||||||||||||||||||||||||||||||
| if (timer.getTimerId().equals(CLEANUP_TIMER_ID)) { | ||||||||||||||||||||||||||||||
| checkState( | ||||||||||||||||||||||||||||||
|
|
@@ -396,6 +438,13 @@ private void processSystemTimer(TimerData timer) throws Exception { | |||||||||||||||||||||||||||||
| WindowNamespace.class.getSimpleName(), | ||||||||||||||||||||||||||||||
| timer); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (sideInputProcessor != null) { | ||||||||||||||||||||||||||||||
| // We must call this to ensure the side-input is cached for onWindowExpiration. Since we | ||||||||||||||||||||||||||||||
| // don't set cleanup | ||||||||||||||||||||||||||||||
| // timers until we actually call processElement, the window must be unblocked here. | ||||||||||||||||||||||||||||||
| sideInputProcessor.handleProcessTimer(timer); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| BoundedWindow window = ((WindowNamespace) timer.getNamespace()).getWindow(); | ||||||||||||||||||||||||||||||
| Instant targetTime = earliestAllowableCleanupTime(window, fnInfo.getWindowingStrategy()); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -436,10 +485,14 @@ private void processSystemTimer(TimerData timer) throws Exception { | |||||||||||||||||||||||||||||
| public void finishBundle() throws Exception { | ||||||||||||||||||||||||||||||
| if (fnRunner != null) { | ||||||||||||||||||||||||||||||
| fnRunner.finishBundle(); | ||||||||||||||||||||||||||||||
| if (sideInputProcessor != null) { | ||||||||||||||||||||||||||||||
| sideInputProcessor.handleFinishBundle(); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| doFnInstanceManager.complete(fnInfo); | ||||||||||||||||||||||||||||||
| fnRunner = null; | ||||||||||||||||||||||||||||||
| fnInfo = null; | ||||||||||||||||||||||||||||||
| fnSignature = null; | ||||||||||||||||||||||||||||||
| sideInputProcessor = null; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -490,7 +543,7 @@ private void processTimers( | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| private <W extends BoundedWindow> void registerStateCleanup( | ||||||||||||||||||||||||||||||
| private void registerStateCleanup( | ||||||||||||||||||||||||||||||
| WindowingStrategy<?, W> windowingStrategy, Collection<W> windowsToCleanup) { | ||||||||||||||||||||||||||||||
| Coder<W> windowCoder = windowingStrategy.getWindowFn().windowCoder(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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
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.
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.
The
outputsPerElementTrackeris not notified when processing unblocked elements instartBundle. This can lead to incorrect progress tracking and metrics in Dataflow, as the tracker expectsonProcessElement()to be called beforeprocessElement(). You should wrap theprocessElementcall with the appropriate tracker notifications.