Search in sources :

Example 1 with ClusterRunnerNode

use of org.codelibs.elasticsearch.runner.node.ClusterRunnerNode in project elasticsearch-cluster-runner by codelibs.

the class ElasticsearchClusterRunner method execute.

protected void execute(final int id) {
    final String nodeName = "Node " + id;
    final Path homePath = Paths.get(basePath, nodeName.replace(' ', '_').toLowerCase(Locale.ROOT));
    final Path confPath = this.confPath == null ? homePath.resolve(CONFIG_DIR) : Paths.get(this.confPath);
    final Path logsPath = this.logsPath == null ? homePath.resolve(LOGS_DIR) : Paths.get(this.logsPath);
    final Path dataPath = this.dataPath == null ? homePath.resolve(DATA_DIR) : Paths.get(this.dataPath);
    createDir(homePath);
    createDir(confPath);
    createDir(logsPath);
    createDir(dataPath);
    final Settings.Builder builder = builder();
    if (settingsBuilder != null) {
        settingsBuilder.build(id, builder);
    }
    putIfAbsent(builder, "path.home", homePath.toAbsolutePath().toString());
    putIfAbsent(builder, "path.data", dataPath.toAbsolutePath().toString());
    putIfAbsent(builder, "path.logs", logsPath.toAbsolutePath().toString());
    final Path esConfPath = confPath.resolve(ELASTICSEARCH_YAML);
    if (!esConfPath.toFile().exists()) {
        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_DIR + "/" + ELASTICSEARCH_YAML)) {
            Files.copy(is, esConfPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (final IOException e) {
            throw new ClusterRunnerException("Could not create: " + esConfPath, e);
        }
    }
    if (!disableESLogger) {
        final Path logConfPath = confPath.resolve(LOG4J2_PROPERTIES);
        if (!logConfPath.toFile().exists()) {
            try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_DIR + "/" + LOG4J2_PROPERTIES)) {
                Files.copy(is, logConfPath, StandardCopyOption.REPLACE_EXISTING);
            } catch (final IOException e) {
                throw new ClusterRunnerException("Could not create: " + logConfPath, e);
            }
        }
    }
    try {
        final String pluginPath = builder.get("path.plugins");
        if (pluginPath != null) {
            final Path sourcePath = Paths.get(pluginPath);
            final Path targetPath = homePath.resolve("plugins");
            Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
                    Files.createDirectories(targetPath.resolve(sourcePath.relativize(dir)));
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    Files.copy(file, targetPath.resolve(sourcePath.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
                    return FileVisitResult.CONTINUE;
                }
            });
            builder.remove("path.plugins");
        }
        final int httpPort = getAvailableHttpPort(id);
        putIfAbsent(builder, "cluster.name", clusterName);
        putIfAbsent(builder, NODE_NAME, nodeName);
        putIfAbsent(builder, HTTP_PORT, String.valueOf(httpPort));
        putIfAbsent(builder, "index.store.type", indexStoreType);
        if (!builder.keys().contains("node.roles")) {
            builder.putList("node.roles", "master", "data");
        }
        print("Node Name:      " + builder.get(NODE_NAME));
        print("HTTP Port:      " + builder.get(HTTP_PORT));
        print("Data Directory: " + dataPath);
        print("Log Directory:  " + logsPath);
        final Settings settings = builder.build();
        final Environment environment = InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), confPath, () -> nodeName);
        if (!disableESLogger) {
            LogConfigurator.registerErrorListener();
            final String envNodeName = Node.NODE_NAME_SETTING.get(environment.settings());
            try {
                LogConfigurator.setNodeName(envNodeName);
            } catch (final IllegalStateException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to set {} to a log configuration.", envNodeName, e);
                }
            }
            LogConfigurator.configure(environment);
        }
        createDir(environment.modulesFile());
        createDir(environment.pluginsFile());
        final Node node = new ClusterRunnerNode(environment, pluginList);
        node.start();
        nodeList.add(node);
        envList.add(environment);
    } catch (final Exception e) {
        throw new ClusterRunnerException("Failed to start node " + id, e);
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ClusterRunnerNode(org.codelibs.elasticsearch.runner.node.ClusterRunnerNode) Node(org.elasticsearch.node.Node) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) NodeValidationException(org.elasticsearch.node.NodeValidationException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) CmdLineException(org.kohsuke.args4j.CmdLineException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) Environment(org.elasticsearch.env.Environment) ClusterRunnerNode(org.codelibs.elasticsearch.runner.node.ClusterRunnerNode) Settings(org.elasticsearch.common.settings.Settings) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with ClusterRunnerNode

use of org.codelibs.elasticsearch.runner.node.ClusterRunnerNode in project elasticsearch-cluster-runner by codelibs.

the class ElasticsearchClusterRunner method startNode.

/**
 * Start a closed node.
 *
 * @param i the number of nodes
 * @return true if the node is started.
 */
public boolean startNode(final int i) {
    if (i >= nodeList.size()) {
        return false;
    }
    if (!nodeList.get(i).isClosed()) {
        return false;
    }
    final Node node = new ClusterRunnerNode(envList.get(i), pluginList);
    try {
        node.start();
        nodeList.set(i, node);
        return true;
    } catch (final NodeValidationException e) {
        print(e.getLocalizedMessage());
    }
    return false;
}
Also used : NodeValidationException(org.elasticsearch.node.NodeValidationException) ClusterRunnerNode(org.codelibs.elasticsearch.runner.node.ClusterRunnerNode) Node(org.elasticsearch.node.Node) ClusterRunnerNode(org.codelibs.elasticsearch.runner.node.ClusterRunnerNode)

Aggregations

ClusterRunnerNode (org.codelibs.elasticsearch.runner.node.ClusterRunnerNode)2 Node (org.elasticsearch.node.Node)2 NodeValidationException (org.elasticsearch.node.NodeValidationException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ConnectException (java.net.ConnectException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)1 Settings (org.elasticsearch.common.settings.Settings)1 Environment (org.elasticsearch.env.Environment)1 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1