mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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;
|
||||
}
|
||||
|
||||
+73
@@ -15,6 +15,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -219,6 +220,78 @@ class AccountAwareStorageBackendTest {
|
||||
Set.copyOf(storage.scanAllAccountEntries(key -> key.startsWith("us-east-1::"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateLegacyEntriesMovesGen0KeyAndDeletesExactSource() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("legacy", "owned");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "111111111111");
|
||||
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/" + value, "owned"::equals);
|
||||
|
||||
assertEquals("owned", raw.get("111111111111/us-east-1/owned").orElseThrow());
|
||||
assertFalse(raw.get("legacy").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateLegacyEntriesMovesGen1KeyAndDeletesExactSource() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("111111111111/legacy", "owned");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/" + value, "owned"::equals);
|
||||
|
||||
assertEquals("owned", raw.get("111111111111/us-east-1/owned").orElseThrow());
|
||||
assertFalse(raw.get("111111111111/legacy").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateLegacyEntriesIsIdempotent() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("legacy", "owned");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "111111111111");
|
||||
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/" + value, "owned"::equals);
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/" + value, "owned"::equals);
|
||||
|
||||
assertEquals(Set.of("111111111111/us-east-1/owned"), raw.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateLegacyEntriesLeavesNonOwnerUntouched() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("legacy", "foreign");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "111111111111");
|
||||
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/" + value, "owned"::equals);
|
||||
|
||||
assertEquals("foreign", raw.get("legacy").orElseThrow());
|
||||
assertTrue(raw.get("111111111111/us-east-1/foreign").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrateLegacyEntriesDeletesSourceWhenDestinationAlreadyExists() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("111111111111/legacy", "stale");
|
||||
raw.put("111111111111/us-east-1/current", "current");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
|
||||
storage.migrateLegacyEntries("111111111111", "legacy"::equals,
|
||||
value -> "us-east-1/current", "stale"::equals);
|
||||
|
||||
assertEquals("current", raw.get("111111111111/us-east-1/current").orElseThrow());
|
||||
assertFalse(raw.get("111111111111/legacy").isPresent());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code get} and {@code getForAccountMigratingLegacy} both migrate a pre-multi-account entry, and
|
||||
* both delete the legacy key once they have copied it. Unless they hold the same monitor, two readers
|
||||
|
||||
Reference in new issue
Block a user