Search in sources :

Example 1 with LifecycleListener

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

the class InternalTestCluster method buildNode.

/**
 * builds a new node
 *
 * @param nodeId                    node ordinal
 * @param settings                  the settings to use
 * @param reuseExisting             if a node with the same name is already part of {@link #nodes}, no new node will be built and
 *                                  the method will return the existing one
 * @param onTransportServiceStarted callback to run when transport service is started
 */
private synchronized NodeAndClient buildNode(int nodeId, Settings settings, boolean reuseExisting, Runnable onTransportServiceStarted) {
    assert Thread.holdsLock(this);
    ensureOpen();
    Collection<Class<? extends Plugin>> plugins = getPlugins();
    String name = settings.get("node.name");
    final NodeAndClient nodeAndClient = nodes.get(name);
    if (reuseExisting && nodeAndClient != null) {
        // reusing an existing node implies its transport service already started
        onTransportServiceStarted.run();
        return nodeAndClient;
    }
    assert reuseExisting || nodeAndClient == null : "node name [" + name + "] already exists but not allowed to use it";
    SecureSettings secureSettings = Settings.builder().put(settings).getSecureSettings();
    if (secureSettings instanceof MockSecureSettings) {
        // we clone this here since in the case of a node restart we might need it again
        secureSettings = ((MockSecureSettings) secureSettings).clone();
    }
    MockNode node = new MockNode(settings, plugins, nodeConfigurationSource.nodeConfigPath(nodeId), forbidPrivateIndexSettings);
    node.injector().getInstance(TransportService.class).addLifecycleListener(new LifecycleListener() {

        @Override
        public void afterStart() {
            onTransportServiceStarted.run();
        }
    });
    try {
        IOUtils.close(secureSettings);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return new NodeAndClient(name, node, settings, nodeId);
}
Also used : MockNode(org.opensearch.node.MockNode) UncheckedIOException(java.io.UncheckedIOException) LifecycleListener(org.opensearch.common.component.LifecycleListener) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) TransportService(org.opensearch.transport.TransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) SecureSettings(org.opensearch.common.settings.SecureSettings) Plugin(org.opensearch.plugins.Plugin)

Aggregations

IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 LifecycleListener (org.opensearch.common.component.LifecycleListener)1 MockSecureSettings (org.opensearch.common.settings.MockSecureSettings)1 SecureSettings (org.opensearch.common.settings.SecureSettings)1 MockNode (org.opensearch.node.MockNode)1 Plugin (org.opensearch.plugins.Plugin)1 MockTransportService (org.opensearch.test.transport.MockTransportService)1 TransportService (org.opensearch.transport.TransportService)1