Search in sources :

Example 21 with NodeValidationException

use of org.elasticsearch.node.NodeValidationException in project elasticsearch by elastic.

the class Bootstrap method init.

/**
     * This method is invoked by {@link Elasticsearch#main(String[])} to startup elasticsearch.
     */
static void init(final boolean foreground, final Path pidFile, final boolean quiet, final Environment initialEnv) throws BootstrapException, NodeValidationException, UserException {
    // Set the system property before anything has a chance to trigger its use
    initLoggerPrefix();
    // force the class initializer for BootstrapInfo to run before
    // the security manager is installed
    BootstrapInfo.init();
    INSTANCE = new Bootstrap();
    final SecureSettings keystore = loadSecureSettings(initialEnv);
    Environment environment = createEnvironment(foreground, pidFile, keystore, initialEnv.settings());
    try {
        LogConfigurator.configure(environment);
    } catch (IOException e) {
        throw new BootstrapException(e);
    }
    checkForCustomConfFile();
    if (environment.pidFile() != null) {
        try {
            PidFile.create(environment.pidFile(), true);
        } catch (IOException e) {
            throw new BootstrapException(e);
        }
    }
    final boolean closeStandardStreams = (foreground == false) || quiet;
    try {
        if (closeStandardStreams) {
            final Logger rootLogger = ESLoggerFactory.getRootLogger();
            final Appender maybeConsoleAppender = Loggers.findAppender(rootLogger, ConsoleAppender.class);
            if (maybeConsoleAppender != null) {
                Loggers.removeAppender(rootLogger, maybeConsoleAppender);
            }
            closeSystOut();
        }
        // fail if somebody replaced the lucene jars
        checkLucene();
        // install the default uncaught exception handler; must be done before security is
        // initialized as we do not want to grant the runtime permission
        // setDefaultUncaughtExceptionHandler
        Thread.setDefaultUncaughtExceptionHandler(new ElasticsearchUncaughtExceptionHandler(() -> Node.NODE_NAME_SETTING.get(environment.settings())));
        INSTANCE.setup(true, environment);
        /* TODO: close this once s3 repository doesn't try to read during repository construction
            try {
                // any secure settings must be read during node construction
                IOUtils.close(keystore);
            } catch (IOException e) {
                throw new BootstrapException(e);
            }*/
        INSTANCE.start();
        if (closeStandardStreams) {
            closeSysError();
        }
    } catch (NodeValidationException | RuntimeException e) {
        // disable console logging, so user does not see the exception twice (jvm will show it already)
        final Logger rootLogger = ESLoggerFactory.getRootLogger();
        final Appender maybeConsoleAppender = Loggers.findAppender(rootLogger, ConsoleAppender.class);
        if (foreground && maybeConsoleAppender != null) {
            Loggers.removeAppender(rootLogger, maybeConsoleAppender);
        }
        Logger logger = Loggers.getLogger(Bootstrap.class);
        if (INSTANCE.node != null) {
            logger = Loggers.getLogger(Bootstrap.class, Node.NODE_NAME_SETTING.get(INSTANCE.node.settings()));
        }
        // HACK, it sucks to do this, but we will run users out of disk space otherwise
        if (e instanceof CreationException) {
            // guice: log the shortened exc to the log file
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            PrintStream ps = null;
            try {
                ps = new PrintStream(os, false, "UTF-8");
            } catch (UnsupportedEncodingException uee) {
                assert false;
                e.addSuppressed(uee);
            }
            new StartupException(e).printStackTrace(ps);
            ps.flush();
            try {
                logger.error("Guice Exception: {}", os.toString("UTF-8"));
            } catch (UnsupportedEncodingException uee) {
                assert false;
                e.addSuppressed(uee);
            }
        } else if (e instanceof NodeValidationException) {
            logger.error("node validation exception\n{}", e.getMessage());
        } else {
            // full exception
            logger.error("Exception", e);
        }
        // re-enable it if appropriate, so they can see any logging during the shutdown process
        if (foreground && maybeConsoleAppender != null) {
            Loggers.addAppender(rootLogger, maybeConsoleAppender);
        }
        throw e;
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) PrintStream(java.io.PrintStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CreationException(org.elasticsearch.common.inject.CreationException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.apache.logging.log4j.Logger) NodeValidationException(org.elasticsearch.node.NodeValidationException) SecureSettings(org.elasticsearch.common.settings.SecureSettings) Environment(org.elasticsearch.env.Environment)

Example 22 with NodeValidationException

use of org.elasticsearch.node.NodeValidationException in project elasticsearch by elastic.

the class Elasticsearch method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws UserException {
    if (options.nonOptionArguments().isEmpty() == false) {
        throw new UserException(ExitCodes.USAGE, "Positional arguments not allowed, found " + options.nonOptionArguments());
    }
    if (options.has(versionOption)) {
        if (options.has(daemonizeOption) || options.has(pidfileOption)) {
            throw new UserException(ExitCodes.USAGE, "Elasticsearch version option is mutually exclusive with any other option");
        }
        terminal.println("Version: " + org.elasticsearch.Version.CURRENT + ", Build: " + Build.CURRENT.shortHash() + "/" + Build.CURRENT.date() + ", JVM: " + JvmInfo.jvmInfo().version());
        return;
    }
    final boolean daemonize = options.has(daemonizeOption);
    final Path pidFile = pidfileOption.value(options);
    final boolean quiet = options.has(quietOption);
    try {
        init(daemonize, pidFile, quiet, env);
    } catch (NodeValidationException e) {
        throw new UserException(ExitCodes.CONFIG, e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) NodeValidationException(org.elasticsearch.node.NodeValidationException) UserException(org.elasticsearch.cli.UserException)

Example 23 with NodeValidationException

use of org.elasticsearch.node.NodeValidationException in project elasticsearch by elastic.

the class BootstrapChecksTests method testMaxNumberOfThreadsCheck.

public void testMaxNumberOfThreadsCheck() throws NodeValidationException {
    final int limit = 1 << 11;
    final AtomicLong maxNumberOfThreads = new AtomicLong(randomIntBetween(1, limit - 1));
    final BootstrapChecks.MaxNumberOfThreadsCheck check = new BootstrapChecks.MaxNumberOfThreadsCheck() {

        @Override
        long getMaxNumberOfThreads() {
            return maxNumberOfThreads.get();
        }
    };
    final NodeValidationException e = expectThrows(NodeValidationException.class, () -> BootstrapChecks.check(true, Collections.singletonList(check), "testMaxNumberOfThreadsCheck"));
    assertThat(e.getMessage(), containsString("max number of threads"));
    maxNumberOfThreads.set(randomIntBetween(limit + 1, Integer.MAX_VALUE));
    BootstrapChecks.check(true, Collections.singletonList(check), "testMaxNumberOfThreadsCheck");
    // nothing should happen if current max number of threads is
    // not available
    maxNumberOfThreads.set(-1);
    BootstrapChecks.check(true, Collections.singletonList(check), "testMaxNumberOfThreadsCheck");
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) NodeValidationException(org.elasticsearch.node.NodeValidationException)

Example 24 with NodeValidationException

use of org.elasticsearch.node.NodeValidationException in project elasticsearch by elastic.

the class BootstrapChecksTests method testG1GCCheck.

public void testG1GCCheck() throws NodeValidationException {
    final AtomicBoolean isG1GCEnabled = new AtomicBoolean(true);
    final AtomicBoolean isJava8 = new AtomicBoolean(true);
    final AtomicReference<String> jvmVersion = new AtomicReference<>(String.format(Locale.ROOT, "25.%d-b%d", randomIntBetween(0, 39), randomIntBetween(1, 128)));
    final BootstrapChecks.G1GCCheck oracleCheck = new BootstrapChecks.G1GCCheck() {

        @Override
        String jvmVendor() {
            return "Oracle Corporation";
        }

        @Override
        boolean isG1GCEnabled() {
            return isG1GCEnabled.get();
        }

        @Override
        String jvmVersion() {
            return jvmVersion.get();
        }

        @Override
        boolean isJava8() {
            return isJava8.get();
        }
    };
    final NodeValidationException e = expectThrows(NodeValidationException.class, () -> BootstrapChecks.check(true, Collections.singletonList(oracleCheck), "testG1GCCheck"));
    assertThat(e.getMessage(), containsString("JVM version [" + jvmVersion.get() + "] can cause data corruption when used with G1GC; upgrade to at least Java 8u40"));
    // if G1GC is disabled, nothing should happen
    isG1GCEnabled.set(false);
    BootstrapChecks.check(true, Collections.singletonList(oracleCheck), "testG1GCCheck");
    // if on or after update 40, nothing should happen independent of whether or not G1GC is enabled
    isG1GCEnabled.set(randomBoolean());
    jvmVersion.set(String.format(Locale.ROOT, "25.%d-b%d", randomIntBetween(40, 112), randomIntBetween(1, 128)));
    BootstrapChecks.check(true, Collections.singletonList(oracleCheck), "testG1GCCheck");
    final BootstrapChecks.G1GCCheck nonOracleCheck = new BootstrapChecks.G1GCCheck() {

        @Override
        String jvmVendor() {
            return randomAsciiOfLength(8);
        }
    };
    // if not on an Oracle JVM, nothing should happen
    BootstrapChecks.check(true, Collections.singletonList(nonOracleCheck), "testG1GCCheck");
    final BootstrapChecks.G1GCCheck nonJava8Check = new BootstrapChecks.G1GCCheck() {

        @Override
        boolean isJava8() {
            return false;
        }
    };
    // if not Java 8, nothing should happen
    BootstrapChecks.check(true, Collections.singletonList(nonJava8Check), "testG1GCCheck");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeValidationException(org.elasticsearch.node.NodeValidationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 25 with NodeValidationException

use of org.elasticsearch.node.NodeValidationException in project elasticsearch by elastic.

the class BootstrapChecksTests method testMlockallCheck.

public void testMlockallCheck() throws NodeValidationException {
    class MlockallCheckTestCase {

        private final boolean mlockallSet;

        private final boolean isMemoryLocked;

        private final boolean shouldFail;

        MlockallCheckTestCase(final boolean mlockallSet, final boolean isMemoryLocked, final boolean shouldFail) {
            this.mlockallSet = mlockallSet;
            this.isMemoryLocked = isMemoryLocked;
            this.shouldFail = shouldFail;
        }
    }
    final List<MlockallCheckTestCase> testCases = new ArrayList<>();
    testCases.add(new MlockallCheckTestCase(true, true, false));
    testCases.add(new MlockallCheckTestCase(true, false, true));
    testCases.add(new MlockallCheckTestCase(false, true, false));
    testCases.add(new MlockallCheckTestCase(false, false, false));
    for (final MlockallCheckTestCase testCase : testCases) {
        final BootstrapChecks.MlockallCheck check = new BootstrapChecks.MlockallCheck(testCase.mlockallSet) {

            @Override
            boolean isMemoryLocked() {
                return testCase.isMemoryLocked;
            }
        };
        if (testCase.shouldFail) {
            final NodeValidationException e = expectThrows(NodeValidationException.class, () -> BootstrapChecks.check(true, Collections.singletonList(check), "testFileDescriptorLimitsThrowsOnInvalidLimit"));
            assertThat(e.getMessage(), containsString("memory locking requested for elasticsearch process but memory is not locked"));
        } else {
            // nothing should happen
            BootstrapChecks.check(true, Collections.singletonList(check), "testFileDescriptorLimitsThrowsOnInvalidLimit");
        }
    }
}
Also used : NodeValidationException(org.elasticsearch.node.NodeValidationException) ArrayList(java.util.ArrayList)

Aggregations

NodeValidationException (org.elasticsearch.node.NodeValidationException)33 IOException (java.io.IOException)8 Settings (org.elasticsearch.common.settings.Settings)8 Node (org.elasticsearch.node.Node)8 Matchers.hasToString (org.hamcrest.Matchers.hasToString)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 ArrayList (java.util.ArrayList)4 Logger (org.apache.logging.log4j.Logger)4 BeforeClass (org.junit.BeforeClass)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Path (java.nio.file.Path)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Plugin (org.elasticsearch.plugins.Plugin)3 Netty4Plugin (org.elasticsearch.transport.Netty4Plugin)3 BootstrapException (io.crate.bootstrap.BootstrapException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 PrintStream (java.io.PrintStream)2 URISyntaxException (java.net.URISyntaxException)2