fix(bedrock-agentcore): serialize runtime tags with deletion (#7721)

This commit is contained in:
Jackson Price authored and GitHub committed 2026-11-08 05:04:49 -04:00
1 parent 9822ad5f05
commit a28c52f951
4 files changed
+261 -67

No files matched your search

@@ -316,19 +316,29 @@ public class BedrockAgentCoreControlService {
// ──────────────────────────── Tagging (Phase 4) ────────────────────────────
public Map<String, String> getTagsByArn(String region, String arn) {
return new HashMap<>(getAgentRuntimeByArn(region, arn).getTags());
Map<String, String> snapshot = new HashMap<>();
updateRuntimeTags(region, arn, snapshot::putAll, false);
return snapshot;
}
public void tagByArn(String region, String arn, Map<String, String> 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<String> 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<Map<String, String>> 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. */
@@ -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<AgentRuntime> 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<Thread> 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());
}
}
@@ -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<AgentRuntimeEndpoint> first = executor.submit(() -> fixture.create("token"));
Future<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, "token"));
try {
assertTrue(fixture.storage.awaitParked());
AtomicReference<Thread> secondThread = new AtomicReference<>();
Future<AgentRuntimeEndpoint> 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<AgentRuntimeEndpoint> first = executor.submit(() -> fixture.create(null));
Future<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, null));
try {
assertTrue(fixture.storage.awaitParked());
AtomicReference<Thread> secondThread = new AtomicReference<>();
Future<AwsException> 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<AgentRuntimeEndpoint> first = executor.submit(() -> fixture.create(null));
Future<AgentRuntimeEndpoint> first = executor.submit(() -> createEndpoint(fixture, null));
try {
assertTrue(fixture.storage.awaitParked());
AtomicReference<Thread> deleteThread = new AtomicReference<>();
Future<AgentRuntime> 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<AgentRuntime> 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();
}
}
@@ -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<Thread> deleteThread = new AtomicReference<>();
Future<AgentRuntime> 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<AgentRuntime> deletion = executor.submit(fixture::delete);
try {
assertTrue(fixture.storage.awaitParked());
AtomicReference<Thread> tagThread = new AtomicReference<>();
Future<AwsException> 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<Thread> readThread = new AtomicReference<>();
Future<Map<String, String>> 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<AgentRuntime> deletion = executor.submit(fixture::delete);
try {
assertTrue(fixture.storage.awaitParked());
AtomicReference<Thread> readThread = new AtomicReference<>();
Future<AwsException> 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<String, String> 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());
}
}