Search in sources :

Example 6 with BiConsumer

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();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 7 with BiConsumer

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;
}
Also used : NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) Lucene(org.elasticsearch.common.lucene.Lucene) IndexId(org.elasticsearch.repositories.IndexId) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) IndexRequest(org.elasticsearch.action.index.IndexRequest) Directory(org.apache.lucene.store.Directory) TimeValue(org.elasticsearch.common.unit.TimeValue) Store(org.elasticsearch.index.store.Store) BiConsumer(java.util.function.BiConsumer) TimeValue.timeValueMillis(org.elasticsearch.common.unit.TimeValue.timeValueMillis) IndexShardRestoreFailedException(org.elasticsearch.index.snapshots.IndexShardRestoreFailedException) IOContext(org.apache.lucene.store.IOContext) EngineException(org.elasticsearch.index.engine.EngineException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Repository(org.elasticsearch.repositories.Repository) IndexInput(org.apache.lucene.store.IndexInput) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SegmentInfos(org.apache.lucene.index.SegmentInfos) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) MapperService(org.elasticsearch.index.mapper.MapperService) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Logger(org.apache.logging.log4j.Logger) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Directory(org.apache.lucene.store.Directory) FilterDirectory(org.apache.lucene.store.FilterDirectory)

Example 8 with BiConsumer

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();
}
Also used : OperationContext(org.neo4j.causalclustering.core.replication.session.OperationContext) LifecycleAdapter(org.neo4j.kernel.lifecycle.LifecycleAdapter) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) Log(org.neo4j.logging.Log) Listener(org.neo4j.kernel.impl.util.Listener) LogProvider(org.neo4j.logging.LogProvider) LocalSessionPool(org.neo4j.causalclustering.core.replication.session.LocalSessionPool) Future(java.util.concurrent.Future) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) BiConsumer(java.util.function.BiConsumer) MemberId(org.neo4j.causalclustering.identity.MemberId) Outbound(org.neo4j.causalclustering.messaging.Outbound) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) OperationContext(org.neo4j.causalclustering.core.replication.session.OperationContext) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) RetryStrategy(org.neo4j.causalclustering.helper.RetryStrategy) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) RetryStrategy(org.neo4j.causalclustering.helper.RetryStrategy)

