Search in sources :

Example 1 with BootstrapException

use of io.crate.bootstrap.BootstrapException in project crate by crate.

the class BootstrapProxy method init.

/**
 * This method is invoked by {@link io.crate.bootstrap.CrateDB#main(String[])} to start CrateDB.
 */
public static void init(final boolean foreground, final boolean quiet, Environment environment) throws BootstrapException, NodeValidationException, UserException {
    // force the class initializer for BootstrapInfo to run before
    // the security manager is installed
    BootstrapInfo.init();
    INSTANCE = new BootstrapProxy();
    LogConfigurator.setNodeName(Node.NODE_NAME_SETTING.get(environment.settings()));
    try {
        LogConfigurator.configure(environment);
    } catch (IOException e) {
        throw new BootstrapException(e);
    }
    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 = LogManager.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());
        INSTANCE.setup(true, environment);
        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 = LogManager.getRootLogger();
        final Appender maybeConsoleAppender = Loggers.findAppender(rootLogger, ConsoleAppender.class);
        if (foreground && maybeConsoleAppender != null) {
            Loggers.removeAppender(rootLogger, maybeConsoleAppender);
        }
        Logger logger = LogManager.getLogger(BootstrapProxy.class);
        // 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;
            ps = new PrintStream(os, false, StandardCharsets.UTF_8);
            new StartupException(e).printStackTrace(ps);
            ps.flush();
            logger.error("Guice Exception: {}", os.toString(StandardCharsets.UTF_8));
        } 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) 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) BootstrapException(io.crate.bootstrap.BootstrapException)

Example 2 with BootstrapException

use of io.crate.bootstrap.BootstrapException in project crate by crate.

the class BootstrapProxy method setup.

private void setup(boolean addShutdownHook, Environment environment) throws BootstrapException {
    Settings settings = environment.settings();
    initializeNatives(BootstrapSettings.MEMORY_LOCK_SETTING.get(settings), BootstrapSettings.CTRLHANDLER_SETTING.get(settings));
    // initialize probes before the security manager is installed
    initializeProbes();
    if (addShutdownHook) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                IOUtils.close(node);
                LoggerContext context = (LoggerContext) LogManager.getContext(false);
                Configurator.shutdown(context);
                if (node != null && node.awaitClose(10, TimeUnit.SECONDS) == false) {
                    throw new IllegalStateException("Node didn't stop within 10 seconds. " + "Any outstanding requests or tasks might get killed.");
                }
            } catch (IOException ex) {
                throw new ElasticsearchException("failed to stop node", ex);
            } catch (InterruptedException e) {
                LogManager.getLogger(BootstrapProxy.class).warn("Thread got interrupted while waiting for the node to shutdown.");
                Thread.currentThread().interrupt();
            }
        }));
    }
    try {
        // look for jar hell
        final Logger logger = LogManager.getLogger(JarHell.class);
        JarHell.checkJarHell(logger::debug);
    } catch (IOException | URISyntaxException e) {
        throw new BootstrapException(e);
    }
    IfConfig.logIfNecessary();
    node = new CrateNode(environment) {

        @Override
        protected void validateNodeBeforeAcceptingRequests(BoundTransportAddress boundTransportAddress, List<BootstrapCheck> bootstrapChecks) throws NodeValidationException {
            BootstrapChecks.check(settings, boundTransportAddress, bootstrapChecks);
        }
    };
}
Also used : IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) URISyntaxException(java.net.URISyntaxException) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) NodeValidationException(org.elasticsearch.node.NodeValidationException) BootstrapException(io.crate.bootstrap.BootstrapException) CrateNode(io.crate.node.CrateNode) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

BootstrapException (io.crate.bootstrap.BootstrapException)2 IOException (java.io.IOException)2 Logger (org.apache.logging.log4j.Logger)2 NodeValidationException (org.elasticsearch.node.NodeValidationException)2 CrateNode (io.crate.node.CrateNode)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 URISyntaxException (java.net.URISyntaxException)1 Appender (org.apache.logging.log4j.core.Appender)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 CreationException (org.elasticsearch.common.inject.CreationException)1 Settings (org.elasticsearch.common.settings.Settings)1 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)1