Search in sources :

Example 6 with InstructionRequest

use of org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest in project beam by apache.

the class RegisterAndProcessBundleOperation method getProcessBundleProgress.

/**
 * Returns the compound metrics recorded, by issuing a request to the SDK harness.
 *
 * <p>This includes key progress indicators as well as user-defined metrics.
 *
 * <p>Use {@link #getInputElementsConsumed} on the future value to extract the elements consumed
 * from the upstream read operation.
 *
 * <p>May be called at any time, including before start() and after finish().
 */
public CompletionStage<BeamFnApi.ProcessBundleProgressResponse> getProcessBundleProgress() {
    // processBundleId may be reset if this bundle finishes asynchronously.
    String processBundleId = this.processBundleId;
    if (processBundleId == null) {
        return CompletableFuture.completedFuture(BeamFnApi.ProcessBundleProgressResponse.getDefaultInstance());
    }
    InstructionRequest processBundleRequest = InstructionRequest.newBuilder().setInstructionId(idGenerator.getId()).setProcessBundleProgress(ProcessBundleProgressRequest.newBuilder().setInstructionId(processBundleId)).build();
    return instructionRequestHandler.handle(processBundleRequest).thenApply(InstructionResponse::getProcessBundleProgress);
}
Also used : InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) InstructionResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionResponse) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)

Example 7 with InstructionRequest

use of org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest in project beam by apache.

the class RegisterAndProcessBundleOperation method start.

@Override
public void start() throws Exception {
    try (Closeable scope = context.enterStart()) {
        super.start();
        // Only register once by using the presence of the future as a signal.
        if (registerFuture == null) {
            InstructionRequest request = InstructionRequest.newBuilder().setInstructionId(idGenerator.getId()).setRegister(registerRequest).build();
            registerFuture = instructionRequestHandler.handle(request);
        }
        checkState(registerRequest.getProcessBundleDescriptorCount() == 1, "Only one bundle registration at a time currently supported.");
        InstructionRequest processBundleRequest = InstructionRequest.newBuilder().setInstructionId(getProcessBundleInstructionId()).setProcessBundle(ProcessBundleRequest.newBuilder().setProcessBundleDescriptorId(registerRequest.getProcessBundleDescriptor(0).getId())).build();
        deregisterStateHandler = beamFnStateDelegator.registerForProcessBundleInstructionId(getProcessBundleInstructionId(), this::delegateByStateKeyType);
        processBundleResponse = getRegisterResponse(registerFuture).thenCompose(registerResponse -> instructionRequestHandler.handle(processBundleRequest));
    }
}
Also used : MoreObjects(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects) LoggerFactory(org.slf4j.LoggerFactory) StateAppendResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateAppendResponse) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) NameContext(org.apache.beam.runners.dataflow.worker.counters.NameContext) GlobalWindow(org.apache.beam.sdk.transforms.windowing.GlobalWindow) StateClearResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateClearResponse) StateDelegator(org.apache.beam.runners.fnexecution.state.StateDelegator) Materializations(org.apache.beam.sdk.transforms.Materializations) IdGenerator(org.apache.beam.sdk.fn.IdGenerator) KvCoder(org.apache.beam.sdk.coders.KvCoder) OperationContext(org.apache.beam.runners.dataflow.worker.util.common.worker.OperationContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BeamFnApi(org.apache.beam.model.fnexecution.v1.BeamFnApi) List(java.util.List) InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) ProcessBundleDescriptor(org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleDescriptor) CompletionStage(java.util.concurrent.CompletionStage) RequestCase(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest.RequestCase) StateRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest) InstructionRequestHandler(org.apache.beam.runners.fnexecution.control.InstructionRequestHandler) Entry(java.util.Map.Entry) DataflowStepContext(org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext) RegisterRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.RegisterRequest) StateKey(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey) StateGetResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateGetResponse) PTransform(org.apache.beam.model.pipeline.v1.RunnerApi.PTransform) MonitoringInfoEncodings.decodeInt64Counter(org.apache.beam.runners.core.metrics.MonitoringInfoEncodings.decodeInt64Counter) ByteStringCoder(org.apache.beam.runners.dataflow.worker.ByteStringCoder) Coder(org.apache.beam.sdk.coders.Coder) CompletableFuture(java.util.concurrent.CompletableFuture) StateNamespaces(org.apache.beam.runners.core.StateNamespaces) StateTags(org.apache.beam.runners.core.StateTags) TextFormat(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.TextFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DataflowOperationContext(org.apache.beam.runners.dataflow.worker.DataflowOperationContext) InstructionResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionResponse) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) MonitoringInfoConstants(org.apache.beam.runners.core.metrics.MonitoringInfoConstants) ByteString(org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString) OutputReceiver(org.apache.beam.runners.dataflow.worker.util.common.worker.OutputReceiver) ProcessBundleProgressRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleProgressRequest) RemoteGrpcPortWrite(org.apache.beam.sdk.fn.data.RemoteGrpcPortWrite) Nullable(org.checkerframework.checker.nullness.qual.Nullable) MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) SideInputReader(org.apache.beam.runners.core.SideInputReader) RunnerApi(org.apache.beam.model.pipeline.v1.RunnerApi) ProcessBundleRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest) Logger(org.slf4j.Logger) Table(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Table) IOException(java.io.IOException) RemoteGrpcPortRead(org.apache.beam.sdk.fn.data.RemoteGrpcPortRead) ExecutionException(java.util.concurrent.ExecutionException) Operation(org.apache.beam.runners.dataflow.worker.util.common.worker.Operation) BagState(org.apache.beam.sdk.state.BagState) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) PCollectionView(org.apache.beam.sdk.values.PCollectionView) Closeable(java.io.Closeable) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) MoreFutures(org.apache.beam.sdk.util.MoreFutures) StateResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse) InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) Closeable(java.io.Closeable)