Example 9 with BiConsumer

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();
    };
}
Also used : PropertyDeduplicator(org.neo4j.kernel.impl.storemigration.legacystore.v21.propertydeduplication.PropertyDeduplicator) Arrays(java.util.Arrays) PageCursor(org.neo4j.io.pagecache.PageCursor) NullLogProvider(org.neo4j.logging.NullLogProvider) StringUtils(org.apache.commons.lang3.StringUtils) UNKNOWN_TX_COMMIT_TIMESTAMP(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.UNKNOWN_TX_COMMIT_TIMESTAMP) COPY(org.neo4j.kernel.impl.storemigration.FileOperation.COPY) SchemaIndexProvider(org.neo4j.kernel.api.index.SchemaIndexProvider) CountsComputer(org.neo4j.kernel.impl.store.CountsComputer) Arrays.asList(java.util.Arrays.asList) BASE_TX_COMMIT_TIMESTAMP(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_COMMIT_TIMESTAMP) Position(org.neo4j.kernel.impl.store.MetaDataStore.Position) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) RecordCursors(org.neo4j.kernel.impl.store.RecordCursors) SilentMigrationProgressMonitor(org.neo4j.kernel.impl.storemigration.monitoring.SilentMigrationProgressMonitor) StandardCharsets(java.nio.charset.StandardCharsets) MetaDataRecordFormat(org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat) DELETE(org.neo4j.kernel.impl.storemigration.FileOperation.DELETE) Stream(java.util.stream.Stream) ExecutionSupervisors.withDynamicProcessorAssignment(org.neo4j.unsafe.impl.batchimport.staging.ExecutionSupervisors.withDynamicProcessorAssignment) StoreType(org.neo4j.kernel.impl.store.StoreType) BatchImporter(org.neo4j.unsafe.impl.batchimport.BatchImporter) NodeStore(org.neo4j.kernel.impl.store.NodeStore) StoreFactory(org.neo4j.kernel.impl.store.StoreFactory) Collectors(org.neo4j.unsafe.impl.batchimport.input.Collectors) PagedFile(org.neo4j.io.pagecache.PagedFile) VERSION_TRAILERS(org.neo4j.kernel.impl.store.format.Capability.VERSION_TRAILERS) FormatFamily(org.neo4j.kernel.impl.store.format.FormatFamily) Supplier(java.util.function.Supplier) UNKNOWN_TX_CHECKSUM(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.UNKNOWN_TX_CHECKSUM) BufferedOutputStream(java.io.BufferedOutputStream) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) TransactionId(org.neo4j.kernel.impl.store.TransactionId) StandardV2_2(org.neo4j.kernel.impl.store.format.standard.StandardV2_2) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) StandardV2_1(org.neo4j.kernel.impl.store.format.standard.StandardV2_1) RelationshipStore(org.neo4j.kernel.impl.store.RelationshipStore) StandardV2_0(org.neo4j.kernel.impl.store.format.standard.StandardV2_0) IdMappers(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdMappers) BiConsumer(java.util.function.BiConsumer) IdGenerators(org.neo4j.unsafe.impl.batchimport.cache.idmapping.IdGenerators) StreamSupport(java.util.stream.StreamSupport) DirectRecordStoreMigrator(org.neo4j.kernel.impl.storemigration.DirectRecordStoreMigrator) CoarseBoundedProgressExecutionMonitor(org.neo4j.unsafe.impl.batchimport.staging.CoarseBoundedProgressExecutionMonitor) StoreFile(org.neo4j.kernel.impl.storemigration.StoreFile) FileOutputStream(java.io.FileOutputStream) RecordFormatSelector.selectForVersion(org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForVersion) IOException(java.io.IOException) NodeRecordFormat(org.neo4j.kernel.impl.store.format.standard.NodeRecordFormat) LegacyLogs(org.neo4j.kernel.impl.storemigration.legacylogs.LegacyLogs) File(java.io.File) Iterables(org.neo4j.helpers.collection.Iterables) ParallelBatchImporter(org.neo4j.unsafe.impl.batchimport.ParallelBatchImporter) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) Configuration(org.neo4j.unsafe.impl.batchimport.Configuration) BufferedReader(java.io.BufferedReader) MigrationProgressMonitor(org.neo4j.kernel.impl.storemigration.monitoring.MigrationProgressMonitor) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) StoreUpgrader(org.neo4j.kernel.impl.storemigration.StoreUpgrader) NoSuchFileException(java.nio.file.NoSuchFileException) BASE_TX_LOG_VERSION(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_LOG_VERSION) BASE_TX_CHECKSUM(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_CHECKSUM) PageCache(org.neo4j.io.pagecache.PageCache) InputNode(org.neo4j.unsafe.impl.batchimport.input.InputNode) Predicate(java.util.function.Predicate) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) MOVE(org.neo4j.kernel.impl.storemigration.FileOperation.MOVE) LogService(org.neo4j.kernel.impl.logging.LogService) List(java.util.List) StoreFileType(org.neo4j.kernel.impl.storemigration.StoreFileType) MetaDataStore(org.neo4j.kernel.impl.store.MetaDataStore) Writer(java.io.Writer) Optional(java.util.Optional) Inputs(org.neo4j.unsafe.impl.batchimport.input.Inputs) InputIterable(org.neo4j.unsafe.impl.batchimport.InputIterable) InputRelationship(org.neo4j.unsafe.impl.batchimport.input.InputRelationship) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) InputEntity(org.neo4j.unsafe.impl.batchimport.input.InputEntity) DEFAULT_NAME(org.neo4j.kernel.impl.store.MetaDataStore.DEFAULT_NAME) RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) CustomIOConfigValidator(org.neo4j.kernel.impl.util.CustomIOConfigValidator) AdditionalInitialIds(org.neo4j.unsafe.impl.batchimport.AdditionalInitialIds) NeoStores(org.neo4j.kernel.impl.store.NeoStores) StorePropertyCursor(org.neo4j.kernel.impl.api.store.StorePropertyCursor) CountsTracker(org.neo4j.kernel.impl.store.counts.CountsTracker) ExistingTargetStrategy(org.neo4j.kernel.impl.storemigration.ExistingTargetStrategy) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles) RelationshipRecordFormat(org.neo4j.kernel.impl.store.format.standard.RelationshipRecordFormat) OutputStream(java.io.OutputStream) Config(org.neo4j.kernel.configuration.Config) ReadOnlyIdGeneratorFactory(org.neo4j.kernel.impl.store.id.ReadOnlyIdGeneratorFactory) LockService(org.neo4j.kernel.impl.locking.LockService) FileHandle(org.neo4j.io.pagecache.FileHandle) CapabilityType(org.neo4j.kernel.impl.store.format.CapabilityType) PrimitiveRecord(org.neo4j.kernel.impl.store.record.PrimitiveRecord) FIELD_NOT_PRESENT(org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat.FIELD_NOT_PRESENT) StoreMigratorCheckPointer(org.neo4j.kernel.impl.storemigration.StoreMigratorCheckPointer) ExecutionMonitor(org.neo4j.unsafe.impl.batchimport.staging.ExecutionMonitor) BASE_TX_LOG_BYTE_OFFSET(org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_LOG_BYTE_OFFSET) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StorePropertyCursor(org.neo4j.kernel.impl.api.store.StorePropertyCursor) ArrayList(java.util.ArrayList)

Example 10 with BiConsumer

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;
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BiConsumer(java.util.function.BiConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

BiConsumer (java.util.function.BiConsumer)264 Test (org.junit.Test)111 List (java.util.List)109 Map (java.util.Map)79 IOException (java.io.IOException)78 ArrayList (java.util.ArrayList)74 Consumer (java.util.function.Consumer)71 HashMap (java.util.HashMap)66 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)54 Collectors (java.util.stream.Collectors)53 CountDownLatch (java.util.concurrent.CountDownLatch)52 Collections (java.util.Collections)46 Set (java.util.Set)46 Collection (java.util.Collection)45 Arrays (java.util.Arrays)44 TimeUnit (java.util.concurrent.TimeUnit)43 Function (java.util.function.Function)43 Assert (org.junit.Assert)43 Optional (java.util.Optional)41 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35