Java Code Examples for org.elasticsearch.cluster.ClusterStateObserver#waitForNextChange()

The following examples show how to use org.elasticsearch.cluster.ClusterStateObserver#waitForNextChange() . 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 File: NodeAndClusterIdStateListener.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribes for the first cluster state update where nodeId and clusterId is present
 * and sets these values in {@link NodeAndClusterIdConverter}.
 */
public static void getAndSetNodeIdAndClusterId(ClusterService clusterService) {
    ClusterState clusterState = clusterService.state();
    ClusterStateObserver observer = new ClusterStateObserver(clusterState, clusterService, null, LOGGER);

    observer.waitForNextChange(new NodeAndClusterIdStateListener(), NodeAndClusterIdStateListener::isNodeAndClusterIdPresent);
}
 
Example 2
Source File: PeerRecoveryTargetService.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(final RecoveryTranslogOperationsRequest request, final TransportChannel channel,
                            Task task) throws IOException {
    try (RecoveryRef recoveryRef =
             onGoingRecoveries.getRecoverySafe(request.recoveryId(), request.shardId())) {
        final ClusterStateObserver observer = new ClusterStateObserver(clusterService, null, LOGGER);
        final RecoveryTarget recoveryTarget = recoveryRef.target();
        final ActionListener<RecoveryTranslogOperationsResponse> listener =
            new HandledTransportAction.ChannelActionListener<>(channel, Actions.TRANSLOG_OPS, request);
        final Consumer<Exception> retryOnMappingException = exception -> {
            // in very rare cases a translog replay from primary is processed before
            // a mapping update on this node which causes local mapping changes since
            // the mapping (clusterstate) might not have arrived on this node.
            LOGGER.debug("delaying recovery due to missing mapping changes", exception);
            // we do not need to use a timeout here since the entire recovery mechanism has an
            // inactivity protection (it will be canceled)
            observer.waitForNextChange(new ClusterStateObserver.Listener() {
                @Override
                public void onNewClusterState(ClusterState state) {
                    try {
                        messageReceived(request, channel, task);
                    } catch (Exception e) {
                        listener.onFailure(e);
                    }
                }

                @Override
                public void onClusterServiceClose() {
                    listener.onFailure(new ElasticsearchException(
                        "cluster service was closed while waiting for mapping updates"));
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    // note that we do not use a timeout (see comment above)
                    listener.onFailure(new ElasticsearchTimeoutException(
                        "timed out waiting for mapping updates (timeout [" + timeout + "])"));
                }
            });
        };
        recoveryTarget.indexTranslogOperations(
            request.operations(),
            request.totalTranslogOps(),
            request.maxSeenAutoIdTimestampOnPrimary(), request.maxSeqNoOfUpdatesOrDeletesOnPrimary(),
            ActionListener.wrap(
                checkpoint -> listener.onResponse(new RecoveryTranslogOperationsResponse(checkpoint)),
                e -> {
                    if (e instanceof MapperException) {
                        retryOnMappingException.accept(e);
                    } else {
                        listener.onFailure(e);
                    }
                })
        );
    }
}
 
Example 3
Source File: TransportClearVotingConfigExclusionsAction.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected void masterOperation(ClearVotingConfigExclusionsRequest request, ClusterState initialState,
                               ActionListener<ClearVotingConfigExclusionsResponse> listener) throws Exception {

    final long startTimeMillis = threadPool.relativeTimeInMillis();

    final Predicate<ClusterState> allExclusionsRemoved = newState -> {
        for (VotingConfigExclusion tombstone : initialState.getVotingConfigExclusions()) {
            // NB checking for the existence of any node with this persistent ID, because persistent IDs are how votes are counted.
            if (newState.nodes().nodeExists(tombstone.getNodeId())) {
                return false;
            }
        }
        return true;
    };

    if (request.getWaitForRemoval() && allExclusionsRemoved.test(initialState) == false) {
        final ClusterStateObserver clusterStateObserver = new ClusterStateObserver(
            initialState,
            clusterService,
            request.getTimeout(),
            logger);

        clusterStateObserver.waitForNextChange(new Listener() {
            @Override
            public void onNewClusterState(ClusterState state) {
                submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
            }

            @Override
            public void onClusterServiceClose() {
                listener.onFailure(new ElasticsearchException("cluster service closed while waiting for removal of nodes "
                    + initialState.getVotingConfigExclusions()));
            }

            @Override
            public void onTimeout(TimeValue timeout) {
                listener.onFailure(new ElasticsearchTimeoutException(
                    "timed out waiting for removal of nodes; if nodes should not be removed, set waitForRemoval to false. "
                    + initialState.getVotingConfigExclusions()));
            }
        }, allExclusionsRemoved);
    } else {
        submitClearVotingConfigExclusionsTask(request, startTimeMillis, listener);
    }
}