Search in sources :

Example 11 with CompletionException

use of java.util.concurrent.CompletionException in project incubator-ratis by apache.

the class FileStoreClient method sendAsync.

static CompletableFuture<ByteString> sendAsync(ByteString request, Function<Message, CompletableFuture<RaftClientReply>> sendFunction) {
    return sendFunction.apply(() -> request).thenApply(reply -> {
        final StateMachineException sme = reply.getStateMachineException();
        if (sme != null) {
            throw new CompletionException("Failed to send request " + request, sme);
        }
        Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
        return reply.getMessage().getContent();
    });
}
Also used : StateMachineException(org.apache.ratis.protocol.StateMachineException) CompletionException(java.util.concurrent.CompletionException)

Example 12 with CompletionException

use of java.util.concurrent.CompletionException in project pravega by pravega.

the class RetryTests method retryFutureTests.

@Test
public void retryFutureTests() {
    ScheduledExecutorService executorService = ExecutorServiceHelpers.newScheduledThreadPool(5, "testpool");
    // 1. series of retryable exceptions followed by a failure
    begin = Instant.now();
    CompletableFuture<Integer> result = retryFuture(uniformDelay, 1, maxLoops, maxDelay, true, executorService);
    assertEquals(result.join().intValue(), expectedResult);
    end = Instant.now();
    duration = end.toEpochMilli() - begin.toEpochMilli();
    log.debug("Expected duration = {}", expectedDurationUniform);
    log.debug("Actual duration   = {}", duration);
    assertTrue(duration >= expectedDurationUniform);
    // 2, series of retryable exceptions followed by a non-retryable failure
    begin = Instant.now();
    result = retryFuture(uniformDelay, 1, maxLoops, maxDelay, false, executorService);
    try {
        result.join();
    } catch (CompletionException ce) {
        end = Instant.now();
        duration = end.toEpochMilli() - begin.toEpochMilli();
        log.debug("Expected duration = {}", expectedDurationUniform);
        log.debug("Actual duration   = {}", duration);
        assertTrue(duration >= expectedDurationUniform);
        assertTrue(ce.getCause() instanceof NonretryableException);
        assertEquals(accumulator.get(), expectedResult);
    }
    // 3. exponential backoff
    begin = Instant.now();
    result = retryFuture(exponentialInitialDelay, multiplier, maxLoops, maxDelay, true, executorService);
    assertEquals(result.join().intValue(), expectedResult);
    end = Instant.now();
    duration = end.toEpochMilli() - begin.toEpochMilli();
    log.debug("Expected duration = {}", expectedDurationExponential);
    log.debug("Actual duration   = {}", duration);
    assertTrue(duration >= expectedDurationExponential);
    // 4. Exhaust retries
    begin = Instant.now();
    result = retryFuture(uniformDelay, 1, maxLoops - 1, maxDelay, true, executorService);
    try {
        result.join();
    } catch (Exception e) {
        end = Instant.now();
        duration = end.toEpochMilli() - begin.toEpochMilli();
        log.debug("Expected duration = {}", expectedDurationUniform - uniformDelay);
        log.debug("Actual duration   = {}", duration);
        assertTrue(duration >= expectedDurationUniform - uniformDelay);
        assertTrue(e instanceof CompletionException);
        assertTrue(e.getCause() instanceof RetriesExhaustedException);
        assertTrue(e.getCause().getCause() instanceof CompletionException);
        assertTrue(e.getCause().getCause().getCause() instanceof RetryableException);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompletionException(java.util.concurrent.CompletionException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 13 with CompletionException

use of java.util.concurrent.CompletionException in project pravega by pravega.

the class ControllerEventProcessors method handleOrphanedReaders.

private CompletableFuture<Void> handleOrphanedReaders(final EventProcessorGroup<? extends ControllerEvent> group, final Supplier<Set<String>> processes) {
    return withRetriesAsync(() -> CompletableFuture.supplyAsync(() -> {
        try {
            return group.getProcesses();
        } catch (CheckpointStoreException e) {
            if (e.getType().equals(CheckpointStoreException.Type.NoNode)) {
                return Collections.<String>emptySet();
            }
            throw new CompletionException(e);
        }
    }, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor).thenComposeAsync(groupProcesses -> withRetriesAsync(() -> CompletableFuture.supplyAsync(() -> {
        try {
            return new ImmutablePair<>(processes.get(), groupProcesses);
        } catch (Exception e) {
            log.error(String.format("Error fetching current processes%s", group.toString()), e);
            throw new CompletionException(e);
        }
    }, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor)).thenComposeAsync(pair -> {
        Set<String> activeProcesses = pair.getLeft();
        Set<String> registeredProcesses = pair.getRight();
        if (registeredProcesses == null || registeredProcesses.isEmpty()) {
            return CompletableFuture.completedFuture(null);
        }
        if (activeProcesses != null) {
            registeredProcesses.removeAll(activeProcesses);
        }
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (String process : registeredProcesses) {
            futureList.add(withRetriesAsync(() -> CompletableFuture.runAsync(() -> {
                try {
                    group.notifyProcessFailure(process);
                } catch (CheckpointStoreException e) {
                    log.error(String.format("Error notifying failure of process=%s in event processor group %s", process, group.toString()), e);
                    throw new CompletionException(e);
                }
            }, executor), RETRYABLE_PREDICATE, Integer.MAX_VALUE, executor));
        }
        return Futures.allOf(futureList);
    });
}
Also used : CommitEvent(io.pravega.shared.controller.event.CommitEvent) Retry(io.pravega.common.util.Retry) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) JavaSerializer(io.pravega.client.stream.impl.JavaSerializer) CheckpointStore(io.pravega.controller.store.checkpoint.CheckpointStore) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) CheckpointStoreException(io.pravega.controller.store.checkpoint.CheckpointStoreException) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) AbortRequestHandler(io.pravega.controller.server.eventProcessor.requesthandlers.AbortRequestHandler) AutoScaleTask(io.pravega.controller.server.eventProcessor.requesthandlers.AutoScaleTask) RETRYABLE_PREDICATE(io.pravega.controller.util.RetryHelper.RETRYABLE_PREDICATE) ExceptionHandler(io.pravega.controller.eventProcessor.ExceptionHandler) RetryHelper.withRetriesAsync(io.pravega.controller.util.RetryHelper.withRetriesAsync) EventProcessorConfig(io.pravega.controller.eventProcessor.EventProcessorConfig) SealStreamTask(io.pravega.controller.server.eventProcessor.requesthandlers.SealStreamTask) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) ScaleOperationTask(io.pravega.controller.server.eventProcessor.requesthandlers.ScaleOperationTask) ControllerEvent(io.pravega.shared.controller.event.ControllerEvent) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ConcurrentEventProcessor(io.pravega.controller.eventProcessor.impl.ConcurrentEventProcessor) EventProcessorSystemImpl(io.pravega.controller.eventProcessor.impl.EventProcessorSystemImpl) Config(io.pravega.controller.util.Config) UpdateStreamTask(io.pravega.controller.server.eventProcessor.requesthandlers.UpdateStreamTask) ClientFactory(io.pravega.client.ClientFactory) Controller(io.pravega.client.stream.impl.Controller) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Futures(io.pravega.common.concurrent.Futures) TruncateStreamTask(io.pravega.controller.server.eventProcessor.requesthandlers.TruncateStreamTask) SegmentHelper(io.pravega.controller.server.SegmentHelper) EventProcessorGroupConfigImpl(io.pravega.controller.eventProcessor.impl.EventProcessorGroupConfigImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AbortEvent(io.pravega.shared.controller.event.AbortEvent) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Serializer(io.pravega.client.stream.Serializer) LoggerHelpers(io.pravega.common.LoggerHelpers) FailoverSweeper(io.pravega.controller.fault.FailoverSweeper) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) EventProcessorGroupConfig(io.pravega.controller.eventProcessor.EventProcessorGroupConfig) EventProcessorSystem(io.pravega.controller.eventProcessor.EventProcessorSystem) HostControllerStore(io.pravega.controller.store.host.HostControllerStore) StreamTransactionMetadataTasks(io.pravega.controller.task.Stream.StreamTransactionMetadataTasks) DeleteStreamTask(io.pravega.controller.server.eventProcessor.requesthandlers.DeleteStreamTask) EventProcessorGroup(io.pravega.controller.eventProcessor.EventProcessorGroup) StreamRequestHandler(io.pravega.controller.server.eventProcessor.requesthandlers.StreamRequestHandler) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ScalingPolicy(io.pravega.client.stream.ScalingPolicy) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ArrayList(java.util.ArrayList) CheckpointStoreException(io.pravega.controller.store.checkpoint.CheckpointStoreException) CheckpointStoreException(io.pravega.controller.store.checkpoint.CheckpointStoreException) CompletionException(java.util.concurrent.CompletionException)

Example 14 with CompletionException

use of java.util.concurrent.CompletionException in project pravega by pravega.

the class DeleteStreamTask method execute.

@Override
public CompletableFuture<Void> execute(final DeleteStreamEvent request) {
    final OperationContext context = streamMetadataStore.createContext(request.getScope(), request.getStream());
    String scope = request.getScope();
    String stream = request.getStream();
    return streamMetadataStore.isSealed(scope, stream, context, executor).thenComposeAsync(sealed -> {
        if (!sealed) {
            log.warn("{}/{} stream not sealed", scope, stream);
            return Futures.failedFuture(new RuntimeException("Stream not sealed"));
        }
        return notifyAndDelete(context, scope, stream);
    }, executor).exceptionally(e -> {
        if (e instanceof StoreException.DataNotFoundException) {
            return null;
        }
        log.error("{}/{} stream delete workflow threw exception.", scope, stream, e);
        throw new CompletionException(e);
    });
}
Also used : OperationContext(io.pravega.controller.store.stream.OperationContext) OperationContext(io.pravega.controller.store.stream.OperationContext) Slf4j(lombok.extern.slf4j.Slf4j) StoreException(io.pravega.controller.store.stream.StoreException) DeleteStreamEvent(io.pravega.shared.controller.event.DeleteStreamEvent) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamMetadataTasks(io.pravega.controller.task.Stream.StreamMetadataTasks) Preconditions(com.google.common.base.Preconditions) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) Futures(io.pravega.common.concurrent.Futures) CompletionException(java.util.concurrent.CompletionException)

