mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudwatch-logs): preserve ingestion order for same-timestamp events (#1592)
* fix(cloudwatch-logs): preserve ingestion order for same-timestamp events GetLogEvents and FilterLogEvents sorted events by timestamp only, so events sharing the same millisecond came back in arbitrary store order. Add a monotonic ingestion sequence to LogEvent and use it as a sort tie-breaker, matching CloudWatch Logs which returns same-timestamp events in ingestion order. The counter is seeded from the highest stored sequence so ordering survives persistence reloads. Closes #1584 * fix(cloudwatch-logs): add eventId as final ordering tie-breaker Legacy events created before the sequence field default to sequence 0, so timestamp+sequence alone left their relative order to arbitrary scan order. Fall back to eventId to keep ordering deterministic. * test(cloudwatch-logs): import ArrayList instead of inline FQN Add an import for java.util.ArrayList and use the short name across the test, replacing inline new java.util.ArrayList<>() usages.
This commit is contained in:
1 parent
189d893803
commit
7f76009cdb
3 files changed
+83
-3
No files matched your search
+25
-2
@@ -20,18 +20,35 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudWatchLogsService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CloudWatchLogsService.class);
|
||||
|
||||
/**
|
||||
* Orders events by timestamp, then by ingestion sequence so that events sharing the same
|
||||
* millisecond timestamp are returned in the order they were ingested (matching CloudWatch Logs).
|
||||
* Falls back to {@code eventId} so ordering stays deterministic even for legacy events that
|
||||
* predate {@code sequence} and therefore share the default value of {@code 0}.
|
||||
*/
|
||||
private static final Comparator<LogEvent> EVENT_ORDER =
|
||||
Comparator.comparingLong(LogEvent::getTimestamp)
|
||||
.thenComparingLong(LogEvent::getSequence)
|
||||
.thenComparing(LogEvent::getEventId);
|
||||
|
||||
private final StorageBackend<String, LogGroup> groupStore;
|
||||
private final StorageBackend<String, LogStream> streamStore;
|
||||
private final StorageBackend<String, LogEvent> eventStore;
|
||||
private final StorageBackend<String, SubscriptionFilter> subscriptionFilterStore;
|
||||
private final RegionResolver regionResolver;
|
||||
private final int maxEventsPerQuery;
|
||||
/**
|
||||
* Monotonic counter assigning an ingestion sequence to each stored event. Seeded from
|
||||
* the highest sequence already in the store so ordering survives persistence reloads.
|
||||
*/
|
||||
private final AtomicLong ingestionSequence;
|
||||
|
||||
@Inject
|
||||
public CloudWatchLogsService(StorageFactory storageFactory,
|
||||
@@ -63,6 +80,11 @@ public class CloudWatchLogsService {
|
||||
this.subscriptionFilterStore = subscriptionFilterStore;
|
||||
this.maxEventsPerQuery = maxEventsPerQuery;
|
||||
this.regionResolver = regionResolver;
|
||||
long maxSequence = eventStore.scan(k -> true).stream()
|
||||
.mapToLong(LogEvent::getSequence)
|
||||
.max()
|
||||
.orElse(0L);
|
||||
this.ingestionSequence = new AtomicLong(maxSequence);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Log Groups ────────────────────────────
|
||||
@@ -247,6 +269,7 @@ public class CloudWatchLogsService {
|
||||
logEvent.setTimestamp(ts);
|
||||
logEvent.setMessage(msg);
|
||||
logEvent.setIngestionTime(now);
|
||||
logEvent.setSequence(ingestionSequence.incrementAndGet());
|
||||
|
||||
String eventKey = eventKey(region, groupName, streamName, ts, logEvent.getEventId());
|
||||
eventStore.put(eventKey, logEvent);
|
||||
@@ -284,7 +307,7 @@ public class CloudWatchLogsService {
|
||||
|
||||
String eventPrefix = eventKeyPrefix(region, groupName, streamName);
|
||||
List<LogEvent> all = eventStore.scan(k -> k.startsWith(eventPrefix));
|
||||
all.sort(Comparator.comparingLong(LogEvent::getTimestamp));
|
||||
all.sort(EVENT_ORDER);
|
||||
|
||||
List<LogEvent> filtered = all.stream()
|
||||
.filter(e -> (startTime == null || e.getTimestamp() >= startTime)
|
||||
@@ -345,7 +368,7 @@ public class CloudWatchLogsService {
|
||||
all.addAll(eventStore.scan(k -> k.startsWith(groupPrefix)));
|
||||
}
|
||||
|
||||
all.sort(Comparator.comparingLong(LogEvent::getTimestamp));
|
||||
all.sort(EVENT_ORDER);
|
||||
|
||||
List<LogEvent> result = all.stream()
|
||||
.filter(e -> (startTime == null || e.getTimestamp() >= startTime)
|
||||
|
||||
@@ -11,6 +11,12 @@ public class LogEvent {
|
||||
private long timestamp;
|
||||
private String message;
|
||||
private long ingestionTime;
|
||||
/**
|
||||
* Monotonically increasing ingestion sequence number, used to preserve ingestion
|
||||
* order as a tie-breaker when multiple events share the same millisecond timestamp.
|
||||
* Matches CloudWatch Logs, which returns same-timestamp events in ingestion order.
|
||||
*/
|
||||
private long sequence;
|
||||
|
||||
public LogEvent() {}
|
||||
|
||||
@@ -25,4 +31,7 @@ public class LogEvent {
|
||||
|
||||
public long getIngestionTime() { return ingestionTime; }
|
||||
public void setIngestionTime(long ingestionTime) { this.ingestionTime = ingestionTime; }
|
||||
|
||||
public long getSequence() { return sequence; }
|
||||
public void setSequence(long sequence) { this.sequence = sequence; }
|
||||
}
|
||||
Reference in new issue
Block a user