mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudwatchlogs): persist event batches once (#3767)
* fix(cloudwatchlogs): persist event batches once * test(storage): cover AccountAware putAll prefixing
This commit is contained in:
1 parent
6a3380d13a
commit
bb67a443ce
7 files changed
+121
-1
No files matched your search
@@ -74,6 +74,14 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
trackOwner(account);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<String, V> entries) {
|
||||
String prefix = prefix() + "/";
|
||||
Map<String, V> prefixedEntries = new LinkedHashMap<>();
|
||||
entries.forEach((key, value) -> prefixedEntries.put(prefix + key, value));
|
||||
delegate.putAll(prefixedEntries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<V> get(String key) {
|
||||
String prefixedKey = prefixed(key);
|
||||
@@ -384,6 +392,13 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
trackOwner(accountId);
|
||||
}
|
||||
|
||||
public void putAllForAccount(String accountId, Map<String, V> entries) {
|
||||
String prefix = accountId + "/";
|
||||
Map<String, V> prefixedEntries = new LinkedHashMap<>();
|
||||
entries.forEach((key, value) -> prefixedEntries.put(prefix + key, value));
|
||||
delegate.putAll(prefixedEntries);
|
||||
}
|
||||
|
||||
public void deleteForAccount(String accountId, String key) {
|
||||
delegate.delete(accountId + "/" + key);
|
||||
}
|
||||
|
||||
@@ -50,6 +50,15 @@ public class PersistentStorage<K, V> implements StorageBackend<K, V> {
|
||||
persistToDisk();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<K, V> entries) {
|
||||
if (entries.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
store.putAll(entries);
|
||||
persistToDisk();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<V> get(K key) {
|
||||
return Optional.ofNullable(store.get(key));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.core.storage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
@@ -15,6 +16,15 @@ public interface StorageBackend<K, V> {
|
||||
|
||||
void put(K key, V value);
|
||||
|
||||
/**
|
||||
* Store several entries as one logical write. Backends that can persist a batch more
|
||||
* efficiently should override this method; the default preserves compatibility for
|
||||
* lightweight and service-specific implementations.
|
||||
*/
|
||||
default void putAll(Map<K, V> entries) {
|
||||
entries.forEach(this::put);
|
||||
}
|
||||
|
||||
Optional<V> get(K key);
|
||||
|
||||
void delete(K key);
|
||||
|
||||
+14
-1
@@ -614,6 +614,7 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
Long minTs = null;
|
||||
Long maxTs = null;
|
||||
List<LogEvent> stored = new ArrayList<>(events.size());
|
||||
Map<String, LogEvent> logEvents = new LinkedHashMap<>();
|
||||
|
||||
for (Map<String, Object> evt : events) {
|
||||
long ts = toLong(evt.get("timestamp"), now);
|
||||
@@ -627,13 +628,14 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
logEvent.setSequence(ingestionSequence.incrementAndGet());
|
||||
|
||||
String eventKey = eventKey(region, groupName, streamName, ts, logEvent.getEventId());
|
||||
putForAccount(eventStore, accountId, eventKey, logEvent);
|
||||
logEvents.put(eventKey, logEvent);
|
||||
stored.add(logEvent);
|
||||
|
||||
totalBytes += msg.getBytes().length + 26; // approx overhead
|
||||
if (minTs == null || ts < minTs) { minTs = ts; }
|
||||
if (maxTs == null || ts > maxTs) { maxTs = ts; }
|
||||
}
|
||||
putAllForAccount(eventStore, accountId, logEvents);
|
||||
|
||||
evictEventsPastRetention(accountId, region, groupName, now);
|
||||
evictEventsBeyondCapacity(accountId);
|
||||
@@ -734,6 +736,17 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
store.put(key, value);
|
||||
}
|
||||
|
||||
private <V> void putAllForAccount(
|
||||
StorageBackend<String, V> store, String accountId, Map<String, V> entries) {
|
||||
if (accountId != null && store instanceof AccountAwareStorageBackend<?> rawAware) {
|
||||
@SuppressWarnings("unchecked")
|
||||
AccountAwareStorageBackend<V> aware = (AccountAwareStorageBackend<V>) rawAware;
|
||||
aware.putAllForAccount(accountId, entries);
|
||||
return;
|
||||
}
|
||||
store.putAll(entries);
|
||||
}
|
||||
|
||||
private <V> Set<String> keysForAccount(StorageBackend<String, V> store, String accountId) {
|
||||
if (accountId != null && store instanceof AccountAwareStorageBackend<?> rawAware) {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
Reference in new issue
Block a user