Skip to content

Commit dc42a34

Browse files
committed
revert 10-minute traffic counting
1 parent 210f32d commit dc42a34

File tree

4 files changed

+32
-160
lines changed

4 files changed

+32
-160
lines changed

graylog2-server/src/main/java/org/graylog2/rest/resources/system/TrafficResource.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,4 @@ public TrafficCounterService.TrafficHistogram get(@Parameter(name = "days", desc
5959
daily ? TrafficCounterService.Interval.DAILY : TrafficCounterService.Interval.HOURLY,
6060
includeToday);
6161
}
62-
63-
@GET
64-
@Path("/detailed")
65-
@Operation(summary = "Get the cluster traffic stats with 10-minute granularity")
66-
public TrafficCounterService.TrafficHistogram getDetailed(@Parameter(name = "days", description = "For how many days the traffic stats should be returned")
67-
@QueryParam("days") @DefaultValue("30") int days,
68-
@Parameter(name = "includeToday", description = "Whether the traffic should include up to the current date/time (in UTC).")
69-
@QueryParam("includeToday") @DefaultValue("true") boolean includeToday) {
70-
return trafficCounterService.clusterTrafficOfLastDays(Duration.standardDays(days),
71-
TrafficCounterService.Interval.TEN_MINUTE,
72-
includeToday);
73-
}
7462
}

graylog2-server/src/main/java/org/graylog2/system/traffic/TrafficCounterService.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ public void updateTraffic(DateTime observationTime,
6363
long inLastMinute,
6464
long outLastMinute,
6565
long decodedLastMinute) {
66-
// we bucket traffic data by 10-minute intervals and aggregate it to a day bucket for reporting
67-
final DateTime tenMinuteBucket = TrafficUpdater.getTenMinuteBucketStart(observationTime);
66+
// we bucket traffic data by the hour and aggregate it to a day bucket for reporting
67+
final DateTime dayBucket = TrafficUpdater.getHourBucketStart(observationTime);
6868

6969
if (LOG.isDebugEnabled()) {
7070
LOG.debug("Updating traffic for node {} at {}: in/decoded/out {}/{}/{} bytes",
71-
nodeId, tenMinuteBucket, inLastMinute, decodedLastMinute, outLastMinute);
71+
nodeId, dayBucket, inLastMinute, decodedLastMinute, outLastMinute);
7272
}
7373

7474
final String escapedNodeId = nodeId.toEscapedString();
75-
UpdateResult update = collection.updateOne(Filters.eq(BUCKET, tenMinuteBucket), Updates.combine(
75+
UpdateResult update = collection.updateOne(Filters.eq(BUCKET, dayBucket), Updates.combine(
7676
incUpdate(FIELD_INPUT, escapedNodeId, inLastMinute),
7777
incUpdate(FIELD_OUTPUT, escapedNodeId, outLastMinute),
7878
incUpdate(FIELD_DECODED, escapedNodeId, decodedLastMinute)
7979
), new UpdateOptions().upsert(true));
8080

8181
if (!update.wasAcknowledged()) {
82-
LOG.warn("Unable to update traffic of node {} with bucket {}", nodeId, tenMinuteBucket);
82+
LOG.warn("Unable to update traffic of node {} with bucket {}", nodeId, dayBucket);
8383
}
8484
}
8585

@@ -114,9 +114,7 @@ public TrafficHistogram clusterTrafficOfLastDays(Duration daysToIncludeDuration,
114114
trafficHistograms.add(FIELD_DECODED, trafficDto.bucket(), sumTraffic(trafficDto.decoded()));
115115
});
116116

