Java Code Examples for org.elasticsearch.common.util.concurrent.FutureUtils

The following examples show how to use org.elasticsearch.common.util.concurrent.FutureUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   Source File: IndexShard.java    License: Apache License 2.0 6 votes vote down vote up
public void close(String reason, boolean flushEngine) throws IOException {
    synchronized (mutex) {
        try {
            indexSettingsService.removeListener(applyRefreshSettings);
            if (state != IndexShardState.CLOSED) {
                FutureUtils.cancel(refreshScheduledFuture);
                refreshScheduledFuture = null;
                FutureUtils.cancel(mergeScheduleFuture);
                mergeScheduleFuture = null;
            }
            changeState(IndexShardState.CLOSED, reason);
            indexShardOperationCounter.decRef();
        } finally {
            final Engine engine = this.currentEngineReference.getAndSet(null);
            try {
                if (engine != null && flushEngine && this.flushOnClose) {
                    engine.flushAndClose();
                }
            } finally { // playing safe here and close the engine even if the above succeeds - close can be called multiple times
                IOUtils.close(engine, percolatorQueriesRegistry);
            }
        }
    }
}
 
Example 2
Source Project: Elasticsearch   Source File: Translog.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
    if (closed.compareAndSet(false, true)) {
        try (ReleasableLock lock = writeLock.acquire()) {
            try {
                current.sync();
            } finally {
                try {
                    IOUtils.close(current, currentCommittingTranslog);
                } finally {
                    IOUtils.close(recoveredTranslogs);
                    recoveredTranslogs.clear();
                }
            }
        } finally {
            FutureUtils.cancel(syncScheduler);
            logger.debug("translog closed");
        }
    }
}
 
Example 3
Source Project: Elasticsearch   Source File: TransportClientNodesService.java    License: Apache License 2.0 6 votes vote down vote up
public void close() {
    synchronized (mutex) {
        if (closed) {
            return;
        }
        closed = true;
        FutureUtils.cancel(nodesSamplerFuture);
        for (DiscoveryNode node : nodes) {
            transportService.disconnectFromNode(node);
        }
        for (DiscoveryNode listedNode : listedNodes) {
            transportService.disconnectFromNode(listedNode);
        }
        nodes = Collections.emptyList();
    }
}
 
Example 4
Source Project: Elasticsearch   Source File: InternalClusterService.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onNodeAck(DiscoveryNode node, @Nullable Throwable t) {
    if (!ackedTaskListener.mustAck(node)) {
        //we always wait for the master ack anyway
        if (!node.equals(nodes.masterNode())) {
            return;
        }
    }
    if (t == null) {
        logger.trace("ack received from node [{}], cluster_state update (version: {})", node, clusterStateVersion);
    } else {
        this.lastFailure = t;
        logger.debug("ack received from node [{}], cluster_state update (version: {})", t, node, clusterStateVersion);
    }

    if (countDown.countDown()) {
        logger.trace("all expected nodes acknowledged cluster_state update (version: {})", clusterStateVersion);
        FutureUtils.cancel(ackTimeoutCallback);
        ackedTaskListener.onAllNodesAcked(lastFailure);
    }
}
 
Example 5
Source Project: elasticsearch-helper   Source File: HttpBulkProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Closes the processor. If flushing by time is enabled, then it's shutdown. Any remaining bulk actions are flushed.
 *
 * If concurrent requests are not enabled, returns {@code true} immediately.
 * If concurrent requests are enabled, waits for up to the specified timeout for all bulk requests to complete then returns {@code true},
 * If the specified waiting time elapses before all bulk requests complete, {@code false} is returned.
 *
 * @param timeout The maximum time to wait for the bulk requests to complete
 * @param unit The time unit of the {@code timeout} argument
 * @return {@code true} if all bulk requests completed and {@code false} if the waiting time elapsed before all the bulk requests completed
 * @throws InterruptedException If the current thread is interrupted
 */
