diff --git a/src/main/java/io/floci/aws/services/bedrock/BedrockService.java b/src/main/java/io/floci/aws/services/bedrock/BedrockService.java index 67c501ed25..9520539c40 100644 --- a/src/main/java/io/floci/aws/services/bedrock/BedrockService.java +++ b/src/main/java/io/floci/aws/services/bedrock/BedrockService.java @@ -203,7 +203,7 @@ public class BedrockService implements ResourceProvider { "Guardrail " + guardrailId + " version " + effectiveVersion + " does not exist.", 404)); } - public Guardrail updateGuardrail(String identifier, JsonNode request, String region) { + public synchronized Guardrail updateGuardrail(String identifier, JsonNode request, String region) { String name = requiredName(request); String description = boundedDescription(request); String blockedInputMessaging = requiredBlockedMessaging(request, "blockedInputMessaging"); @@ -230,7 +230,7 @@ public class BedrockService implements ResourceProvider { * {@code guardrailVersion} is a {@code GuardrailNumericalVersion} in the AWS model, * so DRAFT cannot be deleted on its own. */ - public void deleteGuardrail(String identifier, String version, String region) { + public synchronized void deleteGuardrail(String identifier, String version, String region) { String guardrailId = resolveGuardrailId(identifier); if (version != null && !version.isBlank()) { if (!NUMERICAL_VERSION_PATTERN.matcher(version).matches()) { @@ -398,7 +398,7 @@ public class BedrockService implements ResourceProvider { } } - public Guardrail createGuardrailVersion(String identifier, String description, String region) { + public synchronized Guardrail createGuardrailVersion(String identifier, String description, String region) { Guardrail draft = getGuardrail(identifier, DRAFT_VERSION, region); String guardrailId = draft.getGuardrailId(); String version = String.valueOf(nextVersionNumber(guardrailId, region)); diff --git a/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java new file mode 100644 index 0000000000..a40c508887 --- /dev/null +++ b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java @@ -0,0 +1,101 @@ +package io.floci.aws.services.bedrock; + +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.core.storage.InMemoryStorage; +import io.floci.aws.services.bedrock.model.Guardrail; +import io.floci.aws.services.kms.KmsService; +import io.floci.aws.testing.ParkingStore; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +class BedrockGuardrailLifecycleConcurrencyTest { + + private static final String REGION = "us-east-1"; + + @Test + void deletionAfterAnOverlappingTagWriteLeavesTheGuardrailAbsent() throws Exception { + overlappingWrite(Write.TAG); + } + + @Test + void deletionAfterAnOverlappingUpdateLeavesTheGuardrailAbsent() throws Exception { + overlappingWrite(Write.UPDATE); + } + + @Test + void deletionAfterOverlappingVersionCreationLeavesTheNumberedVersionAbsent() throws Exception { + overlappingWrite(Write.VERSION); + } + + private enum Write { + TAG, UPDATE, VERSION + } + + private static void overlappingWrite(Write operation) throws Exception { + ParkingStore storage = new ParkingStore<>(); + ObjectMapper mapper = new ObjectMapper(); + BedrockService service = new BedrockService(storage, new InMemoryStorage<>(), + new RegionResolver(REGION, "000000000000"), mapper, mock(KmsService.class)); + ObjectNode request = mapper.createObjectNode().put("name", "lifecycle") + .put("blockedInputMessaging", "blocked").put("blockedOutputsMessaging", "blocked"); + Guardrail guardrail = service.createGuardrail(request, REGION); + storage.parkNextWrite(); + AtomicReference failure = new AtomicReference<>(); + Thread writer = new Thread(() -> { + try { + if (operation == Write.UPDATE) { + service.updateGuardrail(guardrail.getGuardrailId(), request.deepCopy().put("name", "changed"), REGION); + } else if (operation == Write.VERSION) { + service.createGuardrailVersion(guardrail.getGuardrailId(), "snapshot", REGION); + } else { + service.tagResource(guardrail.getGuardrailArn(), Map.of("owner", "test"), REGION); + } + } catch (Throwable e) { + failure.set(e); + } + }); + Thread deletion = new Thread(() -> { + try { + service.deleteGuardrail(guardrail.getGuardrailId(), null, REGION); + } catch (Throwable e) { + failure.set(e); + } + }); + writer.start(); + try { + assertTrue(storage.awaitParked()); + deletion.start(); + await().atMost(Duration.ofSeconds(5)).until(() -> + deletion.getState() == Thread.State.BLOCKED || deletion.getState() == Thread.State.TERMINATED); + } finally { + storage.release(); + writer.join(Duration.ofSeconds(5)); + deletion.join(Duration.ofSeconds(5)); + } + assertFalse(writer.isAlive()); + assertFalse(deletion.isAlive()); + assertNull(failure.get()); + AwsException missing = assertThrows(AwsException.class, + () -> service.getGuardrail(guardrail.getGuardrailId(), null, REGION)); + assertEquals("ResourceNotFoundException", missing.getErrorCode()); + if (operation == Write.VERSION) { + AwsException missingVersion = assertThrows(AwsException.class, + () -> service.getGuardrail(guardrail.getGuardrailId(), "1", REGION)); + assertEquals("ResourceNotFoundException", missingVersion.getErrorCode()); + } + } +}