117-
if (interval == TrafficCounterService.Interval.HOURLY) {
118-
trafficHistograms.aggregateToHourly();
119-
} else if (interval == TrafficCounterService.Interval.DAILY) {
117+
if (interval == TrafficCounterService.Interval.DAILY) {
120118
trafficHistograms.aggregateToDaily();
121119
}
122120

@@ -150,18 +148,7 @@ public static long sumTraffic(Map<String, Long> sumTraffic) {
150148
}
151149

152150
public enum Interval {
153-
/**
154-
* Ten-minute interval buckets (144 per day)
155-
*/
156-
TEN_MINUTE,
157-
/**
158-
* Hourly interval buckets (24 per day) - aggregated from 10-minute buckets
159-
*/
160-
HOURLY,
161-
/**
162-
* Daily aggregation (1 per day)
163-
*/
164-
DAILY
151+
HOURLY, DAILY
165152
}
166153

167154
public static class TrafficHistograms {
@@ -189,10 +176,6 @@ public long sumTraffic(String name) {
189176
.orElse(0L);
190177
}
191178

192-
public void aggregateToHourly() {
193-
histograms.replaceAll((key, value) -> TrafficUpdater.aggregateToHourly(value));
194-
}
195-
196179
public void aggregateToDaily() {
197180
histograms.replaceAll((key, value) -> TrafficUpdater.aggregateToDaily(value));
198181
}

graylog2-server/src/main/java/org/graylog2/system/traffic/TrafficUpdater.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,10 @@ static DateTime getDayBucketStart(DateTime observationTime) {
3434
return observationTime.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
3535
}
3636

37-
static DateTime getTenMinuteBucketStart(DateTime observationTime) {
38-
// Round down to the nearest 10-minute interval
39-
int minute = observationTime.minuteOfHour().get();
40-
int roundedMinute = (minute / 10) * 10;
41-
return observationTime.withMinuteOfHour(roundedMinute).withSecondOfMinute(0).withMillisOfSecond(0);
42-
}
43-
4437
static DateTime getHourBucketStart(DateTime observationTime) {
4538
return observationTime.withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
4639
}
4740

48-
static Map<DateTime, Long> aggregateToHourly(Map<DateTime, Long> histogram) {
49-
return histogram.entrySet().stream()
50-
.collect(Collectors.groupingBy(entry -> entry.getKey().withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0),
51-
TreeMap::new,
52-
Collectors.summingLong(Map.Entry::getValue)));
53-
}
54-
5541
static Map<DateTime, Long> aggregateToDaily(Map<DateTime, Long> histogram) {
5642
return histogram.entrySet().stream()
5743
.collect(Collectors.groupingBy(entry -> entry.getKey().withTimeAtStartOfDay(),

graylog2-server/src/test/java/org/graylog2/system/traffic/TrafficCounterServiceTest.java

Lines changed: 25 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ void updateTrafficAndReadPerDay() {
6060
try {
6161
final DateTime today = getDayBucket(now);
6262

63-
// Record traffic for each 10-minute interval of the current day
64-
// At 08:20, we have completed 50 intervals (0-49) and are in the 51st interval
65-
// 8 hours * 6 (intervals/hour) + 2 intervals (10, 20) = 50 intervals total
66-
int currentInterval = (now.hourOfDay().get() * 6) + (now.minuteOfHour().get() / 10);
67-
IntStream.rangeClosed(0, currentInterval).forEach(interval ->
68-
service.updateTraffic(today.plusMinutes(interval * 10), nodeId, 1, 1, 1));
63+
// Record traffic for each hour of the current day - 9 hours (it's 08:20, so we are in the 9th hour of the day)
64+
IntStream.rangeClosed(0, now.hourOfDay().get()).forEach(hour ->
65+
service.updateTraffic(today.plusHours(hour), nodeId, 1, 1, 1));
6966

70-
// Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
67+
// Record traffic for all previous days - 30 x 24 hours
7168
IntStream.rangeClosed(1, 30).forEach(day ->
72-
IntStream.rangeClosed(0, 143).forEach(interval ->
73-
service.updateTraffic(today.minusDays(day).plusMinutes(interval * 10), nodeId, 1, 1, 1)));
69+
IntStream.rangeClosed(0, 23).forEach(hour ->
70+
service.updateTraffic(today.minusDays(day).plusHours(hour), nodeId, 1, 1, 1)));
7471

7572
// Verify that today is included from the histogram.
7673
final TrafficCounterService.TrafficHistogram trafficHistogramIncludesToday =
@@ -104,68 +101,23 @@ private static void verifyDayTrafficVolume(TrafficCounterService.TrafficHistogra
104101
final ImmutableList<Long> outputValues = ImmutableList.copyOf(histogram.values());
105102

106103
// Check that we got the expected count for each of the previous days. We should get the full counter
107-
// for the complete day. (144 ten-minute intervals per day)
104+
// for the complete day. (24)
108105
for (int i = 0; i < 30; i++) {
109106
assertThat(outputValues.get(i))
110107
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
111-
i, 144, outputValues.get(i))
112-
.isEqualTo(144);
108+
i, 24, outputValues.get(i))
109+
.isEqualTo(24);
113110
}
114111

115-
// Check that we got the correct count for the current day. At 08:20, we have 51 ten-minute intervals
116-
// (8 hours * 6 intervals/hour + 2 intervals for 00, 10, 20 = 51 intervals)
112+
// Check that we got the correct count for the current day. The current day is only in its 9th hour,
113+
// so we should only get a value of 9.
117114
assertThat(outputValues.get(30))
118115
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
119-
30, 51, outputValues.get(30))
120-
.isEqualTo(51);
116+
30, 9, outputValues.get(30))
117+
.isEqualTo(9);
121118
});
122119
}
123120

