Search in sources :

Example 1 with ClusterStateObserver

use of org.elasticsearch.cluster.ClusterStateObserver in project elasticsearch by elastic.

the class TransportClusterHealthAction method executeHealth.

private void executeHealth(final ClusterHealthRequest request, final ActionListener<ClusterHealthResponse> listener) {
    int waitFor = 5;
    if (request.waitForStatus() == null) {
        waitFor--;
    }
    if (request.waitForNoRelocatingShards() == false) {
        waitFor--;
    }
    if (request.waitForActiveShards().equals(ActiveShardCount.NONE)) {
        waitFor--;
    }
    if (request.waitForNodes().isEmpty()) {
        waitFor--;
    }
    if (request.indices() == null || request.indices().length == 0) {
        // check that they actually exists in the meta data
        waitFor--;
    }
    assert waitFor >= 0;
    final ClusterState state = clusterService.state();
    final ClusterStateObserver observer = new ClusterStateObserver(state, clusterService, null, logger, threadPool.getThreadContext());
    if (request.timeout().millis() == 0) {
        listener.onResponse(getResponse(request, state, waitFor, request.timeout().millis() == 0));
        return;
    }
    final int concreteWaitFor = waitFor;
    final Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, concreteWaitFor);
    final ClusterStateObserver.Listener stateListener = new ClusterStateObserver.Listener() {

        @Override
        public void onNewClusterState(ClusterState clusterState) {
            listener.onResponse(getResponse(request, clusterState, concreteWaitFor, false));
        }

        @Override
        public void onClusterServiceClose() {
            listener.onFailure(new IllegalStateException("ClusterService was close during health call"));
        }

        @Override
        public void onTimeout(TimeValue timeout) {
            final ClusterHealthResponse response = getResponse(request, observer.setAndGetObservedState(), concreteWaitFor, true);
            listener.onResponse(response);
        }
    };
    if (validationPredicate.test(state)) {
        stateListener.onNewClusterState(state);
    } else {
        observer.waitForNextChange(stateListener, validationPredicate, request.timeout());
    }
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TransportMasterNodeReadAction(org.elasticsearch.action.support.master.TransportMasterNodeReadAction) TimeValue(org.elasticsearch.common.unit.TimeValue) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionFilters(org.elasticsearch.action.support.ActionFilters) Predicate(java.util.function.Predicate) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) Supplier(org.apache.logging.log4j.util.Supplier) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) ActionListener(org.elasticsearch.action.ActionListener) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionListener(org.elasticsearch.action.ActionListener) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 2 with ClusterStateObserver

use of org.elasticsearch.cluster.ClusterStateObserver in project elasticsearch by elastic.

the class ActiveShardsObserver method waitForActiveShards.

/**
     * Waits on the specified number of active shards to be started before executing the
     *
     * @param indexName the index to wait for active shards on
     * @param activeShardCount the number of active shards to wait on before returning
     * @param timeout the timeout value
     * @param onResult a function that is executed in response to the requisite shards becoming active or a timeout (whichever comes first)
     * @param onFailure a function that is executed in response to an error occurring during waiting for the active shards
     */
public void waitForActiveShards(final String indexName, final ActiveShardCount activeShardCount, final TimeValue timeout, final Consumer<Boolean> onResult, final Consumer<Exception> onFailure) {
    // wait for the configured number of active shards to be allocated before executing the result consumer
    if (activeShardCount == ActiveShardCount.NONE) {
        // not waiting, so just run whatever we were to run when the waiting is
        onResult.accept(true);
        return;
    }
    final ClusterState state = clusterService.state();
    final ClusterStateObserver observer = new ClusterStateObserver(state, clusterService, null, logger, threadPool.getThreadContext());
    if (activeShardCount.enoughShardsActive(state, indexName)) {
        onResult.accept(true);
    } else {
        final Predicate<ClusterState> shardsAllocatedPredicate = newState -> activeShardCount.enoughShardsActive(newState, indexName);
        final ClusterStateObserver.Listener observerListener = new ClusterStateObserver.Listener() {

            @Override
            public void onNewClusterState(ClusterState state) {
                onResult.accept(true);
            }

            @Override
            public void onClusterServiceClose() {
                logger.debug("[{}] cluster service closed while waiting for enough shards to be started.", indexName);
                onFailure.accept(new NodeClosedException(clusterService.localNode()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                onResult.accept(false);
            }
        };
        observer.waitForNextChange(observerListener, shardsAllocatedPredicate, timeout);
    }
}
Also used : Consumer(java.util.function.Consumer) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Predicate(java.util.function.Predicate) TimeValue(org.elasticsearch.common.unit.TimeValue) ClusterService(org.elasticsearch.cluster.service.ClusterService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) NodeClosedException(org.elasticsearch.node.NodeClosedException) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionListener(org.elasticsearch.action.ActionListener) NodeClosedException(org.elasticsearch.node.NodeClosedException) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 3 with ClusterStateObserver

use of org.elasticsearch.cluster.ClusterStateObserver in project elasticsearch by elastic.

the class ClusterServiceTests method testClusterStateApplierCanCreateAnObserver.

public void testClusterStateApplierCanCreateAnObserver() throws InterruptedException {
    AtomicReference<Throwable> error = new AtomicReference<>();
    AtomicBoolean applierCalled = new AtomicBoolean();
    clusterService.addStateApplier(event -> {
        try {
            applierCalled.set(true);
            ClusterStateObserver observer = new ClusterStateObserver(event.state(), clusterService, null, logger, threadPool.getThreadContext());
            observer.waitForNextChange(new ClusterStateObserver.Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                }

                @Override
                public void onClusterServiceClose() {
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                }
            });
        } catch (AssertionError e) {
            error.set(e);
        }
    });
    CountDownLatch latch = new CountDownLatch(1);
    clusterService.submitStateUpdateTask("test", new ClusterStateUpdateTask() {

        @Override
        public ClusterState execute(ClusterState currentState) throws Exception {
            return ClusterState.builder(currentState).build();
        }

        @Override
        public void onFailure(String source, Exception e) {
            error.compareAndSet(null, e);
        }

        @Override
        public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
            latch.countDown();
        }
    });
    latch.await();
    assertNull(error.get());
    assertTrue(applierCalled.get());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 4 with ClusterStateObserver

