Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
String msg = "SIGNAL received. Shutting down.";
LOG.info(msg);
finalActivityWriter.write(new Activity(msg, Main.class));
GracefulShutdown shutdown = injector.getInstance(GracefulShutdown.class);
shutdown.runWithoutExit();
finalServiceManager.stopAsync().awaitStopped();
}
});
// Register this node.
final NodeService nodeService = injector.getInstance(NodeService.class);
nodeService.registerServer(serverStatus.getNodeId().toString(), configuration.isMaster(), configuration.getRestTransportUri());
if (configuration.isMaster() && !nodeService.isOnlyMaster(serverStatus.getNodeId())) {
LOG.warn("Detected another master in the cluster. Retrying in {} seconds to make sure it is not "
+ "an old stale instance.", configuration.getStaleMasterTimeout());
try {
Thread.sleep(configuration.getStaleMasterTimeout());
} catch (InterruptedException e) { /* nope */ }
if (!nodeService.isOnlyMaster(serverStatus.getNodeId())) {
// All devils here.
String what = "Detected other master node in the cluster! Starting as non-master! "
+ "This is a mis-configuration you should fix.";
LOG.warn(what);
activityWriter.write(new Activity(what, Main.class));
// Write a notification.
final NotificationService notificationService = injector.getInstance(NotificationService.class);
Notification notification = notificationService.buildNow()
.addType(Notification.Type.MULTI_MASTER)
.addSeverity(Notification.Severity.URGENT);
notificationService.publishIfFirst(notification);
configuration.setIsMaster(false);
} else {
LOG.warn("Stale master has gone. Starting as master.");
}
}
// Enable local mode?
if (commandLineArguments.isLocal() || commandLineArguments.isDebug()) {
// In local mode, systemstats are sent to localhost for example.
LOG.info("Running in local mode");
serverStatus.setLocalMode(true);
}
// Are we in stats mode?
if (commandLineArguments.isStats()) {
LOG.info("Printing system utilization information.");
serverStatus.setStatsMode(true);
}
if (!commandLineArguments.performRetention()) {
configuration.setPerformRetention(false);
}
// propagate default size to input plugins
MessageInput.setDefaultRecvBufferSize(configuration.getUdpRecvBufferSizes());
// Start services.
final ServiceManagerListener serviceManagerListener = injector.getInstance(ServiceManagerListener.class);
serviceManager.addListener(serviceManagerListener);
try {
serviceManager.startAsync().awaitHealthy();
} catch (Exception e) {
try {
serviceManager.stopAsync().awaitStopped(configuration.getShutdownTimeout(), TimeUnit.MILLISECONDS);
} catch (TimeoutException timeoutException) {
LOG.error("Unable to shutdown properly on time. {}", serviceManager.servicesByState());
}
LOG.error("Graylog2 startup failed. Exiting. Exception was:", e);
System.exit(-1);
}
LOG.info("Services started, startup times in ms: {}", serviceManager.startupTimes());
activityWriter.write(new Activity("Started up.", Main.class));
LOG.info("Graylog2 up and running.");
// Block forever.
try {
while (true) {