org.apache.beam.sdk.transforms.windowing.IntervalWindow Java Examples

The following examples show how to use org.apache.beam.sdk.transforms.windowing.IntervalWindow. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WindowMatchersTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsWindowedValueReorderedWindows() {
  long timestamp = 100;
  long windowStart = 0;
  long windowEnd = 200;
  long windowStart2 = 50;
  long windowEnd2 = 150;

  assertThat(
      WindowedValue.of(
          "hello",
          new Instant(timestamp),
          ImmutableList.of(
              new IntervalWindow(new Instant(windowStart), new Instant(windowEnd)),
              new IntervalWindow(new Instant(windowStart2), new Instant(windowEnd2))),
          PaneInfo.NO_FIRING),
      WindowMatchers.isWindowedValue(
          "hello",
          new Instant(timestamp),
          ImmutableList.of(
              new IntervalWindow(new Instant(windowStart), new Instant(windowEnd)),
              new IntervalWindow(new Instant(windowStart2), new Instant(windowEnd2))),
          PaneInfo.NO_FIRING));
}
 
Example #2
Source File: WriteToText.java    From deployment-examples with MIT License 6 votes vote down vote up
@Override
public ResourceId windowedFilename(
    int shardNumber,
    int numShards,
    BoundedWindow window,
    PaneInfo paneInfo,
    OutputFileHints outputFileHints) {
  IntervalWindow intervalWindow = (IntervalWindow) window;
  String filename =
      String.format(
          "%s-%s-of-%s%s",
          filenamePrefixForWindow(intervalWindow),
          shardNumber,
          numShards,
          outputFileHints.getSuggestedFilenameSuffix());
  return prefix.getCurrentDirectory().resolve(filename, StandardResolveOptions.RESOLVE_FILE);
}
 
Example #3
Source File: DoFnTesterTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportsWindowParameter() throws Exception {
  Instant now = Instant.now();
  try (DoFnTester<Integer, KV<Integer, BoundedWindow>> tester =
      DoFnTester.of(new DoFnWithWindowParameter())) {
    BoundedWindow firstWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(1)));
    tester.processWindowedElement(1, now, firstWindow);
    tester.processWindowedElement(2, now, firstWindow);
    BoundedWindow secondWindow = new IntervalWindow(now, now.plus(Duration.standardMinutes(4)));
    tester.processWindowedElement(3, now, secondWindow);
    tester.finishBundle();

    assertThat(
        tester.peekOutputElementsInWindow(firstWindow),
        containsInAnyOrder(
            TimestampedValue.of(KV.of(1, firstWindow), now),
            TimestampedValue.of(KV.of(2, firstWindow), now)));
    assertThat(
        tester.peekOutputElementsInWindow(secondWindow),
        containsInAnyOrder(TimestampedValue.of(KV.of(3, secondWindow), now)));
  }
}
 
Example #4
Source File: SimplePushbackSideInputDoFnRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void processElementSideInputReadyAllWindows() {
  when(reader.isReady(Mockito.eq(singletonView), Mockito.any(BoundedWindow.class)))
      .thenReturn(true);

  ImmutableList<PCollectionView<?>> views = ImmutableList.of(singletonView);
  SimplePushbackSideInputDoFnRunner<Integer, Integer> runner = createRunner(views);

  WindowedValue<Integer> multiWindow =
      WindowedValue.of(
          2,
          new Instant(-2),
          ImmutableList.of(
              new IntervalWindow(new Instant(-500L), new Instant(0L)),
              new IntervalWindow(BoundedWindow.TIMESTAMP_MIN_VALUE, new Instant(250L)),
              GlobalWindow.INSTANCE),
          PaneInfo.ON_TIME_AND_ONLY_FIRING);
  Iterable<WindowedValue<Integer>> multiWindowPushback =
      runner.processElementInReadyWindows(multiWindow);
  assertThat(multiWindowPushback, emptyIterable());
  assertThat(
      underlying.inputElems,
      containsInAnyOrder(ImmutableList.copyOf(multiWindow.explodeWindows()).toArray()));
}
 
