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 9520539c40..024097762f 100644 --- a/src/main/java/io/floci/aws/services/bedrock/BedrockService.java +++ b/src/main/java/io/floci/aws/services/bedrock/BedrockService.java @@ -154,7 +154,7 @@ public class BedrockService implements ResourceProvider { this.kmsService = kmsService; } - public Guardrail createGuardrail(JsonNode request, String region) { + public synchronized Guardrail createGuardrail(JsonNode request, String region) { String name = requiredName(request); String description = boundedDescription(request); String blockedInputMessaging = requiredBlockedMessaging(request, "blockedInputMessaging"); diff --git a/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailCreateNameConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailCreateNameConcurrencyTest.java new file mode 100644 index 0000000000..d639d5794f --- /dev/null +++ b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailCreateNameConcurrencyTest.java @@ -0,0 +1,63 @@ +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.util.concurrent.atomic.AtomicReference; + +import static io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; + +class BedrockGuardrailCreateNameConcurrencyTest { + + @Test + void overlappingCreatesWithTheSameNameStoreOneGuardrail() throws Exception { + overlappingCreates(true); + } + + @Test + void overlappingCreatesWithDifferentNamesStoreBothGuardrails() throws Exception { + overlappingCreates(false); + } + + private static void overlappingCreates(boolean sameName) throws Exception { + ParkingStore storage = new ParkingStore<>(); + ObjectMapper mapper = new ObjectMapper(); + BedrockService service = new BedrockService(storage, new InMemoryStorage<>(), + new RegionResolver("us-east-1", "000000000000"), mapper, mock(KmsService.class)); + AtomicReference firstResult = new AtomicReference<>(); + AtomicReference secondResult = new AtomicReference<>(); + storage.parkNextWrite(); + assertOperationWaitsForParkedStore(storage, + () -> firstResult.set(service.createGuardrail(request(mapper, "first"), "us-east-1")), + () -> { + if (sameName) { + AwsException conflict = assertThrows(AwsException.class, + () -> service.createGuardrail(request(mapper, "first"), "us-east-1")); + assertEquals("ConflictException", conflict.getErrorCode()); + } else { + secondResult.set(service.createGuardrail(request(mapper, "second"), "us-east-1")); + } + }); + assertNotNull(firstResult.get()); + assertEquals(sameName ? 1 : 2, storage.scan(key -> true).size()); + if (!sameName) { + assertNotNull(secondResult.get()); + } + } + + private static ObjectNode request(ObjectMapper mapper, String name) { + return mapper.createObjectNode().put("name", name) + .put("blockedInputMessaging", "blocked").put("blockedOutputsMessaging", "blocked"); + } +} diff --git a/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java index a40c508887..26305e2b15 100644 --- a/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java +++ b/src/test/java/io/floci/aws/services/bedrock/BedrockGuardrailLifecycleConcurrencyTest.java @@ -10,16 +10,11 @@ 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 io.floci.aws.testing.ParkingStoreAssertions.assertOperationWaitsForParkedStore; 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 { @@ -54,41 +49,15 @@ class BedrockGuardrailLifecycleConcurrencyTest { .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); + assertOperationWaitsForParkedStore(storage, () -> { + 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); } - }); - 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()); + }, () -> service.deleteGuardrail(guardrail.getGuardrailId(), null, REGION)); AwsException missing = assertThrows(AwsException.class, () -> service.getGuardrail(guardrail.getGuardrailId(), null, REGION)); assertEquals("ResourceNotFoundException", missing.getErrorCode());