Example 8 with InstructionRequest

use of org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest in project beam by apache.

the class FnApiWindowMappingFn method loadIfNeeded.

private TargetWindowT loadIfNeeded(BoundedWindow mainWindow) {
    try {
        String processRequestInstructionId = idGenerator.getId();
        InstructionRequest processRequest = InstructionRequest.newBuilder().setInstructionId(processRequestInstructionId).setProcessBundle(ProcessBundleRequest.newBuilder().setProcessBundleDescriptorId(registerIfRequired())).build();
        ConcurrentLinkedQueue<WindowedValue<KV<byte[], TargetWindowT>>> outputValue = new ConcurrentLinkedQueue<>();
        // Open the inbound consumer
        InboundDataClient waitForInboundTermination = beamFnDataService.receive(LogicalEndpoint.data(processRequestInstructionId, "write"), inboundCoder, outputValue::add);
        CompletionStage<InstructionResponse> processResponse = instructionRequestHandler.handle(processRequest);
        // Open the outbound consumer
        try (CloseableFnDataReceiver<WindowedValue<KV<byte[], BoundedWindow>>> outboundConsumer = beamFnDataService.send(LogicalEndpoint.data(processRequestInstructionId, "read"), outboundCoder)) {
            outboundConsumer.accept(WindowedValue.valueInGlobalWindow(KV.of(EMPTY_ARRAY, mainWindow)));
        }
        // Check to see if processing the request failed.
        MoreFutures.get(processResponse);
        waitForInboundTermination.awaitCompletion();
        WindowedValue<KV<byte[], TargetWindowT>> sideInputWindow = outputValue.poll();
        checkState(sideInputWindow != null, "Expected side input window to have been emitted by SDK harness.");
        checkState(sideInputWindow.getValue() != null, "Side input window emitted by SDK harness was a WindowedValue with no value in it.");
        checkState(sideInputWindow.getValue().getValue() != null, "Side input window emitted by SDK harness was a WindowedValue<KV<...>> with a null V.");
        checkState(outputValue.isEmpty(), "Expected only a single side input window to have been emitted by " + "the SDK harness but also received %s", outputValue);
        return sideInputWindow.getValue().getValue();
    } catch (Throwable e) {
        LOG.error("Unable to map main input window {} to side input window.", mainWindow, e);
        throw new IllegalStateException(e);
    }
}
Also used : InstructionResponse(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionResponse) KV(org.apache.beam.sdk.values.KV) InboundDataClient(org.apache.beam.sdk.fn.data.InboundDataClient) InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) WindowedValue(org.apache.beam.sdk.util.WindowedValue) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 9 with InstructionRequest

use of org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest in project beam by apache.

the class RegisterAndProcessBundleOperationTest method testAbortCancelsAndCleansUpDuringProcessBundle.