Example 15 with CompletionException

use of java.util.concurrent.CompletionException in project pravega by pravega.

the class StreamSegmentMapperTests method testCreateTransactionAlreadyExists.

/**
 * Tests the ability of the StreamSegmentMapper to create a new Transaction if the Transaction already exists (partially
 * or fully).
 */
@Test
public void testCreateTransactionAlreadyExists() {
    final String parentSegmentName = "NewSegment";
    final UUID txnId = UUID.randomUUID();
    final String txnName = StreamSegmentNameUtils.getTransactionNameFromId(parentSegmentName, txnId);
    testCreateAlreadyExists(txnName, (mapper, attributes) -> mapper.createNewStreamSegment(parentSegmentName, null, TIMEOUT).exceptionally(ex -> {
        if (Exceptions.unwrap(ex) instanceof StreamSegmentExistsException) {
            return null;
        }
        throw new CompletionException(ex);
    }).thenCompose(v -> mapper.createNewTransactionStreamSegment(parentSegmentName, txnId, attributes, TIMEOUT)));
}
Also used : Arrays(java.util.Arrays) Storage(io.pravega.segmentstore.storage.Storage) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) AssertExtensions(io.pravega.test.common.AssertExtensions) BiFunction(java.util.function.BiFunction) ContainerMetadata(io.pravega.segmentstore.server.ContainerMetadata) TimeoutException(java.util.concurrent.TimeoutException) Cleanup(lombok.Cleanup) UpdateableSegmentMetadata(io.pravega.segmentstore.server.UpdateableSegmentMetadata) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) SegmentHandle(io.pravega.segmentstore.storage.SegmentHandle) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InMemoryStorageFactory(io.pravega.segmentstore.storage.mocks.InMemoryStorageFactory) Duration(java.time.Duration) Map(java.util.Map) Operation(io.pravega.segmentstore.server.logs.operations.Operation) Attributes(io.pravega.segmentstore.contracts.Attributes) Predicate(java.util.function.Predicate) Collection(java.util.Collection) InMemoryStorage(io.pravega.segmentstore.storage.mocks.InMemoryStorage) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SegmentMetadataComparer(io.pravega.segmentstore.server.SegmentMetadataComparer) StreamSegmentNameUtils(io.pravega.shared.segment.StreamSegmentNameUtils) List(java.util.List) AsyncMap(io.pravega.common.util.AsyncMap) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Futures(io.pravega.common.concurrent.Futures) MetadataBuilder(io.pravega.segmentstore.server.MetadataBuilder) SegmentRollingPolicy(io.pravega.segmentstore.storage.SegmentRollingPolicy) TooManyActiveSegmentsException(io.pravega.segmentstore.contracts.TooManyActiveSegmentsException) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UpdateableContainerMetadata(io.pravega.segmentstore.server.UpdateableContainerMetadata) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) Timeout(org.junit.rules.Timeout) StreamSegmentMapOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentMapOperation) MathHelpers(io.pravega.common.MathHelpers) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) IntentionalException(io.pravega.test.common.IntentionalException) lombok.val(lombok.val) OperationLog(io.pravega.segmentstore.server.OperationLog) Test(org.junit.Test) Service(com.google.common.util.concurrent.Service) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) DataCorruptionException(io.pravega.segmentstore.server.DataCorruptionException) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

CompletionException (java.util.concurrent.CompletionException)199 Test (org.junit.Test)80 CompletableFuture (java.util.concurrent.CompletableFuture)62 List (java.util.List)52 ArrayList (java.util.ArrayList)51 IOException (java.io.IOException)45 Map (java.util.Map)39 Collection (java.util.Collection)31 ExecutionException (java.util.concurrent.ExecutionException)31 HashMap (java.util.HashMap)30 Collections (java.util.Collections)24 TimeUnit (java.util.concurrent.TimeUnit)22 Collectors (java.util.stream.Collectors)22 FlinkException (org.apache.flink.util.FlinkException)22 Before (org.junit.Before)21 Duration (java.time.Duration)19 Arrays (java.util.Arrays)19 BeforeClass (org.junit.BeforeClass)19 ExecutorService (java.util.concurrent.ExecutorService)18 Nonnull (javax.annotation.Nonnull)17