124-
@Test
125-
void updateTrafficAndReadPerTenMinutes() {
126-
// Make sure we use a fixed time for the test
127-
final DateTime now = DateTime.parse("2017-10-29T08:20:00.000Z");
128-
DateTimeUtils.setCurrentMillisProvider(new InstantMillisProvider(now));
129-
130-
try {
131-
final DateTime today = getDayBucket(now);
132-
133-
// Record traffic for each 10-minute interval of the current day
134-
int currentInterval = (now.hourOfDay().get() * 6) + (now.minuteOfHour().get() / 10);
135-
IntStream.rangeClosed(0, currentInterval).forEach(interval ->
136-
service.updateTraffic(today.plusMinutes(interval * 10), nodeId, 1, 1, 1));
137-
138-
// Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
139-
IntStream.rangeClosed(1, 30).forEach(day ->
140-
IntStream.rangeClosed(0, 143).forEach(interval ->
141-
service.updateTraffic(today.minusDays(day).plusMinutes(interval * 10), nodeId, 1, 1, 1)));
142-
143-
final TrafficCounterService.TrafficHistogram histogramIncludesToday =
144-
service.clusterTrafficOfLastDays(Duration.standardDays(30), TrafficCounterService.Interval.TEN_MINUTE, true);
145-
assertThat(histogramIncludesToday.from()).isEqualTo(getDayBucket(now).minusDays(30));
146-
assertThat(histogramIncludesToday.to()).isEqualTo(now);
147-
148-
// We should have 4371 entries: 30 days (30x144 = 4320) + current day (51 ten-minute intervals)
149-
assertThat(histogramIncludesToday.input()).hasSize(4371);
150-
assertThat(histogramIncludesToday.decoded()).hasSize(4371);
151-
assertThat(histogramIncludesToday.output()).hasSize(4371);
152-
verifyTenMinuteTraffic(histogramIncludesToday, 4371);
153-
154-
final TrafficCounterService.TrafficHistogram histogramExcludesToday =
155-
service.clusterTrafficOfLastDays(Duration.standardDays(30), TrafficCounterService.Interval.TEN_MINUTE, false);
156-
assertThat(histogramExcludesToday.from()).isEqualTo(getDayBucket(now).minusDays(30));
157-
assertThat(histogramExcludesToday.to()).isEqualTo(getDayBucket(now).minusMillis(1));
158-
159-
// We should have 4320 entries: 30 days x 144 ten-minute intervals per day (no current day)
160-
assertThat(histogramExcludesToday.input()).hasSize(4320);
161-
assertThat(histogramExcludesToday.decoded()).hasSize(4320);
162-
assertThat(histogramExcludesToday.output()).hasSize(4320);
163-
verifyTenMinuteTraffic(histogramExcludesToday, 4320);
164-
} finally {
165-
DateTimeUtils.setCurrentMillisSystem();
166-
}
167-
}
168-
169121
@Test
170122
void updateTrafficAndReadPerHour() {
171123
// Make sure we use a fixed time for the test
@@ -175,50 +127,47 @@ void updateTrafficAndReadPerHour() {
175127
try {
176128
final DateTime today = getDayBucket(now);
177129

178-
// Record traffic for each 10-minute interval of the current day
179-
int currentInterval = (now.hourOfDay().get() * 6) + (now.minuteOfHour().get() / 10);
180-
IntStream.rangeClosed(0, currentInterval).forEach(interval ->
181-
service.updateTraffic(today.plusMinutes(interval * 10), nodeId, 1, 1, 1));
130+
// Record traffic for each hour of the current day (now)
131+
IntStream.rangeClosed(0, now.hourOfDay().get()).forEach(hour ->
132+
service.updateTraffic(today.plusHours(hour), nodeId, 1, 1, 1));
182133

183-
// Record traffic for all previous days - 30 days x 144 ten-minute intervals per day
134+
// Record traffic for all previous days
184135
IntStream.rangeClosed(1, 30).forEach(day ->
185-
IntStream.rangeClosed(0, 143).forEach(interval ->
186-
service.updateTraffic(today.minusDays(day).plusMinutes(interval * 10), nodeId, 1, 1, 1)));
136+
IntStream.rangeClosed(0, 23).forEach(hour ->
137+
service.updateTraffic(today.minusDays(day).plusHours(hour), nodeId, 1, 1, 1)));
187138

188-
// Test HOURLY aggregation - should aggregate 10-minute buckets into hourly buckets
189139
final TrafficCounterService.TrafficHistogram histogramIncludesToday =
190140
service.clusterTrafficOfLastDays(Duration.standardDays(30), TrafficCounterService.Interval.HOURLY, true);
191141
assertThat(histogramIncludesToday.from()).isEqualTo(getDayBucket(now).minusDays(30));
192142
assertThat(histogramIncludesToday.to()).isEqualTo(now);
193143

194-
// We should have 729 entries: 30 days (30x24 = 720) + current day (9 hours: 0-8)
195-
// Each hour aggregates 6 ten-minute intervals
144+
// We should have 729 entries, one for each hour in the 30 days (30x24) history and the current day (9 hours)
196145
assertThat(histogramIncludesToday.input()).hasSize(729);
197146
assertThat(histogramIncludesToday.decoded()).hasSize(729);
198147
assertThat(histogramIncludesToday.output()).hasSize(729);
199-
verifyHourlyAggregatedTraffic(histogramIncludesToday, 729, true);
148+
verifyHourTraffic(histogramIncludesToday, 729);
200149

201150
final TrafficCounterService.TrafficHistogram histogramExcludesToday =
202151
service.clusterTrafficOfLastDays(Duration.standardDays(30), TrafficCounterService.Interval.HOURLY, false);
203152
assertThat(histogramExcludesToday.from()).isEqualTo(getDayBucket(now).minusDays(30));
204153
assertThat(histogramExcludesToday.to()).isEqualTo(getDayBucket(now).minusMillis(1));
205154

206-
// We should have 720 entries: 30 days x 24 hours per day (no current day)
155+
// We should have 720 entries, one for each hour in the 30 days (30x24) history and none for the current day.
207156
assertThat(histogramExcludesToday.input()).hasSize(720);
208157
assertThat(histogramExcludesToday.decoded()).hasSize(720);
209158
assertThat(histogramExcludesToday.output()).hasSize(720);
210-
verifyHourlyAggregatedTraffic(histogramExcludesToday, 720, false);
159+
verifyHourTraffic(histogramExcludesToday, 720);
211160
} finally {
212161
DateTimeUtils.setCurrentMillisSystem();
213162
}
214163
}
215164

