@Override
  protected void startUp() throws Exception {
    LOG.info("Starting {} periodicals ...", periodicalSet.size());

    for (Periodical periodical : periodicalSet) {
      try {
        periodical.initialize();

        if (periodical.masterOnly()
            && !serverStatus.hasCapability(ServerStatus.Capability.MASTER)) {
          LOG.info(
              "Not starting [{}] periodical. Only started on graylog2-server master nodes.",
              periodical.getClass().getCanonicalName());
          continue;
        }

        if (!periodical.startOnThisNode()) {
          LOG.info(
              "Not starting [{}] periodical. Not configured to run on this node.",
              periodical.getClass().getCanonicalName());
          continue;
        }

        // Register and start.
        periodicals.registerAndStart(periodical);
      } catch (Exception e) {
        LOG.error("Could not initialize periodical.", e);
      }
    }
  }
  @Override
  protected void startNodeRegistration(Injector injector) {
    // Register this node.
    final NodeService nodeService = injector.getInstance(NodeService.class);
    final ServerStatus serverStatus = injector.getInstance(ServerStatus.class);
    final ActivityWriter activityWriter = injector.getInstance(ActivityWriter.class);
    nodeService.registerServer(
        serverStatus.getNodeId().toString(),
        configuration.isMaster(),
        configuration.getRestTransportUri());
    serverStatus.setLocalMode(isLocal());
    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.",
          TimeUnit.MILLISECONDS.toSeconds(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, Server.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.");
      }
    }
  }
 private MongoDbMetricsReporter(
     MetricRegistry registry,
     MongoConnection mongoConnection,
     ServerStatus serverStatus,
     Clock clock,
     TimeUnit rateUnit,
     TimeUnit durationUnit,
     MetricFilter filter) {
   super(registry, "mongodb-reporter", filter, rateUnit, durationUnit);
   this.mongoConnection = mongoConnection;
   this.nodeId = serverStatus.getNodeId().toString();
   this.clock = clock;
 }
 @Override
 public void failure(Service service) {
   // do not log the failure here again, the ServiceManager itself does so already on Level ERROR.
   serverStatus.fail();
 }
 @Override
 public void healthy() {
   LOG.info("Services are healthy");
   serverStatus.start();
 }
  @Override
  protected void startCommand() {
    LOG.info(
        "Graylog " + commandName + " {} starting up. (JRE: {})",
        version,
        Tools.getSystemInformation());

    // Do not use a PID file if the user requested not to
    if (!isNoPidFile()) {
      savePidFile(getPidFile());
    }

    final ServerStatus serverStatus = injector.getInstance(ServerStatus.class);
    serverStatus.initialize();

    startNodeRegistration(injector);

    final ActivityWriter activityWriter;
    final ServiceManager serviceManager;
    try {
      activityWriter = injector.getInstance(ActivityWriter.class);
      serviceManager = injector.getInstance(ServiceManager.class);
    } catch (ProvisionException e) {
      LOG.error("Guice error", e);
      annotateProvisionException(e);
      System.exit(-1);
      return;
    } catch (Exception e) {
      LOG.error("Unexpected exception", e);
      System.exit(-1);
      return;
    }

    Runtime.getRuntime().addShutdownHook(new Thread(injector.getInstance(shutdownHook())));

    // 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("Graylog 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("Graylog " + commandName + " up and running.");

    // Block forever.
    try {
      Thread.currentThread().join();
    } catch (InterruptedException e) {
      return;
    }
  }