Search in sources :

Example 66 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class SpannerIOWriteTest method testBatchableMutationFilterFn_size.

@Test
public void testBatchableMutationFilterFn_size() {
    Mutation all = Mutation.delete("test", KeySet.all());
    Mutation prefix = Mutation.delete("test", KeySet.prefixRange(Key.of(1L)));
    Mutation range = Mutation.delete("test", KeySet.range(KeyRange.openOpen(Key.of(1L), Key.newBuilder().build())));
    MutationGroup[] mutationGroups = new MutationGroup[] { g(m(1L)), g(m(2L), m(3L)), // not batchable - too big.
    g(m(1L), m(3L), m(4L), m(5L)), g(del(1L)), // not point delete.
    g(del(5L, 6L)), g(all), g(prefix), g(range) };
    long mutationSize = MutationSizeEstimator.sizeOf(m(1L));
    BatchableMutationFilterFn testFn = new BatchableMutationFilterFn(null, null, mutationSize * 3, 1000, 1000);
    DoFn<MutationGroup, MutationGroup>.ProcessContext mockProcessContext = Mockito.mock(ProcessContext.class);
    when(mockProcessContext.sideInput(any())).thenReturn(getSchema());
    // Capture the outputs.
    doNothing().when(mockProcessContext).output(mutationGroupCaptor.capture());
    doNothing().when(mockProcessContext).output(any(), mutationGroupListCaptor.capture());
    // Process all elements.
    for (MutationGroup m : mutationGroups) {
        when(mockProcessContext.element()).thenReturn(m);
        testFn.processElement(mockProcessContext);
    }
    // Verify captured batchable elements.
    assertThat(mutationGroupCaptor.getAllValues(), containsInAnyOrder(g(m(1L)), g(m(2L), m(3L)), g(del(1L))));
    // Verify captured unbatchable mutations
    Iterable<MutationGroup> unbatchableMutations = Iterables.concat(mutationGroupListCaptor.getAllValues());
    assertThat(unbatchableMutations, containsInAnyOrder(// not batchable - too big.
    g(m(1L), m(3L), m(4L), m(5L)), // not point delete.
    g(del(5L, 6L)), g(all), g(prefix), g(range)));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) Mutation(com.google.cloud.spanner.Mutation) BatchableMutationFilterFn(org.apache.beam.sdk.io.gcp.spanner.SpannerIO.BatchableMutationFilterFn) Test(org.junit.Test)

Example 67 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testSplittableDoFnWithAllMethods.