Example #5
Source File: StreamingGroupAlsoByWindowFnsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private void addTimer(
    WorkItem.Builder workItem,
    IntervalWindow window,
    Instant timestamp,
    Windmill.Timer.Type type) {
  StateNamespace namespace = StateNamespaces.window(windowCoder, window);
  workItem
      .getTimersBuilder()
      .addTimersBuilder()
      .setTag(
          WindmillTimerInternals.timerTag(
              WindmillNamespacePrefix.SYSTEM_NAMESPACE_PREFIX,
              TimerData.of(
                  namespace,
                  timestamp,
                  timestamp,
                  type == Windmill.Timer.Type.WATERMARK
                      ? TimeDomain.EVENT_TIME
                      : TimeDomain.PROCESSING_TIME)))
      .setTimestamp(WindmillTimeUtils.harnessToWindmillTimestamp(timestamp))
      .setType(type)
      .setStateFamily(STATE_FAMILY);
}
 
Example #6
Source File: WriteFilesTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceId windowedFilename(
    int shardNumber,
    int numShards,
    BoundedWindow window,
    PaneInfo paneInfo,
    OutputFileHints outputFileHints) {
  DecimalFormat df = new DecimalFormat("0000");
  IntervalWindow intervalWindow = (IntervalWindow) window;
  String filename =
      String.format(
          "%s-%s-of-%s%s%s",
          filenamePrefixForWindow(intervalWindow),
          df.format(shardNumber),
          df.format(numShards),
          outputFileHints.getSuggestedFilenameSuffix(),
          suffix);
  return baseFilename
      .getCurrentDirectory()
      .resolve(filename, StandardResolveOptions.RESOLVE_FILE);
}
 
Example #7
Source File: StreamingDataflowWorkerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private ParallelInstruction makeSourceInstruction(Coder<?> coder) {
  return new ParallelInstruction()
      .setSystemName(DEFAULT_SOURCE_SYSTEM_NAME)
      .setOriginalName(DEFAULT_SOURCE_ORIGINAL_NAME)
      .setRead(
          new ReadInstruction()
              .setSource(
                  new Source()
                      .setSpec(CloudObject.forClass(UngroupedWindmillReader.class))
                      .setCodec(
                          CloudObjects.asCloudObject(
                              WindowedValue.getFullCoder(coder, IntervalWindow.getCoder()),
                              /*sdkComponents=*/ null))))
      .setOutputs(
          Arrays.asList(
              new InstructionOutput()
                  .setName(Long.toString(idGenerator.get()))
                  .setOriginalName(DEFAULT_OUTPUT_ORIGINAL_NAME)
                  .setSystemName(DEFAULT_OUTPUT_SYSTEM_NAME)
                  .setCodec(
                      CloudObjects.asCloudObject(
                          WindowedValue.getFullCoder(coder, IntervalWindow.getCoder()),
                          /*sdkComponents=*/ null))));
}
 
Example #8
Source File: WindowedFilenamePolicy.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves any date variables which exist in the output directory path. This allows for the
 * dynamically changing of the output location based on the window end time.
 *
 * @return The new output directory with all variables resolved.
 */
private ResourceId resolveWithDateTemplates(
    ValueProvider<String> outputDirectoryStr, BoundedWindow window) {
  ResourceId outputDirectory = FileSystems.matchNewResource(outputDirectoryStr.get(), true);

  if (window instanceof IntervalWindow) {
    IntervalWindow intervalWindow = (IntervalWindow) window;
    DateTime time = intervalWindow.end().toDateTime();
    String outputPath = outputDirectory.toString();
    outputPath = outputPath.replace("YYYY", YEAR.print(time));
    outputPath = outputPath.replace("MM", MONTH.print(time));
    outputPath = outputPath.replace("DD", DAY.print(time));
    outputPath = outputPath.replace("HH", HOUR.print(time));
    outputPath = outputPath.replace("mm", MINUTE.print(time));
    outputDirectory = FileSystems.matchNewResource(outputPath, true);
  }
  return outputDirectory;
}
 
