Search in sources :

Example 1 with Modifications

use of org.apache.beam.fn.harness.state.FnApiTimerBundleTracker.Modifications in project beam by apache.

the class FnApiDoFnRunner method processTimer.

private <K> void processTimer(String timerIdOrTimerFamilyId, TimeDomain timeDomain, Timer<K> timer) {
    try {
        currentKey = timer.getUserKey();
        Iterator<BoundedWindow> windowIterator = (Iterator<BoundedWindow>) timer.getWindows().iterator();
        while (windowIterator.hasNext()) {
            currentWindow = windowIterator.next();
            Modifications bundleModifications = timerBundleTracker.getBundleModifications();
            Table<String, String, Timer<K>> modifiedTimerIds = bundleModifications.getModifiedTimerIds();
            NavigableSet<TimerInfo<K>> earlierTimers = bundleModifications.getModifiedTimersOrdered(timeDomain).headSet(TimerInfo.of(timer, "", timeDomain), true);
            while (!earlierTimers.isEmpty()) {
                TimerInfo<K> insertedTimer = earlierTimers.pollFirst();
                if (timerModified(modifiedTimerIds, insertedTimer.getTimerFamilyOrId(), insertedTimer.getTimer())) {
                    continue;
                }
                String timerId = insertedTimer.getTimer().getDynamicTimerTag().isEmpty() ? insertedTimer.getTimerFamilyOrId() : insertedTimer.getTimer().getDynamicTimerTag();
                String timerFamily = insertedTimer.getTimer().getDynamicTimerTag().isEmpty() ? "" : insertedTimer.getTimerFamilyOrId();
                // If this timer was created previously in the bundle as an overwrite of a previous timer,
                // we must make sure
                // to clear the old timer. Since we are firing the timer inline, the runner doesn't know
                // that the old timer
                // was overwritten, and will otherwise fire it - causing a spurious timer fire.
                modifiedTimerIds.put(insertedTimer.getTimerFamilyOrId(), insertedTimer.getTimer().getDynamicTimerTag(), Timer.cleared(insertedTimer.getTimer().getUserKey(), insertedTimer.getTimer().getDynamicTimerTag(), insertedTimer.getTimer().getWindows()));
                // It's important to call processTimer after inserting the above deletion, otherwise the
                // above line
                // would overwrite any looping timer with a deletion.
                processTimerDirect(timerFamily, timerId, insertedTimer.getTimeDomain(), insertedTimer.getTimer());
            }
            if (!timerModified(modifiedTimerIds, timerIdOrTimerFamilyId, timer)) {
                // The timerIdOrTimerFamilyId contains either a timerId from timer declaration or
                // timerFamilyId
                // from timer family declaration.
                boolean isFamily = timerIdOrTimerFamilyId.startsWith(TimerFamilyDeclaration.PREFIX);
                String timerId = isFamily ? "" : timerIdOrTimerFamilyId;
                String timerFamilyId = isFamily ? timerIdOrTimerFamilyId : "";
                processTimerDirect(timerFamilyId, timerId, timeDomain, timer);
            }
        }
    } finally {
        currentKey = null;
        currentTimer = null;
        currentTimeDomain = null;
        currentWindow = null;
    }
}
Also used : Modifications(org.apache.beam.fn.harness.state.FnApiTimerBundleTracker.Modifications) Timer(org.apache.beam.runners.core.construction.Timer) Iterator(java.util.Iterator) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) TimerInfo(org.apache.beam.fn.harness.state.FnApiTimerBundleTracker.TimerInfo) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)

Aggregations

Iterator (java.util.Iterator)1 Modifications (org.apache.beam.fn.harness.state.FnApiTimerBundleTracker.Modifications)1 TimerInfo (org.apache.beam.fn.harness.state.FnApiTimerBundleTracker.TimerInfo)1 Timer (org.apache.beam.runners.core.construction.Timer)1 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)1 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)1