@Test
public void testSplittableDoFnWithAllMethods() throws Exception {
    MockFn fn = mock(MockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    final SomeRestrictionTracker tracker = mock(SomeRestrictionTracker.class);
    final SomeRestrictionCoder coder = mock(SomeRestrictionCoder.class);
    final InstantCoder watermarkEstimatorStateCoder = InstantCoder.of();
    final Instant watermarkEstimatorState = Instant.now();
    final WatermarkEstimator<Instant> watermarkEstimator = new WatermarkEstimators.Manual(watermarkEstimatorState);
    SomeRestriction restriction = new SomeRestriction();
    final SomeRestriction part1 = new SomeRestriction();
    final SomeRestriction part2 = new SomeRestriction();
    final SomeRestriction part3 = new SomeRestriction();
    when(fn.getRestrictionCoder()).thenReturn(coder);
    when(fn.getWatermarkEstimatorStateCoder()).thenReturn(watermarkEstimatorStateCoder);
    when(fn.getInitialRestriction(mockElement)).thenReturn(restriction);
    doAnswer(AdditionalAnswers.delegatesTo(new MockFn() {

        @DoFn.SplitRestriction
        @Override
        public void splitRestriction(@Element String element, @Restriction SomeRestriction restriction, DoFn.OutputReceiver<SomeRestriction> receiver) {
            receiver.output(part1);
            receiver.output(part2);
            receiver.output(part3);
        }
    })).when(fn).splitRestriction(eq(mockElement), same(restriction), any());
    when(fn.getInitialWatermarkEstimatorState()).thenReturn(watermarkEstimatorState);
    when(fn.newTracker(restriction)).thenReturn(tracker);
    when(fn.newWatermarkEstimator(watermarkEstimatorState)).thenReturn(watermarkEstimator);
    when(fn.processElement(mockProcessContext, tracker, watermarkEstimator)).thenReturn(resume());
    when(fn.getSize()).thenReturn(2.0);
    assertEquals(coder, invoker.invokeGetRestrictionCoder(CoderRegistry.createDefault()));
    assertEquals(watermarkEstimatorStateCoder, invoker.invokeGetWatermarkEstimatorStateCoder(CoderRegistry.createDefault()));
    assertEquals(restriction, invoker.invokeGetInitialRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }
    }));
    List<SomeRestriction> outputs = new ArrayList<>();
    invoker.invokeSplitRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }

        @Override
        public Object restriction() {
            return restriction;
        }

        @Override
        public OutputReceiver outputReceiver(DoFn doFn) {
            return new OutputReceiver<SomeRestriction>() {

                @Override
                public void output(SomeRestriction output) {
                    outputs.add(output);
                }

                @Override
                public void outputWithTimestamp(SomeRestriction output, Instant timestamp) {
                    fail("Unexpected output with timestamp");
                }
            };
        }
    });
    assertEquals(Arrays.asList(part1, part2, part3), outputs);
    assertEquals(watermarkEstimatorState, invoker.invokeGetInitialWatermarkEstimatorState(new FakeArgumentProvider<>()));
    assertEquals(tracker, invoker.invokeNewTracker(new FakeArgumentProvider<String, String>() {

        @Override
        public String element(DoFn<String, String> doFn) {
            return mockElement;
        }

        @Override
        public Object restriction() {
            return restriction;
        }
    }));
    assertEquals(watermarkEstimator, invoker.invokeNewWatermarkEstimator(new FakeArgumentProvider<String, String>() {

        @Override
        public Object watermarkEstimatorState() {
            return watermarkEstimatorState;
        }
    }));
    assertEquals(resume(), invoker.invokeProcessElement(new FakeArgumentProvider<String, String>() {

        @Override
        public DoFn<String, String>.ProcessContext processContext(DoFn<String, String> fn) {
            return mockProcessContext;
        }

        @Override
        public RestrictionTracker<?, ?> restrictionTracker() {
            return tracker;
        }

        @Override
        public WatermarkEstimator<?> watermarkEstimator() {
            return watermarkEstimator;
        }
    }));
    assertEquals(2.0, invoker.invokeGetSize(mockArgumentProvider), 0.0001);
}
Also used : InstantCoder(org.apache.beam.sdk.coders.InstantCoder) Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) OutputReceiver(org.apache.beam.sdk.transforms.DoFn.OutputReceiver) MultiOutputReceiver(org.apache.beam.sdk.transforms.DoFn.MultiOutputReceiver) DoFn(org.apache.beam.sdk.transforms.DoFn) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) Test(org.junit.Test)

Example 68 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testTruncateFnWithHasDefaultMethodsWhenBounded.

@Test
public void testTruncateFnWithHasDefaultMethodsWhenBounded() throws Exception {
    class BoundedMockFn extends DoFn<String, String> {

        @ProcessElement
        public void processElement(ProcessContext c, RestrictionTracker<RestrictionWithBoundedDefaultTracker, Void> tracker, WatermarkEstimator<WatermarkEstimatorStateWithDefaultWatermarkEstimator> watermarkEstimator) {
        }

        @GetInitialRestriction
        public RestrictionWithBoundedDefaultTracker getInitialRestriction(@Element String element) {
            return null;
        }

        @GetInitialWatermarkEstimatorState
        public WatermarkEstimatorStateWithDefaultWatermarkEstimator getInitialWatermarkEstimatorState() {
            return null;
        }
    }
    BoundedMockFn fn = mock(BoundedMockFn.class);
    DoFnInvoker<String, String> invoker = DoFnInvokers.invokerFor(fn);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    coderRegistry.registerCoderProvider(CoderProviders.fromStaticMethods(RestrictionWithBoundedDefaultTracker.class, CoderForDefaultTracker.class));
    coderRegistry.registerCoderForClass(WatermarkEstimatorStateWithDefaultWatermarkEstimator.class, new CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator());
    assertThat(invoker.<RestrictionWithBoundedDefaultTracker>invokeGetRestrictionCoder(coderRegistry), instanceOf(CoderForDefaultTracker.class));
    assertThat(invoker.invokeGetWatermarkEstimatorStateCoder(coderRegistry), instanceOf(CoderForWatermarkEstimatorStateWithDefaultWatermarkEstimator.class));
    RestrictionTracker tracker = invoker.invokeNewTracker(new FakeArgumentProvider<String, String>() {

        @Override
        public Object restriction() {
            return new RestrictionWithBoundedDefaultTracker();
        }
    });
    assertThat(tracker, instanceOf(BoundedDefaultTracker.class));
    TruncateResult<?> result = invoker.invokeTruncateRestriction(new FakeArgumentProvider<String, String>() {

        @Override
        public RestrictionTracker restrictionTracker() {
            return tracker;
        }

        @Override
        public String element(DoFn<String, String> doFn) {
            return "blah";
        }

        @Override
        public Object restriction() {
            return "foo";
        }
    });
    assertEquals("foo", result.getTruncatedRestriction());
    assertEquals(stop(), invoker.invokeProcessElement(mockArgumentProvider));
    assertThat(invoker.invokeNewWatermarkEstimator(new FakeArgumentProvider<String, String>() {

        @Override
        public Object watermarkEstimatorState() {
            return new WatermarkEstimatorStateWithDefaultWatermarkEstimator();
        }
    }), instanceOf(DefaultWatermarkEstimator.class));
}
Also used : RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) DoFn(org.apache.beam.sdk.transforms.DoFn) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) WatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.WatermarkEstimator) FakeArgumentProvider(org.apache.beam.sdk.transforms.reflect.DoFnInvoker.FakeArgumentProvider) HasDefaultWatermarkEstimator(org.apache.beam.sdk.transforms.splittabledofn.HasDefaultWatermarkEstimator) Test(org.junit.Test)

