Search in sources :

Example 1 with LifecycleComponent

use of org.opensearch.common.component.LifecycleComponent in project OpenSearch by opensearch-project.

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.info("starting ...");
    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(RepositoriesService.class).start();
    injector.getInstance(SearchService.class).start();
    injector.getInstance(FsHealthService.class).start();
    nodeService.getMonitorService().start();
    final ClusterService clusterService = injector.getInstance(ClusterService.class);
    final NodeConnectionsService nodeConnectionsService = injector.getInstance(NodeConnectionsService.class);
    nodeConnectionsService.start();
    clusterService.setNodeConnectionsService(nodeConnectionsService);
    injector.getInstance(GatewayService.class).start();
    Discovery discovery = injector.getInstance(Discovery.class);
    clusterService.getMasterService().setClusterStatePublisher(discovery::publish);
    // 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.getTaskManager().setTaskCancellationService(new TaskCancellationService(transportService));
    transportService.start();
    assert localNodeFactory.getNode() != null;
    assert transportService.getLocalNode().equals(localNodeFactory.getNode()) : "transportService has a different local node than the factory provided";
    injector.getInstance(PeerRecoverySourceService.class).start();
    // Load (and maybe upgrade) the metadata stored on disk
    final GatewayMetaState gatewayMetaState = injector.getInstance(GatewayMetaState.class);
    gatewayMetaState.start(settings(), transportService, clusterService, injector.getInstance(MetaStateService.class), injector.getInstance(MetadataIndexUpgradeService.class), injector.getInstance(MetadataUpgrader.class), injector.getInstance(PersistedClusterStateService.class));
    if (Assertions.ENABLED) {
        try {
            assert injector.getInstance(MetaStateService.class).loadFullState().v1().isEmpty();
            final NodeMetadata nodeMetadata = NodeMetadata.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnvironment.nodeDataPaths());
            assert nodeMetadata != null;
            assert nodeMetadata.nodeVersion().equals(Version.CURRENT);
            assert nodeMetadata.nodeId().equals(localNodeFactory.getNode().getId());
        } catch (IOException e) {
            assert false : e;
        }
    }
    // we load the global state here (the persistent part of the cluster state stored on disk) to
    // pass it to the bootstrap checks to allow plugins to enforce certain preconditions based on the recovered state.
    final Metadata onDiskMetadata = gatewayMetaState.getPersistedState().getLastAcceptedState().metadata();
    // this is never null
    assert onDiskMetadata != null : "metadata is null but shouldn't";
    validateNodeBeforeAcceptingRequests(new BootstrapContext(environment, onDiskMetadata), transportService.boundAddress(), pluginsService.filterPlugins(Plugin.class).stream().flatMap(p -> p.getBootstrapChecks().stream()).collect(Collectors.toList()));
    clusterService.addStateApplier(transportService.getTaskManager());
    // start after transport service so the local disco is known
    // start before cluster service so that it can set initial state on ClusterApplierService
    discovery.start();
    clusterService.start();
    assert clusterService.localNode().equals(localNodeFactory.getNode()) : "clusterService has a different local node than the factory provided";
    transportService.acceptIncomingRequests();
    discovery.startInitialJoin();
    final TimeValue initialStateTimeout = DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.get(settings());
    configureNodeAndClusterIdStateListener(clusterService);
    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 OpenSearchTimeoutException("Interrupted while waiting for initial discovery state");
            }
        }
    }
    injector.getInstance(HttpServerTransport.class).start();
    if (WRITE_PORTS_FILE_SETTING.get(settings())) {
        TransportService transport = injector.getInstance(TransportService.class);
        writePortsFile("transport", transport.boundAddress());
        HttpServerTransport http = injector.getInstance(HttpServerTransport.class);
        writePortsFile("http", http.boundAddress());
    }
    logger.info("started");
    pluginsService.filterPlugins(ClusterPlugin.class).forEach(ClusterPlugin::onNodeStarted);
    return this;
}
Also used : OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) SnapshotsService(org.opensearch.snapshots.SnapshotsService) SnapshotShardsService(org.opensearch.snapshots.SnapshotShardsService) NodeConnectionsService(org.opensearch.cluster.NodeConnectionsService) Metadata(org.opensearch.cluster.metadata.Metadata) NodeMetadata(org.opensearch.env.NodeMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) MetadataUpgrader(org.opensearch.plugins.MetadataUpgrader) BootstrapContext(org.opensearch.bootstrap.BootstrapContext) TaskResultsService(org.opensearch.tasks.TaskResultsService) HttpServerTransport(org.opensearch.http.HttpServerTransport) GatewayMetaState(org.opensearch.gateway.GatewayMetaState) MetaStateService(org.opensearch.gateway.MetaStateService) IndicesClusterStateService(org.opensearch.indices.cluster.IndicesClusterStateService) LifecycleComponent(org.opensearch.common.component.LifecycleComponent) SearchService(org.opensearch.search.SearchService) PeerRecoverySourceService(org.opensearch.indices.recovery.PeerRecoverySourceService) TimeValue(org.opensearch.common.unit.TimeValue) FsHealthService(org.opensearch.monitor.fs.FsHealthService) ClusterState(org.opensearch.cluster.ClusterState) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) ClusterPlugin(org.opensearch.plugins.ClusterPlugin) Discovery(org.opensearch.discovery.Discovery) IndicesService(org.opensearch.indices.IndicesService) MetadataIndexUpgradeService(org.opensearch.cluster.metadata.MetadataIndexUpgradeService) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) GatewayService(org.opensearch.gateway.GatewayService) NodeMetadata(org.opensearch.env.NodeMetadata) ClusterService(org.opensearch.cluster.service.ClusterService) PersistentTasksClusterService(org.opensearch.persistent.PersistentTasksClusterService) RemoteClusterService(org.opensearch.transport.RemoteClusterService) TransportService(org.opensearch.transport.TransportService) SearchTransportService(org.opensearch.action.search.SearchTransportService) RepositoriesService(org.opensearch.repositories.RepositoriesService) MappingUpdatedAction(org.opensearch.cluster.action.index.MappingUpdatedAction) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService) TaskCancellationService(org.opensearch.tasks.TaskCancellationService)

