mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
4f4e7be90e
commit
345e83c34d
9 files changed
+144
-11
No files matched your search
@@ -194,6 +194,7 @@ public interface EmulatorConfig {
|
||||
ConfigStorageConfig config();
|
||||
CodeDeployStorageConfig codedeploy();
|
||||
TranscribeStorageConfig transcribe();
|
||||
TaggingStorageConfig tagging();
|
||||
}
|
||||
|
||||
interface SsmStorageConfig {
|
||||
@@ -374,6 +375,13 @@ public interface EmulatorConfig {
|
||||
long flushIntervalMs();
|
||||
}
|
||||
|
||||
interface TaggingStorageConfig {
|
||||
Optional<String> mode();
|
||||
|
||||
@WithDefault("5000")
|
||||
long flushIntervalMs();
|
||||
}
|
||||
|
||||
interface CodeDeployStorageConfig {
|
||||
Optional<String> mode();
|
||||
|
||||
|
||||
@@ -227,7 +227,8 @@ public class ResolvedServiceCatalog {
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("AmazonEC2ContainerRegistry_V20150921."), Set.of("ecr"), Set.of(), Set.of()),
|
||||
descriptor("tagging", "tagging", config.services().tagging().enabled(), true,
|
||||
null, null, 5000L, null, ServiceProtocol.JSON,
|
||||
"tagging", storageMode(config.storage().services().tagging().mode(), config.storage().mode()),
|
||||
config.storage().services().tagging().flushIntervalMs(), null, ServiceProtocol.JSON,
|
||||
protocols(ServiceProtocol.JSON),
|
||||
Set.of("ResourceGroupsTaggingAPI_20170126."), Set.of("tagging"), Set.of(), Set.of()),
|
||||
descriptor("bedrock-runtime", "bedrock-runtime",
|
||||
|
||||
+38
-7
@@ -1,8 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.resourcegroupstagging;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackedMap;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.resourcegroupstagging.model.ResourceTagMapping;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
@@ -12,8 +17,24 @@ import java.util.stream.Collectors;
|
||||
@ApplicationScoped
|
||||
public class ResourceGroupsTaggingService implements Resettable {
|
||||
|
||||
private final StorageFactory storageFactory;
|
||||
|
||||
// region::arn → ResourceTagMapping
|
||||
private final Map<String, ResourceTagMapping> store = new ConcurrentHashMap<>();
|
||||
private Map<String, ResourceTagMapping> store = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public ResourceGroupsTaggingService(StorageFactory storageFactory) {
|
||||
this.storageFactory = storageFactory;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void initializeStorage() {
|
||||
if (storageFactory == null) {
|
||||
return; // keeps non-CDI unit tests working
|
||||
}
|
||||
this.store = new StorageBackedMap<>(storageFactory.create("tagging",
|
||||
"tagging-resource-mappings.json", new TypeReference<Map<String, ResourceTagMapping>>() {}));
|
||||
}
|
||||
|
||||
private String key(String region, String arn) {
|
||||
return region + "::" + arn;
|
||||
@@ -21,25 +42,35 @@ public class ResourceGroupsTaggingService implements Resettable {
|
||||
|
||||
// ─── TagResources ──────────────────────────────────────────────────────────
|
||||
|
||||
public void tagResources(List<String> resourceArns, Map<String, String> tags, String region) {
|
||||
// Mutators are synchronized: StorageBackedMap has no atomic computeIfAbsent, so
|
||||
// the get-mutate-put sequence would otherwise lose updates under concurrent calls.
|
||||
public synchronized void tagResources(List<String> resourceArns, Map<String, String> tags, String region) {
|
||||
for (String arn : resourceArns) {
|
||||
store.computeIfAbsent(key(region, arn), k -> new ResourceTagMapping(arn))
|
||||
.getTags().putAll(tags);
|
||||
String storeKey = key(region, arn);
|
||||
ResourceTagMapping mapping = store.get(storeKey);
|
||||
if (mapping == null) {
|
||||
mapping = new ResourceTagMapping(arn);
|
||||
}
|
||||
mapping.getTags().putAll(tags);
|
||||
// Re-put so StorageBackedMap routes the mutation through the backend
|
||||
store.put(storeKey, mapping);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── UntagResources ────────────────────────────────────────────────────────
|
||||
|
||||
public void untagResources(List<String> resourceArns, List<String> tagKeys, String region) {
|
||||
public synchronized void untagResources(List<String> resourceArns, List<String> tagKeys, String region) {
|
||||
for (String arn : resourceArns) {
|
||||
ResourceTagMapping mapping = store.get(key(region, arn));
|
||||
String storeKey = key(region, arn);
|
||||
ResourceTagMapping mapping = store.get(storeKey);
|
||||
if (mapping != null) {
|
||||
tagKeys.forEach(mapping.getTags()::remove);
|
||||
store.put(storeKey, mapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteResources(List<String> resourceArns, String region) {
|
||||
public synchronized void deleteResources(List<String> resourceArns, String region) {
|
||||
for (String arn : resourceArns) {
|
||||
store.remove(key(region, arn));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user