fix(ram): serialize resource share mutations (#6784)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-28 00:38:12 -04:00
1 parent 2d3fb0ce87
commit ac42b3064e
2 files changed
+103 -11

No files matched your search

@@ -349,14 +349,15 @@ public class RamService {
return result;
}
public ResourceShare deleteResourceShare(String resourceShareArn, String callerAccountId, String region) {
public synchronized ResourceShare deleteResourceShare(String resourceShareArn, String callerAccountId,
String region) {
return putForOwner(
requireOwnedShare(resourceShareArn, callerAccountId, region).withStatus("DELETED"));
}
public ResourceShare updateResourceShare(String resourceShareArn, String name,
Boolean allowExternalPrincipals, String callerAccountId,
String region) {
public synchronized ResourceShare updateResourceShare(String resourceShareArn, String name,
Boolean allowExternalPrincipals, String callerAccountId,
String region) {
ResourceShare share = requireOwnedShare(resourceShareArn, callerAccountId, region);
if (name != null) {
share = share.withName(name);
@@ -386,9 +387,9 @@ public class RamService {
}
}
public ResourceShare disassociateResourceShare(String resourceShareArn, List<String> resourceArns,
List<String> principals, String callerAccountId,
String region) {
public synchronized ResourceShare disassociateResourceShare(String resourceShareArn,
List<String> resourceArns, List<String> principals,
String callerAccountId, String region) {
ResourceShare share = requireOwnedShare(resourceShareArn, callerAccountId, region);
ResourceShare updated = share.withPrincipalsAndResources(
withoutAll(share.getPrincipals(), principals),
@@ -529,16 +530,16 @@ public class RamService {
&& parsed.resource().length() > RESOURCE_SHARE_PREFIX.length();
}
public void tagResource(String resourceShareArn, Map<String, String> newTags, String callerAccountId,
String region) {
public synchronized void tagResource(String resourceShareArn, Map<String, String> newTags,
String callerAccountId, String region) {
ResourceShare share = requireOwnedShare(resourceShareArn, callerAccountId, region);
Map<String, String> merged = new LinkedHashMap<>(share.getTags());
merged.putAll(newTags);
putForOwner(share.withTags(merged));
}
public void untagResource(String resourceShareArn, List<String> tagKeys, String callerAccountId,
String region) {
public synchronized void untagResource(String resourceShareArn, List<String> tagKeys,
String callerAccountId, String region) {
ResourceShare share = requireOwnedShare(resourceShareArn, callerAccountId, region);
Map<String, String> remaining = new LinkedHashMap<>(share.getTags());
tagKeys.forEach(remaining::remove);
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.ram.model.ResourceShare;
import io.github.hectorvent.floci.services.ram.model.ResourceShareAssociation;
import io.github.hectorvent.floci.services.ram.model.ResourceShareInvitation;
import io.github.hectorvent.floci.services.ram.model.SharedResource;
import io.github.hectorvent.floci.testing.ThreadStates;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
@@ -33,6 +34,8 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
@@ -569,6 +572,45 @@ class RamServiceTest {
.findFirst().orElseThrow().getLastUpdatedTime().isAfter(seeded.getLastUpdatedTime()));
}
@Test
void concurrentTagAndDisassociateDoNotLoseEitherMutation() throws Exception {
TestStorageFactory storage = new TestStorageFactory(false);
RamService concurrentService = new RamService(storage, null);
concurrentService.initializeStorage();
ResourceShare share = concurrentService.createResourceShare(
"concurrent-mutation", List.of(), List.of(TGW_ARN), false, "us-east-1", OWNER);
storage.armShareScans();
ExecutorService executor = Executors.newFixedThreadPool(2);
AtomicReference<Thread> tagThread = new AtomicReference<>();
try {
Future<?> disassociate = executor.submit(() -> concurrentService.disassociateResourceShare(
share.getResourceShareArn(), List.of(TGW_ARN), List.of(), OWNER, "us-east-1"));
assertTrue(storage.awaitFirstShareScan(), "the disassociation must reach its share scan");
Future<?> tag = executor.submit(() -> {
tagThread.set(Thread.currentThread());
concurrentService.tagResource(share.getResourceShareArn(), Map.of("race", "preserve"),
OWNER, "us-east-1");
});
ThreadStates.awaitBlockedOrDone(tagThread, tag);
assertTrue(tag.isDone() || storage.firstShareScanReleased()
|| tagThread.get() != null && tagThread.get().getState() == Thread.State.BLOCKED,
"the tag worker must scan or block on the service lock");
storage.releaseFirstShareScan();
disassociate.get(15, TimeUnit.SECONDS);
tag.get(15, TimeUnit.SECONDS);
} finally {
storage.releaseFirstShareScan();
executor.shutdownNow();
}
ResourceShare stored = concurrentService.getResourceShares(OWNER, "SELF", "us-east-1").getFirst();
assertTrue(stored.getResourceArns().isEmpty(),
"the disassociation must survive the concurrent tag update");
assertEquals("preserve", stored.getTags().get("race"),
"the tag must survive the concurrent disassociation");
}
@Test
void accountPrincipalWithoutOrganizationSharingCreatesPendingInvitation() {
ResourceShare share = service.createResourceShare(
@@ -877,12 +919,32 @@ class RamServiceTest {
private void armShareReads() {
rawStores.get("ram-resource-shares.json").armReads();
}
private void armShareScans() {
rawStores.get("ram-resource-shares.json").armScans();
}
private boolean awaitFirstShareScan() throws InterruptedException {
return rawStores.get("ram-resource-shares.json").firstScanEntered.await(5, TimeUnit.SECONDS);
}
private void releaseFirstShareScan() {
rawStores.get("ram-resource-shares.json").releaseFirstScan.countDown();
}
private boolean firstShareScanReleased() {
return rawStores.get("ram-resource-shares.json").releaseFirstScan.getCount() == 0;
}
}
private static final class RawStorage extends InMemoryStorage<String, Object> {
private final boolean coordinateReads;
private final AtomicInteger coordinatedReads = new AtomicInteger();
private volatile CountDownLatch readGate = new CountDownLatch(0);
private final AtomicInteger coordinatedScans = new AtomicInteger();
private volatile CountDownLatch firstScanEntered = new CountDownLatch(0);
private volatile CountDownLatch releaseFirstScan = new CountDownLatch(0);
private volatile boolean coordinateScans;
private RawStorage(boolean coordinateReads) {
this.coordinateReads = coordinateReads;
@@ -893,6 +955,35 @@ class RamServiceTest {
readGate = new CountDownLatch(2);
}
private void armScans() {
coordinatedScans.set(0);
firstScanEntered = new CountDownLatch(1);
releaseFirstScan = new CountDownLatch(1);
coordinateScans = true;
}
@Override
public List<Object> scan(Predicate<String> keyFilter) {
List<Object> snapshot = super.scan(keyFilter);
if (!coordinateScans) {
return snapshot;
}
int scanNumber = coordinatedScans.incrementAndGet();
if (scanNumber == 1) {
firstScanEntered.countDown();
try {
assertTrue(releaseFirstScan.await(5, TimeUnit.SECONDS),
"the first share scan must be released");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError("share scan interrupted", e);
}
} else if (scanNumber == 2) {
releaseFirstScan.countDown();
}
return snapshot;
}
@Override
public Optional<Object> get(String key) {
if (coordinateReads && key.startsWith(OWNER + "/") && coordinatedReads.incrementAndGet() <= 2) {