Search in sources :

Example 6 with WindowNamespace

use of org.apache.beam.runners.core.StateNamespaces.WindowNamespace in project beam by apache.

the class SimpleParDoFn method processSystemTimer.

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(timer.getDomain().equals(TimeDomain.EVENT_TIME), "%s received cleanup timer with domain not EVENT_TIME: %s", this, timer);
        checkState(timer.getNamespace() instanceof WindowNamespace, "%s received cleanup timer not for a %s: %s", this, WindowNamespace.class.getSimpleName(), timer);
        BoundedWindow window = ((WindowNamespace) timer.getNamespace()).getWindow();
        Instant targetTime = earliestAllowableCleanupTime(window, fnInfo.getWindowingStrategy());
        checkState(!targetTime.isAfter(timer.getTimestamp()), "%s received state cleanup timer for window %s " + " that is before the appropriate cleanup time %s", this, window, targetTime);
        fnRunner.onWindowExpiration(window, timer.getOutputTimestamp(), this.stepContext.stateInternals().getKey());
        // This is for a timer for a window that is expired, so clean it up.
        for (StateDeclaration stateDecl : fnSignature.stateDeclarations().values()) {
            StateTag<?> tag;
            try {
                tag = StateTags.tagForSpec(stateDecl.id(), (StateSpec) stateDecl.field().get(fnInfo.getDoFn()));
            } catch (IllegalAccessException e) {
                throw new RuntimeException(String.format("Error accessing %s for %s", StateSpec.class.getName(), fnInfo.getDoFn().getClass().getName()), e);
            }
            StateInternals stateInternals = userStepContext.stateInternals();
            org.apache.beam.sdk.state.State state = stateInternals.state(timer.getNamespace(), tag);
            state.clear();
        }
    }
}
Also used : Instant(org.joda.time.Instant) StateSpec(org.apache.beam.sdk.state.StateSpec) WindowNamespace(org.apache.beam.runners.core.StateNamespaces.WindowNamespace) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) StateInternals(org.apache.beam.runners.core.StateInternals) StateDeclaration(org.apache.beam.sdk.transforms.reflect.DoFnSignature.StateDeclaration)

Aggregations

WindowNamespace (org.apache.beam.runners.core.StateNamespaces.WindowNamespace)6 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)6 StateNamespace (org.apache.beam.runners.core.StateNamespace)3 TimerData (org.apache.beam.runners.core.TimerInternals.TimerData)3 Instant (org.joda.time.Instant)3 HashMap (java.util.HashMap)1 StateInternalsProxy (org.apache.beam.runners.apex.translation.utils.StateInternalsProxy)1 KeyedWorkItemCoder (org.apache.beam.runners.core.KeyedWorkItemCoder)1 StateInternals (org.apache.beam.runners.core.StateInternals)1 TimerInternals (org.apache.beam.runners.core.TimerInternals)1 Coder (org.apache.beam.sdk.coders.Coder)1 KvCoder (org.apache.beam.sdk.coders.KvCoder)1 ListCoder (org.apache.beam.sdk.coders.ListCoder)1 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)1 VoidCoder (org.apache.beam.sdk.coders.VoidCoder)1 StateSpec (org.apache.beam.sdk.state.StateSpec)1 StateDeclaration (org.apache.beam.sdk.transforms.reflect.DoFnSignature.StateDeclaration)1 WindowedValueCoder (org.apache.beam.sdk.util.WindowedValue.WindowedValueCoder)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1