Search in sources :

Example 1 with NodeService

use of org.graylog2.cluster.NodeService 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

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 ServerStatus (org.graylog2.plugin.ServerStatus)1 Activity (org.graylog2.shared.system.activities.Activity)1 ActivityWriter (org.graylog2.shared.system.activities.ActivityWriter)1