Search in sources :

Example 6 with ReadableState

use of org.apache.beam.sdk.state.ReadableState in project beam by apache.

the class WatermarkHold method extractAndRelease.

/**
 * Return (a future for) the earliest hold for {@code context}. Clear all the holds after reading,
 * but add/restore an end-of-window or garbage collection hold if required.
 *
 * <p>The returned timestamp is the output timestamp according to the {@link TimestampCombiner}
 * from the windowing strategy of this {@link WatermarkHold}, combined across all the non-late
 * elements in the current pane. If there is no such value the timestamp is the end of the window.
 */
public ReadableState<OldAndNewHolds> extractAndRelease(final ReduceFn<?, ?, ?, W>.Context context, final boolean isFinished) {
    WindowTracing.debug("WatermarkHold.extractAndRelease: for key:{}; window:{}; inputWatermark:{}; " + "outputWatermark:{}", context.key(), context.window(), timerInternals.currentInputWatermarkTime(), timerInternals.currentOutputWatermarkTime());
    final WatermarkHoldState elementHoldState = context.state().access(elementHoldTag);
    final WatermarkHoldState extraHoldState = context.state().access(EXTRA_HOLD_TAG);
    return new ReadableState<OldAndNewHolds>() {

        @Override
        public ReadableState<OldAndNewHolds> readLater() {
            elementHoldState.readLater();
            extraHoldState.readLater();
            return this;
        }

        @Override
        public OldAndNewHolds read() {
            // Read both the element and extra holds.
            @Nullable Instant elementHold = elementHoldState.read();
            @Nullable Instant extraHold = extraHoldState.read();
            @Nullable Instant oldHold;
            // Find the minimum, accounting for null.
            if (elementHold == null) {
                oldHold = extraHold;
            } else if (extraHold == null) {
                oldHold = elementHold;
            } else if (elementHold.isBefore(extraHold)) {
                oldHold = elementHold;
            } else {
                oldHold = extraHold;
            }
            if (oldHold == null || oldHold.isAfter(context.window().maxTimestamp())) {
                // If no hold (eg because all elements came in before the output watermark), or
                // the hold was for garbage collection, take the end of window as the result.
                WindowTracing.debug("WatermarkHold.extractAndRelease.read: clipping from {} to end of window " + "for key:{}; window:{}", oldHold, context.key(), context.window());
                oldHold = context.window().maxTimestamp();
            }
            WindowTracing.debug("WatermarkHold.extractAndRelease.read: clearing for key:{}; window:{}", context.key(), context.window());
            // Clear the underlying state to allow the output watermark to progress.
            elementHoldState.clear();
            extraHoldState.clear();
            @Nullable Instant newHold = null;
            if (!isFinished) {
                newHold = addGarbageCollectionHold(context, true);
            }
            return new OldAndNewHolds(oldHold, newHold);
        }
    };
}
Also used : Instant(org.joda.time.Instant) ReadableState(org.apache.beam.sdk.state.ReadableState) WatermarkHoldState(org.apache.beam.sdk.state.WatermarkHoldState) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

ReadableState (org.apache.beam.sdk.state.ReadableState)6 Instant (org.joda.time.Instant)5 WatermarkHoldState (org.apache.beam.sdk.state.WatermarkHoldState)3 ArrayList (java.util.ArrayList)2 PaneInfo (org.apache.beam.sdk.transforms.windowing.PaneInfo)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Nullable (javax.annotation.Nullable)1 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)1 ExecutableStage (org.apache.beam.runners.core.construction.graph.ExecutableStage)1 FusedPipeline (org.apache.beam.runners.core.construction.graph.FusedPipeline)1 ExecutableProcessBundleDescriptor (org.apache.beam.runners.fnexecution.control.ProcessBundleDescriptors.ExecutableProcessBundleDescriptor)1 BundleProcessor (org.apache.beam.runners.fnexecution.control.SdkHarnessClient.BundleProcessor)1 BagUserStateHandler (org.apache.beam.runners.fnexecution.state.StateRequestHandlers.BagUserStateHandler)1 BagUserStateHandlerFactory (org.apache.beam.runners.fnexecution.state.StateRequestHandlers.BagUserStateHandlerFactory)1 Pipeline (org.apache.beam.sdk.Pipeline)1 BigEndianLongCoder (org.apache.beam.sdk.coders.BigEndianLongCoder)1