use of org.elasticsearch.cluster.ClusterStateObserver in project crate by crate.

the class RetryOnFailureResultReceiver method fail.

@Override
public void fail(@Nonnull Throwable wrappedError) {
    final Throwable error = SQLExceptions.unwrap(wrappedError);
    if (attempt <= Constants.MAX_SHARD_MISSING_RETRIES && (SQLExceptions.isShardFailure(error) || error instanceof ConnectTransportException || indexWasTemporaryUnavailable(error))) {
        if (clusterService.state().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE)) {
            delegate.fail(error);
        } else {
            ClusterStateObserver clusterStateObserver = new ClusterStateObserver(initialState, clusterService, null, LOGGER);
            clusterStateObserver.waitForNextChange(new ClusterStateObserver.Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                    attempt += 1;
                    retry();
                }

                @Override
                public void onClusterServiceClose() {
                    delegate.fail(error);
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    delegate.fail(error);
                }
            });
        }
    } else {
        delegate.fail(error);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TimeValue(io.crate.common.unit.TimeValue)

Example 5 with ClusterStateObserver

use of org.elasticsearch.cluster.ClusterStateObserver in project crate by crate.

the class TransportClusterHealthAction method executeHealth.

private void executeHealth(final ClusterHealthRequest request, final ClusterState currentState, final ActionListener<ClusterHealthResponse> listener, final int waitCount, final Consumer<ClusterState> onNewClusterStateAfterDelay) {
    if (request.timeout().millis() == 0) {
        listener.onResponse(getResponse(request, currentState, waitCount, true));
        return;
    }
    final Predicate<ClusterState> validationPredicate = newState -> validateRequest(request, newState, waitCount);
    if (validationPredicate.test(currentState)) {
        listener.onResponse(getResponse(request, currentState, waitCount, false));
    } else {
        final ClusterStateObserver observer = new ClusterStateObserver(currentState, clusterService, null, LOGGER);
        final ClusterStateObserver.Listener stateListener = new ClusterStateObserver.Listener() {

            @Override
            public void onNewClusterState(ClusterState newState) {
                onNewClusterStateAfterDelay.accept(newState);
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new IllegalStateException("ClusterService was close during health call"));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                listener.onResponse(getResponse(request, observer.setAndGetObservedState(), waitCount, true));
            }
        };
        observer.waitForNextChange(stateListener, validationPredicate, request.timeout());
    }
}
Also used : NotMasterException(org.elasticsearch.cluster.NotMasterException) ClusterService(org.elasticsearch.cluster.service.ClusterService) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) TransportMasterNodeReadAction(org.elasticsearch.action.support.master.TransportMasterNodeReadAction) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) Predicate(java.util.function.Predicate) IOException(java.io.IOException) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) Consumer(java.util.function.Consumer) Logger(org.apache.logging.log4j.Logger) StreamInput(org.elasticsearch.common.io.stream.StreamInput) TimeValue(io.crate.common.unit.TimeValue) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) LogManager(org.apache.logging.log4j.LogManager) ActionListener(org.elasticsearch.action.ActionListener) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ActionListener(org.elasticsearch.action.ActionListener) TimeValue(io.crate.common.unit.TimeValue)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)17 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)17 IOException (java.io.IOException)9 TimeValue (io.crate.common.unit.TimeValue)8 ClusterService (org.elasticsearch.cluster.service.ClusterService)8 ThreadPool (org.elasticsearch.threadpool.ThreadPool)8 Predicate (java.util.function.Predicate)7 ActionListener (org.elasticsearch.action.ActionListener)7 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)6 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)6 NodeClosedException (org.elasticsearch.node.NodeClosedException)6 TransportService (org.elasticsearch.transport.TransportService)6 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)5 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)5 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)5 Metadata (org.elasticsearch.cluster.metadata.Metadata)5 Settings (org.elasticsearch.common.settings.Settings)5 TimeValue (org.elasticsearch.common.unit.TimeValue)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Consumer (java.util.function.Consumer)4