public synchronized boolean awaitClose(long timeout, TimeUnit unit) throws InterruptedException {
    if (closed) {
        return true;
    }
    closed = true;
    if (this.scheduledFuture != null) {
        FutureUtils.cancel(this.scheduledFuture);
        this.scheduler.shutdown();
    }
    if (bulkRequest.numberOfActions() > 0) {
        execute();
    }
    if (this.concurrentRequests < 1) {
        return true;
    }
    if (semaphore.tryAcquire(this.concurrentRequests, timeout, unit)) {
        semaphore.release(this.concurrentRequests);
        return true;
    }
    return false;
}
 
Example 6
Source Project: crate   Source File: MasterService.java    License: Apache License 2.0 6 votes vote down vote up
protected void publish(ClusterChangedEvent clusterChangedEvent, TaskOutputs taskOutputs, long startTimeNS) {
    final PlainActionFuture<Void> fut = new PlainActionFuture<Void>() {
        @Override
        protected boolean blockingAllowed() {
            return isMasterUpdateThread() || super.blockingAllowed();
        }
    };
    clusterStatePublisher.publish(clusterChangedEvent, fut, taskOutputs.createAckListener(threadPool, clusterChangedEvent.state()));

    // indefinitely wait for publication to complete
    try {
        FutureUtils.get(fut);
        onPublicationSuccess(clusterChangedEvent, taskOutputs, startTimeNS);
    } catch (Exception e) {
        onPublicationFailed(clusterChangedEvent, taskOutputs, startTimeNS, e);
    }
}
 
Example 7
Source Project: crate   Source File: NodeJoinTests.java    License: Apache License 2.0 6 votes vote down vote up
public void testBecomeFollowerFailsPendingJoin() throws Exception {
    DiscoveryNode node0 = newNode(0, true);
    DiscoveryNode node1 = newNode(1, true);
    long initialTerm = randomLongBetween(1, 10);
    long initialVersion = randomLongBetween(1, 10);
    setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
        new VotingConfiguration(Collections.singleton(node1.getId()))));
    long newTerm = initialTerm + randomLongBetween(1, 10);
    SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(fut.isDone());
    assertFalse(isLocalNodeElectedMaster());
    handleFollowerCheckFrom(node1, newTerm);
    assertFalse(isLocalNodeElectedMaster());
    assertThat(expectThrows(CoordinationStateRejectedException.class,
        () -> FutureUtils.get(fut)).getMessage(),
        containsString("became follower"));
    assertFalse(isLocalNodeElectedMaster());
}
 
Example 8
Source Project: Elasticsearch   Source File: ResourceWatcherService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doStop() {
    if (!enabled) {
        return;
    }
    FutureUtils.cancel(lowFuture);
    FutureUtils.cancel(mediumFuture);
    FutureUtils.cancel(highFuture);
}
 
Example 9
Source Project: Elasticsearch   Source File: InternalClusterService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doStop() {
    FutureUtils.cancel(this.reconnectToNodes);
    for (NotifyTimeout onGoingTimeout : onGoingTimeouts) {
        onGoingTimeout.cancel();
        onGoingTimeout.listener.onClose();
    }
    ThreadPool.terminate(updateTasksExecutor, 10, TimeUnit.SECONDS);
    remove(localNodeMasterListeners);
}
 
Example 10
Source Project: Elasticsearch   Source File: MetaDataDeleteIndexService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onResponse(final Response response) {
    if (notified.compareAndSet(false, true)) {
        FutureUtils.cancel(future);
        listener.onResponse(response);
    }
}
 
Example 11
Source Project: Elasticsearch   Source File: MetaDataDeleteIndexService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable t) {
    if (notified.compareAndSet(false, true)) {
        FutureUtils.cancel(future);
        listener.onFailure(t);
    }
}
 
Example 12
Source Project: Elasticsearch   Source File: JvmMonitorService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doStop() {
    if (!enabled) {
        return;
    }
    FutureUtils.cancel(scheduledFuture);
}
 
Example 13
Source Project: Elasticsearch   Source File: Retry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onFailure(Throwable e) {
    try {
        listener.onFailure(e);
    } finally {
        FutureUtils.cancel(scheduledRequestFuture);
    }
}
 
Example 14
Source Project: Elasticsearch   Source File: Retry.java    License: Apache License 2.0 5 votes vote down vote up
private void finishHim() {
    try {
        listener.onResponse(getAccumulatedResponse());
    } finally {
        FutureUtils.cancel(scheduledRequestFuture);
    }
}
 