Example 69 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnSignaturesSplittableDoFnTest method testHasRestrictionTracker.

@Test
// used via reflection
@SuppressWarnings("unused")
public void testHasRestrictionTracker() throws Exception {
    DoFnSignature.ProcessElementMethod signature = analyzeProcessElementMethod(new AnonymousMethod() {

        private void method(DoFn<Integer, String>.ProcessContext context, SomeRestrictionTracker tracker) {
        }
    });
    assertTrue(signature.isSplittable());
    assertTrue(signature.extraParameters().stream().anyMatch(Predicates.instanceOf(DoFnSignature.Parameter.RestrictionTrackerParameter.class)::apply));
    assertEquals(SomeRestrictionTracker.class, signature.trackerT().getRawType());
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) FakeDoFn(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.FakeDoFn) AnonymousMethod(org.apache.beam.sdk.transforms.reflect.DoFnSignaturesTestUtils.AnonymousMethod) Test(org.junit.Test)

Example 70 with DoFn

use of org.apache.beam.sdk.transforms.DoFn in project beam by apache.

the class DoFnInvokersTest method testDoFnWithTimer.

/**
 * Tests that the generated {@link DoFnInvoker} passes the timer parameter that it should.
 */
@Test
public void testDoFnWithTimer() throws Exception {
    Timer mockTimer = mock(Timer.class);
    final String timerId = "my-timer-id-here";
    when(mockArgumentProvider.timer(TimerDeclaration.PREFIX + timerId)).thenReturn(mockTimer);
    class MockFn extends DoFn<String, String> {

        @TimerId(timerId)
        private final TimerSpec spec = TimerSpecs.timer(TimeDomain.EVENT_TIME);

        @ProcessElement
        public void processElement(ProcessContext c, @TimerId(timerId) Timer timer) throws Exception {
        }

        @OnTimer(timerId)
        public void onTimer() {
        }
    }
    MockFn fn = mock(MockFn.class);
    assertEquals(stop(), invokeProcessElement(fn));
    verify(fn).processElement(mockProcessContext, mockTimer);
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) Timer(org.apache.beam.sdk.state.Timer) TimerSpec(org.apache.beam.sdk.state.TimerSpec) Test(org.junit.Test)

Aggregations

DoFn (org.apache.beam.sdk.transforms.DoFn)154 Test (org.junit.Test)98 Pipeline (org.apache.beam.sdk.Pipeline)60 KV (org.apache.beam.sdk.values.KV)45 TupleTag (org.apache.beam.sdk.values.TupleTag)28 StateSpec (org.apache.beam.sdk.state.StateSpec)26 Instant (org.joda.time.Instant)26 ArrayList (java.util.ArrayList)23 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)23 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)22 PCollection (org.apache.beam.sdk.values.PCollection)21 TimerSpec (org.apache.beam.sdk.state.TimerSpec)19 WindowedValue (org.apache.beam.sdk.util.WindowedValue)18 PCollectionView (org.apache.beam.sdk.values.PCollectionView)18 HashMap (java.util.HashMap)17 Coder (org.apache.beam.sdk.coders.Coder)17 List (java.util.List)16 Map (java.util.Map)14 ValueState (org.apache.beam.sdk.state.ValueState)14 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)13