Search in sources :

Example 1 with LeaderChangedEvent

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

the class InputEventListener method leaderChanged.

@Subscribe
public void leaderChanged(LeaderChangedEvent event) {
    if (serverStatus.getLifecycle() == Lifecycle.STARTING) {
        LOG.debug("Ignoring LeaderChangedEvent during server startup.");
        return;
    }
    if (leaderElectionService.isLeader()) {
        for (MessageInput input : persistedInputs) {
            final IOState<MessageInput> inputState = inputRegistry.getInputState(input.getId());
            if (input.onlyOnePerCluster() && input.isGlobal() && (inputState == null || inputState.canBeStarted()) && inputLauncher.shouldStartAutomatically(input)) {
                LOG.info("Got leader role. Starting input [{}/{}/{}]", input.getName(), input.getTitle(), input.getId());
                startMessageInput(input);
            }
        }
    } else {
        inputRegistry.getRunningInputs().stream().map(IOState::getStoppable).filter(input -> input.isGlobal() && input.onlyOnePerCluster()).forEach(input -> {
            LOG.info("Lost leader role. Stopping input [{}/{}/{}]", input.getName(), input.getTitle(), input.getId());
            inputDeleted(InputDeleted.create(input.getId()));
        });
    }
}
Also used : InputCreated(org.graylog2.rest.models.system.inputs.responses.InputCreated) Logger(org.slf4j.Logger) InputUpdated(org.graylog2.rest.models.system.inputs.responses.InputUpdated) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) PersistedInputs(org.graylog2.shared.inputs.PersistedInputs) LoggerFactory(org.slf4j.LoggerFactory) ServerStatus(org.graylog2.plugin.ServerStatus) EventBus(com.google.common.eventbus.EventBus) InputLauncher(org.graylog2.shared.inputs.InputLauncher) InputRegistry(org.graylog2.shared.inputs.InputRegistry) Inject(javax.inject.Inject) LeaderChangedEvent(org.graylog2.cluster.leader.LeaderChangedEvent) LeaderElectionService(org.graylog2.cluster.leader.LeaderElectionService) MessageInput(org.graylog2.plugin.inputs.MessageInput) Lifecycle(org.graylog2.plugin.lifecycles.Lifecycle) NodeId(org.graylog2.plugin.system.NodeId) InputDeleted(org.graylog2.rest.models.system.inputs.responses.InputDeleted) Subscribe(com.google.common.eventbus.Subscribe) NotFoundException(org.graylog2.database.NotFoundException) IOState(org.graylog2.plugin.IOState) IOState(org.graylog2.plugin.IOState) MessageInput(org.graylog2.plugin.inputs.MessageInput) Subscribe(com.google.common.eventbus.Subscribe)

Aggregations

EventBus (com.google.common.eventbus.EventBus)1 Subscribe (com.google.common.eventbus.Subscribe)1 Inject (javax.inject.Inject)1 LeaderChangedEvent (org.graylog2.cluster.leader.LeaderChangedEvent)1 LeaderElectionService (org.graylog2.cluster.leader.LeaderElectionService)1 NotFoundException (org.graylog2.database.NotFoundException)1 IOState (org.graylog2.plugin.IOState)1 ServerStatus (org.graylog2.plugin.ServerStatus)1 MessageInput (org.graylog2.plugin.inputs.MessageInput)1 Lifecycle (org.graylog2.plugin.lifecycles.Lifecycle)1 NodeId (org.graylog2.plugin.system.NodeId)1 InputCreated (org.graylog2.rest.models.system.inputs.responses.InputCreated)1 InputDeleted (org.graylog2.rest.models.system.inputs.responses.InputDeleted)1 InputUpdated (org.graylog2.rest.models.system.inputs.responses.InputUpdated)1 InputLauncher (org.graylog2.shared.inputs.InputLauncher)1 InputRegistry (org.graylog2.shared.inputs.InputRegistry)1 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)1 PersistedInputs (org.graylog2.shared.inputs.PersistedInputs)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1