Search in sources :

Example 1 with LeaderElectionService

use of org.graylog2.cluster.leader.LeaderElectionService in project graylog2-server by Graylog2.

the class ServerBootstrap method startCommand.

@Override
protected void startCommand() {
    final AuditEventSender auditEventSender = injector.getInstance(AuditEventSender.class);
    final NodeId nodeId = injector.getInstance(NodeId.class);
    final String systemInformation = Tools.getSystemInformation();
    final Map<String, Object> auditEventContext = ImmutableMap.of("version", version.toString(), "java", systemInformation, "node_id", nodeId.toString());
    auditEventSender.success(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
    final OS os = OS.getOs();
    LOG.info("Graylog {} {} starting up", commandName, version);
    LOG.info("JRE: {}", systemInformation);
    LOG.info("Deployment: {}", configuration.getInstallationSource());
    LOG.info("OS: {}", os.getPlatformName());
    LOG.info("Arch: {}", os.getArch());
    try {
        if (configuration.isLeader() && configuration.runMigrations()) {
            runMigrations();
        }
    } catch (Exception e) {
        LOG.warn("Exception while running migrations", e);
        System.exit(1);
    }
    final ServerStatus serverStatus = injector.getInstance(ServerStatus.class);
    serverStatus.initialize();
    startNodeRegistration(injector);
    final ActivityWriter activityWriter;
    final ServiceManager serviceManager;
    final Service leaderElectionService;
    try {
        activityWriter = injector.getInstance(ActivityWriter.class);
        serviceManager = injector.getInstance(ServiceManager.class);
        leaderElectionService = injector.getInstance(Key.get(Service.class, Names.named("LeaderElectionService")));
    } catch (ProvisionException e) {
        LOG.error("Guice error", e);
        annotateProvisionException(e);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        System.exit(-1);
        return;
    } catch (Exception e) {
        LOG.error("Unexpected exception", e);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        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, MoreExecutors.directExecutor());
    try {
        leaderElectionService.startAsync().awaitRunning();
        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);
        auditEventSender.failure(AuditActor.system(nodeId), NODE_STARTUP_INITIATE, auditEventContext);
        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.");
    auditEventSender.success(AuditActor.system(nodeId), NODE_STARTUP_COMPLETE, auditEventContext);
    // Block forever.
    try {
        Thread.currentThread().join();
    } catch (InterruptedException e) {
        return;
    }
}
Also used : OS(org.jsoftbiz.utils.OS) PreflightCheckService(org.graylog2.bootstrap.preflight.PreflightCheckService) Service(com.google.common.util.concurrent.Service) Activity(org.graylog2.shared.system.activities.Activity) TimeoutException(java.util.concurrent.TimeoutException) ProvisionException(com.google.inject.ProvisionException) ProvisionException(com.google.inject.ProvisionException) AuditEventSender(org.graylog2.audit.AuditEventSender) ServiceManager(com.google.common.util.concurrent.ServiceManager) ServerStatus(org.graylog2.plugin.ServerStatus) NodeId(org.graylog2.plugin.system.NodeId) ServiceManagerListener(org.graylog2.shared.initializers.ServiceManagerListener) ActivityWriter(org.graylog2.shared.system.activities.ActivityWriter) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with LeaderElectionService

use of org.graylog2.cluster.leader.LeaderElectionService in project graylog2-server by Graylog2.

the class Server method startNodeRegistration.

@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);
    final LeaderElectionService leaderElectionService = injector.getInstance(LeaderElectionService.class);
    nodeService.registerServer(serverStatus.getNodeId().toString(), leaderElectionService.isLeader(), httpConfiguration.getHttpPublishUri(), Tools.getLocalCanonicalHostname());
    serverStatus.setLocalMode(isLocal());
    if (leaderElectionService.isLeader() && !nodeService.isOnlyLeader(serverStatus.getNodeId())) {
        LOG.warn("Detected another leader in the cluster. Retrying in {} seconds to make sure it is not " + "an old stale instance.", TimeUnit.MILLISECONDS.toSeconds(configuration.getStaleLeaderTimeout()));
        try {
            Thread.sleep(configuration.getStaleLeaderTimeout());
        } catch (InterruptedException e) {
        /* nope */
        }
        if (!nodeService.isOnlyLeader(serverStatus.getNodeId())) {
            // All devils here.
            String what = "Detected other leader node in the cluster! Starting as non-leader! " + "This is a mis-configuration you should fix.";
            LOG.warn(what);
            activityWriter.write(new Activity(what, Server.class));
            final NotificationService notificationService = injector.getInstance(NotificationService.class);
            // remove legacy notification, if present
            // noinspection deprecation
            notificationService.fixed(notificationService.build().addType(Notification.Type.MULTI_MASTER));
            // Write a notification.
            Notification notification = notificationService.buildNow().addType(Notification.Type.MULTI_LEADER).addSeverity(Notification.Severity.URGENT);
            notificationService.publishIfFirst(notification);
            configuration.setIsLeader(false);
        } else {
            LOG.warn("Stale leader has gone. Starting as leader.");
        }
    }
}
Also used : NodeService(org.graylog2.cluster.NodeService) ServerStatus(org.graylog2.plugin.ServerStatus) ActivityWriter(org.graylog2.shared.system.activities.ActivityWriter) LeaderElectionService(org.graylog2.cluster.leader.LeaderElectionService) Activity(org.graylog2.shared.system.activities.Activity) NotificationService(org.graylog2.notifications.NotificationService) Notification(org.graylog2.notifications.Notification)

Aggregations

ServerStatus (org.graylog2.plugin.ServerStatus)2 Activity (org.graylog2.shared.system.activities.Activity)2 ActivityWriter (org.graylog2.shared.system.activities.ActivityWriter)2 Service (com.google.common.util.concurrent.Service)1 ServiceManager (com.google.common.util.concurrent.ServiceManager)1 ProvisionException (com.google.inject.ProvisionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AuditEventSender (org.graylog2.audit.AuditEventSender)1 PreflightCheckService (org.graylog2.bootstrap.preflight.PreflightCheckService)1 NodeService (org.graylog2.cluster.NodeService)1 LeaderElectionService (org.graylog2.cluster.leader.LeaderElectionService)1 Notification (org.graylog2.notifications.Notification)1 NotificationService (org.graylog2.notifications.NotificationService)1 NodeId (org.graylog2.plugin.system.NodeId)1 ServiceManagerListener (org.graylog2.shared.initializers.ServiceManagerListener)1 OS (org.jsoftbiz.utils.OS)1