Search in sources :

Example 11 with IOState

use of org.graylog2.plugin.IOState in project graylog2-server by Graylog2.

the class InputEventListenerTest method inputCreatedStopsInputIfItIsRunning.

@Test
public void inputCreatedStopsInputIfItIsRunning() throws Exception {
    final String inputId = "input-id";
    final Input input = mock(Input.class);
    @SuppressWarnings("unchecked") final IOState<MessageInput> inputState = mock(IOState.class);
    when(inputService.find(inputId)).thenReturn(input);
    when(inputRegistry.getInputState(inputId)).thenReturn(inputState);
    listener.inputCreated(InputCreated.create(inputId));
    verify(inputRegistry, times(1)).remove(inputState);
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) Test(org.junit.Test)

Example 12 with IOState

use of org.graylog2.plugin.IOState in project graylog2-server by Graylog2.

the class IOStateTest method testNotEqualIfDifferentState.

@Test
public void testNotEqualIfDifferentState() throws Exception {
    EventBus eventBus = mock(EventBus.class);
    MessageInput messageInput = mock(MessageInput.class);
    IOState<MessageInput> inputState1 = new IOState<>(eventBus, messageInput, IOState.Type.RUNNING);
    IOState<MessageInput> inputState2 = new IOState<>(eventBus, messageInput, IOState.Type.STOPPED);
    assertTrue(inputState1.equals(inputState2));
    assertTrue(inputState2.equals(inputState1));
}
Also used : IOState(org.graylog2.plugin.IOState) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 13 with IOState

use of org.graylog2.plugin.IOState in project graylog2-server by Graylog2.

the class IOStateTest method testEqualsSameState.

@Test
public void testEqualsSameState() throws Exception {
    EventBus eventBus = mock(EventBus.class);
    MessageInput messageInput = mock(MessageInput.class);
    IOState<MessageInput> inputState1 = new IOState<>(eventBus, messageInput, IOState.Type.RUNNING);
    IOState<MessageInput> inputState2 = new IOState<>(eventBus, messageInput, IOState.Type.RUNNING);
    assertTrue(inputState1.equals(inputState2));
    assertTrue(inputState2.equals(inputState1));
}
Also used : IOState(org.graylog2.plugin.IOState) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 14 with IOState

use of org.graylog2.plugin.IOState in project graylog2-server by Graylog2.

the class FromInput method evaluate.

@Override
public Boolean evaluate(FunctionArgs args, EvaluationContext context) {
    String id = idParam.optional(args, context).orElse("");
    MessageInput input = null;
    if ("".equals(id)) {
        final String name = nameParam.optional(args, context).orElse("");
        for (IOState<MessageInput> messageInputIOState : inputRegistry.getInputStates()) {
            final MessageInput messageInput = messageInputIOState.getStoppable();
            if (messageInput.getTitle().equalsIgnoreCase(name)) {
                input = messageInput;
                break;
            }
        }
        if ("".equals(name)) {
            return null;
        }
    } else {
        final IOState<MessageInput> inputState = inputRegistry.getInputState(id);
        if (inputState != null) {
            input = inputState.getStoppable();
        }
    }
    return input != null && input.getId().equals(context.currentMessage().getSourceInputId());
}
Also used : MessageInput(org.graylog2.plugin.inputs.MessageInput)

Example 15 with IOState

use of org.graylog2.plugin.IOState 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

MessageInput (org.graylog2.plugin.inputs.MessageInput)13 Test (org.junit.Test)9 EventBus (com.google.common.eventbus.EventBus)4 Subscribe (com.google.common.eventbus.Subscribe)4 IOState (org.graylog2.plugin.IOState)4 NotFoundException (org.graylog2.database.NotFoundException)3 InputRegistry (org.graylog2.shared.inputs.InputRegistry)2 Stopwatch (com.google.common.base.Stopwatch)1 Inject (javax.inject.Inject)1 LeaderChangedEvent (org.graylog2.cluster.leader.LeaderChangedEvent)1 LeaderElectionService (org.graylog2.cluster.leader.LeaderElectionService)1 Notification (org.graylog2.notifications.Notification)1 ServerStatus (org.graylog2.plugin.ServerStatus)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 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)1