Example 15
Source Project: Elasticsearch   Source File: BulkProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Closes the processor. If flushing by time is enabled, then it's shutdown. Any remaining bulk actions are flushed.
 *
 * If concurrent requests are not enabled, returns {@code true} immediately.
 * If concurrent requests are enabled, waits for up to the specified timeout for all bulk requests to complete then returns {@code true},
 * If the specified waiting time elapses before all bulk requests complete, {@code false} is returned.
 *
 * @param timeout The maximum time to wait for the bulk requests to complete
 * @param unit The time unit of the {@code timeout} argument
 * @return {@code true} if all bulk requests completed and {@code false} if the waiting time elapsed before all the bulk requests completed
 * @throws InterruptedException If the current thread is interrupted
 */
public synchronized boolean awaitClose(long timeout, TimeUnit unit) throws InterruptedException {
    if (closed) {
        return true;
    }
    closed = true;
    if (this.scheduledFuture != null) {
        FutureUtils.cancel(this.scheduledFuture);
        this.scheduler.shutdown();
    }
    if (bulkRequest.numberOfActions() > 0) {
        execute();
    }
    return this.bulkRequestHandler.awaitClose(timeout, unit);
}
 
Example 16
Source Project: elasticsearch-helper   Source File: BulkProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Closes the processor. If flushing by time is enabled, then it's shutdown. Any remaining bulk actions are flushed.
 *
 * If concurrent requests are not enabled, returns {@code true} immediately.
 * If concurrent requests are enabled, waits for up to the specified timeout for all bulk requests to complete then returns {@code true},
 * If the specified waiting time elapses before all bulk requests complete, {@code false} is returned.
 *
 * @param timeout The maximum time to wait for the bulk requests to complete
 * @param unit The time unit of the {@code timeout} argument
 * @return {@code true} if all bulk requests completed and {@code false} if the waiting time elapsed before all the bulk requests completed
 * @throws InterruptedException If the current thread is interrupted
 */
public synchronized boolean awaitClose(long timeout, TimeUnit unit) throws InterruptedException {
    if (closed) {
        return true;
    }
    closed = true;
    if (this.scheduledFuture != null) {
        FutureUtils.cancel(this.scheduledFuture);
        this.scheduler.shutdown();
    }
    if (bulkRequest.numberOfActions() > 0) {
        execute();
    }
    return this.bulkRequestHandler.awaitClose(timeout, unit);
}
 
Example 17
Source Project: crate   Source File: RecoverySourceHandler.java    License: Apache License 2.0 5 votes vote down vote up
static void runUnderPrimaryPermit(CancellableThreads.Interruptable runnable, String reason,
                                  IndexShard primary, CancellableThreads cancellableThreads, Logger logger) {
    cancellableThreads.execute(() -> {
        CompletableFuture<Releasable> permit = new CompletableFuture<>();
        final ActionListener<Releasable> onAcquired = new ActionListener<>() {
            @Override
            public void onResponse(Releasable releasable) {
                if (permit.complete(releasable) == false) {
                    releasable.close();
                }
            }

            @Override
            public void onFailure(Exception e) {
                permit.completeExceptionally(e);
            }
        };
        primary.acquirePrimaryOperationPermit(onAcquired, ThreadPool.Names.SAME, reason);
        try (Releasable ignored = FutureUtils.get(permit)) {
            // check that the IndexShard still has the primary authority. This needs to be checked under operation permit to prevent
            // races, as IndexShard will switch its authority only when it holds all operation permits, see IndexShard.relocated()
            if (primary.isRelocatedPrimary()) {
                throw new IndexShardRelocatedException(primary.shardId());
            }
            runnable.run();
        } finally {
            // just in case we got an exception (likely interrupted) while waiting for the get
            permit.whenComplete((r, e) -> {
                if (r != null) {
                    r.close();
                }
                if (e != null) {
                    logger.trace("suppressing exception on completion (it was already bubbled up or the operation was aborted)", e);
                }
            });
        }
    });
}
 
Example 18
Source Project: crate   Source File: StepListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the result of this step. This method will throw {@link IllegalStateException} if this step is not completed yet.
 */
