mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3vectors): preserve resource configuration and serialize writes (#5892)
* fix(s3vectors): preserve resource configuration and serialize writes * fix(s3vectors): match index configuration response contracts * fix(s3vectors): stabilize legacy metadata and query validation * fix(s3vectors): use an injectable clock for resource timestamps
This commit is contained in:
1 parent
7dc514b52f
commit
06bd8c2207
11 files changed
+1012
-66
No files matched your search
+86
@@ -0,0 +1,86 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3vectors.S3VectorsClient;
|
||||
import software.amazon.awssdk.services.s3vectors.model.GetVectorBucketResponse;
|
||||
import software.amazon.awssdk.services.s3vectors.model.Index;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class S3VectorsLegacySdkIntegrationTest {
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@Inject
|
||||
S3VectorsService service;
|
||||
|
||||
@Inject
|
||||
StorageFactory factory;
|
||||
|
||||
@Inject
|
||||
ObjectMapper mapper;
|
||||
|
||||
@Test
|
||||
void legacyBucketAndIndexMetadataIsAvailableThroughSdkGetAndList() throws Exception {
|
||||
String region = "us-east-1";
|
||||
String name = "legacy-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
service.createVectorBucket(name, null, region);
|
||||
service.createIndex(name, "old", 1, "float32", "cosine", List.of(), region);
|
||||
ObjectNode legacyJson = mapper.valueToTree(service.getVectorBucket(name, region));
|
||||
legacyJson.remove(List.of("creationTime", "encryptionConfiguration"));
|
||||
ObjectNode legacyIndex = (ObjectNode) legacyJson.path("indexes").path("old");
|
||||
legacyIndex.remove(List.of("creationTime", "encryptionConfiguration"));
|
||||
AccountAwareStorageBackend<VectorBucket> store = factory.create("s3vectors", "s3vectors.json",
|
||||
new TypeReference<Map<String, VectorBucket>>() {});
|
||||
store.put(region + "::" + name, mapper.treeToValue(legacyJson, VectorBucket.class));
|
||||
|
||||
try (S3VectorsClient vectors = S3VectorsClient.builder().endpointOverride(endpoint).region(Region.of(region))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder()).build()) {
|
||||
try {
|
||||
// Creating an index must also resolve the legacy bucket's missing encryption default.
|
||||
vectors.createIndex(request -> request.vectorBucketName(name).indexName("new")
|
||||
.dimension(1).dataType("float32").distanceMetric("cosine"));
|
||||
GetVectorBucketResponse bucket = vectors.getVectorBucket(request -> request.vectorBucketName(name));
|
||||
assertEquals("AES256", bucket.vectorBucket().encryptionConfiguration().sseTypeAsString());
|
||||
assertNotNull(bucket.vectorBucket().creationTime());
|
||||
assertEquals(bucket.vectorBucket().creationTime(), vectors.listVectorBuckets(request -> request.maxResults(100))
|
||||
.vectorBuckets().stream().filter(item -> name.equals(item.vectorBucketName()))
|
||||
.findFirst().orElseThrow().creationTime());
|
||||
for (String indexName : List.of("old", "new")) {
|
||||
Index index = vectors.getIndex(request -> request.vectorBucketName(name).indexName(indexName)).index();
|
||||
assertEquals("AES256", index.encryptionConfiguration().sseTypeAsString());
|
||||
assertNotNull(index.creationTime());
|
||||
assertEquals(index.creationTime(), vectors.listIndexes(request -> request.vectorBucketName(name))
|
||||
.indexes().stream().filter(item -> indexName.equals(item.indexName()))
|
||||
.findFirst().orElseThrow().creationTime());
|
||||
}
|
||||
} finally {
|
||||
for (VectorIndex index : service.listIndexes(name, region)) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index.getIndexName()));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.HybridStorage;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.core.storage.WalStorage;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class S3VectorsPersistenceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String KEY = ACCOUNT + "/" + REGION + "::legacy";
|
||||
private static final Map<String, String> DEFAULT_ENCRYPTION = Map.of("sseType", "AES256");
|
||||
private static final Instant KNOWN_TIME = Instant.parse("2026-01-01T00:00:00Z");
|
||||
private static final Instant MIGRATION_TIME = Instant.parse("2026-02-01T00:00:00Z");
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"persistent,false", "persistent,true", "hybrid,false", "hybrid,true", "wal,false", "wal,true"})
|
||||
void legacyMetadataIsSavedOnceAndSurvivesReload(String mode, boolean listFirst, @TempDir Path directory)
|
||||
throws Exception {
|
||||
Path snapshot = directory.resolve("s3vectors.json");
|
||||
Files.writeString(snapshot, """
|
||||
{
|
||||
"000000000000/us-east-1::legacy": {
|
||||
"vectorBucketName": "legacy",
|
||||
"encryptionConfiguration": null,
|
||||
"indexes": {
|
||||
"old": {"indexName": "old", "dimension": 1, "dataType": "float32", "distanceMetric": "cosine"},
|
||||
"configured": {
|
||||
"indexName": "configured",
|
||||
"creationTime": "2026-01-01T00:00:00Z",
|
||||
"encryptionConfiguration": {"sseType": "aws:kms", "kmsKeyArn": "key"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""");
|
||||
StorageBackend<String, VectorBucket> first = open(mode, snapshot);
|
||||
try {
|
||||
VectorBucket legacy = first.get(KEY).orElseThrow();
|
||||
assertNull(legacy.getCreationTime());
|
||||
assertNull(legacy.getIndexes().get("old").getCreationTime());
|
||||
S3VectorsService service = service(first, Clock.fixed(MIGRATION_TIME, ZoneOffset.UTC));
|
||||
VectorBucket bucket = listFirst ? service.listVectorBuckets(REGION).getFirst()
|
||||
: service.getVectorBucket("legacy", REGION);
|
||||
VectorIndex index = service.getIndex("legacy", "old", REGION);
|
||||
assertEquals(MIGRATION_TIME, bucket.getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, index.getCreationTime());
|
||||
assertEquals(DEFAULT_ENCRYPTION, bucket.getEncryptionConfiguration());
|
||||
assertEquals(DEFAULT_ENCRYPTION, index.getEncryptionConfiguration());
|
||||
assertEquals(KNOWN_TIME, bucket.getIndexes().get("configured").getCreationTime());
|
||||
assertEquals(Map.of("sseType", "aws:kms", "kmsKeyArn", "key"),
|
||||
bucket.getIndexes().get("configured").getEncryptionConfiguration());
|
||||
assertEquals(MIGRATION_TIME, service.listVectorBuckets(REGION).getFirst().getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, service.listIndexes("legacy", REGION).stream()
|
||||
.filter(item -> "old".equals(item.getIndexName())).findFirst().orElseThrow().getCreationTime());
|
||||
verify(first, times(1)).put(eq(KEY), any());
|
||||
|
||||
VectorIndex created = service.createIndex("legacy", "new", 1, "float32", "cosine", List.of(), REGION);
|
||||
assertEquals(DEFAULT_ENCRYPTION, created.getEncryptionConfiguration());
|
||||
assertEquals(MIGRATION_TIME, created.getCreationTime());
|
||||
} finally {
|
||||
close(first);
|
||||
}
|
||||
|
||||
StorageBackend<String, VectorBucket> second = open(mode, snapshot);
|
||||
try {
|
||||
VectorBucket stored = second.get(KEY).orElseThrow();
|
||||
assertEquals(MIGRATION_TIME, stored.getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, stored.getIndexes().get("old").getCreationTime());
|
||||
assertEquals(DEFAULT_ENCRYPTION, stored.getEncryptionConfiguration());
|
||||
S3VectorsService restarted = service(second, Clock.fixed(MIGRATION_TIME.plusSeconds(86_400), ZoneOffset.UTC));
|
||||
assertEquals(MIGRATION_TIME, restarted.getVectorBucket("legacy", REGION).getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, restarted.listVectorBuckets(REGION).getFirst().getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, restarted.getIndex("legacy", "old", REGION).getCreationTime());
|
||||
assertEquals(MIGRATION_TIME, restarted.getIndex("legacy", "new", REGION).getCreationTime());
|
||||
assertEquals(KNOWN_TIME, restarted.getIndex("legacy", "configured", REGION).getCreationTime());
|
||||
assertEquals(DEFAULT_ENCRYPTION, restarted.getIndex("legacy", "new", REGION).getEncryptionConfiguration());
|
||||
assertEquals(3, restarted.listIndexes("legacy", REGION).size());
|
||||
verify(second, never()).put(anyString(), any());
|
||||
} finally {
|
||||
close(second);
|
||||
}
|
||||
}
|
||||
|
||||
private static S3VectorsService service(StorageBackend<String, VectorBucket> backend, Clock clock) {
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.<VectorBucket>create(anyString(), anyString(), any()))
|
||||
.thenReturn(new AccountAwareStorageBackend<>(backend, null, ACCOUNT));
|
||||
return new S3VectorsService(factory, new RegionResolver(REGION, ACCOUNT), clock);
|
||||
}
|
||||
|
||||
private static StorageBackend<String, VectorBucket> open(String mode, Path snapshot) {
|
||||
TypeReference<Map<String, VectorBucket>> type = new TypeReference<>() {};
|
||||
StorageBackend<String, VectorBucket> backend = spy(switch (mode) {
|
||||
case "persistent" -> new PersistentStorage<>(snapshot, type);
|
||||
case "hybrid" -> new HybridStorage<>(snapshot, type, 60_000);
|
||||
case "wal" -> new WalStorage<>(snapshot, snapshot.resolveSibling("s3vectors.wal"), type, 60_000);
|
||||
default -> throw new IllegalArgumentException(mode);
|
||||
});
|
||||
backend.load();
|
||||
return backend;
|
||||
}
|
||||
|
||||
private static void close(StorageBackend<String, VectorBucket> backend) {
|
||||
if (backend instanceof HybridStorage<?, ?> hybrid) {
|
||||
hybrid.shutdown();
|
||||
} else if (backend instanceof WalStorage<?, ?> wal) {
|
||||
wal.shutdown();
|
||||
} else {
|
||||
backend.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.document.Document;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3vectors.S3VectorsClient;
|
||||
import software.amazon.awssdk.services.s3vectors.model.Index;
|
||||
import software.amazon.awssdk.services.s3vectors.model.PutInputVector;
|
||||
import software.amazon.awssdk.services.s3vectors.model.QueryVectorsResponse;
|
||||
import software.amazon.awssdk.services.s3vectors.model.ValidationException;
|
||||
import software.amazon.awssdk.services.s3vectors.model.VectorBucket;
|
||||
import software.amazon.awssdk.services.s3vectors.model.VectorData;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
class S3VectorsSdkIntegrationTest {
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@Inject
|
||||
S3VectorsService service;
|
||||
|
||||
@Test
|
||||
void sdkCreatedResourcesPreserveConfigurationAndRejectNonFilterableQueries() {
|
||||
String name = "sdk-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (S3VectorsClient vectors = vectors("us-east-1")) {
|
||||
vectors.createVectorBucket(request -> request.vectorBucketName(name)
|
||||
.encryptionConfiguration(encryption -> encryption.sseType("AES256"))
|
||||
.tags(Map.of("team", "search")));
|
||||
try {
|
||||
vectors.createIndex(request -> request.vectorBucketName(name).indexName("index")
|
||||
.dataType("float32").dimension(3).distanceMetric("cosine")
|
||||
.metadataConfiguration(metadata -> metadata.nonFilterableMetadataKeys("text"))
|
||||
.tags(Map.of("team", "search")));
|
||||
Index index = vectors.getIndex(request -> request.vectorBucketName(name).indexName("index")).index();
|
||||
VectorBucket bucket = vectors.getVectorBucket(request -> request.vectorBucketName(name)).vectorBucket();
|
||||
assertNotNull(bucket.creationTime());
|
||||
assertNotNull(index.creationTime());
|
||||
assertEquals(service.getVectorBucket(name, "us-east-1").getCreationTime().toEpochMilli(),
|
||||
bucket.creationTime().toEpochMilli());
|
||||
assertEquals(service.getIndex(name, "index", "us-east-1").getCreationTime().toEpochMilli(),
|
||||
index.creationTime().toEpochMilli());
|
||||
assertEquals(bucket.creationTime(), vectors.listVectorBuckets(request -> request.maxResults(100))
|
||||
.vectorBuckets().stream()
|
||||
.filter(item -> name.equals(item.vectorBucketName())).findFirst().orElseThrow().creationTime());
|
||||
assertEquals(index.creationTime(), vectors.listIndexes(request -> request.vectorBucketName(name))
|
||||
.indexes().getFirst().creationTime());
|
||||
assertEquals(List.of("text"), index.metadataConfiguration().nonFilterableMetadataKeys());
|
||||
assertEquals("AES256", index.encryptionConfiguration().sseTypeAsString());
|
||||
assertEquals(Map.of("team", "search"), service.getVectorBucket(name, "us-east-1").getTags());
|
||||
assertEquals(Map.of("team", "search"), service.getIndex(name, "index", "us-east-1").getTags());
|
||||
assertThrows(ValidationException.class, () -> vectors.queryVectors(request -> request
|
||||
.vectorBucketName(name).indexName("index").topK(1)
|
||||
.queryVector(VectorData.builder().float32(1.0f, 0.0f, 0.0f).build())
|
||||
.filter(Document.fromMap(Map.of("text", Document.fromString("hello"))))));
|
||||
} finally {
|
||||
for (String index : service.listIndexes(name, "us-east-1").stream()
|
||||
.map(VectorIndex::getIndexName).toList()) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Stream<Arguments> invalidIndexSettings() {
|
||||
return Stream.of(
|
||||
Arguments.of("float64", "cosine", List.of(), "dataType"),
|
||||
Arguments.of("float32", "manhattan", List.of(), "distanceMetric"),
|
||||
Arguments.of("float32", "cosine", List.of(""), "NonFilterableMetadataKeys"),
|
||||
Arguments.of("float32", "cosine", List.of(" "), "NonFilterableMetadataKeys"),
|
||||
Arguments.of("float32", "cosine", Collections.singletonList(null), "NonFilterableMetadataKeys"),
|
||||
Arguments.of("float32", "cosine", List.of("a".repeat(64)), "NonFilterableMetadataKeys"),
|
||||
Arguments.of("float32", "cosine", List.of("text", "text"), "NonFilterableMetadataKeys"),
|
||||
Arguments.of("float32", "cosine", Collections.nCopies(11, "key"), "NonFilterableMetadataKeys"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"$and", "$or"})
|
||||
void logicalOperatorsAreNotTreatedAsNonFilterableFields(String operator) {
|
||||
String name = "logical-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (S3VectorsClient vectors = vectors("us-east-1")) {
|
||||
vectors.createVectorBucket(request -> request.vectorBucketName(name));
|
||||
try {
|
||||
vectors.createIndex(request -> request.vectorBucketName(name).indexName("index")
|
||||
.dataType("float32").dimension(1).distanceMetric("cosine")
|
||||
.metadataConfiguration(metadata -> metadata.nonFilterableMetadataKeys("$and", "$or", "text")));
|
||||
vectors.putVectors(request -> request.vectorBucketName(name).indexName("index")
|
||||
.vectors(PutInputVector.builder().key("match")
|
||||
.data(VectorData.builder().float32(1.0f).build())
|
||||
.metadata(Document.fromMap(Map.of("category", Document.fromString("x")))).build()));
|
||||
Document filter = Document.fromMap(Map.of(operator, Document.fromList(List.of(
|
||||
Document.fromMap(Map.of("category", Document.fromString("x")))))));
|
||||
QueryVectorsResponse result = vectors.queryVectors(request -> request.vectorBucketName(name)
|
||||
.indexName("index").queryVector(VectorData.builder().float32(1.0f).build()).topK(1).filter(filter));
|
||||
assertEquals(List.of("match"), result.vectors().stream().map(item -> item.key()).toList());
|
||||
|
||||
Document forbidden = Document.fromMap(Map.of(operator, Document.fromList(List.of(
|
||||
Document.fromMap(Map.of("$and", Document.fromList(List.of(
|
||||
Document.fromMap(Map.of("text", Document.fromString("hidden")))))))))));
|
||||
ValidationException error = assertThrows(ValidationException.class, () -> vectors.queryVectors(request -> request
|
||||
.vectorBucketName(name).indexName("index").queryVector(VectorData.builder().float32(1.0f).build())
|
||||
.topK(1).filter(forbidden)));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertTrue(error.getMessage().contains("text"));
|
||||
} finally {
|
||||
for (VectorIndex index : service.listIndexes(name, "us-east-1")) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index.getIndexName()));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void configurationAppearsOnlyInGetIndexAndEmptyMetadataIsOmitted(boolean configured) {
|
||||
String name = "response-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (S3VectorsClient vectors = vectors("us-east-1")) {
|
||||
vectors.createVectorBucket(request -> request.vectorBucketName(name));
|
||||
try {
|
||||
vectors.createIndex(request -> {
|
||||
request.vectorBucketName(name).indexName("index").dimension(3)
|
||||
.dataType("float32").distanceMetric("cosine");
|
||||
if (configured) {
|
||||
request.metadataConfiguration(metadata -> metadata.nonFilterableMetadataKeys("text"));
|
||||
}
|
||||
});
|
||||
Index index = vectors.getIndex(request -> request.vectorBucketName(name).indexName("index")).index();
|
||||
assertEquals("AES256", index.encryptionConfiguration().sseTypeAsString());
|
||||
if (configured) {
|
||||
assertEquals(List.of("text"), index.metadataConfiguration().nonFilterableMetadataKeys());
|
||||
} else {
|
||||
assertNull(index.metadataConfiguration());
|
||||
}
|
||||
|
||||
Map<String, Object> detail = given().baseUri(endpoint.toString()).contentType("application/json")
|
||||
.body(Map.of("vectorBucketName", name, "indexName", "index"))
|
||||
.post("/GetIndex").then().statusCode(200).extract().path("index");
|
||||
assertEquals(configured, detail.containsKey("metadataConfiguration"));
|
||||
assertTrue(detail.containsKey("encryptionConfiguration"));
|
||||
// SDK clients ignore unknown fields, so check the wire response as well.
|
||||
Map<String, Object> summary = given().baseUri(endpoint.toString()).contentType("application/json")
|
||||
.body(Map.of("vectorBucketName", name))
|
||||
.post("/ListIndexes").then().statusCode(200).extract().path("indexes[0]");
|
||||
assertEquals(index.indexArn(), summary.get("indexArn"));
|
||||
assertFalse(summary.containsKey("metadataConfiguration"));
|
||||
assertFalse(summary.containsKey("encryptionConfiguration"));
|
||||
} finally {
|
||||
for (VectorIndex index : service.listIndexes(name, "us-east-1")) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index.getIndexName()));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidIndexSettings")
|
||||
void invalidIndexSettingsReturnValidationErrors(String dataType, String distanceMetric,
|
||||
List<String> keys, String errorMember) {
|
||||
String name = "invalid-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (S3VectorsClient vectors = vectors("us-east-1")) {
|
||||
vectors.createVectorBucket(request -> request.vectorBucketName(name));
|
||||
try {
|
||||
ValidationException error = assertThrows(ValidationException.class,
|
||||
() -> vectors.createIndex(request -> request.vectorBucketName(name).indexName("index")
|
||||
.dimension(3).dataType(dataType).distanceMetric(distanceMetric)
|
||||
.metadataConfiguration(metadata -> metadata.nonFilterableMetadataKeys(keys))));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertTrue(error.getMessage().contains(errorMember), error.getMessage());
|
||||
assertTrue(vectors.listIndexes(request -> request.vectorBucketName(name)).indexes().isEmpty());
|
||||
} finally {
|
||||
for (VectorIndex index : service.listIndexes(name, "us-east-1")) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index.getIndexName()));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 1, 4096, 4097})
|
||||
void dimensionBoundariesAreEnforcedThroughTheSdk(int dimension) {
|
||||
String name = "dimension-vectors-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
try (S3VectorsClient vectors = vectors("us-east-1")) {
|
||||
vectors.createVectorBucket(request -> request.vectorBucketName(name));
|
||||
try {
|
||||
if (dimension == 0 || dimension == 4097) {
|
||||
ValidationException error = assertThrows(ValidationException.class,
|
||||
() -> vectors.createIndex(request -> request.vectorBucketName(name).indexName("index")
|
||||
.dimension(dimension).dataType("float32").distanceMetric("cosine")));
|
||||
assertEquals(400, error.statusCode());
|
||||
assertTrue(vectors.listIndexes(request -> request.vectorBucketName(name)).indexes().isEmpty());
|
||||
} else {
|
||||
vectors.createIndex(request -> request.vectorBucketName(name).indexName("index")
|
||||
.dimension(dimension).dataType("float32").distanceMetric("cosine"));
|
||||
assertEquals(dimension, vectors.getIndex(request -> request.vectorBucketName(name)
|
||||
.indexName("index")).index().dimension());
|
||||
}
|
||||
} finally {
|
||||
for (VectorIndex index : service.listIndexes(name, "us-east-1")) {
|
||||
vectors.deleteIndex(request -> request.vectorBucketName(name).indexName(index.getIndexName()));
|
||||
}
|
||||
vectors.deleteVectorBucket(request -> request.vectorBucketName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private S3VectorsClient vectors(String region) {
|
||||
return S3VectorsClient.builder().endpointOverride(endpoint).region(Region.of(region))
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder()).build();
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorIndex;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class S3VectorsServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final Instant CREATION_TIME = Instant.parse("2026-02-01T00:00:00Z");
|
||||
private S3VectorsService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageFactory factory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory("000000000000");
|
||||
}
|
||||
};
|
||||
service = new S3VectorsService(factory, new RegionResolver(REGION, "000000000000"),
|
||||
Clock.fixed(CREATION_TIME, ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistsCreationTimesConfigurationAndCreationTags() throws Exception {
|
||||
Map<String, String> encryption = Map.of("sseType", "aws:kms", "kmsKeyArn", "key");
|
||||
VectorBucket bucket = service.createVectorBucket("bucket", encryption, Map.of("old", "value"), REGION);
|
||||
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "cosine", List.of("text"),
|
||||
null, Map.of("old", "value"), REGION);
|
||||
assertEquals(CREATION_TIME, bucket.getCreationTime());
|
||||
assertEquals(CREATION_TIME, index.getCreationTime());
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
|
||||
VectorBucket restored = mapper.readValue(mapper.writeValueAsString(bucket), VectorBucket.class);
|
||||
VectorIndex restoredIndex = restored.getIndexes().get("index");
|
||||
assertEquals(bucket.getCreationTime(), restored.getCreationTime());
|
||||
assertEquals(index.getCreationTime(), restoredIndex.getCreationTime());
|
||||
assertEquals(encryption, restoredIndex.getEncryptionConfiguration());
|
||||
assertEquals(List.of("text"), restoredIndex.getNonFilterableMetadataKeys());
|
||||
assertEquals(Map.of("old", "value"), restored.getTags());
|
||||
assertEquals(Map.of("old", "value"), restoredIndex.getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void indexEncryptionCanOverrideTheBucketDefault() {
|
||||
VectorBucket bucket = service.createVectorBucket("bucket", null, REGION);
|
||||
assertEquals(Map.of("sseType", "AES256"), bucket.getEncryptionConfiguration());
|
||||
Map<String, String> encryption = Map.of("sseType", "aws:kms", "kmsKeyArn", "key");
|
||||
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "cosine", List.of(),
|
||||
encryption, Map.of(), REGION);
|
||||
assertEquals(encryption, index.getEncryptionConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonFilterableMetadataKeysAreRejectedEvenInsideLogicalFilters() {
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
service.createIndex("bucket", "index", 3, "float32", "cosine", List.of("text"), REGION);
|
||||
for (Object filter : List.of(Map.of("text", "hello"), Map.of("$and", List.of(Map.of("text", "hello"))))) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.queryVectors("bucket", "index", List.of(1.0f, 0.0f, 0.0f), filter, 1, REGION));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
}
|
||||
assertEquals(List.of(), service.queryVectors("bucket", "index", List.of(1.0f, 0.0f, 0.0f),
|
||||
Map.of("category", "hello"), 1, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidIndexDimensions() {
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
for (int dimension : List.of(0, 4097)) {
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.createIndex("bucket", "index", dimension, "float32", "cosine", List.of(), REGION));
|
||||
}
|
||||
assertTrue(service.listIndexes("bucket", REGION).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidMetadataKeysFailBeforeCreatingAnIndex() {
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
for (List<String> keys : List.of(List.of(""), List.of(" "), List.of("a".repeat(64)),
|
||||
Collections.nCopies(11, "key"), List.of("text", "text"), Collections.<String>singletonList(null))) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.createIndex("bucket", "index", 3, "float32", "cosine", keys, REGION));
|
||||
assertEquals("ValidationException", error.getErrorCode());
|
||||
}
|
||||
assertTrue(service.listIndexes("bucket", REGION).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void metadataKeyLimitsAreInclusive() {
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
List<String> keys = List.of("a".repeat(63), "b", "c", "d", "e", "f", "g", "h", "i", "j");
|
||||
VectorIndex index = service.createIndex("bucket", "index", 3, "float32", "euclidean", keys, REGION);
|
||||
assertEquals(keys, index.getNonFilterableMetadataKeys());
|
||||
}
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
package io.github.hectorvent.floci.services.s3vectors;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorBucket;
|
||||
import io.github.hectorvent.floci.services.s3vectors.model.VectorData;
|
||||
import io.github.hectorvent.floci.testing.ThreadStates;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class S3VectorsWriteLockTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private ParkingStorage storage;
|
||||
private S3VectorsService service;
|
||||
|
||||
private void setUp(boolean copyOnAccess) {
|
||||
storage = new ParkingStorage(copyOnAccess);
|
||||
StorageFactory factory = mock(StorageFactory.class);
|
||||
when(factory.<VectorBucket>create(anyString(), anyString(), any()))
|
||||
.thenReturn(new AccountAwareStorageBackend<>(storage, null, "000000000000"));
|
||||
service = new S3VectorsService(factory, new RegionResolver(REGION, "000000000000"));
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
}
|
||||
|
||||
private void createIndex() {
|
||||
service.createIndex("bucket", "index", 1, "float32", "cosine", List.of(), REGION);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void vectorWritesPreserveConcurrentIndexCreation(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
createIndex();
|
||||
overlap(() -> putVector("first"), () -> service.createIndex("bucket", "second", 1,
|
||||
"float32", "cosine", List.of(), REGION));
|
||||
assertEquals("second", service.getIndex("bucket", "second", REGION).getIndexName());
|
||||
assertEquals(1, service.getVectors("bucket", "index", List.of("first"), REGION).size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void concurrentVectorWritesPreserveBothVectors(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
createIndex();
|
||||
overlap(() -> putVector("first"), () -> putVector("second"));
|
||||
assertEquals(2, service.getVectors("bucket", "index", List.of("first", "second"), REGION).size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void vectorWritesDoNotReviveDeletedVectorsOrIndexes(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
createIndex();
|
||||
putVector("old");
|
||||
overlap(() -> putVector("new"), () -> service.deleteVectors("bucket", "index", List.of("old"), REGION));
|
||||
assertTrue(service.getVectors("bucket", "index", List.of("old"), REGION).isEmpty());
|
||||
assertEquals(1, service.getVectors("bucket", "index", List.of("new"), REGION).size());
|
||||
|
||||
overlap(() -> putVector("new"), () -> service.deleteIndex("bucket", "index", REGION));
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.getIndex("bucket", "index", REGION));
|
||||
assertEquals("NotFoundException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void indexDeletionCannotResurrectADeletedOrRecreatedBucket(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
createIndex();
|
||||
overlap(() -> service.deleteIndex("bucket", "index", REGION), () -> service.deleteVectorBucket("bucket", REGION));
|
||||
assertThrows(AwsException.class, () -> service.getVectorBucket("bucket", REGION));
|
||||
|
||||
service.createVectorBucket("bucket", null, REGION);
|
||||
createIndex();
|
||||
overlap(() -> service.deleteIndex("bucket", "index", REGION), () -> {
|
||||
service.deleteVectorBucket("bucket", REGION);
|
||||
service.createVectorBucket("bucket", null, Map.of("new", "tag"), REGION);
|
||||
});
|
||||
assertEquals(Map.of("new", "tag"), service.getVectorBucket("bucket", REGION).getTags());
|
||||
}
|
||||
|
||||
private void putVector(String key) {
|
||||
service.putVectors("bucket", "index", List.of(new VectorData(key, List.of(1.0f), Map.of())), REGION);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void legacyMetadataMigrationDoesNotOverwriteConcurrentWrites(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
VectorBucket legacy = new VectorBucket("bucket", "bucket-arn", null);
|
||||
storage.put("000000000000/" + REGION + "::bucket", legacy);
|
||||
overlap(() -> service.listVectorBuckets(REGION), this::createIndex);
|
||||
assertNotNull(service.getVectorBucket("bucket", REGION).getCreationTime());
|
||||
assertEquals(Map.of("sseType", "AES256"), service.getIndex("bucket", "index", REGION).getEncryptionConfiguration());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void legacyMetadataMigrationDoesNotReviveADeletedBucket(boolean copyOnAccess) throws Exception {
|
||||
setUp(copyOnAccess);
|
||||
storage.put("000000000000/" + REGION + "::bucket", new VectorBucket("bucket", "bucket-arn", null));
|
||||
overlap(() -> service.getVectorBucket("bucket", REGION), () -> service.deleteVectorBucket("bucket", REGION));
|
||||
assertTrue(service.listVectorBuckets(REGION).isEmpty());
|
||||
}
|
||||
|
||||
private void overlap(Runnable first, Runnable second) throws Exception {
|
||||
Gate gate = new Gate();
|
||||
storage.nextWrite.set(gate);
|
||||
FutureTask<Void> firstTask = new FutureTask<>(first, null);
|
||||
FutureTask<Void> secondTask = new FutureTask<>(second, null);
|
||||
Thread firstThread = Thread.ofPlatform().daemon().unstarted(firstTask);
|
||||
Thread secondThread = Thread.ofPlatform().daemon().unstarted(secondTask);
|
||||
firstThread.start();
|
||||
try {
|
||||
assertTrue(gate.entered.await(5, TimeUnit.SECONDS), "First writer did not reach storage");
|
||||
secondThread.start();
|
||||
ThreadStates.awaitBlockedOrDone(secondThread);
|
||||
} finally {
|
||||
gate.release.countDown();
|
||||
}
|
||||
firstTask.get(5, TimeUnit.SECONDS);
|
||||
secondTask.get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private static final class Gate {
|
||||
private final CountDownLatch entered = new CountDownLatch(1);
|
||||
private final CountDownLatch release = new CountDownLatch(1);
|
||||
}
|
||||
|
||||
private static final class ParkingStorage extends InMemoryStorage<String, VectorBucket> {
|
||||
private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
|
||||
private final boolean copyOnAccess;
|
||||
private final AtomicReference<Gate> nextWrite = new AtomicReference<>();
|
||||
|
||||
private ParkingStorage(boolean copyOnAccess) {
|
||||
this.copyOnAccess = copyOnAccess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<VectorBucket> get(String key) {
|
||||
return super.get(key).map(this::copy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, VectorBucket value) {
|
||||
Gate gate = nextWrite.getAndSet(null);
|
||||
if (gate != null) {
|
||||
gate.entered.countDown();
|
||||
try {
|
||||
assertTrue(gate.release.await(5, TimeUnit.SECONDS), "Paused write was not released");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new AssertionError("Paused write was interrupted", e);
|
||||
}
|
||||
}
|
||||
super.put(key, copy(value));
|
||||
}
|
||||
|
||||
private VectorBucket copy(VectorBucket value) {
|
||||
return copyOnAccess ? mapper.convertValue(value, VectorBucket.class) : value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user