Example #9
Source File: AfterFirstStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testBothShouldFireFixedWindows() throws Exception {
  tester =
      TriggerStateMachineTester.forTrigger(
          AfterFirstStateMachine.of(mockTrigger1, mockTrigger2),
          FixedWindows.of(Duration.millis(10)));
  tester.injectElements(1);
  IntervalWindow window = new IntervalWindow(new Instant(1), new Instant(11));

  when(mockTrigger1.shouldFire(anyTriggerContext())).thenReturn(true);
  when(mockTrigger2.shouldFire(anyTriggerContext())).thenReturn(true);
  assertTrue(tester.shouldFire(window)); // should fire

  tester.fireIfShouldFire(window);
  assertTrue(tester.isMarkedFinished(window));
}
 
Example #10
Source File: AfterAllStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testT1FiresFirst() throws Exception {
  tester =
      TriggerStateMachineTester.forTrigger(
          AfterAllStateMachine.of(
              AfterPaneStateMachine.elementCountAtLeast(1),
              AfterPaneStateMachine.elementCountAtLeast(2)),
          FixedWindows.of(Duration.millis(100)));

  IntervalWindow window = new IntervalWindow(new Instant(0), new Instant(100));

  tester.injectElements(1);
  assertFalse(tester.shouldFire(window));

  tester.injectElements(2);
  assertTrue(tester.shouldFire(window));
  tester.fireIfShouldFire(window);
  assertTrue(tester.isMarkedFinished(window));
}
 
Example #11
Source File: ParDoTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRejectsWrongWindowType() {

  thrown.expect(IllegalArgumentException.class);
  thrown.expectMessage(GlobalWindow.class.getSimpleName());
  thrown.expectMessage(IntervalWindow.class.getSimpleName());
  thrown.expectMessage("window type");
  thrown.expectMessage("not a supertype");

  pipeline
      .apply(Create.of(1, 2, 3))
      .apply(
          ParDo.of(
              new DoFn<Integer, Integer>() {
                @ProcessElement
                public void process(ProcessContext c, IntervalWindow w) {}
              }));
}
 
Example #12
Source File: DynamicOneFilePerWindow.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceId windowedFilename(
    int shardNumber,
    int numShards,
    BoundedWindow window,
    PaneInfo paneInfo,
    OutputFileHints outputFileHints) {
  IntervalWindow intervalWindow = (IntervalWindow) window;
  String filename =
      String.format(
          "%s-%s-of-%s%s",
          filenamePrefixForWindow(intervalWindow),
          shardNumber,
          numShards,
          outputFileHints.getSuggestedFilenameSuffix());
  return baseFilename
      .getCurrentDirectory()
      .resolve(filename, StandardResolveOptions.RESOLVE_FILE);
}
 
Example #13
Source File: OrFinallyStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that for {@code OrFinally(actual, ...)} when {@code actual} fires and finishes, the
 * {@code OrFinally} also fires and finishes.
 */
@Test
public void testActualFiresAndFinishes() throws Exception {
  tester =
      TriggerStateMachineTester.forTrigger(
          new OrFinallyStateMachine(
              AfterPaneStateMachine.elementCountAtLeast(2),
              AfterPaneStateMachine.elementCountAtLeast(100)),
          FixedWindows.of(Duration.millis(100)));

  IntervalWindow window = new IntervalWindow(new Instant(0), new Instant(100));

  // Not yet firing
  tester.injectElements(1);
  assertFalse(tester.shouldFire(window));
  assertFalse(tester.isMarkedFinished(window));

  // The actual fires and finishes
  tester.injectElements(2);
  assertTrue(tester.shouldFire(window));
  tester.fireIfShouldFire(window);
  assertTrue(tester.isMarkedFinished(window));
}
 
