Search in sources :

Example 6 with ClusterStateObserver

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

the class Node method start.

/**
     * Start the node. If the node is already started, this method is no-op.
     */
public Node start() throws NodeValidationException {
    if (!lifecycle.moveToStarted()) {
        return this;
    }
    Logger logger = Loggers.getLogger(Node.class, NODE_NAME_SETTING.get(settings));
    logger.info("starting ...");
    // hack around dependency injection problem (for now...)
    injector.getInstance(Discovery.class).setAllocationService(injector.getInstance(AllocationService.class));
    pluginLifecycleComponents.forEach(LifecycleComponent::start);
    injector.getInstance(MappingUpdatedAction.class).setClient(client);
    injector.getInstance(IndicesService.class).start();
    injector.getInstance(IndicesClusterStateService.class).start();
    injector.getInstance(SnapshotsService.class).start();
    injector.getInstance(SnapshotShardsService.class).start();
    injector.getInstance(RoutingService.class).start();
    injector.getInstance(SearchService.class).start();
    injector.getInstance(MonitorService.class).start();
    final ClusterService clusterService = injector.getInstance(ClusterService.class);
    final NodeConnectionsService nodeConnectionsService = injector.getInstance(NodeConnectionsService.class);
    nodeConnectionsService.start();
    clusterService.setNodeConnectionsService(nodeConnectionsService);
    // TODO hack around circular dependencies problems
    injector.getInstance(GatewayAllocator.class).setReallocation(clusterService, injector.getInstance(RoutingService.class));
    injector.getInstance(ResourceWatcherService.class).start();
    injector.getInstance(GatewayService.class).start();
    Discovery discovery = injector.getInstance(Discovery.class);
    clusterService.setDiscoverySettings(discovery.getDiscoverySettings());
    clusterService.addInitialStateBlock(discovery.getDiscoverySettings().getNoMasterBlock());
    clusterService.setClusterStatePublisher(discovery::publish);
    // start before the cluster service since it adds/removes initial Cluster state blocks
    final TribeService tribeService = injector.getInstance(TribeService.class);
    tribeService.start();
    // Start the transport service now so the publish address will be added to the local disco node in ClusterService
    TransportService transportService = injector.getInstance(TransportService.class);
    transportService.getTaskManager().setTaskResultsService(injector.getInstance(TaskResultsService.class));
    transportService.start();
    validateNodeBeforeAcceptingRequests(settings, transportService.boundAddress(), pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getBootstrapChecks().stream()).collect(Collectors.toList()));
    clusterService.addStateApplier(transportService.getTaskManager());
    clusterService.start();
    assert localNodeFactory.getNode() != null;
    assert transportService.getLocalNode().equals(localNodeFactory.getNode()) : "transportService has a different local node than the factory provided";
    assert clusterService.localNode().equals(localNodeFactory.getNode()) : "clusterService has a different local node than the factory provided";
    // start after cluster service so the local disco is known
    discovery.start();
    transportService.acceptIncomingRequests();
    discovery.startInitialJoin();
    // tribe nodes don't have a master so we shouldn't register an observer         s
    final TimeValue initialStateTimeout = DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings);
    if (initialStateTimeout.millis() > 0) {
        final ThreadPool thread = injector.getInstance(ThreadPool.class);
        ClusterState clusterState = clusterService.state();
        ClusterStateObserver observer = new ClusterStateObserver(clusterState, clusterService, null, logger, thread.getThreadContext());
        if (clusterState.nodes().getMasterNodeId() == null) {
            logger.debug("waiting to join the cluster. timeout [{}]", initialStateTimeout);
            final CountDownLatch latch = new CountDownLatch(1);
            observer.waitForNextChange(new ClusterStateObserver.Listener() {

                @Override
                public void onNewClusterState(ClusterState state) {
                    latch.countDown();
                }

                @Override
                public void onClusterServiceClose() {
                    latch.countDown();
                }

                @Override
                public void onTimeout(TimeValue timeout) {
                    logger.warn("timed out while waiting for initial discovery state - timeout: {}", initialStateTimeout);
                    latch.countDown();
                }
            }, state -> state.nodes().getMasterNodeId() != null, initialStateTimeout);
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new ElasticsearchTimeoutException("Interrupted while waiting for initial discovery state");
            }
        }
    }
    if (NetworkModule.HTTP_ENABLED.get(settings)) {
        injector.getInstance(HttpServerTransport.class).start();
    }
    // start nodes now, after the http server, because it may take some time
    tribeService.startNodes();
    // starts connecting to remote clusters if any cluster is configured
    SearchTransportService searchTransportService = injector.getInstance(SearchTransportService.class);
    searchTransportService.start();
    if (WRITE_PORTS_FIELD_SETTING.get(settings)) {
        if (NetworkModule.HTTP_ENABLED.get(settings)) {
            HttpServerTransport http = injector.getInstance(HttpServerTransport.class);
            writePortsFile("http", http.boundAddress());
        }
        TransportService transport = injector.getInstance(TransportService.class);
        writePortsFile("transport", transport.boundAddress());
    }
    logger.info("started");
    return this;
}
Also used : TribeService(org.elasticsearch.tribe.TribeService) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) SnapshotShardsService(org.elasticsearch.snapshots.SnapshotShardsService) NodeConnectionsService(org.elasticsearch.cluster.NodeConnectionsService) MonitorService(org.elasticsearch.monitor.MonitorService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) Logger(org.apache.logging.log4j.Logger) DeprecationLogger(org.elasticsearch.common.logging.DeprecationLogger) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) IndicesClusterStateService(org.elasticsearch.indices.cluster.IndicesClusterStateService) LifecycleComponent(org.elasticsearch.common.component.LifecycleComponent) SearchService(org.elasticsearch.search.SearchService) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) TimeValue(org.elasticsearch.common.unit.TimeValue) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) RoutingService(org.elasticsearch.cluster.routing.RoutingService) Discovery(org.elasticsearch.discovery.Discovery) IndicesService(org.elasticsearch.indices.IndicesService) CountDownLatch(java.util.concurrent.CountDownLatch) GatewayService(org.elasticsearch.gateway.GatewayService) ClusterService(org.elasticsearch.cluster.service.ClusterService) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) SearchTransportService(org.elasticsearch.action.search.SearchTransportService) TransportService(org.elasticsearch.transport.TransportService) MappingUpdatedAction(org.elasticsearch.cluster.action.index.MappingUpdatedAction) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService)

