Search in sources :

Example 86 with Instant

use of org.joda.time.Instant in project beam by apache.

the class ReduceFnRunnerTest method setGarbageCollectionHoldOnLateElements.

/**
   * Late elements should still have a garbage collection hold set so that they
   * can make a late pane rather than be dropped due to lateness.
   */
@Test
public void setGarbageCollectionHoldOnLateElements() throws Exception {
    ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester = ReduceFnTester.nonCombining(WindowingStrategy.of(FixedWindows.of(Duration.millis(10))).withTrigger(AfterWatermark.pastEndOfWindow().withLateFirings(AfterPane.elementCountAtLeast(2))).withMode(AccumulationMode.DISCARDING_FIRED_PANES).withAllowedLateness(Duration.millis(100)).withClosingBehavior(ClosingBehavior.FIRE_IF_NON_EMPTY));
    tester.advanceInputWatermark(new Instant(0));
    tester.advanceOutputWatermark(new Instant(0));
    tester.injectElements(TimestampedValue.of(1, new Instant(1)));
    // Fire ON_TIME pane @ 9 with 1
    tester.advanceInputWatermark(new Instant(109));
    tester.advanceOutputWatermark(new Instant(109));
    tester.injectElements(TimestampedValue.of(2, new Instant(2)));
    // We should have set a garbage collection hold for the final pane.
    Instant hold = tester.getWatermarkHold();
    assertEquals(new Instant(109), hold);
    tester.advanceInputWatermark(new Instant(110));
    tester.advanceOutputWatermark(new Instant(110));
    // Fire final LATE pane @ 9 with 2
    List<WindowedValue<Iterable<Integer>>> output = tester.extractOutput();
    assertEquals(2, output.size());
}
Also used : Matchers.emptyIterable(org.hamcrest.Matchers.emptyIterable) WindowedValue(org.apache.beam.sdk.util.WindowedValue) WindowMatchers.isWindowedValue(org.apache.beam.runners.core.WindowMatchers.isWindowedValue) WindowMatchers.isSingleWindowedValue(org.apache.beam.runners.core.WindowMatchers.isSingleWindowedValue) Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 87 with Instant

use of org.joda.time.Instant in project beam by apache.

the class SideInputHandlerTest method testMultipleSideInputs.

@Test
public void testMultipleSideInputs() {
    SideInputHandler sideInputHandler = new SideInputHandler(ImmutableList.<PCollectionView<?>>of(view1, view2), InMemoryStateInternals.<Void>forKey(null));
    // two windows that we'll later use for adding elements/retrieving side input
    IntervalWindow firstWindow = new IntervalWindow(new Instant(0), new Instant(WINDOW_MSECS_1));
    // add value for view1 in the first window
    sideInputHandler.addSideInputValue(view1, valuesInWindow(ImmutableList.of("Hello"), new Instant(0), firstWindow));
    Assert.assertThat(sideInputHandler.get(view1, firstWindow), contains("Hello"));
    // view2 should not have any data
    assertFalse(sideInputHandler.isReady(view2, firstWindow));
    // also add some data for view2
    sideInputHandler.addSideInputValue(view2, valuesInWindow(ImmutableList.of("Salut"), new Instant(0), firstWindow));
    assertTrue(sideInputHandler.isReady(view2, firstWindow));
    Assert.assertThat(sideInputHandler.get(view2, firstWindow), contains("Salut"));
    // view1 should not be affected by that
    Assert.assertThat(sideInputHandler.get(view1, firstWindow), contains("Hello"));
}
Also used : Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 88 with Instant

use of org.joda.time.Instant in project beam by apache.

the class SideInputHandlerTest method testMultipleWindows.

@Test
public void testMultipleWindows() {
    SideInputHandler sideInputHandler = new SideInputHandler(ImmutableList.<PCollectionView<?>>of(view1), InMemoryStateInternals.<Void>forKey(null));
    // two windows that we'll later use for adding elements/retrieving side input
    IntervalWindow firstWindow = new IntervalWindow(new Instant(0), new Instant(WINDOW_MSECS_1));
    IntervalWindow secondWindow = new IntervalWindow(new Instant(1000), new Instant(1000 + WINDOW_MSECS_2));
    // add a first value for view1 in the first window
    sideInputHandler.addSideInputValue(view1, valuesInWindow(ImmutableList.of("Hello"), new Instant(0), firstWindow));
    Assert.assertThat(sideInputHandler.get(view1, firstWindow), contains("Hello"));
    // add something for second window of view1
    sideInputHandler.addSideInputValue(view1, valuesInWindow(ImmutableList.of("Arrivederci"), new Instant(0), secondWindow));
    Assert.assertThat(sideInputHandler.get(view1, secondWindow), contains("Arrivederci"));
    // contents for first window should be unaffected
    Assert.assertThat(sideInputHandler.get(view1, firstWindow), contains("Hello"));
}
Also used : Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 89 with Instant

