mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(storage): migrate legacy entries by exact key (#3408)
* fix(storage): migrate legacy entries by exact key * fix(storage): use legacy migrator for bulk scans * chore: retrigger CI
This commit is contained in:
1 parent
ebe9cf2264
commit
d92211d1c1
2 files changed
+138
-15
No files matched your search
+65
-15
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.storage;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -11,6 +12,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -31,6 +34,8 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class AccountAwareStorageBackend<V> implements StorageBackend<String, V> {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(AccountAwareStorageBackend.class);
|
||||
|
||||
/** A stored value together with its owning AWS account and account-relative key. */
|
||||
public record AccountEntry<T>(String accountId, String key, T value) {}
|
||||
|
||||
@@ -163,6 +168,59 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates legacy entries owned by {@code accountId} to account-relative destination keys.
|
||||
* The source key is deleted exactly, including for unprefixed gen-0 entries.
|
||||
*/
|
||||
public synchronized void migrateLegacyEntries(
|
||||
String accountId,
|
||||
Predicate<String> legacyKeyFilter,
|
||||
Function<V, String> destinationKey,
|
||||
Predicate<V> legacyOwner) {
|
||||
migrateLegacyEntries(accountId, legacyKeyFilter,
|
||||
(ignored, value) -> destinationKey.apply(value), legacyOwner);
|
||||
}
|
||||
|
||||
private synchronized void migrateLegacyEntries(
|
||||
String accountId,
|
||||
Predicate<String> legacyKeyFilter,
|
||||
BiFunction<String, V, String> destinationKey,
|
||||
Predicate<V> legacyOwner) {
|
||||
for (String rawKey : new ArrayList<>(delegate.keys())) {
|
||||
boolean prefixed = hasAccountPrefix(rawKey);
|
||||
String owner = prefixed ? rawKey.substring(0, 12) : defaultAccountId;
|
||||
if (!accountId.equals(owner)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String logicalKey = prefixed ? rawKey.substring(13) : rawKey;
|
||||
if (!legacyKeyFilter.test(logicalKey)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Optional<V> value = delegate.get(rawKey);
|
||||
if (value.isEmpty() || !legacyOwner.test(value.get())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String newLogicalKey = destinationKey.apply(logicalKey, value.get());
|
||||
if (newLogicalKey == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String destination = accountId + "/" + newLogicalKey;
|
||||
if (!destination.equals(rawKey)) {
|
||||
if (delegate.get(destination).isEmpty()) {
|
||||
delegate.put(destination, value.get());
|
||||
} else {
|
||||
LOG.warnv("Legacy storage migration skipped stale value at {0}; destination {1} already exists",
|
||||
rawKey, destination);
|
||||
}
|
||||
delegate.delete(rawKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all entries across every account as a map of logical-key (account prefix stripped)
|
||||
* to value. Entries without a slash-prefixed account segment are skipped.
|
||||
@@ -194,25 +252,17 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
*/
|
||||
public Map<String, V> scanAllAccountsRaw() {
|
||||
Map<String, V> result = new LinkedHashMap<>();
|
||||
List<String> legacyKeys = new ArrayList<>();
|
||||
for (String rawKey : delegate.keys()) {
|
||||
if (rawKey.indexOf('/') < 0) {
|
||||
legacyKeys.add(rawKey);
|
||||
continue;
|
||||
if (rawKey.indexOf('/') >= 0) {
|
||||
delegate.get(rawKey).ifPresent(v -> result.put(rawKey, v));
|
||||
}
|
||||
delegate.get(rawKey).ifPresent(v -> result.put(rawKey, v));
|
||||
}
|
||||
for (String rawKey : legacyKeys) {
|
||||
String effectiveKey = defaultAccountId + "/" + rawKey;
|
||||
if (result.containsKey(effectiveKey)) {
|
||||
delegate.delete(rawKey);
|
||||
continue;
|
||||
migrateLegacyEntries(defaultAccountId, key -> key.indexOf('/') < 0,
|
||||
(key, value) -> key, value -> true);
|
||||
for (String rawKey : delegate.keys()) {
|
||||
if (rawKey.indexOf('/') >= 0) {
|
||||
delegate.get(rawKey).ifPresent(v -> result.put(rawKey, v));
|
||||
}
|
||||
delegate.get(rawKey).ifPresent(v -> {
|
||||
delegate.put(effectiveKey, v);
|
||||
delegate.delete(rawKey);
|
||||
result.put(effectiveKey, v);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user