Example 7 with ClusterStateObserver

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

the class ShardStateAction method sendShardAction.

private void sendShardAction(final String actionName, final ClusterState currentState, final ShardEntry shardEntry, final Listener listener) {
    ClusterStateObserver observer = new ClusterStateObserver(currentState, clusterService, null, logger, threadPool.getThreadContext());
    DiscoveryNode masterNode = currentState.nodes().getMasterNode();
    Predicate<ClusterState> changePredicate = MasterNodeChangePredicate.build(currentState);
    if (masterNode == null) {
        logger.warn("{} no master known for action [{}] for shard entry [{}]", shardEntry.shardId, actionName, shardEntry);
        waitForNewMasterAndRetry(actionName, observer, shardEntry, listener, changePredicate);
    } else {
        logger.debug("{} sending [{}] to [{}] for shard entry [{}]", shardEntry.shardId, actionName, masterNode.getId(), shardEntry);
        transportService.sendRequest(masterNode, actionName, shardEntry, new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                listener.onSuccess();
            }

            @Override
            public void handleException(TransportException exp) {
                if (isMasterChannelException(exp)) {
                    waitForNewMasterAndRetry(actionName, observer, shardEntry, listener, changePredicate);
                } else {
                    logger.warn((Supplier<?>) () -> new ParameterizedMessage("{} unexpected failure while sending request [{}] to [{}] for shard entry [{}]", shardEntry.shardId, actionName, masterNode, shardEntry), exp);
                    listener.onFailure(exp instanceof RemoteTransportException ? (Exception) (exp.getCause() instanceof Exception ? exp.getCause() : new ElasticsearchException(exp.getCause())) : exp);
                }
            }
        });
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportResponse(org.elasticsearch.transport.TransportResponse) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchException(org.elasticsearch.ElasticsearchException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) EmptyTransportResponseHandler(org.elasticsearch.transport.EmptyTransportResponseHandler)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)7 ClusterStateObserver (org.elasticsearch.cluster.ClusterStateObserver)7 TimeValue (org.elasticsearch.common.unit.TimeValue)5 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)3 Supplier (org.apache.logging.log4j.util.Supplier)3 ClusterService (org.elasticsearch.cluster.service.ClusterService)3 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)3 NodeClosedException (org.elasticsearch.node.NodeClosedException)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Predicate (java.util.function.Predicate)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)2 ActionListener (org.elasticsearch.action.ActionListener)2 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)2 Settings (org.elasticsearch.common.settings.Settings)2 GatewayAllocator (org.elasticsearch.gateway.GatewayAllocator)2 ThreadPool (org.elasticsearch.threadpool.ThreadPool)2 ConnectTransportException (org.elasticsearch.transport.ConnectTransportException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1