From 5f9edd26eaa138acbb43196a7c952b43820a5d39 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 31 Jul 2015 14:00:18 -0400 Subject: [PATCH] Fix Bootstrap to not call System.exit Its not going to work: its blocked by security policy and will just add a confusing SecurityException to the mix, and bogusly give an exit status of 0 when in fact something bad happened. Finally, if ES can't startup, it is a serious problem, there is no sense in hiding the reason why: deliver the full stack trace. --- .../elasticsearch/bootstrap/Bootstrap.java | 71 +++---------------- .../bootstrap/Elasticsearch.java | 2 +- .../bootstrap/ElasticsearchF.java | 2 +- 3 files changed, 11 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index d110942388f..3b97749c084 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -222,7 +222,7 @@ public class Bootstrap { } } - public static void main(String[] args) { + public static void main(String[] args) throws Exception { BootstrapCLIParser bootstrapCLIParser = new BootstrapCLIParser(); CliTool.ExitStatus status = bootstrapCLIParser.execute(args); @@ -239,28 +239,16 @@ public class Bootstrap { foreground = false; } - String stage = "Settings"; + Tuple tuple = initialSettings(foreground); + Settings settings = tuple.v1(); + Environment environment = tuple.v2(); - Settings settings = null; - Environment environment = null; - try { - Tuple tuple = initialSettings(foreground); - settings = tuple.v1(); - environment = tuple.v2(); - - if (environment.pidFile() != null) { - stage = "Pid"; - PidFile.create(environment.pidFile(), true); - } - - stage = "Logging"; - setupLogging(settings, environment); - } catch (Exception e) { - String errorMessage = buildErrorMessage(stage, e); - sysError(errorMessage, true); - System.exit(3); + if (environment.pidFile() != null) { + PidFile.create(environment.pidFile(), true); } + setupLogging(settings, environment); + if (System.getProperty("es.max-open-files", "false").equals("true")) { ESLogger logger = Loggers.getLogger(Bootstrap.class); logger.info("max_open_files [{}]", ProcessProbe.getInstance().getMaxFileDescriptorCount()); @@ -272,7 +260,6 @@ public class Bootstrap { logger.warn("jvm uses the client vm, make sure to run `java` with the server vm for best performance by adding `-server` to the command line"); } - stage = "Initialization"; try { if (!foreground) { Loggers.disableConsoleLogging(); @@ -284,7 +271,6 @@ public class Bootstrap { INSTANCE.setup(true, settings, environment); - stage = "Startup"; INSTANCE.start(); if (!foreground) { @@ -295,14 +281,9 @@ public class Bootstrap { if (INSTANCE.node != null) { logger = Loggers.getLogger(Bootstrap.class, INSTANCE.node.settings().get("name")); } - String errorMessage = buildErrorMessage(stage, e); - if (foreground) { - sysError(errorMessage, true); - Loggers.disableConsoleLogging(); - } logger.error("Exception", e); - System.exit(3); + throw e; } } @@ -323,38 +304,4 @@ public class Bootstrap { System.err.flush(); } } - - private static String buildErrorMessage(String stage, Throwable e) { - StringBuilder errorMessage = new StringBuilder("{").append(Version.CURRENT).append("}: "); - errorMessage.append(stage).append(" Failed ...\n"); - if (e instanceof CreationException) { - CreationException createException = (CreationException) e; - Set seenMessages = newHashSet(); - int counter = 1; - for (Message message : createException.getErrorMessages()) { - String detailedMessage; - if (message.getCause() == null) { - detailedMessage = message.getMessage(); - } else { - detailedMessage = ExceptionsHelper.detailedMessage(message.getCause(), true, 0); - } - if (detailedMessage == null) { - detailedMessage = message.getMessage(); - } - if (seenMessages.contains(detailedMessage)) { - continue; - } - seenMessages.add(detailedMessage); - errorMessage.append("").append(counter++).append(") ").append(detailedMessage); - } - } else { - errorMessage.append("- ").append(ExceptionsHelper.detailedMessage(e, true, 0)); - } - if (Loggers.getLogger(Bootstrap.class).isDebugEnabled()) { - errorMessage.append("\n").append(ExceptionsHelper.stackTrace(e)); - } - return errorMessage.toString(); - } - - } diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java index e9a851ecea1..e996e5ae651 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java @@ -24,7 +24,7 @@ package org.elasticsearch.bootstrap; */ public class Elasticsearch extends Bootstrap { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { Bootstrap.main(args); } } \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/bootstrap/ElasticsearchF.java b/core/src/main/java/org/elasticsearch/bootstrap/ElasticsearchF.java index 96e310eff3f..1fb1dcc9d8a 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/ElasticsearchF.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/ElasticsearchF.java @@ -25,7 +25,7 @@ package org.elasticsearch.bootstrap; */ public class ElasticsearchF { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { System.setProperty("es.foreground", "yes"); Bootstrap.main(args); }