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;
}
}
}
}
}
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;
}
}
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;
}
}
}
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);
}
}
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);
}
Aggregations