fix(tagging): persist resource tag mappings across restart (#1711)

* fix(tagging): persist resource tag mappings across restart

ResourceGroupsTagging was the last stateful service keeping durable state in
a raw in-memory map. Wrap the store in a StorageBackedMap via StorageFactory
(storage key 'tagging'), restructure tag/untag to get-mutate-put so mutations
flush in persistent and hybrid modes, and wire the storage mode and flush
interval through EmulatorConfig, both application.yml files, and the service
catalog descriptor.

* fix(tagging): synchronize mutators to keep tag updates atomic
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-02 23:39:22 -05:00
1 parent 4f4e7be90e
commit 345e83c34d
9 files changed
+144 -11

No files matched your search

@@ -50,7 +50,7 @@ class EventBridgeSchedulerIntegrationTest {
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT),
new ObjectMapper(), scheduler, invoker, replayDispatcher,
new ResourceGroupsTaggingService());
new ResourceGroupsTaggingService(null));
}
@AfterEach
@@ -44,7 +44,7 @@ class EventBridgeServiceTest {
null,
invokerMock,
null,
new ResourceGroupsTaggingService()
new ResourceGroupsTaggingService(null)
);
}
@@ -69,7 +69,7 @@ class GlueServiceTest {
partitionStore,
new InMemoryStorage<String, Map<String, Object>>(),
new InMemoryStorage<String, UserDefinedFunction>(),
schemaRegistryService, regionResolver, new ResourceGroupsTaggingService());
schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(null));
glueService.createDatabase(new Database("db1"));
}
@@ -0,0 +1,89 @@
package io.github.hectorvent.floci.services.resourcegroupstagging;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.resourcegroupstagging.model.ResourceTagMapping;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Verifies resource tag mappings survive a restart. Two service instances share the same
* {@link StorageFactory} backend; the second simulates a process restart reloading from disk.
*/
class ResourceGroupsTaggingServicePersistenceTest {
private static final String ARN = "arn:aws:s3:us-east-1:000000000000:bucket/persisted-bucket";
@Test
void tagMappingsSurviveRestart() {
SharedStorageFactory storage = new SharedStorageFactory();
ResourceGroupsTaggingService first = serviceWithStorage(storage);
first.tagResources(List.of(ARN), Map.of("Team", "platform", "Env", "dev"), "us-east-1");
ResourceGroupsTaggingService reloaded = serviceWithStorage(storage);
List<ResourceTagMapping> resources =
reloaded.getResources(List.of(), List.of(), List.of(), null, 0, "us-east-1").items();
assertEquals(1, resources.size());
assertEquals(ARN, resources.getFirst().getResourceArn());
assertEquals(Map.of("Team", "platform", "Env", "dev"), resources.getFirst().getTags());
}
@Test
void untaggedKeysDoNotReappearAfterRestart() {
SharedStorageFactory storage = new SharedStorageFactory();
ResourceGroupsTaggingService first = serviceWithStorage(storage);
first.tagResources(List.of(ARN), Map.of("Team", "platform", "Env", "dev"), "us-east-1");
first.untagResources(List.of(ARN), List.of("Env"), "us-east-1");
ResourceGroupsTaggingService reloaded = serviceWithStorage(storage);
List<ResourceTagMapping> resources =
reloaded.getResources(List.of(), List.of(), List.of(), null, 0, "us-east-1").items();
assertEquals(1, resources.size());
assertEquals(Map.of("Team", "platform"), resources.getFirst().getTags());
}
@Test
void deletedResourcesDoNotReappearAfterRestart() {
SharedStorageFactory storage = new SharedStorageFactory();
ResourceGroupsTaggingService first = serviceWithStorage(storage);
first.tagResources(List.of(ARN), Map.of("Team", "platform"), "us-east-1");
first.deleteResources(List.of(ARN), "us-east-1");
ResourceGroupsTaggingService reloaded = serviceWithStorage(storage);
assertTrue(reloaded.getResources(List.of(), List.of(), List.of(), null, 0, "us-east-1")
.items().isEmpty());
}
private static ResourceGroupsTaggingService serviceWithStorage(StorageFactory storage) {
ResourceGroupsTaggingService service = new ResourceGroupsTaggingService(storage);
service.initializeStorage();
return service;
}
private static final class SharedStorageFactory extends StorageFactory {
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
private SharedStorageFactory() {
super(null, null);
}
@Override
@SuppressWarnings("unchecked")
public <V> StorageBackend<String, V> create(String serviceName,
String fileName,
TypeReference<Map<String, V>> typeReference) {
return (StorageBackend<String, V>) stores.computeIfAbsent(fileName, ignored -> new InMemoryStorage<>());
}
}
}