Example #14
Source File: WindowFnTestUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies that later-ending merged windows from any of the timestamps hold up output of
 * earlier-ending windows, using the provided {@link WindowFn} and {@link TimestampCombiner}.
 *
 * <p>Given a list of lists of timestamps, where each list is expected to merge into a single
 * window with end times in ascending order, assigns and merges windows for each list (as though
 * each were a separate key/user session). Then combines each timestamp in the list according to
 * the provided {@link TimestampCombiner}.
 *
 * <p>Verifies that a overlapping windows do not hold each other up via the watermark.
 */
public static <T, W extends IntervalWindow> void validateGetOutputTimestamps(
    WindowFn<T, W> windowFn,
    TimestampCombiner timestampCombiner,
    List<List<Long>> timestampsPerWindow)
    throws Exception {

  List<List<TimestampedValue<T>>> timestampValuesPerWindow = new ArrayList<>();
  for (List<Long> timestamps : timestampsPerWindow) {
    List<TimestampedValue<T>> timestampedValues = new ArrayList<>();
    for (Long timestamp : timestamps) {
      TimestampedValue<T> tv = TimestampedValue.of(null, new Instant(timestamp));
      timestampedValues.add(tv);
    }
    timestampValuesPerWindow.add(timestampedValues);
  }
  validateGetOutputTimestampsWithValue(windowFn, timestampCombiner, timestampValuesPerWindow);
}
 
Example #15
Source File: SampleTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
@Category(NeedsRunner.class)
public void testSampleAnyZero() {
  PCollection<Integer> input =
      pipeline.apply(
          Create.timestamped(ImmutableList.of(tv(0), tv(1), tv(2), tv(3), tv(4), tv(5)))
              .withCoder(BigEndianIntegerCoder.of()));
  PCollection<Integer> output =
      input
          .apply(Window.into(FixedWindows.of(Duration.standardSeconds(3))))
          .apply(Sample.any(0));

  PAssert.that(output)
      .inWindow(new IntervalWindow(new Instant(0), Duration.standardSeconds(3)))
      .satisfies(new VerifyCorrectSample<>(0, EMPTY));
  PAssert.that(output)
      .inWindow(new IntervalWindow(new Instant(3000), Duration.standardSeconds(3)))
      .satisfies(new VerifyCorrectSample<>(0, EMPTY));
  pipeline.run();
}
 
Example #16
Source File: WindowMappingFnRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testWindowMapping() throws Exception {
  String pTransformId = "pTransformId";

  SdkComponents components = SdkComponents.create();
  components.registerEnvironment(Environments.createDockerEnvironment("java"));
  RunnerApi.FunctionSpec functionSpec =
      RunnerApi.FunctionSpec.newBuilder()
          .setUrn(WindowMappingFnRunner.URN)
          .setPayload(
              ParDoTranslation.translateWindowMappingFn(
                      new GlobalWindows().getDefaultWindowMappingFn(), components)
                  .toByteString())
          .build();
  RunnerApi.PTransform pTransform =
      RunnerApi.PTransform.newBuilder().setSpec(functionSpec).build();

  ThrowingFunction<KV<Object, BoundedWindow>, KV<Object, BoundedWindow>> mapFunction =
      WindowMappingFnRunner.createMapFunctionForPTransform(pTransformId, pTransform);

  KV<Object, BoundedWindow> input =
      KV.of("abc", new IntervalWindow(Instant.now(), Duration.standardMinutes(1)));

  assertEquals(KV.of(input.getKey(), GlobalWindow.INSTANCE), mapFunction.apply(input));
}
 