216-
private static void verifyTenMinuteTraffic(TrafficCounterService.TrafficHistogram trafficHistogram, int bucketCount) {
165+
private static void verifyHourTraffic(TrafficCounterService.TrafficHistogram trafficHistogram, int bucketCount) {
217166
// For each type of traffic, check that we got the correct values
218167
ImmutableList.of(trafficHistogram.input(), trafficHistogram.decoded(), trafficHistogram.output()).forEach(histogram -> {
219168
final ImmutableList<Long> outputValues = ImmutableList.copyOf(histogram.values());
220169

221-
// Check that we got the expected count for each 10-minute interval. We should get one value per interval. (1)
170+
// Check that we got the expected count for each of the previous days. We should get one value per hour. (1)
222171
for (int i = 0; i < bucketCount; i++) {
223172
assertThat(outputValues.get(i))
224173
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
@@ -227,38 +176,4 @@ private static void verifyTenMinuteTraffic(TrafficCounterService.TrafficHistogra
227176
}
228177
});
229178
}
230-
231-
private static void verifyHourlyAggregatedTraffic(TrafficCounterService.TrafficHistogram trafficHistogram,
232-
int bucketCount,
233-
boolean includesPartialCurrentHour) {
234-
// For each type of traffic, check that we got the correct values
235-
ImmutableList.of(trafficHistogram.input(), trafficHistogram.decoded(), trafficHistogram.output()).forEach(histogram -> {
236-
final ImmutableList<Long> outputValues = ImmutableList.copyOf(histogram.values());
237-
238-
if (includesPartialCurrentHour) {
239-
// Check that we got the expected count for each complete hourly bucket
240-
// Each complete hour should have 6 (ten-minute intervals aggregated)
241-
for (int i = 0; i < bucketCount - 1; i++) {
242-
assertThat(outputValues.get(i))
243-
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
244-
i, 6, outputValues.get(i))
245-
.isEqualTo(6);
246-
}
247-
248-
// The last bucket (current hour 08:00-08:59 at 08:20) has only 3 intervals: 08:00, 08:10, 08:20
249-
assertThat(outputValues.get(bucketCount - 1))
250-
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
251-
bucketCount - 1, 3, outputValues.get(bucketCount - 1))
252-
.isEqualTo(3);
253-
} else {
254-
// All hours are complete, each should have 6 ten-minute intervals
255-
for (int i = 0; i < bucketCount; i++) {
256-
assertThat(outputValues.get(i))
257-
.withFailMessage("Value <%s> is not the expected value - expected=%s but got=%s",
258-
i, 6, outputValues.get(i))
259-
.isEqualTo(6);
260-
}
261-
}
262-
});
263-
}
264179
}

0 commit comments

Comments
 (0)