Example 2 with LifecycleComponent

use of org.opensearch.common.component.LifecycleComponent in project OpenSearch by opensearch-project.

the class Node method close.

// During concurrent close() calls we want to make sure that all of them return after the node has completed it's shutdown cycle.
// If not, the hook that is added in Bootstrap#setup() will be useless:
// close() might not be executed, in case another (for example api) call to close() has already set some lifecycles to stopped.
// In this case the process will be terminated even if the first call to close() has not finished yet.
@Override
public synchronized void close() throws IOException {
    synchronized (lifecycle) {
        if (lifecycle.started()) {
            stop();
        }
        if (!lifecycle.moveToClosed()) {
            return;
        }
    }
    logger.info("closing ...");
    List<Closeable> toClose = new ArrayList<>();
    StopWatch stopWatch = new StopWatch("node_close");
    toClose.add(() -> stopWatch.start("node_service"));
    toClose.add(nodeService);
    toClose.add(() -> stopWatch.stop().start("http"));
    toClose.add(injector.getInstance(HttpServerTransport.class));
    toClose.add(() -> stopWatch.stop().start("snapshot_service"));
    toClose.add(injector.getInstance(SnapshotsService.class));
    toClose.add(injector.getInstance(SnapshotShardsService.class));
    toClose.add(injector.getInstance(RepositoriesService.class));
    toClose.add(() -> stopWatch.stop().start("client"));
    Releasables.close(injector.getInstance(Client.class));
    toClose.add(() -> stopWatch.stop().start("indices_cluster"));
    toClose.add(injector.getInstance(IndicesClusterStateService.class));
    toClose.add(() -> stopWatch.stop().start("indices"));
    toClose.add(injector.getInstance(IndicesService.class));
    // close filter/fielddata caches after indices
    toClose.add(injector.getInstance(IndicesStore.class));
    toClose.add(injector.getInstance(PeerRecoverySourceService.class));
    toClose.add(() -> stopWatch.stop().start("cluster"));
    toClose.add(injector.getInstance(ClusterService.class));
    toClose.add(() -> stopWatch.stop().start("node_connections_service"));
    toClose.add(injector.getInstance(NodeConnectionsService.class));
    toClose.add(() -> stopWatch.stop().start("discovery"));
    toClose.add(injector.getInstance(Discovery.class));
    toClose.add(() -> stopWatch.stop().start("monitor"));
    toClose.add(nodeService.getMonitorService());
    toClose.add(() -> stopWatch.stop().start("fsHealth"));
    toClose.add(injector.getInstance(FsHealthService.class));
    toClose.add(() -> stopWatch.stop().start("gateway"));
    toClose.add(injector.getInstance(GatewayService.class));
    toClose.add(() -> stopWatch.stop().start("search"));
    toClose.add(injector.getInstance(SearchService.class));
    toClose.add(() -> stopWatch.stop().start("transport"));
    toClose.add(injector.getInstance(TransportService.class));
    for (LifecycleComponent plugin : pluginLifecycleComponents) {
        toClose.add(() -> stopWatch.stop().start("plugin(" + plugin.getClass().getName() + ")"));
        toClose.add(plugin);
    }
    toClose.addAll(pluginsService.filterPlugins(Plugin.class));
    toClose.add(() -> stopWatch.stop().start("script"));
    toClose.add(injector.getInstance(ScriptService.class));
    toClose.add(() -> stopWatch.stop().start("thread_pool"));
    toClose.add(() -> injector.getInstance(ThreadPool.class).shutdown());
    // Don't call shutdownNow here, it might break ongoing operations on Lucene indices.
    // See https://issues.apache.org/jira/browse/LUCENE-7248. We call shutdownNow in
    // awaitClose if the node doesn't finish closing within the specified time.
    toClose.add(() -> stopWatch.stop().start("gateway_meta_state"));
    toClose.add(injector.getInstance(GatewayMetaState.class));
    toClose.add(() -> stopWatch.stop().start("node_environment"));
    toClose.add(injector.getInstance(NodeEnvironment.class));
    toClose.add(stopWatch::stop);
    if (logger.isTraceEnabled()) {
        toClose.add(() -> logger.trace("Close times for each service:\n{}", stopWatch.prettyPrint()));
    }
    IOUtils.close(toClose);
    logger.info("closed");
}
Also used : SnapshotsService(org.opensearch.snapshots.SnapshotsService) SnapshotShardsService(org.opensearch.snapshots.SnapshotShardsService) NodeConnectionsService(org.opensearch.cluster.NodeConnectionsService) NodeEnvironment(org.opensearch.env.NodeEnvironment) Closeable(java.io.Closeable) IndicesStore(org.opensearch.indices.store.IndicesStore) ArrayList(java.util.ArrayList) HttpServerTransport(org.opensearch.http.HttpServerTransport) GatewayMetaState(org.opensearch.gateway.GatewayMetaState) ScriptService(org.opensearch.script.ScriptService) IndicesClusterStateService(org.opensearch.indices.cluster.IndicesClusterStateService) LifecycleComponent(org.opensearch.common.component.LifecycleComponent) SearchService(org.opensearch.search.SearchService) PeerRecoverySourceService(org.opensearch.indices.recovery.PeerRecoverySourceService) Client(org.opensearch.client.Client) NodeClient(org.opensearch.client.node.NodeClient) FsHealthService(org.opensearch.monitor.fs.FsHealthService) Discovery(org.opensearch.discovery.Discovery) IndicesService(org.opensearch.indices.IndicesService) StopWatch(org.opensearch.common.StopWatch) GatewayService(org.opensearch.gateway.GatewayService) ClusterService(org.opensearch.cluster.service.ClusterService) PersistentTasksClusterService(org.opensearch.persistent.PersistentTasksClusterService) RemoteClusterService(org.opensearch.transport.RemoteClusterService) TransportService(org.opensearch.transport.TransportService) SearchTransportService(org.opensearch.action.search.SearchTransportService) RepositoriesService(org.opensearch.repositories.RepositoriesService) ActionPlugin(org.opensearch.plugins.ActionPlugin) SystemIndexPlugin(org.opensearch.plugins.SystemIndexPlugin) CircuitBreakerPlugin(org.opensearch.plugins.CircuitBreakerPlugin) EnginePlugin(org.opensearch.plugins.EnginePlugin) DiscoveryPlugin(org.opensearch.plugins.DiscoveryPlugin) RepositoryPlugin(org.opensearch.plugins.RepositoryPlugin) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) SearchPlugin(org.opensearch.plugins.SearchPlugin) Plugin(org.opensearch.plugins.Plugin) IngestPlugin(org.opensearch.plugins.IngestPlugin) IndexStorePlugin(org.opensearch.plugins.IndexStorePlugin) ScriptPlugin(org.opensearch.plugins.ScriptPlugin) ClusterPlugin(org.opensearch.plugins.ClusterPlugin) MapperPlugin(org.opensearch.plugins.MapperPlugin) PersistentTaskPlugin(org.opensearch.plugins.PersistentTaskPlugin) AnalysisPlugin(org.opensearch.plugins.AnalysisPlugin)

Aggregations

SearchTransportService (org.opensearch.action.search.SearchTransportService)2 NodeConnectionsService (org.opensearch.cluster.NodeConnectionsService)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 LifecycleComponent (org.opensearch.common.component.LifecycleComponent)2 Discovery (org.opensearch.discovery.Discovery)2 GatewayMetaState (org.opensearch.gateway.GatewayMetaState)2 GatewayService (org.opensearch.gateway.GatewayService)2 HttpServerTransport (org.opensearch.http.HttpServerTransport)2 IndicesService (org.opensearch.indices.IndicesService)2 IndicesClusterStateService (org.opensearch.indices.cluster.IndicesClusterStateService)2 PeerRecoverySourceService (org.opensearch.indices.recovery.PeerRecoverySourceService)2 FsHealthService (org.opensearch.monitor.fs.FsHealthService)2 PersistentTasksClusterService (org.opensearch.persistent.PersistentTasksClusterService)2 ClusterPlugin (org.opensearch.plugins.ClusterPlugin)2 RepositoriesService (org.opensearch.repositories.RepositoriesService)2 SearchService (org.opensearch.search.SearchService)2 SnapshotShardsService (org.opensearch.snapshots.SnapshotShardsService)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1