Example #17
Source File: WindowMergingFnRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testWindowMergingWithNonMergingWindowFn() throws Exception {
  ThrowingFunction<
          KV<Object, Iterable<BoundedWindow>>,
          KV<
              Object,
              KV<Iterable<BoundedWindow>, Iterable<KV<BoundedWindow, Iterable<BoundedWindow>>>>>>
      mapFunction =
          WindowMergingFnRunner.createMapFunctionForPTransform(
              "ptransformId", createMergeTransformForWindowFn(new GlobalWindows()));

  KV<Object, Iterable<BoundedWindow>> input =
      KV.of(
          "abc",
          ImmutableList.of(new IntervalWindow(Instant.now(), Duration.standardMinutes(1))));

  assertEquals(
      KV.of(input.getKey(), KV.of(input.getValue(), Collections.emptyList())),
      mapFunction.apply(input));
}
 
Example #18
Source File: StreamingGroupAlsoByWindowsReshuffleDoFnTest.java    From beam with Apache License 2.0 6 votes vote down vote up
private <V> void addElement(
    InputMessageBundle.Builder messageBundle,
    Collection<IntervalWindow> windows,
    Instant timestamp,
    Coder<V> valueCoder,
    V value)
    throws IOException {
  @SuppressWarnings({"unchecked", "rawtypes"})
  Coder<Collection<? extends BoundedWindow>> windowsCoder =
      (Coder) CollectionCoder.of(windowCoder);

  ByteString.Output dataOutput = ByteString.newOutput();
  valueCoder.encode(value, dataOutput, Context.OUTER);
  messageBundle
      .addMessagesBuilder()
      .setMetadata(WindmillSink.encodeMetadata(windowsCoder, windows, PaneInfo.NO_FIRING))
      .setData(dataOutput.toByteString())
      .setTimestamp(WindmillTimeUtils.harnessToWindmillTimestamp(timestamp));
}
 
Example #19
Source File: FlinkMergingNonShuffleReduceFunction.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void reduce(
    Iterable<WindowedValue<KV<K, InputT>>> elements, Collector<WindowedValue<KV<K, OutputT>>> out)
    throws Exception {

  PipelineOptions options = serializedOptions.get();

  FlinkSideInputReader sideInputReader =
      new FlinkSideInputReader(sideInputs, getRuntimeContext());

  AbstractFlinkCombineRunner<K, InputT, AccumT, OutputT, W> reduceRunner;
  if (windowingStrategy.getWindowFn().windowCoder().equals(IntervalWindow.getCoder())) {
    reduceRunner = new SortingFlinkCombineRunner<>();
  } else {
    reduceRunner = new HashingFlinkCombineRunner<>();
  }

  reduceRunner.combine(
      new AbstractFlinkCombineRunner.CompleteFlinkCombiner<>(combineFn),
      windowingStrategy,
      sideInputReader,
      options,
      elements,
      out);
}
 
Example #20
Source File: AfterSynchronizedProcessingTimeStateMachineTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testAfterProcessingTimeWithMergingWindow() throws Exception {
  Duration windowDuration = Duration.millis(10);
  SimpleTriggerStateMachineTester<IntervalWindow> tester =
      TriggerStateMachineTester.forTrigger(
          AfterProcessingTimeStateMachine.pastFirstElementInPane()
              .plusDelayOf(Duration.millis(5)),
          Sessions.withGapDuration(windowDuration));

  tester.advanceProcessingTime(new Instant(10));
  tester.injectElements(1); // in [1, 11), timer for 15
  IntervalWindow firstWindow = new IntervalWindow(new Instant(1), new Instant(11));
  assertFalse(tester.shouldFire(firstWindow));

  tester.advanceProcessingTime(new Instant(12));
  tester.injectElements(3); // in [3, 13), timer for 17
  IntervalWindow secondWindow = new IntervalWindow(new Instant(3), new Instant(13));
  assertFalse(tester.shouldFire(secondWindow));

  tester.mergeWindows();
  IntervalWindow mergedWindow = new IntervalWindow(new Instant(1), new Instant(13));

  tester.advanceProcessingTime(new Instant(16));
  assertTrue(tester.shouldFire(mergedWindow));
}
 
