2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2014-01-06 22:48:02 +01:00
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch licenses this file to you under
|
|
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-02-08 15:30:06 +02:00
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.elasticsearch.bootstrap;
|
|
|
|
|
|
|
|
import org.elasticsearch.ExceptionsHelper;
|
|
|
|
import org.elasticsearch.Version;
|
2014-12-04 10:20:05 +01:00
|
|
|
import org.elasticsearch.common.PidFile;
|
2010-06-15 17:28:05 +03:00
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
2010-06-15 16:51:38 +03:00
|
|
|
import org.elasticsearch.common.inject.CreationException;
|
|
|
|
import org.elasticsearch.common.inject.spi.Message;
|
2010-10-30 19:40:24 +02:00
|
|
|
import org.elasticsearch.common.jna.Natives;
|
2010-06-15 16:51:38 +03:00
|
|
|
import org.elasticsearch.common.logging.ESLogger;
|
|
|
|
import org.elasticsearch.common.logging.Loggers;
|
|
|
|
import org.elasticsearch.common.logging.log4j.LogConfigurator;
|
|
|
|
import org.elasticsearch.common.settings.Settings;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.env.Environment;
|
2011-03-23 18:06:29 +02:00
|
|
|
import org.elasticsearch.monitor.jvm.JvmInfo;
|
2014-04-14 21:39:13 +02:00
|
|
|
import org.elasticsearch.monitor.process.JmxProcessProbe;
|
2010-04-09 00:54:54 +03:00
|
|
|
import org.elasticsearch.node.Node;
|
|
|
|
import org.elasticsearch.node.NodeBuilder;
|
2013-09-11 13:49:33 -05:00
|
|
|
import org.elasticsearch.node.internal.InternalSettingsPreparer;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2014-12-17 18:00:53 +01:00
|
|
|
import java.nio.file.Paths;
|
2013-05-18 23:49:04 +02:00
|
|
|
import java.util.Locale;
|
2010-02-08 15:30:06 +02:00
|
|
|
import java.util.Set;
|
2010-05-01 03:00:06 +03:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
import static com.google.common.collect.Sets.newHashSet;
|
2014-12-17 18:00:53 +01:00
|
|
|
import static org.elasticsearch.common.jna.Kernel32Library.ConsoleCtrlHandler;
|
2011-12-06 02:42:25 +02:00
|
|
|
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
2010-04-04 17:18:18 +03:00
|
|
|
* A main entry point when starting from the command line.
|
2010-02-08 15:30:06 +02:00
|
|
|
*/
|
|
|
|
public class Bootstrap {
|
|
|
|
|
2010-04-09 00:54:54 +03:00
|
|
|
private Node node;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-05-01 03:10:47 +03:00
|
|
|
private static volatile Thread keepAliveThread;
|
|
|
|
private static volatile CountDownLatch keepAliveLatch;
|
2011-10-17 20:25:41 +02:00
|
|
|
private static Bootstrap bootstrap;
|
|
|
|
|
2010-03-20 00:28:15 +02:00
|
|
|
private void setup(boolean addShutdownHook, Tuple<Settings, Environment> tuple) throws Exception {
|
2010-12-19 02:30:48 +02:00
|
|
|
if (tuple.v1().getAsBoolean("bootstrap.mlockall", false)) {
|
2010-11-14 15:54:40 +02:00
|
|
|
Natives.tryMlockall();
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-04-09 00:54:54 +03:00
|
|
|
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(tuple.v1()).loadConfigSettings(false);
|
|
|
|
node = nodeBuilder.build();
|
2010-03-20 00:28:15 +02:00
|
|
|
if (addShutdownHook) {
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2010-04-09 00:54:54 +03:00
|
|
|
node.close();
|
2010-03-20 00:28:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:00:53 +01:00
|
|
|
if (tuple.v1().getAsBoolean("bootstrap.ctrlhandler", true)) {
|
|
|
|
Natives.addConsoleCtrlHandler(new ConsoleCtrlHandler() {
|
|
|
|
@Override
|
|
|
|
public boolean handle(int code) {
|
|
|
|
if (CTRL_CLOSE_EVENT == code) {
|
|
|
|
ESLogger logger = Loggers.getLogger(Bootstrap.class);
|
|
|
|
logger.info("running graceful exit on windows");
|
|
|
|
|
|
|
|
System.exit(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void setupLogging(Tuple<Settings, Environment> tuple) {
|
2010-02-08 15:30:06 +02:00
|
|
|
try {
|
2012-02-12 23:55:16 +02:00
|
|
|
tuple.v1().getClassLoader().loadClass("org.apache.log4j.Logger");
|
2010-02-08 15:30:06 +02:00
|
|
|
LogConfigurator.configure(tuple.v1());
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
// no log4j
|
|
|
|
} catch (NoClassDefFoundError e) {
|
|
|
|
// no log4j
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.err.println("Failed to configure logging...");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-03-20 00:28:15 +02:00
|
|
|
private static Tuple<Settings, Environment> initialSettings() {
|
2013-09-11 13:49:33 -05:00
|
|
|
return InternalSettingsPreparer.prepareSettings(EMPTY_SETTINGS, true);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hook for JSVC
|
|
|
|
*/
|
|
|
|
public void init(String[] args) throws Exception {
|
2010-03-20 00:28:15 +02:00
|
|
|
Tuple<Settings, Environment> tuple = initialSettings();
|
|
|
|
setupLogging(tuple);
|
|
|
|
setup(true, tuple);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hook for JSVC
|
|
|
|
*/
|
|
|
|
public void start() {
|
2010-04-09 00:54:54 +03:00
|
|
|
node.start();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hook for JSVC
|
|
|
|
*/
|
|
|
|
public void stop() {
|
2015-02-27 13:49:40 +01:00
|
|
|
destroy();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hook for JSVC
|
|
|
|
*/
|
|
|
|
public void destroy() {
|
2010-04-09 00:54:54 +03:00
|
|
|
node.close();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-10-17 20:25:41 +02:00
|
|
|
public static void close(String[] args) {
|
|
|
|
bootstrap.destroy();
|
|
|
|
keepAliveLatch.countDown();
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2010-10-28 23:39:37 +02:00
|
|
|
System.setProperty("es.logger.prefix", "");
|
2011-10-17 20:25:41 +02:00
|
|
|
bootstrap = new Bootstrap();
|
2011-12-21 05:28:28 +02:00
|
|
|
final String pidFile = System.getProperty("es.pidfile", System.getProperty("es-pidfile"));
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-12-21 05:28:28 +02:00
|
|
|
if (pidFile != null) {
|
|
|
|
try {
|
2014-12-04 10:20:05 +01:00
|
|
|
PidFile.create(Paths.get(pidFile), true);
|
2011-12-21 05:28:28 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
String errorMessage = buildErrorMessage("pid", e);
|
|
|
|
System.err.println(errorMessage);
|
|
|
|
System.err.flush();
|
|
|
|
System.exit(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boolean foreground = System.getProperty("es.foreground", System.getProperty("es-foreground")) != null;
|
2010-05-01 01:42:30 +03:00
|
|
|
// handle the wrapper system property, if its a service, don't run as a service
|
|
|
|
if (System.getProperty("wrapper.service", "XXX").equalsIgnoreCase("true")) {
|
|
|
|
foreground = false;
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-03-20 00:28:15 +02:00
|
|
|
Tuple<Settings, Environment> tuple = null;
|
|
|
|
try {
|
|
|
|
tuple = initialSettings();
|
|
|
|
setupLogging(tuple);
|
|
|
|
} catch (Exception e) {
|
|
|
|
String errorMessage = buildErrorMessage("Setup", e);
|
|
|
|
System.err.println(errorMessage);
|
|
|
|
System.err.flush();
|
|
|
|
System.exit(3);
|
|
|
|
}
|
|
|
|
|
2010-11-06 22:47:09 +02:00
|
|
|
if (System.getProperty("es.max-open-files", "false").equals("true")) {
|
|
|
|
ESLogger logger = Loggers.getLogger(Bootstrap.class);
|
2014-04-14 21:39:13 +02:00
|
|
|
logger.info("max_open_files [{}]", JmxProcessProbe.getMaxFileDescriptorCount());
|
2010-11-06 22:47:09 +02:00
|
|
|
}
|
|
|
|
|
2011-03-23 18:06:29 +02:00
|
|
|
// warn if running using the client VM
|
2013-05-18 23:49:04 +02:00
|
|
|
if (JvmInfo.jvmInfo().vmName().toLowerCase(Locale.ROOT).contains("client")) {
|
2011-03-23 18:06:29 +02:00
|
|
|
ESLogger logger = Loggers.getLogger(Bootstrap.class);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
String stage = "Initialization";
|
|
|
|
try {
|
|
|
|
if (!foreground) {
|
|
|
|
Loggers.disableConsoleLogging();
|
|
|
|
System.out.close();
|
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
bootstrap.setup(true, tuple);
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
stage = "Startup";
|
|
|
|
bootstrap.start();
|
|
|
|
|
|
|
|
if (!foreground) {
|
|
|
|
System.err.close();
|
|
|
|
}
|
2010-05-01 03:00:06 +03:00
|
|
|
|
2010-05-01 03:10:47 +03:00
|
|
|
keepAliveLatch = new CountDownLatch(1);
|
2010-05-01 03:09:44 +03:00
|
|
|
// keep this thread alive (non daemon thread) until we shutdown
|
2010-05-01 03:00:06 +03:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2010-05-01 03:10:47 +03:00
|
|
|
keepAliveLatch.countDown();
|
2010-05-01 03:00:06 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-05-01 03:09:44 +03:00
|
|
|
keepAliveThread = new Thread(new Runnable() {
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2010-05-01 03:09:44 +03:00
|
|
|
try {
|
2010-05-01 03:10:47 +03:00
|
|
|
keepAliveLatch.await();
|
2010-05-01 03:09:44 +03:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// bail out
|
|
|
|
}
|
2010-05-01 03:00:06 +03:00
|
|
|
}
|
2012-08-02 10:50:28 +03:00
|
|
|
}, "elasticsearch[keepAlive/" + Version.CURRENT + "]");
|
2010-05-01 03:09:44 +03:00
|
|
|
keepAliveThread.setDaemon(false);
|
|
|
|
keepAliveThread.start();
|
2010-02-08 15:30:06 +02:00
|
|
|
} catch (Throwable e) {
|
2010-04-21 00:29:42 +03:00
|
|
|
ESLogger logger = Loggers.getLogger(Bootstrap.class);
|
2010-04-09 00:54:54 +03:00
|
|
|
if (bootstrap.node != null) {
|
|
|
|
logger = Loggers.getLogger(Bootstrap.class, bootstrap.node.settings().get("name"));
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
String errorMessage = buildErrorMessage(stage, e);
|
2010-02-08 15:30:06 +02:00
|
|
|
if (foreground) {
|
|
|
|
System.err.println(errorMessage);
|
|
|
|
System.err.flush();
|
2014-06-20 11:01:01 -04:00
|
|
|
Loggers.disableConsoleLogging();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2014-06-20 11:01:01 -04:00
|
|
|
logger.error("Exception", e);
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
System.exit(3);
|
|
|
|
}
|
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
|
|
|
|
private static String buildErrorMessage(String stage, Throwable e) {
|
2011-10-05 12:42:27 +02:00
|
|
|
StringBuilder errorMessage = new StringBuilder("{").append(Version.CURRENT).append("}: ");
|
2011-12-16 15:36:22 +02:00
|
|
|
errorMessage.append(stage).append(" Failed ...\n");
|
2010-03-20 00:28:15 +02:00
|
|
|
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));
|
|
|
|
}
|
2014-02-12 21:53:23 +02:00
|
|
|
if (Loggers.getLogger(Bootstrap.class).isDebugEnabled()) {
|
|
|
|
errorMessage.append("\n").append(ExceptionsHelper.stackTrace(e));
|
|
|
|
}
|
2010-03-20 00:28:15 +02:00
|
|
|
return errorMessage.toString();
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|