mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock): serialize guardrail name creation (#8484)
CreateGuardrail now runs under the service lock every other guardrail writer already holds, so its name check and write can no longer overlap another create: of two concurrent creates with the same name, the second gets ConflictException instead of storing a second guardrail. The new concurrency test and BedrockGuardrailLifecycleConcurrencyTest stage their races through ParkingStoreAssertions instead of their own threads and waits. Closes #8483
This commit is contained in:
1 parent
fdbcbb68ec
commit
db36808d38
3 files changed
+73
-41
No files matched your search
+63
@@ -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<Guardrail> 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<Guardrail> firstResult = new AtomicReference<>();
|
||||
AtomicReference<Guardrail> 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");
|
||||
}
|
||||
}
|
||||
+9
-40
@@ -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<Throwable> 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());
|
||||
|
||||
Reference in new issue
Block a user