@Test
public void testAbortCancelsAndCleansUpDuringProcessBundle() throws Exception {
    IdGenerator idGenerator = makeIdGeneratorStartingFrom(777L);
    ExecutorService executorService = Executors.newCachedThreadPool();
    CountDownLatch waitForAbortToComplete = new CountDownLatch(1);
    AtomicReference<ThrowingRunnable> abortReference = new AtomicReference<>();
    RegisterAndProcessBundleOperation operation = new RegisterAndProcessBundleOperation(idGenerator, new TestInstructionRequestHandler() {

        @Override
        public CompletionStage<InstructionResponse> handle(InstructionRequest request) {
            CompletableFuture<InstructionResponse> responseFuture = new CompletableFuture<>();
            if (request.getRequestCase() == RequestCase.PROCESS_BUNDLE) {
                executorService.submit((Callable<Void>) () -> {
                    abortReference.get().run();
                    waitForAbortToComplete.countDown();
                    return null;
                });
            } else {
                completeFuture(request, responseFuture);
            }
            return responseFuture;
        }
    }, mockBeamFnStateDelegator, REGISTER_REQUEST, ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableTable.of(), ImmutableMap.of(), mockContext);
    abortReference.set(operation::abort);
    operation.start();
    waitForAbortToComplete.await();
    // Ensure that the number of registrations matches the number of aborts
    assertEquals(stateServiceRegisterCounter.get(), stateServiceAbortCounter.get());
    assertEquals(0, stateServiceDeregisterCounter.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IdGenerator(org.apache.beam.sdk.fn.IdGenerator) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ThrowingRunnable(org.apache.beam.sdk.util.ThrowingRunnable) CompletableFuture(java.util.concurrent.CompletableFuture) InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) ExecutorService(java.util.concurrent.ExecutorService) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.Test)

Example 10 with InstructionRequest

use of org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest in project beam by apache.

the class RegisterAndProcessBundleOperationTest method testAbortCancelsAndCleansUpDuringRegister.

@Test
public void testAbortCancelsAndCleansUpDuringRegister() throws Exception {
    IdGenerator idGenerator = makeIdGeneratorStartingFrom(777L);
    ExecutorService executorService = Executors.newCachedThreadPool();
    CountDownLatch waitForAbortToComplete = new CountDownLatch(1);
    AtomicReference<ThrowingRunnable> abortReference = new AtomicReference<>();
    RegisterAndProcessBundleOperation operation = new RegisterAndProcessBundleOperation(idGenerator, new TestInstructionRequestHandler() {

        @Override
        public CompletionStage<InstructionResponse> handle(InstructionRequest request) {
            CompletableFuture<InstructionResponse> responseFuture = new CompletableFuture<>();
            if (request.getRequestCase() == RequestCase.PROCESS_BUNDLE) {
                executorService.submit((Callable<Void>) () -> {
                    abortReference.get().run();
                    waitForAbortToComplete.countDown();
                    return null;
                });
            } else {
                completeFuture(request, responseFuture);
            }
            return responseFuture;
        }
    }, mockBeamFnStateDelegator, REGISTER_REQUEST, ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of(), ImmutableTable.of(), ImmutableMap.of(), mockContext);
    abortReference.set(operation::abort);
    operation.start();
    waitForAbortToComplete.await();
    // Ensure that the number of registrations matches the number of aborts
    assertEquals(stateServiceRegisterCounter.get(), stateServiceAbortCounter.get());
    assertEquals(0, stateServiceDeregisterCounter.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IdGenerator(org.apache.beam.sdk.fn.IdGenerator) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) ThrowingRunnable(org.apache.beam.sdk.util.ThrowingRunnable) CompletableFuture(java.util.concurrent.CompletableFuture) InstructionRequest(org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest) ExecutorService(java.util.concurrent.ExecutorService) CompletionStage(java.util.concurrent.CompletionStage) Test(org.junit.Test)

Aggregations

InstructionRequest (org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest)13 Test (org.junit.Test)10 IdGenerator (org.apache.beam.sdk.fn.IdGenerator)9 CompletableFuture (java.util.concurrent.CompletableFuture)7 InstructionResponse (org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionResponse)7 CompletionStage (java.util.concurrent.CompletionStage)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ArrayList (java.util.ArrayList)5 ExecutorService (java.util.concurrent.ExecutorService)5 DataflowStepContext (org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext)4 InstructionRequestHandler (org.apache.beam.runners.fnexecution.control.InstructionRequestHandler)4 BeamFnApi (org.apache.beam.model.fnexecution.v1.BeamFnApi)3 ProcessBundleDescriptor (org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleDescriptor)3 StateRequest (org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest)3 StateResponse (org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse)3 BoundedWindow (org.apache.beam.sdk.transforms.windowing.BoundedWindow)3 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)3 List (java.util.List)2 Callable (java.util.concurrent.Callable)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2