Example #21
Source File: AfterWatermarkStateMachineTest.java    From beam with Apache License 2.0 5 votes vote down vote up
public void testRunningAsTrigger(TriggerStateMachine mockTrigger, IntervalWindow window)
    throws Exception {

  // Don't fire due to mock saying no
  when(mockTrigger.shouldFire(anyTriggerContext())).thenReturn(false);
  assertFalse(tester.shouldFire(window)); // not ready

  // Fire due to mock trigger; early trigger is required to be a OnceTrigger
  when(mockTrigger.shouldFire(anyTriggerContext())).thenReturn(true);
  assertTrue(tester.shouldFire(window)); // ready
  tester.fireIfShouldFire(window);
  assertFalse(tester.isMarkedFinished(window));
}
 
Example #22
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that when a processing time timer comes in after a window is expired but in the same
 * bundle it does not cause a spurious output.
 */
@Test
public void testCombiningAccumulatingProcessingTime() throws Exception {
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(100)))
          .withTimestampCombiner(TimestampCombiner.EARLIEST)
          .withMode(AccumulationMode.ACCUMULATING_FIRED_PANES)
          .withAllowedLateness(Duration.ZERO)
          .withTrigger(
              Repeatedly.forever(
                  AfterProcessingTime.pastFirstElementInPane().plusDelayOf(Duration.millis(10))));

  ReduceFnTester<Integer, Integer, IntervalWindow> tester =
      ReduceFnTester.combining(strategy, Sum.ofIntegers(), VarIntCoder.of());

  tester.advanceProcessingTime(new Instant(5000));
  injectElement(tester, 2); // processing timer @ 5000 + 10; EOW timer @ 100
  injectElement(tester, 5);

  tester.advanceInputWatermarkNoTimers(new Instant(100));
  tester.advanceProcessingTimeNoTimers(new Instant(5010));

  // Fires the GC/EOW timer at the same time as the processing time timer.
  tester.fireTimers(
      new IntervalWindow(new Instant(0), new Instant(100)),
      TimestampedValue.of(TimeDomain.EVENT_TIME, new Instant(100)),
      TimestampedValue.of(TimeDomain.PROCESSING_TIME, new Instant(5010)));

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(
              equalTo(7), 2, 0, 100, PaneInfo.createPane(true, true, Timing.ON_TIME, 0, 0))));
}
 
Example #23
Source File: RepeatedlyStateMachineTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Tests that the repeatedly is ready to fire whenever the subtrigger is ready. */
@Test
public void testShouldFire() throws Exception {
  setUp(FixedWindows.of(Duration.millis(10)));

  when(mockTrigger.shouldFire(anyTriggerContext())).thenReturn(true);
  assertTrue(tester.shouldFire(new IntervalWindow(new Instant(0), new Instant(10))));

  when(mockTrigger.shouldFire(Mockito.any())).thenReturn(false);
  assertFalse(tester.shouldFire(new IntervalWindow(new Instant(0), new Instant(10))));
}
 
Example #24
Source File: FlinkReduceFunction.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void reduce(
    Iterable<WindowedValue<KV<K, AccumT>>> elements, Collector<WindowedValue<KV<K, OutputT>>> out)
    throws Exception {

  PipelineOptions options = serializedOptions.get();

  FlinkSideInputReader sideInputReader =
      new FlinkSideInputReader(sideInputs, getRuntimeContext());

  AbstractFlinkCombineRunner<K, AccumT, AccumT, OutputT, W> reduceRunner;

  if (groupedByWindow) {
    reduceRunner = new SingleWindowFlinkCombineRunner<>();
  } else {
    if (!windowingStrategy.getWindowFn().isNonMerging()
        && !windowingStrategy.getWindowFn().windowCoder().equals(IntervalWindow.getCoder())) {
      reduceRunner = new HashingFlinkCombineRunner<>();
    } else {
      reduceRunner = new SortingFlinkCombineRunner<>();
    }
  }

  reduceRunner.combine(
      new AbstractFlinkCombineRunner.FinalFlinkCombiner<>(combineFn),
      windowingStrategy,
      sideInputReader,
      options,
      elements,
      out);
}
 
