Search in sources :

Example 1 with ThrowingRunnable

use of org.apache.beam.sdk.util.ThrowingRunnable 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 2 with ThrowingRunnable

use of org.apache.beam.sdk.util.ThrowingRunnable 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

Callable (java.util.concurrent.Callable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 InstructionRequest (org.apache.beam.model.fnexecution.v1.BeamFnApi.InstructionRequest)2 IdGenerator (org.apache.beam.sdk.fn.IdGenerator)2 ThrowingRunnable (org.apache.beam.sdk.util.ThrowingRunnable)2 Test (org.junit.Test)2