use of org.joda.time.Instant in project beam by apache.

the class SideInputHandlerTest method testIsReady.

@Test
public void testIsReady() {
    SideInputHandler sideInputHandler = new SideInputHandler(ImmutableList.<PCollectionView<?>>of(view1, view2), InMemoryStateInternals.<Void>forKey(null));
    IntervalWindow firstWindow = new IntervalWindow(new Instant(0), new Instant(WINDOW_MSECS_1));
    IntervalWindow secondWindow = new IntervalWindow(new Instant(0), new Instant(WINDOW_MSECS_2));
    // side input should not yet be ready
    assertFalse(sideInputHandler.isReady(view1, firstWindow));
    // add a value for view1
    sideInputHandler.addSideInputValue(view1, valuesInWindow(ImmutableList.of("Hello"), new Instant(0), firstWindow));
    // now side input should be ready
    assertTrue(sideInputHandler.isReady(view1, firstWindow));
    // second window input should still not be ready
    assertFalse(sideInputHandler.isReady(view1, secondWindow));
}
Also used : Instant(org.joda.time.Instant) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Example 90 with Instant

use of org.joda.time.Instant in project beam by apache.

the class SimpleDoFnRunnerTest method testSkew.

/**
   * Demonstrates that attempting to output an element before the timestamp of the current element
   * plus the value of {@link DoFn#getAllowedTimestampSkew()} throws, but between that value and
   * the current timestamp succeeds.
   */
@Test
public void testSkew() {
    SkewingDoFn fn = new SkewingDoFn(Duration.standardMinutes(10L));
    DoFnRunner<Duration, Duration> runner = new SimpleDoFnRunner<>(null, fn, NullSideInputReader.empty(), new ListOutputManager(), new TupleTag<Duration>(), Collections.<TupleTag<?>>emptyList(), mockStepContext, WindowingStrategy.of(new GlobalWindows()));
    runner.startBundle();
    // Outputting between "now" and "now - allowed skew" succeeds.
    runner.processElement(WindowedValue.timestampedValueInGlobalWindow(Duration.standardMinutes(5L), new Instant(0)));
    thrown.expect(UserCodeException.class);
    thrown.expectCause(isA(IllegalArgumentException.class));
    thrown.expectMessage("must be no earlier");
    thrown.expectMessage(String.format("timestamp of the current input (%s)", new Instant(0).toString()));
    thrown.expectMessage(String.format("the allowed skew (%s)", PeriodFormat.getDefault().print(Duration.standardMinutes(10L).toPeriod())));
    // Outputting before "now - allowed skew" fails.
    runner.processElement(WindowedValue.timestampedValueInGlobalWindow(Duration.standardHours(1L), new Instant(0)));
}
Also used : GlobalWindows(org.apache.beam.sdk.transforms.windowing.GlobalWindows) Instant(org.joda.time.Instant) Duration(org.joda.time.Duration) Test(org.junit.Test)

Aggregations

Instant (org.joda.time.Instant)411 Test (org.junit.Test)326 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)135 KV (org.apache.beam.sdk.values.KV)64 Category (org.junit.experimental.categories.Category)60 WindowedValue (org.apache.beam.sdk.util.WindowedValue)47 Duration (org.joda.time.Duration)44 ReadableInstant (org.joda.time.ReadableInstant)36 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)30 WatermarkHoldState (org.apache.beam.sdk.state.WatermarkHoldState)24 TimerData (org.apache.beam.runners.core.TimerInternals.TimerData)22 Matchers.emptyIterable (org.hamcrest.Matchers.emptyIterable)22 HashMap (java.util.HashMap)19 TransformWatermarks (org.apache.beam.runners.direct.WatermarkManager.TransformWatermarks)18 StateNamespaceForTest (org.apache.beam.runners.core.StateNamespaceForTest)17 WindowMatchers.isSingleWindowedValue (org.apache.beam.runners.core.WindowMatchers.isSingleWindowedValue)17 WindowMatchers.isWindowedValue (org.apache.beam.runners.core.WindowMatchers.isWindowedValue)17 TupleTag (org.apache.beam.sdk.values.TupleTag)14 ArrayList (java.util.ArrayList)13 Map (java.util.Map)12