Example #25
Source File: AssignWindowsRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void factoryCreatesFromKnownWindowFn() throws Exception {
  SdkComponents components = SdkComponents.create();
  components.registerEnvironment(Environments.createDockerEnvironment("java"));
  PTransform windowPTransform =
      PTransform.newBuilder()
          .putInputs("in", "input")
          .putOutputs("out", "output")
          .setSpec(
              FunctionSpec.newBuilder()
                  .setUrn(PTransformTranslation.ASSIGN_WINDOWS_TRANSFORM_URN)
                  .setPayload(
                      WindowIntoPayload.newBuilder()
                          .setWindowFn(
                              WindowingStrategyTranslation.toProto(
                                  Sessions.withGapDuration(Duration.standardMinutes(12L)),
                                  components))
                          .build()
                          .toByteString())
                  .build())
          .build();
  ThrowingFunction<WindowedValue<?>, WindowedValue<?>> fn =
      (ThrowingFunction) factory.forPTransform("transform", windowPTransform);
  WindowedValue<?> output =
      fn.apply(
          WindowedValue.of(
              22L,
              new Instant(5),
              new IntervalWindow(new Instant(0L), new Instant(20027L)),
              PaneInfo.ON_TIME_AND_ONLY_FIRING));

  assertThat(
      output,
      equalTo(
          WindowedValue.of(
              22L,
              new Instant(5),
              new IntervalWindow(new Instant(5L), Duration.standardMinutes(12L)),
              PaneInfo.ON_TIME_AND_ONLY_FIRING)));
}
 
Example #26
Source File: Utils.java    From streamingbook with Apache License 2.0 5 votes vote down vote up
public static String formatWindow(BoundedWindow window) {
    if (window instanceof GlobalWindow) {
        return "[global window]";
    } else if (window instanceof IntervalWindow) {
        IntervalWindow interval = (IntervalWindow) window;
        return "[" + formatTime(interval.start()) + ", " + formatTime(interval.end()) + ")";
    } else {
        return "..., " + formatTime(window.maxTimestamp()) + "]";
    }
}
 
Example #27
Source File: StreamingSideInputDoFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testSideInputReady() throws Exception {
  PCollectionView<String> view = createView();

  when(stepContext.getSideInputNotifications())
      .thenReturn(Arrays.<Windmill.GlobalDataId>asList());
  when(stepContext.issueSideInputFetch(
          eq(view), any(BoundedWindow.class), eq(SideInputState.UNKNOWN)))
      .thenReturn(true);
  when(execContext.getSideInputReaderForViews(
          Mockito.<Iterable<? extends PCollectionView<?>>>any()))
      .thenReturn(mockSideInputReader);
  when(mockSideInputReader.contains(eq(view))).thenReturn(true);
  when(mockSideInputReader.get(eq(view), any(BoundedWindow.class))).thenReturn("data");

  ListOutputManager outputManager = new ListOutputManager();
  List<PCollectionView<String>> views = Arrays.asList(view);
  StreamingSideInputFetcher<String, IntervalWindow> sideInputFetcher = createFetcher(views);
  StreamingSideInputDoFnRunner<String, String, IntervalWindow> runner =
      createRunner(outputManager, views, sideInputFetcher);

  runner.startBundle();
  runner.processElement(createDatum("e", 0));
  runner.finishBundle();

  assertThat(outputManager.getOutput(mainOutputTag), contains(createDatum("e:data", 0)));
}
 
Example #28
Source File: StreamingGroupAlsoByWindowsReshuffleDoFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private <InputT, OutputT>
    DoFnRunner<KeyedWorkItem<String, InputT>, KV<String, OutputT>> makeRunner(
        TupleTag<KV<String, OutputT>> outputTag,
        DoFnRunners.OutputManager outputManager,
        WindowingStrategy<? super String, IntervalWindow> windowingStrategy,
        GroupAlsoByWindowFn<KeyedWorkItem<String, InputT>, KV<String, OutputT>> fn) {
  return new GroupAlsoByWindowFnRunner<>(
      PipelineOptionsFactory.create(),
      fn,
      NullSideInputReader.empty(),
      outputManager,
      outputTag,
      stepContext);
}
 
