Search in sources :

Example 86 with CompletionException

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

the class AkkaDbCloner method cloneDatabase.

@Override
public void cloneDatabase(String dstDb, MongoClient remoteClient, MongodServer localServer, CloneOptions opts) throws CloningException, NotMasterException, MongoException {
    Preconditions.checkState(isRunning(), "This db cloner is not running");
    if (!remoteClient.isRemote() && opts.getDbToClone().equals(dstDb)) {
        LOGGER.warn("Trying to clone a database to itself! Ignoring it");
        return;
    }
    String fromDb = opts.getDbToClone();
    CursorResult<Entry> listCollections;
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        listCollections = ListCollectionsRequester.getListCollections(remoteConnection, fromDb, null);
    } catch (MongoException ex) {
        throw new CloningException("It was impossible to get information from the remote server", ex);
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written");
    }
    List<Entry> collsToClone = getCollsToClone(listCollections, fromDb, opts);
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written " + "after get collections info");
    }
    try {
        for (Entry entry : collsToClone) {
            prepareCollection(localServer, dstDb, entry);
        }
    } catch (RollbackException ex) {
        throw new AssertionError("Unexpected rollback exception", ex);
    }
    Materializer materializer = ActorMaterializer.create(getActorSystem());
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        if (opts.isCloneData()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection data {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneCollection(localServer, remoteConnection, dstDb, opts, materializer, entry);
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
        if (opts.isCloneIndexes()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection indexes {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneIndex(localServer, dstDb, dstDb, remoteConnection, opts, entry.getCollectionName(), entry.getCollectionName());
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) RollbackException(com.torodb.core.transaction.RollbackException) Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) CompletionException(java.util.concurrent.CompletionException) NotMasterException(com.eightkdata.mongowp.exceptions.NotMasterException) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) ActorMaterializer(akka.stream.ActorMaterializer) Materializer(akka.stream.Materializer)

Example 87 with CompletionException

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

the class AkkaDbCloner method cloneCollection.

private void cloneCollection(MongodServer localServer, MongoConnection remoteConnection, String toDb, CloneOptions opts, Materializer materializer, Entry collToClone) throws MongoException {
    String collName = collToClone.getCollectionName();
    MongoCursor<BsonDocument> cursor = openCursor(remoteConnection, collName, opts);
    CollectionIterator iterator = new CollectionIterator(cursor, retrier);
    Source<BsonDocument, NotUsed> source = Source.fromIterator(() -> iterator).buffer(cursorBatchBufferSize, OverflowStrategy.backpressure()).async();
    Flow<BsonDocument, Pair<Integer, Integer>, NotUsed> inserterFlow;
    if (maxParallelInsertTasks == 1) {
        inserterFlow = createCloneDocsWorker(localServer, toDb, collName);
    } else {
        Graph<FlowShape<BsonDocument, Pair<Integer, Integer>>, NotUsed> graph = GraphDSL.create(builder -> {
            UniformFanOutShape<BsonDocument, BsonDocument> balance = builder.add(Balance.create(maxParallelInsertTasks, false));
            UniformFanInShape<Pair<Integer, Integer>, Pair<Integer, Integer>> merge = builder.add(Merge.create(maxParallelInsertTasks, false));
            for (int i = 0; i < maxParallelInsertTasks; i++) {
                builder.from(balance.out(i)).via(builder.add(createCloneDocsWorker(localServer, toDb, collName).async())).toInlet(merge.in(i));
            }
            return FlowShape.of(balance.in(), merge.out());
        });
        inserterFlow = Flow.fromGraph(graph);
    }
    try {
        source.via(inserterFlow).fold(new Tuple3<>(0, 0, clock.instant()), (acum, batch) -> postInsertFold(toDb, collName, acum, batch)).toMat(Sink.foreach(tuple -> logCollectionCloning(toDb, collName, tuple.t1(), tuple.t2())), Keep.right()).run(materializer).toCompletableFuture().join();
    } catch (CompletionException ex) {
        Throwable cause = ex.getCause();
        if (cause != null) {
            throw new CloningException("Error while cloning " + toDb + "." + collName, cause);
        }
        throw ex;
    }
}
Also used : NotUsed(akka.NotUsed) Hint(com.torodb.core.retrier.Retrier.Hint) FlowShape(akka.stream.FlowShape) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) CompletionException(java.util.concurrent.CompletionException) Pair(akka.japi.Pair)

Example 88 with CompletionException

use of java.util.concurrent.CompletionException in project jdk8u_jdk by JetBrains.

the class ConcurrentContainsKeyTest method testOnce.

private static void testOnce(Object[] content, ConcurrentHashMap<Object, Object> m) {
    CountDownLatch s = new CountDownLatch(1);
    Supplier<Runnable> sr = () -> () -> {
        try {
            s.await();
        } catch (InterruptedException e) {
        }
        for (int i = 0; i < R * N; i++) {
            Object o = content[i % content.length];
            if (!m.containsKey(o)) {
                throw new AssociationFailure("CHM.containsKey failed: entry does not exist");
            }
        }
    };
    int ps = Runtime.getRuntime().availableProcessors();
    Stream<CompletableFuture> runners = IntStream.range(0, ps).mapToObj(i -> sr.get()).map(CompletableFuture::runAsync);
    CompletableFuture all = CompletableFuture.allOf(runners.toArray(CompletableFuture[]::new));
    // Trigger the runners to start checking key membership
    s.countDown();
    try {
        all.join();
    } catch (CompletionException e) {
        Throwable t = e.getCause();
        if (t instanceof AssociationFailure) {
            throw (AssociationFailure) t;
        } else {
            throw e;
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Supplier(java.util.function.Supplier) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 89 with CompletionException

use of java.util.concurrent.CompletionException in project sling by apache.

the class SimpleMailService method sendMail.

private MailResult sendMail(final String message, final String recipient, final Map data, final MailBuilder mailBuilder) {
    try {
        final Email email = mailBuilder.build(message, recipient, data);
        final String messageId = email.send();
        logger.info("mail '{}' sent", messageId);
        final byte[] bytes = MailUtil.toByteArray(email);
        return new MailResult(bytes);
    } catch (EmailException | MessagingException | IOException e) {
        throw new CompletionException(e);
    }
}
Also used : MailResult(org.apache.sling.commons.messaging.mail.MailResult) Email(org.apache.commons.mail.Email) MessagingException(javax.mail.MessagingException) CompletionException(java.util.concurrent.CompletionException) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException)

Example 90 with CompletionException

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

the class TestMoreFutures method testUnwrapCompletionException.

@Test
public void testUnwrapCompletionException() {
    RuntimeException original = new RuntimeException();
    assertSame(unwrapCompletionException(original), original);
    assertSame(unwrapCompletionException(new CompletionException(original)), original);
    CompletionException completion = new CompletionException(null);
    assertSame(unwrapCompletionException(completion), completion);
}
Also used : CompletionException(java.util.concurrent.CompletionException) MoreFutures.unwrapCompletionException(io.airlift.concurrent.MoreFutures.unwrapCompletionException) Test(org.testng.annotations.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