Skip to content

Commit d6d6ef3

Browse files
Make maximum event age configurable. (#24441)
* Make maximum event age configurable. * Adding changelog snippet. * Fixing type of tunable.
1 parent ad6caf0 commit d6d6ef3

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

changelog/unreleased/pr-24441.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type = "a"
2+
message = "Make maximum event age of cluster events configurable. "
3+
4+
pulls = ["24441"]

graylog2-server/src/main/java/org/graylog2/Configuration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ public class Configuration extends CaConfiguration implements CommonNodeConfigur
301301
@Parameter(value = "global_inputs_only")
302302
private boolean globalInputsOnly = false;
303303

304+
@Parameter(value = "max_event_age", validators = PositiveDurationValidator.class)
305+
private java.time.Duration maxEventAge = java.time.Duration.ofDays(1L);
306+
304307
public boolean maintainsStreamAwareFieldTypes() {
305308
return streamAwareFieldTypes;
306309
}

graylog2-server/src/main/java/org/graylog2/events/ClusterEventCleanupPeriodical.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.annotations.VisibleForTesting;
2020
import com.google.common.primitives.Ints;
2121
import com.mongodb.WriteConcern;
22+
import jakarta.inject.Named;
2223
import org.graylog2.database.MongoCollection;
2324
import com.mongodb.client.model.Filters;
2425
import jakarta.inject.Inject;
@@ -29,21 +30,21 @@
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
3132

33+
import java.time.Duration;
3234
import java.util.concurrent.TimeUnit;
3335

3436
public class ClusterEventCleanupPeriodical extends Periodical {
3537
private static final Logger LOG = LoggerFactory.getLogger(ClusterEventCleanupPeriodical.class);
3638
private static final String COLLECTION_NAME = ClusterEventPeriodical.COLLECTION_NAME;
3739

38-
@VisibleForTesting
39-
static final long DEFAULT_MAX_EVENT_AGE = TimeUnit.DAYS.toMillis(1L);
40-
4140
private final MongoCollection<ClusterEvent> collection;
41+
private final Duration maxEventAge;
4242

4343
@Inject
44-
public ClusterEventCleanupPeriodical(MongoCollections mongoCollections) {
44+
public ClusterEventCleanupPeriodical(MongoCollections mongoCollections, @Named("max_event_age") Duration maxEventAge) {
4545
this.collection = mongoCollections.collection(COLLECTION_NAME, ClusterEvent.class)
4646
.withWriteConcern(WriteConcern.JOURNALED);
47+
this.maxEventAge = maxEventAge;
4748
}
4849

4950
@Override
@@ -91,7 +92,7 @@ public void doRun() {
9192
try {
9293
LOG.debug("Removing stale events from MongoDB collection \"{}\"", COLLECTION_NAME);
9394

94-
final long timestamp = DateTime.now(DateTimeZone.UTC).getMillis() - DEFAULT_MAX_EVENT_AGE;
95+
final long timestamp = DateTime.now(DateTimeZone.UTC).getMillis() - maxEventAge.toMillis();
9596
final var deleted = collection.deleteMany(Filters.lt("timestamp", timestamp)).getDeletedCount();
9697

9798
LOG.debug("Removed {} stale events from \"{}\"", deleted, COLLECTION_NAME);

graylog2-server/src/test/java/org/graylog2/events/ClusterEventCleanupPeriodicalTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
*/
1717
package org.graylog2.events;
1818

19-
import com.fasterxml.jackson.databind.ObjectMapper;
2019
import com.mongodb.BasicDBObjectBuilder;
2120
import com.mongodb.DBCollection;
2221
import com.mongodb.DBObject;
2322
import org.graylog.testing.mongodb.MongoDBExtension;
2423
import org.graylog2.database.MongoCollections;
2524
import org.graylog2.database.MongoConnection;
26-
import org.graylog2.shared.bindings.providers.ObjectMapperProvider;
2725
import org.joda.time.DateTime;
2826
import org.joda.time.DateTimeUtils;
2927
import org.joda.time.DateTimeZone;
@@ -35,6 +33,7 @@
3533
import org.mockito.junit.jupiter.MockitoSettings;
3634
import org.mockito.quality.Strictness;
3735

36+
import java.time.Duration;
3837
import java.util.Collections;
3938

4039
import static org.assertj.core.api.Assertions.assertThat;
@@ -44,8 +43,8 @@
4443
@MockitoSettings(strictness = Strictness.WARN)
4544
public class ClusterEventCleanupPeriodicalTest {
4645
private static final DateTime TIME = new DateTime(2015, 4, 1, 0, 0, DateTimeZone.UTC);
46+
private static final Duration maxEventAge = Duration.ofDays(1);
4747

48-
private final ObjectMapper objectMapper = new ObjectMapperProvider().get();
4948
private MongoConnection mongoConnection;
5049
private ClusterEventCleanupPeriodical clusterEventCleanupPeriodical;
5150

@@ -55,7 +54,7 @@ public void setUpService(MongoCollections mongoCollections) throws Exception {
5554

5655
this.mongoConnection = mongoCollections.mongoConnection();
5756

58-
this.clusterEventCleanupPeriodical = new ClusterEventCleanupPeriodical(mongoCollections);
57+
this.clusterEventCleanupPeriodical = new ClusterEventCleanupPeriodical(mongoCollections, maxEventAge);
5958
}
6059

6160
@AfterEach
@@ -66,11 +65,12 @@ public void tearDown() {
6665

6766
@Test
6867
public void testDoRun() throws Exception {
68+
final var maxEventAgeMillis = maxEventAge.toMillis();
6969
final DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
7070
assertThat(insertEvent(collection, 0L)).isTrue();
7171
assertThat(insertEvent(collection, TIME.getMillis())).isTrue();
72-
assertThat(insertEvent(collection, TIME.minus(ClusterEventCleanupPeriodical.DEFAULT_MAX_EVENT_AGE).getMillis())).isTrue();
73-
assertThat(insertEvent(collection, TIME.minus(2 * ClusterEventCleanupPeriodical.DEFAULT_MAX_EVENT_AGE).getMillis())).isTrue();
72+
assertThat(insertEvent(collection, TIME.minus(maxEventAgeMillis).getMillis())).isTrue();
73+
assertThat(insertEvent(collection, TIME.minus(2 * maxEventAgeMillis).getMillis())).isTrue();
7474
assertThat(collection.count()).isEqualTo(4L);
7575

7676
clusterEventCleanupPeriodical.run();

0 commit comments

Comments
 (0)