Example #29
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnElementCombiningDiscarding() throws Exception {
  // Test basic execution of a trigger using a non-combining window set and discarding mode.
  WindowingStrategy<?, IntervalWindow> strategy =
      WindowingStrategy.of((WindowFn<?, IntervalWindow>) FixedWindows.of(Duration.millis(10)))
          .withTimestampCombiner(TimestampCombiner.EARLIEST)
          .withMode(AccumulationMode.DISCARDING_FIRED_PANES)
          .withAllowedLateness(Duration.millis(100));

  ReduceFnTester<Integer, Integer, IntervalWindow> tester =
      ReduceFnTester.combining(
          strategy, mockTriggerStateMachine, Sum.ofIntegers(), VarIntCoder.of());

  injectElement(tester, 2);

  when(mockTriggerStateMachine.shouldFire(anyTriggerContext())).thenReturn(true);
  injectElement(tester, 3);

  when(mockTriggerStateMachine.shouldFire(anyTriggerContext())).thenReturn(true);
  triggerShouldFinish(mockTriggerStateMachine);
  injectElement(tester, 4);

  // This element shouldn't be seen, because the trigger has finished
  injectElement(tester, 6);

  assertThat(
      tester.extractOutput(),
      contains(
          isSingleWindowedValue(equalTo(5), 2, 0, 10),
          isSingleWindowedValue(equalTo(4), 4, 0, 10)));
  assertTrue(tester.isMarkedFinished(firstWindow));
  tester.assertHasOnlyGlobalAndFinishedSetsFor(firstWindow);
}
 
Example #30
Source File: AfterWatermarkStateMachineTest.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the trigger rewinds to be non-finished in the merged window.
 *
 * <p>Because windows are discarded when a trigger finishes, we need to embed this in a sequence
 * in order to check that it is re-activated. So this test is potentially sensitive to other
 * triggers' correctness.
 */
@Test
public void testOnMergeRewinds() throws Exception {
  tester =
      TriggerStateMachineTester.forTrigger(
          AfterEachStateMachine.inOrder(
              AfterWatermarkStateMachine.pastEndOfWindow(),
              RepeatedlyStateMachine.forever(AfterPaneStateMachine.elementCountAtLeast(1))),
          Sessions.withGapDuration(Duration.millis(10)));

  tester.injectElements(1);
  tester.injectElements(5);
  IntervalWindow firstWindow = new IntervalWindow(new Instant(1), new Instant(11));
  IntervalWindow secondWindow = new IntervalWindow(new Instant(5), new Instant(15));
  IntervalWindow mergedWindow = new IntervalWindow(new Instant(1), new Instant(15));

  // Finish the AfterWatermark.pastEndOfWindow() trigger in only the first window
  tester.advanceInputWatermark(new Instant(11));
  assertTrue(tester.shouldFire(firstWindow));
  assertFalse(tester.shouldFire(secondWindow));
  tester.fireIfShouldFire(firstWindow);

  // Confirm that we are on the second trigger by probing
  assertFalse(tester.shouldFire(firstWindow));
  tester.injectElements(1);
  assertTrue(tester.shouldFire(firstWindow));
  tester.fireIfShouldFire(firstWindow);

  // Merging should re-activate the watermark trigger in the merged window
  tester.mergeWindows();

  // Confirm that we are not on the second trigger by probing
  assertFalse(tester.shouldFire(mergedWindow));
  tester.injectElements(1);
  assertFalse(tester.shouldFire(mergedWindow));

  // And confirm that advancing the watermark fires again
  tester.advanceInputWatermark(new Instant(15));
  assertTrue(tester.shouldFire(mergedWindow));
}