public Response result() {
    if (delegate.isDone() == false) {
        throw new IllegalStateException("step is not completed yet");
    }
    return FutureUtils.get(delegate, 0L, TimeUnit.NANOSECONDS); // this future is done already - use a non-blocking method.
}
 
Example 19
Source Project: crate   Source File: NodeJoinTests.java    License: Apache License 2.0 5 votes vote down vote up
public void testJoinAccumulation() {
    DiscoveryNode node0 = newNode(0, true);
    DiscoveryNode node1 = newNode(1, true);
    DiscoveryNode node2 = newNode(2, true);
    long initialTerm = randomLongBetween(1, 10);
    long initialVersion = randomLongBetween(1, 10);
    setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
        new VotingConfiguration(Collections.singleton(node2.getId()))));
    assertFalse(isLocalNodeElectedMaster());
    long newTerm = initialTerm + randomLongBetween(1, 10);
    SimpleFuture futNode0 = joinNodeAsync(new JoinRequest(node0, Optional.of(
        new Join(node0, node0, newTerm, initialTerm, initialVersion))));
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(futNode0.isDone());
    assertFalse(isLocalNodeElectedMaster());
    SimpleFuture futNode1 = joinNodeAsync(new JoinRequest(node1, Optional.of(
        new Join(node1, node0, newTerm, initialTerm, initialVersion))));
    deterministicTaskQueue.runAllRunnableTasks();
    assertFalse(futNode1.isDone());
    assertFalse(isLocalNodeElectedMaster());
    joinNodeAndRun(new JoinRequest(node2, Optional.of(new Join(node2, node0, newTerm, initialTerm, initialVersion))));
    assertTrue(isLocalNodeElectedMaster());
    assertTrue(clusterStateHasNode(node1));
    assertTrue(clusterStateHasNode(node2));
    FutureUtils.get(futNode0);
    FutureUtils.get(futNode1);
}
 
Example 20
Source Project: Elasticsearch   Source File: IndexingMemoryController.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doStop() {
    FutureUtils.cancel(scheduler);
    scheduler = null;
}
 
Example 21
Source Project: Elasticsearch   Source File: SearchService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doClose() {
    doStop();
    FutureUtils.cancel(keepAliveReaper);
}
 
Example 22
Source Project: Elasticsearch   Source File: TranslogService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    indexSettingsService.removeListener(applySettings);
    FutureUtils.cancel(this.future);
}
 
Example 23
Source Project: Elasticsearch   Source File: RoutingService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void doClose() {
    FutureUtils.cancel(registeredNextDelayFuture);
    clusterService.remove(this);
}
 
Example 24
Source Project: Elasticsearch   Source File: InternalClusterService.java    License: Apache License 2.0 4 votes vote down vote up
public void cancel() {
    FutureUtils.cancel(future);
}
 
Example 25
Source Project: crate   Source File: ScheduledCancellableAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean cancel() {
    return FutureUtils.cancel(scheduledFuture);
}
 
Example 26
Source Project: crate   Source File: AdapterActionFuture.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public T actionGet() {
    return FutureUtils.get(this);
}
 
Example 27
Source Project: crate   Source File: AdapterActionFuture.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public T actionGet(long timeout, TimeUnit unit) {
    return FutureUtils.get(this, timeout, unit);
}
 
Example 28
Source Project: crate   Source File: NodeJoinTests.java    License: Apache License 2.0 4 votes vote down vote up
private void joinNode(final JoinRequest joinRequest) {
    FutureUtils.get(joinNodeAsync(joinRequest));
}
 
Example 29
Source Project: crate   Source File: NodeJoinTests.java    License: Apache License 2.0 4 votes vote down vote up
private void joinNodeAndRun(final JoinRequest joinRequest) {
    SimpleFuture fut = joinNodeAsync(joinRequest);
    deterministicTaskQueue.runAllRunnableTasks();
    assertTrue(fut.isDone());
    FutureUtils.get(fut);
}
 
Example 30
Source Project: Elasticsearch   Source File: MeterMetric.java    License: Apache License 2.0 votes vote down vote up
public void stop() { FutureUtils.cancel(future);}