@Override public void onMerge(MergingStateAccessor<K, W> context) { StateMerging.mergeCombiningValues(context, PANE_ADDITIONS_TAG); } }
@Override public void onMerge(OnMergeContext c) throws Exception { StateMerging.mergeCombiningValues(c.state(), bufferTag); } };
/** Merge all value state in {@code address} across all merging windows in {@code context}. */ public static <K, InputT, AccumT, OutputT, W extends BoundedWindow> void mergeCombiningValues( MergingStateAccessor<K, W> context, StateTag<CombiningState<InputT, AccumT, OutputT>> address) { mergeCombiningValues( context.accessInEachMergingWindow(address).values(), context.access(address)); }
@Override public void onMerge(OnMergeContext context) throws Exception { // If we've already received enough elements and finished in some window, // then this trigger is just finished. if (context.trigger().finishedInAnyMergingWindow()) { context.trigger().setFinished(true); StateMerging.clear(context.state(), ELEMENTS_IN_PANE_TAG); return; } // Otherwise, compute the sum of elements in all the active panes. StateMerging.mergeCombiningValues(context.state(), ELEMENTS_IN_PANE_TAG); }
@Test public void testMergeCombiningValueIntoSource() throws Exception { CombiningState<Integer, int[], Integer> value1 = underTest.state(NAMESPACE_1, SUM_INTEGER_ADDR); CombiningState<Integer, int[], Integer> value2 = underTest.state(NAMESPACE_2, SUM_INTEGER_ADDR); value1.add(5); value2.add(10); value1.add(6); assertThat(value1.read(), equalTo(11)); assertThat(value2.read(), equalTo(10)); // Merging clears the old values and updates the result value. StateMerging.mergeCombiningValues(Arrays.asList(value1, value2), value1); assertThat(value1.read(), equalTo(21)); assertThat(value2.read(), equalTo(0)); }
@Test public void testMergeCombiningValueIntoNewNamespace() throws Exception { CombiningState<Integer, int[], Integer> value1 = underTest.state(NAMESPACE_1, SUM_INTEGER_ADDR); CombiningState<Integer, int[], Integer> value2 = underTest.state(NAMESPACE_2, SUM_INTEGER_ADDR); CombiningState<Integer, int[], Integer> value3 = underTest.state(NAMESPACE_3, SUM_INTEGER_ADDR); value1.add(5); value2.add(10); value1.add(6); StateMerging.mergeCombiningValues(Arrays.asList(value1, value2), value3); // Merging clears the old values and updates the result value. assertThat(value1.read(), equalTo(0)); assertThat(value2.read(), equalTo(0)); assertThat(value3.read(), equalTo(21)); }
@Override public void onMerge(OnMergeContext c) throws Exception { // NOTE: We could try to delete all timers which are still active, but we would // need access to a timer context for each merging window. // for (CombiningValueStateInternal<Instant, Combine.Holder<Instant>, Instant> state : // c.state().accessInEachMergingWindow(DELAYED_UNTIL_TAG).values()) { // Instant timestamp = state.get().read(); // if (timestamp != null) { // <context for merging window>.deleteTimer(timestamp, timeDomain); // } // } // Instead let them fire and be ignored. // If the trigger is already finished, there is no way it will become re-activated if (c.trigger().isFinished()) { StateMerging.clear(c.state(), DELAYED_UNTIL_TAG); // NOTE: We do not attempt to delete the timers. return; } // Determine the earliest point across all the windows, and delay to that. StateMerging.mergeCombiningValues(c.state(), DELAYED_UNTIL_TAG); Instant earliestTargetTime = c.state().access(DELAYED_UNTIL_TAG).read(); if (earliestTargetTime != null) { c.setTimer(earliestTargetTime, timeDomain); } }