Search in sources :

Example 11 with ServerStatus

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

the class MessageFilterChainProcessorTest method testAllFiltersAreBeingRun.

@Test
public void testAllFiltersAreBeingRun() {
    final DummyFilter first = new DummyFilter(10);
    final DummyFilter second = new DummyFilter(20);
    final DummyFilter third = new DummyFilter(30);
    final Set<MessageFilter> filters = ImmutableSet.of(first, second, third);
    final MessageFilterChainProcessor processor = new MessageFilterChainProcessor(new MetricRegistry(), filters, acknowledger, serverStatus);
    final Message message = new Message("message", "source", new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC));
    final Message result = Iterables.getFirst(processor.process(message), null);
    assertThat(result).isNotNull();
    assertThat(result.getFields()).containsKeys("prio-10", "prio-20", "prio-30");
}
Also used : Message(org.graylog2.plugin.Message) MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 12 with ServerStatus

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

the class MessageFilterChainProcessorTest method testHandleMessage.

@Test
public void testHandleMessage() {
    MessageFilter filterOnlyFirst = new MessageFilter() {

        private boolean filterOut = true;

        @Override
        public boolean filter(Message msg) {
            if (filterOut) {
                msg.setFilterOut(true);
                filterOut = false;
                return true;
            }
            return false;
        }

        @Override
        public String getName() {
            return "first filtered out, subsequent pass";
        }

        @Override
        public int getPriority() {
            return 0;
        }
    };
    final MessageFilterChainProcessor filterTest = new MessageFilterChainProcessor(new MetricRegistry(), Collections.singleton(filterOnlyFirst), acknowledger, serverStatus);
    Message filteredoutMessage = new Message("filtered out", "source", Tools.nowUTC());
    filteredoutMessage.setJournalOffset(1);
    Message unfilteredMessage = new Message("filtered out", "source", Tools.nowUTC());
    final Messages messages1 = filterTest.process(filteredoutMessage);
    final Messages messages2 = filterTest.process(unfilteredMessage);
    Assert.assertTrue(filteredoutMessage.getFilterOut());
    Assert.assertFalse(unfilteredMessage.getFilterOut());
    Assert.assertEquals(0, Iterables.size(messages1));
    Assert.assertEquals(1, Iterables.size(messages2));
}
Also used : Messages(org.graylog2.plugin.Messages) Message(org.graylog2.plugin.Message) MetricRegistry(com.codahale.metrics.MetricRegistry) MessageFilter(org.graylog2.plugin.filters.MessageFilter) Test(org.junit.Test)

Example 13 with ServerStatus

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

the class LocalKafkaJournalTest method segmentAgeCleanup.

@Test
public void segmentAgeCleanup() throws Exception {
    final InstantMillisProvider clock = new InstantMillisProvider(DateTime.now(DateTimeZone.UTC));
    DateTimeUtils.setCurrentMillisProvider(clock);
    try {
        final Size segmentSize = Size.kilobytes(1L);
        final LocalKafkaJournal journal = new LocalKafkaJournal(journalDirectory.toPath(), scheduler, segmentSize, Duration.standardHours(1), Size.kilobytes(10L), Duration.standardMinutes(1), 1_000_000, Duration.standardMinutes(1), 100, new MetricRegistry(), serverStatus);
        final File messageJournalDir = new File(journalDirectory, "messagejournal-0");
        assertTrue(messageJournalDir.exists());
        // we need to fix up the last modified times of the actual files.
        long[] lastModifiedTs = new long[2];
        // create two chunks, 30 seconds apart
        createBulkChunks(journal, segmentSize, 1);
        journal.flushDirtyLogs();
        lastModifiedTs[0] = clock.getMillis();
        clock.tick(Period.seconds(30));
        createBulkChunks(journal, segmentSize, 1);
        journal.flushDirtyLogs();
        lastModifiedTs[1] = clock.getMillis();
        int i = 0;
        for (final LogSegment segment : journal.getSegments()) {
            assertTrue(i < 2);
            segment.lastModified_$eq(lastModifiedTs[i]);
            i++;
        }
        int cleanedLogs = journal.cleanupLogs();
        assertEquals("no segments should've been cleaned", 0, cleanedLogs);
        assertEquals("two segments segment should remain", 2, countSegmentsInDir(messageJournalDir));
        // move clock beyond the retention period and clean again
        clock.tick(Period.seconds(120));
        cleanedLogs = journal.cleanupLogs();
        assertEquals("two segments should've been cleaned (only one will actually be removed...)", 2, cleanedLogs);
        assertEquals("one segment should remain", 1, countSegmentsInDir(messageJournalDir));
    } finally {
        DateTimeUtils.setCurrentMillisSystem();
    }
}
Also used : LogSegment(org.graylog.shaded.kafka09.log.LogSegment) Size(com.github.joschi.jadconfig.util.Size) InstantMillisProvider(org.graylog2.plugin.InstantMillisProvider) MetricRegistry(com.codahale.metrics.MetricRegistry) File(java.io.File) Test(org.junit.Test)

Example 14 with ServerStatus

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

the class LocalKafkaJournalTest method setUp.

@Before
public void setUp() throws IOException {
    scheduler = new ScheduledThreadPoolExecutor(1);
    scheduler.prestartCoreThread();
    journalDirectory = temporaryFolder.newFolder();
    final File nodeId = temporaryFolder.newFile("node-id");
    Files.write(nodeId.toPath(), UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8));
    final Configuration configuration = new Configuration() {

        @Override
        public String getNodeIdFile() {
            return nodeId.getAbsolutePath();
        }
    };
    serverStatus = new ServerStatus(configuration, EnumSet.of(ServerStatus.Capability.SERVER), new EventBus("KafkaJournalTest"), NullAuditEventSender::new);
}
Also used : Configuration(org.graylog2.Configuration) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ServerStatus(org.graylog2.plugin.ServerStatus) EventBus(com.google.common.eventbus.EventBus) File(java.io.File) Before(org.junit.Before)

Example 15 with ServerStatus

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

MetricRegistry (com.codahale.metrics.MetricRegistry)8 Message (org.graylog2.plugin.Message)6 MessageFilter (org.graylog2.plugin.filters.MessageFilter)6 Test (org.junit.Test)6 File (java.io.File)4 ServerStatus (org.graylog2.plugin.ServerStatus)4 EventBus (com.google.common.eventbus.EventBus)3 Messages (org.graylog2.plugin.Messages)3 DateTime (org.joda.time.DateTime)3 Before (org.junit.Before)3 Size (com.github.joschi.jadconfig.util.Size)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 Configuration (org.graylog2.Configuration)2 InstantMillisProvider (org.graylog2.plugin.InstantMillisProvider)2 Activity (org.graylog2.shared.system.activities.Activity)2 ActivityWriter (org.graylog2.shared.system.activities.ActivityWriter)2 Timer (com.codahale.metrics.Timer)1 Service (com.google.common.util.concurrent.Service)1 ServiceManager (com.google.common.util.concurrent.ServiceManager)1 ProvisionException (com.google.inject.ProvisionException)1