use of java.util.function.BiConsumer in project vert.x by eclipse.
the class MetricsContextTest method testDeploy.
private void testDeploy(boolean worker, boolean multiThreaded, BiConsumer<Thread, Context> checker) {
AtomicReference<Thread> verticleThread = new AtomicReference<>();
AtomicReference<Context> verticleContext = new AtomicReference<>();
AtomicBoolean deployedCalled = new AtomicBoolean();
AtomicBoolean undeployedCalled = new AtomicBoolean();
VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {
@Override
public void verticleDeployed(Verticle verticle) {
deployedCalled.set(true);
checker.accept(verticleThread.get(), verticleContext.get());
}
@Override
public void verticleUndeployed(Verticle verticle) {
undeployedCalled.set(true);
checker.accept(verticleThread.get(), verticleContext.get());
}
};
Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
verticleThread.set(Thread.currentThread());
verticleContext.set(Vertx.currentContext());
}
}, new DeploymentOptions().setWorker(worker).setMultiThreaded(multiThreaded), ar1 -> {
assertTrue(ar1.succeeded());
vertx.undeploy(ar1.result(), ar2 -> {
assertTrue(ar1.succeeded());
assertTrue(deployedCalled.get());
assertTrue(undeployedCalled.get());
testComplete();
});
});
await();
}
use of java.util.function.BiConsumer in project elasticsearch by elastic.
the class StoreRecovery method recoverFromLocalShards.
boolean recoverFromLocalShards(BiConsumer<String, MappingMetaData> mappingUpdateConsumer, final IndexShard indexShard, final List<LocalShardSnapshot> shards) throws IOException {
if (canRecover(indexShard)) {
RecoverySource.Type recoveryType = indexShard.recoveryState().getRecoverySource().getType();
assert recoveryType == RecoverySource.Type.LOCAL_SHARDS : "expected local shards recovery type: " + recoveryType;
if (shards.isEmpty()) {
throw new IllegalArgumentException("shards must not be empty");
}
Set<Index> indices = shards.stream().map((s) -> s.getIndex()).collect(Collectors.toSet());
if (indices.size() > 1) {
throw new IllegalArgumentException("can't add shards from more than one index");
}
IndexMetaData indexMetaData = shards.get(0).getIndexMetaData();
for (ObjectObjectCursor<String, MappingMetaData> mapping : indexMetaData.getMappings()) {
mappingUpdateConsumer.accept(mapping.key, mapping.value);
}
indexShard.mapperService().merge(indexMetaData, MapperService.MergeReason.MAPPING_RECOVERY, true);
return executeRecovery(indexShard, () -> {
logger.debug("starting recovery from local shards {}", shards);
try {
// don't close this directory!!
final Directory directory = indexShard.store().directory();
addIndices(indexShard.recoveryState().getIndex(), directory, shards.stream().map(s -> s.getSnapshotDirectory()).collect(Collectors.toList()).toArray(new Directory[shards.size()]));
internalRecoverFromStore(indexShard);
// just trigger a merge to do housekeeping on the
// copied segments - we will also see them in stats etc.
indexShard.getEngine().forceMerge(false, -1, false, false, false);
} catch (IOException ex) {
throw new IndexShardRecoveryException(indexShard.shardId(), "failed to recover from local shards", ex);
}
});
}
return false;
}
use of java.util.function.BiConsumer in project neo4j by neo4j.
the class RaftReplicator method replicate.
@Override
public Future<Object> replicate(ReplicatedContent command, boolean trackResult) throws InterruptedException, NoLeaderFoundException {
OperationContext session = sessionPool.acquireSession();
DistributedOperation operation = new DistributedOperation(command, session.globalSession(), session.localOperationId());
Progress progress = progressTracker.start(operation);
RetryStrategy.Timeout timeout = retryStrategy.newTimeout();
do {
assertDatabaseNotShutdown();
try {
outbound.send(leaderLocator.getLeader(), new RaftMessages.NewEntry.Request(me, operation));
progress.awaitReplication(timeout.getMillis());
timeout.increment();
} catch (InterruptedException e) {
progressTracker.abort(operation);
throw e;
} catch (NoLeaderFoundException e) {
log.debug("Could not replicate operation " + operation + " because no leader was found. Retrying.", e);
}
} while (!progress.isReplicated());
BiConsumer<Object, Throwable> cleanup = (ignored1, ignored2) -> sessionPool.releaseSession(session);
if (trackResult) {
progress.futureResult().whenComplete(cleanup);
} else {
cleanup.accept(null, null);
}
return progress.futureResult();
}
use of java.util.function.BiConsumer in project neo4j by neo4j.
the class StoreMigrator method propertyDecorator.
private <ENTITY extends InputEntity, RECORD extends PrimitiveRecord> BiConsumer<ENTITY, RECORD> propertyDecorator(boolean requiresPropertyMigration, RecordCursors cursors) {
if (!requiresPropertyMigration) {
return (a, b) -> {
};
}
final StorePropertyCursor cursor = new StorePropertyCursor(cursors, ignored -> {
});
final List<Object> scratch = new ArrayList<>();
return (ENTITY entity, RECORD record) -> {
cursor.init(record.getNextProp(), LockService.NO_LOCK);
scratch.clear();
while (cursor.next()) {
// add key as int here as to have the importer use the token id
scratch.add(cursor.propertyKeyId());
scratch.add(cursor.value());
}
entity.setProperties(scratch.isEmpty() ? InputEntity.NO_PROPERTIES : scratch.toArray());
cursor.close();
};
}
use of java.util.function.BiConsumer in project jdk8u_jdk by JetBrains.
the class ConcurrentAssociateTest method testOnce.
private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
CountDownLatch s = new CountDownLatch(1);
Supplier<Runnable> sr = () -> () -> {
try {
s.await();
} catch (InterruptedException e) {
}
for (int i = 0; i < N; i++) {
Object o = new X();
associator.accept(m, o);
if (!m.containsKey(o)) {
throw new AssociationFailure(desc + " 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 associating
s.countDown();
try {
all.join();
} catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
} else {
throw e;
}
}
}
Aggregations