Search in sources :

Example 11 with Timer

use of org.apache.beam.runners.core.construction.Timer in project beam by apache.

the class CommonCoderTest method verifyDecodedValue.

private void verifyDecodedValue(CommonCoder coder, Object expectedValue, Object actualValue) {
    String s = coder.getUrn();
    if (s.equals(getUrn(StandardCoders.Enum.BYTES))) {
        assertThat(expectedValue, equalTo(actualValue));
    } else if (s.equals(getUrn(StandardCoders.Enum.BOOL))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.STRING_UTF8))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.KV))) {
        assertThat(actualValue, instanceOf(KV.class));
        verifyDecodedValue(coder.getComponents().get(0), ((KV) expectedValue).getKey(), ((KV) actualValue).getKey());
        verifyDecodedValue(coder.getComponents().get(0), ((KV) expectedValue).getValue(), ((KV) actualValue).getValue());
    } else if (s.equals(getUrn(StandardCoders.Enum.VARINT))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.INTERVAL_WINDOW))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.ITERABLE)) || s.equals(getUrn(StandardCoders.Enum.STATE_BACKED_ITERABLE))) {
        assertThat(actualValue, instanceOf(Iterable.class));
        CommonCoder componentCoder = coder.getComponents().get(0);
        Iterator<Object> expectedValueIterator = ((Iterable<Object>) expectedValue).iterator();
        for (Object value : (Iterable<Object>) actualValue) {
            verifyDecodedValue(componentCoder, expectedValueIterator.next(), value);
        }
        assertFalse(expectedValueIterator.hasNext());
    } else if (s.equals(getUrn(StandardCoders.Enum.TIMER))) {
        assertEquals((Timer) expectedValue, (Timer) actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.GLOBAL_WINDOW))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.WINDOWED_VALUE))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.PARAM_WINDOWED_VALUE))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.DOUBLE))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.ROW))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.SHARDED_KEY))) {
        assertEquals(expectedValue, actualValue);
    } else if (s.equals(getUrn(StandardCoders.Enum.CUSTOM_WINDOW))) {
        assertEquals(expectedValue, actualValue);
    } else {
        throw new IllegalStateException("Unknown coder URN: " + coder.getUrn());
    }
}
Also used : StateBackedIterable(org.apache.beam.fn.harness.state.StateBackedIterable) Timer(org.apache.beam.runners.core.construction.Timer) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV)

Example 12 with Timer

use of org.apache.beam.runners.core.construction.Timer in project beam by apache.

the class SparkExecutableStageFunction method call.

@Override
public Iterator<RawUnionValue> call(Iterator<WindowedValue<InputT>> inputs) throws Exception {
    SparkPipelineOptions options = pipelineOptions.get().as(SparkPipelineOptions.class);
    // Register standard file systems.
    FileSystems.setDefaultPipelineOptions(options);
    // Otherwise, this may cause validation errors (e.g. ParDoTest)
    if (!inputs.hasNext()) {
        return Collections.emptyIterator();
    }
    try (ExecutableStageContext stageContext = contextFactory.get(jobInfo)) {
        ExecutableStage executableStage = ExecutableStage.fromPayload(stagePayload);
        try (StageBundleFactory stageBundleFactory = stageContext.getStageBundleFactory(executableStage)) {
            ConcurrentLinkedQueue<RawUnionValue> collector = new ConcurrentLinkedQueue<>();
            StateRequestHandler stateRequestHandler = getStateRequestHandler(executableStage, stageBundleFactory.getProcessBundleDescriptor());
            if (executableStage.getTimers().size() == 0) {
                ReceiverFactory receiverFactory = new ReceiverFactory(collector, outputMap);
                processElements(stateRequestHandler, receiverFactory, null, stageBundleFactory, inputs);
                return collector.iterator();
            }
            // Used with Batch, we know that all the data is available for this key. We can't use the
            // timer manager from the context because it doesn't exist. So we create one and advance
            // time to the end after processing all elements.
            final InMemoryTimerInternals timerInternals = new InMemoryTimerInternals();
            timerInternals.advanceProcessingTime(Instant.now());
            timerInternals.advanceSynchronizedProcessingTime(Instant.now());
            ReceiverFactory receiverFactory = new ReceiverFactory(collector, outputMap);
            TimerReceiverFactory timerReceiverFactory = new TimerReceiverFactory(stageBundleFactory, (Timer<?> timer, TimerInternals.TimerData timerData) -> {
                currentTimerKey = timer.getUserKey();
                if (timer.getClearBit()) {
                    timerInternals.deleteTimer(timerData);
                } else {
                    timerInternals.setTimer(timerData);
                }
            }, windowCoder);
            // Process inputs.
            processElements(stateRequestHandler, receiverFactory, timerReceiverFactory, stageBundleFactory, inputs);
            // Finish any pending windows by advancing the input watermark to infinity.
            timerInternals.advanceInputWatermark(BoundedWindow.TIMESTAMP_MAX_VALUE);
            // Finally, advance the processing time to infinity to fire any timers.
            timerInternals.advanceProcessingTime(BoundedWindow.TIMESTAMP_MAX_VALUE);
            timerInternals.advanceSynchronizedProcessingTime(BoundedWindow.TIMESTAMP_MAX_VALUE);
            // itself)
            while (timerInternals.hasPendingTimers()) {
                try (RemoteBundle bundle = stageBundleFactory.getBundle(receiverFactory, timerReceiverFactory, stateRequestHandler, getBundleProgressHandler())) {
                    PipelineTranslatorUtils.fireEligibleTimers(timerInternals, bundle.getTimerReceivers(), currentTimerKey);
                }
            }
            return collector.iterator();
        }
    }
}
Also used : TimerReceiverFactory(org.apache.beam.runners.fnexecution.control.TimerReceiverFactory) OutputReceiverFactory(org.apache.beam.runners.fnexecution.control.OutputReceiverFactory) StateRequestHandler(org.apache.beam.runners.fnexecution.state.StateRequestHandler) RawUnionValue(org.apache.beam.sdk.transforms.join.RawUnionValue) InMemoryTimerInternals(org.apache.beam.runners.core.InMemoryTimerInternals) SparkPipelineOptions(org.apache.beam.runners.spark.SparkPipelineOptions) StageBundleFactory(org.apache.beam.runners.fnexecution.control.StageBundleFactory) Timer(org.apache.beam.runners.core.construction.Timer) ExecutableStageContext(org.apache.beam.runners.fnexecution.control.ExecutableStageContext) TimerReceiverFactory(org.apache.beam.runners.fnexecution.control.TimerReceiverFactory) ExecutableStage(org.apache.beam.runners.core.construction.graph.ExecutableStage) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RemoteBundle(org.apache.beam.runners.fnexecution.control.RemoteBundle)

Aggregations

Timer (org.apache.beam.runners.core.construction.Timer)12 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)6 KV (org.apache.beam.sdk.values.KV)5 Instant (org.joda.time.Instant)4 Map (java.util.Map)3 InMemoryTimerInternals (org.apache.beam.runners.core.InMemoryTimerInternals)3 RemoteBundle (org.apache.beam.runners.fnexecution.control.RemoteBundle)3 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)3 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Lock (java.util.concurrent.locks.Lock)2 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)2 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)2 MutableObject (org.apache.beam.repackaged.core.org.apache.commons.lang3.mutable.MutableObject)2 StateNamespace (org.apache.beam.runners.core.StateNamespace)2 TimerInternals (org.apache.beam.runners.core.TimerInternals)2