Search in sources :

Example 1 with DefaultMessageOutput

use of org.graylog2.outputs.DefaultMessageOutput in project graylog2-server by Graylog2.

the class OutputRouterTest method testGetOutputsFromTwoStreams.

@Test
public void testGetOutputsFromTwoStreams() throws Exception {
    final Stream stream1 = mock(Stream.class);
    final Stream stream2 = mock(Stream.class);
    final MessageOutput messageOutput1 = mock(MessageOutput.class);
    final Set<MessageOutput> messageOutputSet1 = ImmutableSet.of(messageOutput1);
    final MessageOutput messageOutput2 = mock(MessageOutput.class);
    final Set<MessageOutput> messageOutputSet2 = ImmutableSet.of(messageOutput2);
    final Message message = mock(Message.class);
    when(message.getStreams()).thenReturn(ImmutableSet.of(stream1, stream2));
    OutputRouter outputRouter = Mockito.spy(new OutputRouter(defaultMessageOutput, outputRegistry));
    doReturn(messageOutputSet1).when(outputRouter).getMessageOutputsForStream(eq(stream1));
    doReturn(messageOutputSet2).when(outputRouter).getMessageOutputsForStream(eq(stream2));
    final Collection<MessageOutput> result = outputRouter.getOutputsForMessage(message);
    assertEquals(result.size(), 3);
    assertTrue(result.contains(defaultMessageOutput));
    assertTrue(result.contains(messageOutput1));
    assertTrue(result.contains(messageOutput2));
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Message(org.graylog2.plugin.Message) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 2 with DefaultMessageOutput

use of org.graylog2.outputs.DefaultMessageOutput in project graylog2-server by Graylog2.

the class OutputRouterTest method testGetOutputFromSingleStreams.

@Test
public void testGetOutputFromSingleStreams() throws Exception {
    final Stream stream = mock(Stream.class);
    final Message message = mock(Message.class);
    when(message.getStreams()).thenReturn(ImmutableSet.of(stream));
    final MessageOutput messageOutput = mock(MessageOutput.class);
    final Set<MessageOutput> messageOutputList = ImmutableSet.of(messageOutput);
    final OutputRouter outputRouter = Mockito.spy(new OutputRouter(defaultMessageOutput, outputRegistry));
    doReturn(messageOutputList).when(outputRouter).getMessageOutputsForStream(eq(stream));
    // Call to test
    final Collection<MessageOutput> messageOutputs = outputRouter.getOutputsForMessage(message);
    // Verification
    assertEquals(messageOutputs.size(), 2);
    assertTrue(messageOutputs.contains(defaultMessageOutput));
    assertTrue(messageOutputs.contains(messageOutput));
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Message(org.graylog2.plugin.Message) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 3 with DefaultMessageOutput

use of org.graylog2.outputs.DefaultMessageOutput in project graylog2-server by Graylog2.

the class OutputBufferProcessor method onEvent.

/**
     * Each message will be written to one or more outputs.
     * <p>
     * The default output is always being used for every message, but optionally the message can be routed to additional
     * outputs, currently based on the stream outputs that are configured in the system.
     * </p>
     * <p>
     * The stream outputs are time limited so one bad output does not impact throughput too much. Essentially this means
     * that the work of writing to the outputs is performed, but the writer threads will not wait forever for stream
     * outputs to finish their work. <b>This might lead to increased memory usage!</b>
     * </p>
     * <p>
     * The default output, however, is allowed to block and is not subject to time limiting. This is important because it
     * can exert back pressure on the processing pipeline this way, making sure we don't run into excessive heap usage.
     * </p>
     *
     * @param event the message to write to outputs
     * @throws Exception
     */
@Override
public void onEvent(MessageEvent event) throws Exception {
    incomingMessages.mark();
    final Message msg = event.getMessage();
    if (msg == null) {
        LOG.debug("Skipping null message.");
        return;
    }
    LOG.debug("Processing message <{}> from OutputBuffer.", msg.getId());
    final Set<MessageOutput> messageOutputs = outputRouter.getStreamOutputsForMessage(msg);
    msg.recordCounter(serverStatus, "matched-outputs", messageOutputs.size());
    final Future<?> defaultOutputCompletion = processMessage(msg, defaultMessageOutput);
    final CountDownLatch streamOutputsDoneSignal = new CountDownLatch(messageOutputs.size());
    for (final MessageOutput output : messageOutputs) {
        processMessage(msg, output, streamOutputsDoneSignal);
    }
    // Wait until all writer threads for stream outputs have finished or timeout is reached.
    if (!streamOutputsDoneSignal.await(configuration.getOutputModuleTimeout(), TimeUnit.MILLISECONDS)) {
        LOG.warn("Timeout reached. Not waiting any longer for stream output writer threads to complete.");
    }
    // this exerts the back pressure to the system
    if (defaultOutputCompletion != null) {
        Uninterruptibles.getUninterruptibly(defaultOutputCompletion);
    } else {
        LOG.error("The default output future was null, this is a bug!");
    }
    if (msg.hasRecordings()) {
        LOG.debug("Message event trace: {}", msg.recordingsAsString());
    }
    outputThroughput.inc();
    LOG.debug("Wrote message <{}> to all outputs. Finished handling.", msg.getId());
    event.clearMessages();
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) DefaultMessageOutput(org.graylog2.outputs.DefaultMessageOutput) Message(org.graylog2.plugin.Message) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with DefaultMessageOutput

use of org.graylog2.outputs.DefaultMessageOutput in project graylog2-server by Graylog2.

the class OutputRouterTest method testGetMessageOutputsForEmptyStream.

@Test
public void testGetMessageOutputsForEmptyStream() throws Exception {
    final Stream stream = mock(Stream.class);
    final OutputRouter outputRouter = new OutputRouter(defaultMessageOutput, outputRegistry);
    final Collection<MessageOutput> messageOutputs = outputRouter.getMessageOutputsForStream(stream);
    assertEquals(messageOutputs.size(), 0);
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 5 with DefaultMessageOutput

use of org.graylog2.outputs.DefaultMessageOutput in project graylog2-server by Graylog2.

the class OutputRouterTest method testGetMessageOutputsForStreamWithTwoOutputs.

@Test
public void testGetMessageOutputsForStreamWithTwoOutputs() throws Exception {
    final Stream stream = mock(Stream.class);
    final Output output1 = mock(Output.class);
    final Output output2 = mock(Output.class);
    final String output1Id = "foo";
    final String output2Id = "bar";
    final MessageOutput messageOutput1 = mock(MessageOutput.class);
    final MessageOutput messageOutput2 = mock(MessageOutput.class);
    final Set<Output> outputSet = ImmutableSet.of(output1, output2);
    when(stream.getOutputs()).thenReturn(outputSet);
    when(output1.getId()).thenReturn(output1Id);
    when(output2.getId()).thenReturn(output2Id);
    when(outputRegistry.getOutputForIdAndStream(eq(output1Id), eq(stream))).thenReturn(messageOutput1);
    when(outputRegistry.getOutputForIdAndStream(eq(output2Id), eq(stream))).thenReturn(messageOutput2);
    final OutputRouter outputRouter = new OutputRouter(defaultMessageOutput, outputRegistry);
    final Collection<MessageOutput> messageOutputs = outputRouter.getMessageOutputsForStream(stream);
    assertEquals(messageOutputs.size(), 2);
    assertTrue(messageOutputs.contains(messageOutput1));
    assertTrue(messageOutputs.contains(messageOutput2));
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Aggregations

MessageOutput (org.graylog2.plugin.outputs.MessageOutput)8 Test (org.junit.Test)7 Stream (org.graylog2.plugin.streams.Stream)6 Message (org.graylog2.plugin.Message)5 Output (org.graylog2.plugin.streams.Output)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 DefaultMessageOutput (org.graylog2.outputs.DefaultMessageOutput)1