Search in sources :

Example 71 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class DocLevelCollectTest method collect.

private Bucket collect(RoutedCollectPhase collectNode) throws Throwable {
    ContextPreparer contextPreparer = internalCluster().getDataNodeInstance(ContextPreparer.class);
    JobContextService contextService = internalCluster().getDataNodeInstance(JobContextService.class);
    SharedShardContexts sharedShardContexts = new SharedShardContexts(internalCluster().getDataNodeInstance(IndicesService.class));
    JobExecutionContext.Builder builder = contextService.newBuilder(collectNode.jobId());
    NodeOperation nodeOperation = NodeOperation.withDownstream(collectNode, mock(ExecutionPhase.class), (byte) 0, "remoteNode");
    List<CompletableFuture<Bucket>> results = contextPreparer.prepareOnRemote(ImmutableList.of(nodeOperation), builder, sharedShardContexts);
    JobExecutionContext context = contextService.createContext(builder);
    context.start();
    return results.get(0).get(2, TimeUnit.SECONDS);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SharedShardContexts(io.crate.action.job.SharedShardContexts) ContextPreparer(io.crate.action.job.ContextPreparer) IndicesService(org.elasticsearch.indices.IndicesService) JobExecutionContext(io.crate.jobs.JobExecutionContext) NodeOperation(io.crate.operation.NodeOperation) ExecutionPhase(io.crate.planner.node.ExecutionPhase) JobContextService(io.crate.jobs.JobContextService)

Example 72 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class SnapshotRestoreDDLDispatcher method dispatch.

public CompletableFuture<Long> dispatch(final DropSnapshotAnalyzedStatement statement) {
    final CompletableFuture<Long> future = new CompletableFuture<>();
    final String repositoryName = statement.repository();
    final String snapshotName = statement.snapshot();
    transportActionProvider.transportDeleteSnapshotAction().execute(new DeleteSnapshotRequest(repositoryName, snapshotName), new ActionListener<DeleteSnapshotResponse>() {

        @Override
        public void onResponse(DeleteSnapshotResponse response) {
            if (!response.isAcknowledged()) {
                LOGGER.info("delete snapshot '{}.{}' not acknowledged", repositoryName, snapshotName);
            }
            future.complete(1L);
        }

        @Override
        public void onFailure(Throwable e) {
            future.completeExceptionally(e);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) DeleteSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest) DeleteSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse)

Example 73 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class SnapshotRestoreDDLDispatcher method dispatch.

public CompletableFuture<Long> dispatch(final CreateSnapshotAnalyzedStatement statement) {
    final CompletableFuture<Long> resultFuture = new CompletableFuture<>();
    boolean waitForCompletion = statement.snapshotSettings().getAsBoolean(WAIT_FOR_COMPLETION.settingName(), WAIT_FOR_COMPLETION.defaultValue());
    boolean ignoreUnavailable = statement.snapshotSettings().getAsBoolean(IGNORE_UNAVAILABLE.settingName(), IGNORE_UNAVAILABLE.defaultValue());
    // ignore_unavailable as set by statement
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, IndicesOptions.lenientExpandOpen());
    CreateSnapshotRequest request = new CreateSnapshotRequest(statement.snapshotId().getRepository(), statement.snapshotId().getSnapshot()).includeGlobalState(statement.includeMetadata()).waitForCompletion(waitForCompletion).indices(statement.indices()).indicesOptions(indicesOptions).settings(statement.snapshotSettings());
    //noinspection ThrowableResultOfMethodCallIgnored
    assert request.validate() == null : "invalid CREATE SNAPSHOT statement";
    transportActionProvider.transportCreateSnapshotAction().execute(request, new ActionListener<CreateSnapshotResponse>() {

        @Override
        public void onResponse(CreateSnapshotResponse createSnapshotResponse) {
            SnapshotInfo snapshotInfo = createSnapshotResponse.getSnapshotInfo();
            if (snapshotInfo == null) {
                // if wait_for_completion is false the snapshotInfo is null
                resultFuture.complete(1L);
            } else if (snapshotInfo.state() == SnapshotState.FAILED) {
                // fail request if snapshot creation failed
                String reason = createSnapshotResponse.getSnapshotInfo().reason().replaceAll("Index", "Table").replaceAll("Indices", "Tables");
                resultFuture.completeExceptionally(new CreateSnapshotException(statement.snapshotId(), reason));
            } else {
                resultFuture.complete(1L);
            }
        }

        @Override
        public void onFailure(Throwable e) {
            resultFuture.completeExceptionally(e);
        }
    });
    return resultFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 74 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class NodeStatsIterator method getNodeStatsContextFromRemoteState.

private CompletableFuture<List<NodeStatsContext>> getNodeStatsContextFromRemoteState(Set<ColumnIdent> toCollect) {
    final CompletableFuture<List<NodeStatsContext>> nodeStatsContextsFuture = new CompletableFuture<>();
    final List<NodeStatsContext> rows = Collections.synchronizedList(new ArrayList<NodeStatsContext>(nodes.size()));
    final AtomicInteger remainingNodesToCollect = new AtomicInteger(nodes.size());
    for (final DiscoveryNode node : nodes) {
        final String nodeId = node.getId();
        final NodeStatsRequest request = new NodeStatsRequest(toCollect);
        transportStatTablesAction.execute(nodeId, request, new ActionListener<NodeStatsResponse>() {

            @Override
            public void onResponse(NodeStatsResponse response) {
                rows.add(response.nodeStatsContext());
                if (remainingNodesToCollect.decrementAndGet() == 0) {
                    nodeStatsContextsFuture.complete(rows);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                if (t instanceof ReceiveTimeoutTransportException) {
                    rows.add(new NodeStatsContext(nodeId, node.name()));
                    if (remainingNodesToCollect.decrementAndGet() == 0) {
                        nodeStatsContextsFuture.complete(rows);
                    }
                } else {
                    nodeStatsContextsFuture.completeExceptionally(t);
                }
            }
        }, TimeValue.timeValueMillis(3000L));
    }
    return nodeStatsContextsFuture;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NodeStatsResponse(io.crate.executor.transport.NodeStatsResponse) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeStatsRequest(io.crate.executor.transport.NodeStatsRequest) NodeStatsContext(io.crate.operation.reference.sys.node.NodeStatsContext)

Example 75 with CompletableFuture

use of java.util.concurrent.CompletableFuture in project crate by crate.

the class SystemCollectSource method getCollector.

@Override
public CrateCollector getCollector(CollectPhase phase, BatchConsumer consumer, JobCollectContext jobCollectContext) {
    RoutedCollectPhase collectPhase = (RoutedCollectPhase) phase;
    // sys.operations can contain a _node column - these refs need to be normalized into literals
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(functions, RowGranularity.DOC, ReplaceMode.COPY, new NodeSysReferenceResolver(nodeSysExpression), null);
    final RoutedCollectPhase routedCollectPhase = collectPhase.normalize(normalizer, null);
    Map<String, Map<String, List<Integer>>> locations = collectPhase.routing().locations();
    String table = Iterables.getOnlyElement(locations.get(clusterService.localNode().getId()).keySet());
    Supplier<CompletableFuture<? extends Iterable<?>>> iterableGetter = iterableGetters.get(table);
    assert iterableGetter != null : "iterableGetter for " + table + " must exist";
    boolean requiresScroll = consumer.requiresScroll();
    return BatchIteratorCollectorBridge.newInstance(() -> iterableGetter.get().thenApply(dataIterable -> RowsBatchIterator.newInstance(dataIterableToRowsIterable(routedCollectPhase, requiresScroll, dataIterable), collectPhase.toCollect().size())), consumer);
}
Also used : BatchIteratorCollectorBridge(io.crate.operation.collect.BatchIteratorCollectorBridge) Iterables(com.google.common.collect.Iterables) CompletableFuture(java.util.concurrent.CompletableFuture) ReplaceMode(io.crate.metadata.ReplaceMode) Function(java.util.function.Function) Supplier(java.util.function.Supplier) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Inject(org.elasticsearch.common.inject.Inject) JobCollectContext(io.crate.operation.collect.JobCollectContext) SysSnapshots(io.crate.operation.reference.sys.snapshot.SysSnapshots) ImmutableList(com.google.common.collect.ImmutableList) Functions(io.crate.metadata.Functions) BatchConsumer(io.crate.data.BatchConsumer) io.crate.metadata.sys(io.crate.metadata.sys) NodeSysReferenceResolver(io.crate.operation.reference.sys.node.local.NodeSysReferenceResolver) ClusterService(org.elasticsearch.cluster.ClusterService) Map(java.util.Map) SysRowUpdater(io.crate.operation.reference.sys.SysRowUpdater) SysRepositoriesService(io.crate.operation.reference.sys.repositories.SysRepositoriesService) RowsBatchIterator(io.crate.data.RowsBatchIterator) CrateCollector(io.crate.operation.collect.CrateCollector) SysCheck(io.crate.operation.reference.sys.check.SysCheck) ImmutableMap(com.google.common.collect.ImmutableMap) RowsTransformer(io.crate.operation.collect.RowsTransformer) Set(java.util.Set) SysChecker(io.crate.operation.reference.sys.check.SysChecker) TableIdent(io.crate.metadata.TableIdent) RowContextReferenceResolver(io.crate.operation.reference.sys.RowContextReferenceResolver) JobsLogs(io.crate.operation.collect.stats.JobsLogs) CollectPhase(io.crate.planner.node.dql.CollectPhase) PgCatalogTables(io.crate.metadata.pg_catalog.PgCatalogTables) InputFactory(io.crate.operation.InputFactory) SysNodeChecks(io.crate.operation.reference.sys.check.node.SysNodeChecks) io.crate.metadata.information(io.crate.metadata.information) SummitsIterable(io.crate.operation.collect.files.SummitsIterable) List(java.util.List) RowGranularity(io.crate.metadata.RowGranularity) Row(io.crate.data.Row) NodeSysExpression(io.crate.operation.reference.sys.node.local.NodeSysExpression) PgTypeTable(io.crate.metadata.pg_catalog.PgTypeTable) EvaluatingNormalizer(io.crate.analyze.EvaluatingNormalizer) CompletableFuture(java.util.concurrent.CompletableFuture) EvaluatingNormalizer(io.crate.analyze.EvaluatingNormalizer) SummitsIterable(io.crate.operation.collect.files.SummitsIterable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) NodeSysReferenceResolver(io.crate.operation.reference.sys.node.local.NodeSysReferenceResolver)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)490 Test (org.junit.Test)152 ArrayList (java.util.ArrayList)88 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 List (java.util.List)75 UUID (java.util.UUID)62 Futures (io.pravega.common.concurrent.Futures)60 Map (java.util.Map)59 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)57 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)53 HashMap (java.util.HashMap)52 TimeUnit (java.util.concurrent.TimeUnit)52 Cleanup (lombok.Cleanup)49 Exceptions (io.pravega.common.Exceptions)48 Collectors (java.util.stream.Collectors)48 lombok.val (lombok.val)47 IOException (java.io.IOException)46 Duration (java.time.Duration)46 Slf4j (lombok.extern.slf4j.Slf4j)46 AtomicReference (java.util.concurrent.atomic.AtomicReference)45