use of org.apache.beam.runners.dataflow.worker.util.ListOutputManager in project beam by apache.
the class StreamingGroupAlsoByWindowFnsTest method testEmpty.
@Test
public void testEmpty() throws Exception {
TupleTag<KV<String, Iterable<String>>> outputTag = new TupleTag<>();
ListOutputManager outputManager = new ListOutputManager();
DoFnRunner<KeyedWorkItem<String, String>, KV<String, Iterable<String>>> runner = makeRunner(outputTag, outputManager, WindowingStrategy.of(FixedWindows.of(Duration.millis(10))));
runner.startBundle();
runner.finishBundle();
List<?> result = outputManager.getOutput(outputTag);
assertThat(result.size(), equalTo(0));
}
use of org.apache.beam.runners.dataflow.worker.util.ListOutputManager in project beam by apache.
the class StreamingGroupAlsoByWindowFnsTest method testSlidingWindows.
@Test
public void testSlidingWindows() throws Exception {
TupleTag<KV<String, Iterable<String>>> outputTag = new TupleTag<>();
ListOutputManager outputManager = new ListOutputManager();
DoFnRunner<KeyedWorkItem<String, String>, KV<String, Iterable<String>>> runner = makeRunner(outputTag, outputManager, WindowingStrategy.of(SlidingWindows.of(Duration.millis(20)).every(Duration.millis(10))).withTimestampCombiner(TimestampCombiner.EARLIEST));
when(mockTimerInternals.currentInputWatermarkTime()).thenReturn(new Instant(5));
runner.startBundle();
WorkItem.Builder workItem1 = WorkItem.newBuilder();
workItem1.setKey(ByteString.copyFromUtf8(KEY));
workItem1.setWorkToken(WORK_TOKEN);
InputMessageBundle.Builder messageBundle = workItem1.addMessageBundlesBuilder();
messageBundle.setSourceComputationId(SOURCE_COMPUTATION_ID);
Coder<String> valueCoder = StringUtf8Coder.of();
addElement(messageBundle, Arrays.asList(window(-10, 10), window(0, 20)), new Instant(5), valueCoder, "v1");
addElement(messageBundle, Arrays.asList(window(-10, 10), window(0, 20)), new Instant(2), valueCoder, "v0");
addElement(messageBundle, Arrays.asList(window(0, 20), window(10, 30)), new Instant(15), valueCoder, "v2");
runner.processElement(createValue(workItem1, valueCoder));
runner.finishBundle();
runner.startBundle();
WorkItem.Builder workItem2 = WorkItem.newBuilder();
workItem2.setKey(ByteString.copyFromUtf8(KEY));
workItem2.setWorkToken(WORK_TOKEN);
addTimer(workItem2, window(-10, 10), new Instant(9), Timer.Type.WATERMARK);
addTimer(workItem2, window(0, 20), new Instant(19), Timer.Type.WATERMARK);
addTimer(workItem2, window(10, 30), new Instant(29), Timer.Type.WATERMARK);
when(mockTimerInternals.currentInputWatermarkTime()).thenReturn(new Instant(30));
runner.processElement(createValue(workItem2, valueCoder));
runner.finishBundle();
List<WindowedValue<KV<String, Iterable<String>>>> result = outputManager.getOutput(outputTag);
assertThat(result.size(), equalTo(3));
assertThat(result, containsInAnyOrder(WindowMatchers.isSingleWindowedValue(isKv(equalTo(KEY), containsInAnyOrder("v0", "v1")), equalTo(new Instant(2)), equalTo(window(-10, 10))), WindowMatchers.isSingleWindowedValue(isKv(equalTo(KEY), containsInAnyOrder("v0", "v1", "v2")), equalTo(new Instant(2)), equalTo(window(0, 20))), WindowMatchers.isSingleWindowedValue(isKv(equalTo(KEY), containsInAnyOrder("v2")), equalTo(new Instant(15)), equalTo(window(10, 30)))));
}
use of org.apache.beam.runners.dataflow.worker.util.ListOutputManager in project beam by apache.
the class StreamingSideInputDoFnRunnerTest method testMultipleSideInputs.
@Test
public void testMultipleSideInputs() throws Exception {
PCollectionView<String> view1 = createView();
PCollectionView<String> view2 = createView();
IntervalWindow window = new IntervalWindow(new Instant(0), new Instant(10));
Windmill.GlobalDataId id = Windmill.GlobalDataId.newBuilder().setTag(view1.getTagInternal().getId()).setVersion(ByteString.copyFrom(CoderUtils.encodeToByteArray(IntervalWindow.getCoder(), window))).build();
Set<Windmill.GlobalDataRequest> requestSet = new HashSet<>();
requestSet.add(Windmill.GlobalDataRequest.newBuilder().setDataId(id).build());
Map<IntervalWindow, Set<Windmill.GlobalDataRequest>> blockedMap = new HashMap<>();
blockedMap.put(window, requestSet);
ValueState<Map<IntervalWindow, Set<GlobalDataRequest>>> blockedMapState = state.state(StateNamespaces.global(), StreamingSideInputFetcher.blockedMapAddr(WINDOW_FN.windowCoder()));
blockedMapState.write(blockedMap);
when(stepContext.getSideInputNotifications()).thenReturn(Arrays.asList(id));
when(stepContext.issueSideInputFetch(any(PCollectionView.class), any(BoundedWindow.class), any(SideInputState.class))).thenReturn(true);
when(execContext.getSideInputReaderForViews(Mockito.<Iterable<? extends PCollectionView<?>>>any())).thenReturn(mockSideInputReader);
when(mockSideInputReader.contains(eq(view1))).thenReturn(true);
when(mockSideInputReader.contains(eq(view2))).thenReturn(true);
when(mockSideInputReader.get(eq(view1), any(BoundedWindow.class))).thenReturn("data1");
when(mockSideInputReader.get(eq(view2), any(BoundedWindow.class))).thenReturn("data2");
ListOutputManager outputManager = new ListOutputManager();
List<PCollectionView<String>> views = Arrays.asList(view1, view2);
StreamingSideInputFetcher<String, IntervalWindow> sideInputFetcher = createFetcher(views);
StreamingSideInputDoFnRunner<String, String, IntervalWindow> runner = createRunner(outputManager, views, sideInputFetcher);
sideInputFetcher.watermarkHold(createWindow(0)).add(new Instant(0));
sideInputFetcher.elementBag(createWindow(0)).add(createDatum("e1", 0));
runner.startBundle();
runner.processElement(createDatum("e2", 2));
runner.finishBundle();
assertThat(outputManager.getOutput(mainOutputTag), contains(createDatum("e1:data1:data2", 0), createDatum("e2:data1:data2", 2)));
assertThat(blockedMapState.read(), Matchers.nullValue());
assertThat(sideInputFetcher.watermarkHold(createWindow(0)).read(), Matchers.nullValue());
assertThat(sideInputFetcher.elementBag(createWindow(0)).read(), Matchers.emptyIterable());
}
use of org.apache.beam.runners.dataflow.worker.util.ListOutputManager in project beam by apache.
the class StreamingSideInputDoFnRunnerTest method testSideInputReady.
@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)));
}
use of org.apache.beam.runners.dataflow.worker.util.ListOutputManager in project beam by apache.
the class StreamingSideInputDoFnRunnerTest method testSideInputNotification.
@Test
public void testSideInputNotification() throws Exception {
PCollectionView<String> view = createView();
IntervalWindow window = new IntervalWindow(new Instant(0), new Instant(10));
Windmill.GlobalDataId id = Windmill.GlobalDataId.newBuilder().setTag(view.getTagInternal().getId()).setVersion(ByteString.copyFrom(CoderUtils.encodeToByteArray(IntervalWindow.getCoder(), window))).build();
Set<Windmill.GlobalDataRequest> requestSet = new HashSet<>();
requestSet.add(Windmill.GlobalDataRequest.newBuilder().setDataId(id).build());
Map<IntervalWindow, Set<Windmill.GlobalDataRequest>> blockedMap = new HashMap<>();
blockedMap.put(window, requestSet);
ValueState<Map<IntervalWindow, Set<GlobalDataRequest>>> blockedMapState = state.state(StateNamespaces.global(), StreamingSideInputFetcher.blockedMapAddr(WINDOW_FN.windowCoder()));
blockedMapState.write(blockedMap);
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);
sideInputFetcher.watermarkHold(createWindow(0)).add(new Instant(0));
sideInputFetcher.elementBag(createWindow(0)).add(createDatum("e", 0));
when(stepContext.getSideInputNotifications()).thenReturn(Arrays.asList(id));
when(stepContext.issueSideInputFetch(eq(view), any(BoundedWindow.class), eq(SideInputState.UNKNOWN))).thenReturn(false);
when(stepContext.issueSideInputFetch(eq(view), any(BoundedWindow.class), eq(SideInputState.KNOWN_READY))).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");
runner.startBundle();
runner.finishBundle();
assertThat(outputManager.getOutput(mainOutputTag), contains(createDatum("e:data", 0)));
assertThat(blockedMapState.read(), Matchers.nullValue());
assertThat(sideInputFetcher.watermarkHold(createWindow(0)).read(), Matchers.nullValue());
assertThat(sideInputFetcher.elementBag(createWindow(0)).read(), Matchers.emptyIterable());
}
Aggregations