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.
This commit is contained in:
Robert Muir 2015-07-31 14:00:18 -04:00
parent 8d5b5ad862
commit 5f9edd26ea
3 changed files with 11 additions and 64 deletions

View File

@ -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(); BootstrapCLIParser bootstrapCLIParser = new BootstrapCLIParser();
CliTool.ExitStatus status = bootstrapCLIParser.execute(args); CliTool.ExitStatus status = bootstrapCLIParser.execute(args);
@ -239,28 +239,16 @@ public class Bootstrap {
foreground = false; foreground = false;
} }
String stage = "Settings"; Tuple<Settings, Environment> tuple = initialSettings(foreground);
Settings settings = tuple.v1();
Environment environment = tuple.v2();
Settings settings = null; if (environment.pidFile() != null) {
Environment environment = null; PidFile.create(environment.pidFile(), true);
try {
Tuple<Settings, Environment> 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);
} }
setupLogging(settings, environment);
if (System.getProperty("es.max-open-files", "false").equals("true")) { if (System.getProperty("es.max-open-files", "false").equals("true")) {
ESLogger logger = Loggers.getLogger(Bootstrap.class); ESLogger logger = Loggers.getLogger(Bootstrap.class);
logger.info("max_open_files [{}]", ProcessProbe.getInstance().getMaxFileDescriptorCount()); 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"); 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 { try {
if (!foreground) { if (!foreground) {
Loggers.disableConsoleLogging(); Loggers.disableConsoleLogging();
@ -284,7 +271,6 @@ public class Bootstrap {
INSTANCE.setup(true, settings, environment); INSTANCE.setup(true, settings, environment);
stage = "Startup";
INSTANCE.start(); INSTANCE.start();
if (!foreground) { if (!foreground) {
@ -295,14 +281,9 @@ public class Bootstrap {
if (INSTANCE.node != null) { if (INSTANCE.node != null) {
logger = Loggers.getLogger(Bootstrap.class, INSTANCE.node.settings().get("name")); 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); logger.error("Exception", e);
System.exit(3); throw e;
} }
} }
@ -323,38 +304,4 @@ public class Bootstrap {
System.err.flush(); 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<String> 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();
}
} }

View File

@ -24,7 +24,7 @@ package org.elasticsearch.bootstrap;
*/ */
public class Elasticsearch extends Bootstrap { public class Elasticsearch extends Bootstrap {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
Bootstrap.main(args); Bootstrap.main(args);
} }
} }

View File

@ -25,7 +25,7 @@ package org.elasticsearch.bootstrap;
*/ */
public class ElasticsearchF { public class ElasticsearchF {
public static void main(String[] args) { public static void main(String[] args) throws Exception {
System.setProperty("es.foreground", "yes"); System.setProperty("es.foreground", "yes");
Bootstrap.main(args); Bootstrap.main(args);
} }