diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java index 5d3dc38305..b8c5ac6aa6 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java @@ -316,19 +316,29 @@ public class BedrockAgentCoreControlService { // ──────────────────────────── Tagging (Phase 4) ──────────────────────────── public Map getTagsByArn(String region, String arn) { - return new HashMap<>(getAgentRuntimeByArn(region, arn).getTags()); + Map snapshot = new HashMap<>(); + updateRuntimeTags(region, arn, snapshot::putAll, false); + return snapshot; } public void tagByArn(String region, String arn, Map tags) { - AgentRuntime runtime = getAgentRuntimeByArn(region, arn); - runtime.getTags().putAll(tags); - storage.put(key(region, runtime.getAgentRuntimeId()), runtime); + updateRuntimeTags(region, arn, runtimeTags -> runtimeTags.putAll(tags), true); } public void untagByArn(String region, String arn, List keys) { - AgentRuntime runtime = getAgentRuntimeByArn(region, arn); - keys.forEach(runtime.getTags()::remove); - storage.put(key(region, runtime.getAgentRuntimeId()), runtime); + updateRuntimeTags(region, arn, runtimeTags -> keys.forEach(runtimeTags::remove), true); + } + + private void updateRuntimeTags(String region, String arn, Consumer> change, + boolean persist) { + String runtimeId = getAgentRuntimeByArn(region, arn).getAgentRuntimeId(); + synchronized (runtimeLock(region, runtimeId)) { + AgentRuntime runtime = getAgentRuntime(runtimeId, region); + change.accept(runtime.getTags()); + if (persist) { + storage.put(key(region, runtimeId), runtime); + } + } } /** The runtime a runtime ARN names, at any version; ResourceNotFoundException when none does. */ diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java new file mode 100644 index 0000000000..1e7f7be5dc --- /dev/null +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java @@ -0,0 +1,49 @@ +package io.floci.aws.services.bedrockagentcorecontrol; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; +import io.floci.aws.testing.ParkingStore; +import io.floci.aws.testing.ThreadStates; + +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +final class BedrockAgentCoreConcurrencyFixture { + + static final String REGION = "us-east-1"; + + final ParkingStore storage = new ParkingStore<>(); + final BedrockAgentCoreControlService service = new BedrockAgentCoreControlService( + storage, new RegionResolver(REGION, "000000000000")); + final String runtimeId; + final String arn; + + BedrockAgentCoreConcurrencyFixture() { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode artifact = mapper.createObjectNode(); + artifact.putObject("containerConfiguration").put("containerUri", "example.invalid/probe:1"); + ObjectNode network = mapper.createObjectNode().put("networkMode", "PUBLIC"); + AgentRuntime runtime = service.createAgentRuntime("probe", artifact, network, + "arn:aws:iam::000000000000:role/probe", null, null, null, null, null, REGION); + runtimeId = runtime.getAgentRuntimeId(); + arn = service.arn(runtime, "1", REGION); + } + + AgentRuntime delete() { + return service.deleteAgentRuntime(runtimeId, null, REGION); + } + + static void assertBlocked(AtomicReference worker, Future task) throws InterruptedException { + ThreadStates.awaitBlockedOrDone(worker, task); + assertNotNull(worker.get(), "the competing worker must start"); + assertEquals(Thread.State.BLOCKED, worker.get().getState(), + "the competing worker must overlap the parked storage operation"); + assertFalse(task.isDone()); + } +} diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java index cf243d92b1..03e039b178 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointConcurrencyTest.java @@ -1,13 +1,8 @@ package io.floci.aws.services.bedrockagentcorecontrol; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsException; -import io.floci.aws.core.common.RegionResolver; import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint; -import io.floci.aws.testing.ParkingStore; -import io.floci.aws.testing.ThreadStates; import org.junit.jupiter.api.Test; import java.util.concurrent.ExecutorService; @@ -16,37 +11,32 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.REGION; +import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.assertBlocked; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; class BedrockAgentCoreEndpointConcurrencyTest { - private static final String REGION = "us-east-1"; - @Test void concurrentClientTokenReplayWaitsForPublication() throws Exception { - Fixture fixture = new Fixture(); + BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); + fixture.storage.parkNextWrite(); try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> fixture.create("token")); + Future first = executor.submit(() -> createEndpoint(fixture, "token")); try { assertTrue(fixture.storage.awaitParked()); AtomicReference secondThread = new AtomicReference<>(); Future second = executor.submit(() -> { secondThread.set(Thread.currentThread()); - return fixture.create("token"); + return createEndpoint(fixture, "token"); }); - ThreadStates.awaitBlockedOrDone(secondThread, second); - assertNotNull(secondThread.get(), "the competing worker must start"); - assertEquals(Thread.State.BLOCKED, secondThread.get().getState(), - "the competing worker must overlap the parked publication"); - assertFalse(second.isDone(), "replay must await the original publication"); + assertBlocked(secondThread, second); fixture.storage.release(); assertSame(first.get(5, TimeUnit.SECONDS), second.get(5, TimeUnit.SECONDS)); - assertEquals(1, fixture.namedEndpointCount()); + assertEquals(1, namedEndpointCount(fixture)); } finally { fixture.storage.release(); } @@ -55,25 +45,22 @@ class BedrockAgentCoreEndpointConcurrencyTest { @Test void concurrentSameNameGetsConflictAfterPublication() throws Exception { - Fixture fixture = new Fixture(); + BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); + fixture.storage.parkNextWrite(); try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> fixture.create(null)); + Future first = executor.submit(() -> createEndpoint(fixture, null)); try { assertTrue(fixture.storage.awaitParked()); AtomicReference secondThread = new AtomicReference<>(); Future second = executor.submit(() -> { secondThread.set(Thread.currentThread()); - return assertThrows(AwsException.class, () -> fixture.create(null)); + return assertThrows(AwsException.class, () -> createEndpoint(fixture, null)); }); - ThreadStates.awaitBlockedOrDone(secondThread, second); - assertNotNull(secondThread.get(), "the competing worker must start"); - assertEquals(Thread.State.BLOCKED, secondThread.get().getState(), - "the competing worker must overlap the parked publication"); - assertFalse(second.isDone(), "the uniqueness decision must await publication"); + assertBlocked(secondThread, second); fixture.storage.release(); first.get(5, TimeUnit.SECONDS); assertEquals("ConflictException", second.get(5, TimeUnit.SECONDS).getErrorCode()); - assertEquals(1, fixture.namedEndpointCount()); + assertEquals(1, namedEndpointCount(fixture)); } finally { fixture.storage.release(); } @@ -82,21 +69,18 @@ class BedrockAgentCoreEndpointConcurrencyTest { @Test void deletingRuntimeCannotBeUndoneByEndpointPublication() throws Exception { - Fixture fixture = new Fixture(); + BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); + fixture.storage.parkNextWrite(); try (ExecutorService executor = Executors.newFixedThreadPool(2)) { - Future first = executor.submit(() -> fixture.create(null)); + Future first = executor.submit(() -> createEndpoint(fixture, null)); try { assertTrue(fixture.storage.awaitParked()); AtomicReference deleteThread = new AtomicReference<>(); Future deletion = executor.submit(() -> { deleteThread.set(Thread.currentThread()); - return fixture.service.deleteAgentRuntime(fixture.runtimeId, null, REGION); + return fixture.delete(); }); - ThreadStates.awaitBlockedOrDone(deleteThread, deletion); - assertNotNull(deleteThread.get(), "the competing worker must start"); - assertEquals(Thread.State.BLOCKED, deleteThread.get().getState(), - "the competing worker must overlap the parked publication"); - assertFalse(deletion.isDone(), "deletion must wait for the in-flight publication"); + assertBlocked(deleteThread, deletion); fixture.storage.release(); first.get(5, TimeUnit.SECONDS); deletion.get(5, TimeUnit.SECONDS); @@ -108,31 +92,13 @@ class BedrockAgentCoreEndpointConcurrencyTest { } } - private static final class Fixture { - private final ParkingStore storage = new ParkingStore<>(); - private final BedrockAgentCoreControlService service = new BedrockAgentCoreControlService( - storage, new RegionResolver(REGION, "000000000000")); - private final String runtimeId; - - private Fixture() { - ObjectMapper mapper = new ObjectMapper(); - ObjectNode artifact = mapper.createObjectNode(); - artifact.putObject("containerConfiguration").put("containerUri", "example.invalid/probe:1"); - ObjectNode network = mapper.createObjectNode().put("networkMode", "PUBLIC"); - runtimeId = service.createAgentRuntime("probe", artifact, network, - "arn:aws:iam::000000000000:role/probe", null, null, - null, null, null, REGION).getAgentRuntimeId(); - storage.parkNextWrite(); - } - - private AgentRuntimeEndpoint create(String clientToken) { - return service.createEndpoint(runtimeId, "shared", null, null, clientToken, REGION); - } - - private long namedEndpointCount() { - return service.getAgentRuntime(runtimeId, REGION).getEndpoints().stream() - .filter(endpoint -> "shared".equals(endpoint.getName())).count(); - } + private static AgentRuntimeEndpoint createEndpoint(BedrockAgentCoreConcurrencyFixture fixture, + String clientToken) { + return fixture.service.createEndpoint(fixture.runtimeId, "shared", null, null, clientToken, REGION); } + private static long namedEndpointCount(BedrockAgentCoreConcurrencyFixture fixture) { + return fixture.service.getAgentRuntime(fixture.runtimeId, REGION).getEndpoints().stream() + .filter(endpoint -> "shared".equals(endpoint.getName())).count(); + } } diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java new file mode 100644 index 0000000000..2e063814cc --- /dev/null +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeTagConcurrencyTest.java @@ -0,0 +1,169 @@ +package io.floci.aws.services.bedrockagentcorecontrol; + +import io.floci.aws.core.common.AwsException; +import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.REGION; +import static io.floci.aws.services.bedrockagentcorecontrol.BedrockAgentCoreConcurrencyFixture.assertBlocked; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BedrockAgentCoreRuntimeTagConcurrencyTest { + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deletionWaitsForTagPublication(boolean remove) throws Exception { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + fixture.storage.parkNextWrite(); + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + Future tagging = executor.submit(() -> changeTags(fixture, remove)); + try { + assertTrue(fixture.storage.awaitParked()); + AtomicReference deleteThread = new AtomicReference<>(); + Future deletion = executor.submit(() -> { + deleteThread.set(Thread.currentThread()); + return fixture.delete(); + }); + assertBlocked(deleteThread, deletion); + fixture.storage.release(); + tagging.get(5, TimeUnit.SECONDS); + deletion.get(5, TimeUnit.SECONDS); + assertAbsent(fixture); + } finally { + fixture.storage.release(); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void tagWriteRechecksExistenceAfterDeletion(boolean remove) throws Exception { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + fixture.storage.parkNextRead(); + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + Future deletion = executor.submit(fixture::delete); + try { + assertTrue(fixture.storage.awaitParked()); + AtomicReference tagThread = new AtomicReference<>(); + Future tagging = executor.submit(() -> { + tagThread.set(Thread.currentThread()); + return assertThrows(AwsException.class, () -> changeTags(fixture, remove)); + }); + assertBlocked(tagThread, tagging); + fixture.storage.release(); + deletion.get(5, TimeUnit.SECONDS); + assertEquals("ResourceNotFoundException", tagging.get(5, TimeUnit.SECONDS).getErrorCode()); + assertAbsent(fixture); + } finally { + fixture.storage.release(); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void tagChangesPreserveOtherTags(boolean remove) { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + changeTags(fixture, remove); + assertEquals(remove ? Map.of("other", "retained") : Map.of("key", "new", "other", "retained"), + fixture.service.getTagsByArn(REGION, fixture.arn)); + fixture.delete(); + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> changeTags(fixture, remove)).getErrorCode()); + assertAbsent(fixture); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void tagReadWaitsForTagPublication(boolean remove) throws Exception { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + fixture.storage.parkNextWrite(); + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + Future tagging = executor.submit(() -> changeTags(fixture, remove)); + try { + assertTrue(fixture.storage.awaitParked()); + AtomicReference readThread = new AtomicReference<>(); + Future> reading = executor.submit(() -> { + readThread.set(Thread.currentThread()); + return fixture.service.getTagsByArn(REGION, fixture.arn); + }); + assertBlocked(readThread, reading); + fixture.storage.release(); + tagging.get(5, TimeUnit.SECONDS); + assertEquals(remove ? Map.of("other", "retained") : Map.of("key", "new", "other", "retained"), + reading.get(5, TimeUnit.SECONDS)); + } finally { + fixture.storage.release(); + } + } + } + + @Test + void tagReadRechecksExistenceAfterDeletion() throws Exception { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + fixture.storage.parkNextRead(); + try (ExecutorService executor = Executors.newFixedThreadPool(2)) { + Future deletion = executor.submit(fixture::delete); + try { + assertTrue(fixture.storage.awaitParked()); + AtomicReference readThread = new AtomicReference<>(); + Future reading = executor.submit(() -> { + readThread.set(Thread.currentThread()); + return assertThrows(AwsException.class, + () -> fixture.service.getTagsByArn(REGION, fixture.arn)); + }); + assertBlocked(readThread, reading); + fixture.storage.release(); + deletion.get(5, TimeUnit.SECONDS); + assertEquals("ResourceNotFoundException", reading.get(5, TimeUnit.SECONDS).getErrorCode()); + assertAbsent(fixture); + } finally { + fixture.storage.release(); + } + } + } + + @Test + void tagReadReturnsDetachedSnapshot() { + BedrockAgentCoreConcurrencyFixture fixture = taggedRuntime(); + Map tags = fixture.service.getTagsByArn(REGION, fixture.arn); + changeTags(fixture, false); + assertEquals(Map.of("key", "old", "other", "retained"), tags); + tags.clear(); + assertEquals(Map.of("key", "new", "other", "retained"), + fixture.service.getTagsByArn(REGION, fixture.arn)); + } + + private static BedrockAgentCoreConcurrencyFixture taggedRuntime() { + BedrockAgentCoreConcurrencyFixture fixture = new BedrockAgentCoreConcurrencyFixture(); + fixture.service.tagByArn(REGION, fixture.arn, Map.of("key", "old", "other", "retained")); + return fixture; + } + + private static void changeTags(BedrockAgentCoreConcurrencyFixture fixture, boolean remove) { + if (remove) { + fixture.service.untagByArn(REGION, fixture.arn, List.of("key")); + } else { + fixture.service.tagByArn(REGION, fixture.arn, Map.of("key", "new")); + } + } + + private static void assertAbsent(BedrockAgentCoreConcurrencyFixture fixture) { + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> fixture.service.getAgentRuntime(fixture.runtimeId, REGION)).getErrorCode()); + assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, + () -> fixture.service.getTagsByArn(REGION, fixture.arn)).getErrorCode()); + } +}