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:
andrehoong-px authored and GitHub committed 2026-10-18 15:15:06 -04:00
1 parent 7dc514b52f
commit 06bd8c2207
11 files changed
+1012 -66

No files matched your search

@@ -19,7 +19,9 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Path("/")
@@ -43,7 +45,8 @@ public class S3VectorsController {
@RegisterForReflection
public record CreateVectorBucketRequest(
String vectorBucketName,
Object encryptionConfiguration
Object encryptionConfiguration,
Map<String, String> tags
) {}
@RegisterForReflection
@@ -67,6 +70,7 @@ public class S3VectorsController {
public record VectorBucketRepresentation(
String vectorBucketName,
String vectorBucketArn,
double creationTime,
Object encryptionConfiguration
) {}
@@ -98,10 +102,14 @@ public class S3VectorsController {
String dataType,
int dimension,
String distanceMetric,
Object metadataConfiguration,
Object encryptionConfiguration
MetadataConfiguration metadataConfiguration,
Object encryptionConfiguration,
Map<String, String> tags
) {}
@RegisterForReflection
public record MetadataConfiguration(List<String> nonFilterableMetadataKeys) {}
@RegisterForReflection
public record CreateIndexResponse(
String indexArn
@@ -122,6 +130,7 @@ public class S3VectorsController {
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public record IndexRepresentation(
double creationTime,
String vectorBucketName,
String indexName,
String indexArn,
@@ -256,7 +265,8 @@ public class S3VectorsController {
@Path("/CreateVectorBucket")
public Response createVectorBucket(@Context HttpHeaders headers, CreateVectorBucketRequest request) {
String region = regionResolver.resolveRegion(headers);
VectorBucket bucket = service.createVectorBucket(request.vectorBucketName(), request.encryptionConfiguration(), region);
VectorBucket bucket = service.createVectorBucket(request.vectorBucketName(), request.encryptionConfiguration(),
request.tags() != null ? request.tags() : Map.of(), region);
return Response.ok(new CreateVectorBucketResponse(bucket.getVectorBucketArn())).build();
}
@@ -265,7 +275,8 @@ public class S3VectorsController {
public Response getVectorBucket(@Context HttpHeaders headers, GetVectorBucketRequest request) {
String region = regionResolver.resolveRegion(headers);
VectorBucket bucket = service.getVectorBucket(request.vectorBucketName(), region);
VectorBucketRepresentation rep = new VectorBucketRepresentation(bucket.getVectorBucketName(), bucket.getVectorBucketArn(), bucket.getEncryptionConfiguration());
VectorBucketRepresentation rep = new VectorBucketRepresentation(bucket.getVectorBucketName(),
bucket.getVectorBucketArn(), bucket.getCreationTime().toEpochMilli() / 1000.0, bucket.getEncryptionConfiguration());
return Response.ok(new GetVectorBucketResponse(rep)).build();
}
@@ -275,7 +286,8 @@ public class S3VectorsController {
String region = regionResolver.resolveRegion(headers);
List<VectorBucket> buckets = service.listVectorBuckets(region);
List<VectorBucketRepresentation> reps = buckets.stream()
.map(b -> new VectorBucketRepresentation(b.getVectorBucketName(), b.getVectorBucketArn(), b.getEncryptionConfiguration()))
.map(b -> new VectorBucketRepresentation(b.getVectorBucketName(), b.getVectorBucketArn(),
b.getCreationTime().toEpochMilli() / 1000.0, b.getEncryptionConfiguration()))
.toList();
return Response.ok(new ListVectorBucketsResponse(reps, null)).build();
}
@@ -298,7 +310,10 @@ public class S3VectorsController {
request.dimension(),
request.dataType() != null ? request.dataType() : "float32",
request.distanceMetric() != null ? request.distanceMetric() : "cosine",
List.of(),
request.metadataConfiguration() != null && request.metadataConfiguration().nonFilterableMetadataKeys() != null
? request.metadataConfiguration().nonFilterableMetadataKeys() : List.of(),
request.encryptionConfiguration(),
request.tags() != null ? request.tags() : Map.of(),
region
);
return Response.ok(new CreateIndexResponse(index.getIndexArn())).build();
@@ -310,14 +325,16 @@ public class S3VectorsController {
String region = regionResolver.resolveRegion(headers);
VectorIndex index = service.getIndex(request.vectorBucketName(), request.indexName(), region);
IndexRepresentation rep = new IndexRepresentation(
index.getCreationTime().toEpochMilli() / 1000.0,
index.getVectorBucketName(),
index.getIndexName(),
index.getIndexArn(),
index.getDataType(),
index.getDimension(),
index.getDistanceMetric(),
null,
null
index.getNonFilterableMetadataKeys().isEmpty()
? null : new MetadataConfiguration(index.getNonFilterableMetadataKeys()),
index.getEncryptionConfiguration()
);
return Response.ok(new GetIndexResponse(rep)).build();
}
@@ -329,6 +346,7 @@ public class S3VectorsController {
List<VectorIndex> indexes = service.listIndexes(request.vectorBucketName(), region);
List<IndexRepresentation> reps = indexes.stream()
.map(i -> new IndexRepresentation(
i.getCreationTime().toEpochMilli() / 1000.0,
i.getVectorBucketName(),
i.getIndexName(),
i.getIndexArn(),
@@ -4,7 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
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.VectorData;
@@ -14,7 +14,15 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@@ -22,20 +30,36 @@ import java.util.stream.Collectors;
public class S3VectorsService {
private static final Logger LOG = Logger.getLogger(S3VectorsService.class);
private final StorageBackend<String, VectorBucket> store;
private final AccountAwareStorageBackend<VectorBucket> store;
private final RegionResolver regionResolver;
private final Clock clock;
// Bounded, stable monitors also serialize deletion and recreation of the same bucket.
private final Object[] bucketLocks = new Object[256];
@Inject
public S3VectorsService(StorageFactory factory, RegionResolver regionResolver) {
this(factory, regionResolver, Clock.systemUTC());
}
S3VectorsService(StorageFactory factory, RegionResolver regionResolver, Clock clock) {
this.store = factory.create("s3vectors", "s3vectors.json",
new TypeReference<Map<String, VectorBucket>>() {});
this.regionResolver = regionResolver;
this.clock = clock;
for (int i = 0; i < bucketLocks.length; i++) {
bucketLocks[i] = new Object();
}
}
private String buildStorageKey(String region, String bucketName) {
return region + "::" + bucketName;
}
private Object lockFor(String bucketName, String region) {
String key = store.accountId() + "/" + buildStorageKey(region, bucketName);
return bucketLocks[Math.floorMod(key.hashCode(), bucketLocks.length)];
}
private String buildBucketArn(String region, String bucketName) {
return AwsArnUtils.Arn.of("s3vectors", region, regionResolver.getAccountId(), "bucket/" + bucketName).toString();
}
@@ -45,68 +69,144 @@ public class S3VectorsService {
}
public VectorBucket createVectorBucket(String bucketName, Object encryptionConfiguration, String region) {
String key = buildStorageKey(region, bucketName);
if (store.get(key).isPresent()) {
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " already exists.", 409);
return createVectorBucket(bucketName, encryptionConfiguration, Map.of(), region);
}
public VectorBucket createVectorBucket(String bucketName, Object encryptionConfiguration,
Map<String, String> tags, String region) {
synchronized (lockFor(bucketName, region)) {
String key = buildStorageKey(region, bucketName);
if (store.get(key).isPresent()) {
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " already exists.", 409);
}
String arn = buildBucketArn(region, bucketName);
VectorBucket bucket = new VectorBucket(bucketName, arn,
encryptionConfiguration != null ? encryptionConfiguration : Map.of("sseType", "AES256"));
bucket.setCreationTime(clock.instant());
bucket.setTags(new ConcurrentHashMap<>(tags));
store.put(key, bucket);
LOG.infov("Created Vector Bucket: {0} in region {1}", bucketName, region);
return bucket;
}
String arn = buildBucketArn(region, bucketName);
VectorBucket bucket = new VectorBucket(bucketName, arn, encryptionConfiguration);
store.put(key, bucket);
LOG.infov("Created Vector Bucket: {0} in region {1}", bucketName, region);
return bucket;
}
public VectorBucket getVectorBucket(String bucketName, String region) {
String key = buildStorageKey(region, bucketName);
return store.get(key).orElseThrow(() ->
return findVectorBucket(bucketName, region).orElseThrow(() ->
new AwsException("NotFoundException", "The vector bucket " + bucketName + " does not exist.", 404));
}
public List<VectorBucket> listVectorBuckets(String region) {
return store.scan(k -> k.startsWith(region + "::")).stream()
String prefix = region + "::";
return store.keys().stream().filter(key -> key.startsWith(prefix))
.flatMap(key -> findVectorBucket(key.substring(prefix.length()), region).stream())
.sorted(Comparator.comparing(VectorBucket::getVectorBucketName))
.collect(Collectors.toList());
}
public void deleteVectorBucket(String bucketName, String region) {
String key = buildStorageKey(region, bucketName);
VectorBucket bucket = store.get(key).orElseThrow(() ->
new AwsException("NotFoundException", "The vector bucket " + bucketName + " does not exist.", 404));
if (!bucket.getIndexes().isEmpty()) {
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " is not empty.", 409);
private Optional<VectorBucket> findVectorBucket(String bucketName, String region) {
synchronized (lockFor(bucketName, region)) {
String key = buildStorageKey(region, bucketName);
return store.get(key).map(bucket -> {
if (backfillLegacyMetadata(bucket)) {
store.put(key, bucket);
}
return bucket;
});
}
}
private boolean backfillLegacyMetadata(VectorBucket bucket) {
boolean changed = false;
Instant migrationTime = clock.instant();
if (bucket.getCreationTime() == null) {
bucket.setCreationTime(migrationTime);
changed = true;
}
if (bucket.getEncryptionConfiguration() == null) {
bucket.setEncryptionConfiguration(Map.of("sseType", "AES256"));
changed = true;
}
for (VectorIndex index : bucket.getIndexes().values()) {
if (index.getCreationTime() == null) {
index.setCreationTime(migrationTime);
changed = true;
}
if (index.getEncryptionConfiguration() == null) {
index.setEncryptionConfiguration(bucket.getEncryptionConfiguration());
changed = true;
}
}
return changed;
}
public void deleteVectorBucket(String bucketName, String region) {
synchronized (lockFor(bucketName, region)) {
String key = buildStorageKey(region, bucketName);
VectorBucket bucket = store.get(key).orElseThrow(() ->
new AwsException("NotFoundException", "The vector bucket " + bucketName + " does not exist.", 404));
if (!bucket.getIndexes().isEmpty()) {
throw new AwsException("ConflictException", "The vector bucket " + bucketName + " is not empty.", 409);
}
store.delete(key);
LOG.infov("Deleted Vector Bucket: {0}", bucketName);
}
store.delete(key);
LOG.infov("Deleted Vector Bucket: {0}", bucketName);
}
public VectorIndex createIndex(String bucketName, String indexName, int dimension, String dataType,
String distanceMetric, List<String> nonFilterableMetadataKeys, String region) {
return createIndex(bucketName, indexName, dimension, dataType, distanceMetric,
nonFilterableMetadataKeys, null, Map.of(), region);
}
public VectorIndex createIndex(String bucketName, String indexName, int dimension, String dataType,
String distanceMetric, List<String> nonFilterableMetadataKeys,
Object encryptionConfiguration, Map<String, String> tags, String region) {
if (dimension < 1 || dimension > 4096) {
throw new AwsException("ValidationException", "Dimension must be between 1 and 4096.", 400);
}
if (!"float32".equals(dataType)) {
throw new AwsException("ValidationException", "Unsupported dataType: " + dataType + ". Only float32 is supported.", 400);
}
if (!"cosine".equals(distanceMetric) && !"euclidean".equals(distanceMetric)) {
throw new AwsException("ValidationException", "Unsupported distanceMetric: " + distanceMetric + ". Supported metrics are cosine and euclidean.", 400);
}
VectorBucket bucket = getVectorBucket(bucketName, region);
if (bucket.getIndexes().containsKey(indexName)) {
throw new AwsException("ConflictException", "The index " + indexName + " already exists.", 409);
if (nonFilterableMetadataKeys == null || nonFilterableMetadataKeys.size() > 10
|| nonFilterableMetadataKeys.stream().anyMatch(key -> key == null || key.isBlank() || key.length() > 63)
|| new HashSet<>(nonFilterableMetadataKeys).size() != nonFilterableMetadataKeys.size()) {
throw new AwsException("ValidationException",
"NonFilterableMetadataKeys must contain at most 10 unique keys of 1 to 63 characters.", 400);
}
String arn = buildIndexArn(region, bucketName, indexName);
VectorIndex index = new VectorIndex(indexName, arn, bucketName, dimension, dataType, distanceMetric);
bucket.getIndexes().put(indexName, index);
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Created Index: {0} in bucket {1}", indexName, bucketName);
return index;
synchronized (lockFor(bucketName, region)) {
VectorBucket bucket = getVectorBucket(bucketName, region);
if (bucket.getIndexes().containsKey(indexName)) {
throw new AwsException("ConflictException", "The index " + indexName + " already exists.", 409);
}
String arn = buildIndexArn(region, bucketName, indexName);
VectorIndex index = new VectorIndex(indexName, arn, bucketName, dimension, dataType, distanceMetric);
index.setCreationTime(clock.instant());
index.setNonFilterableMetadataKeys(List.copyOf(nonFilterableMetadataKeys));
index.setEncryptionConfiguration(encryptionConfiguration != null
? encryptionConfiguration : bucket.getEncryptionConfiguration());
index.setTags(new ConcurrentHashMap<>(tags));
bucket.getIndexes().put(indexName, index);
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Created Index: {0} in bucket {1}", indexName, bucketName);
return index;
}
}
public VectorIndex getIndex(String bucketName, String indexName, String region) {
VectorBucket bucket = getVectorBucket(bucketName, region);
return getIndex(getVectorBucket(bucketName, region), indexName);
}
private VectorIndex getIndex(VectorBucket bucket, String indexName) {
VectorIndex index = bucket.getIndexes().get(indexName);
if (index == null) {
throw new AwsException("NotFoundException", "The index " + indexName + " does not exist in bucket " + bucketName + ".", 404);
throw new AwsException("NotFoundException",
"The index " + indexName + " does not exist in bucket " + bucket.getVectorBucketName() + ".", 404);
}
return index;
}
@@ -119,28 +219,32 @@ public class S3VectorsService {
}
public void deleteIndex(String bucketName, String indexName, String region) {
VectorBucket bucket = getVectorBucket(bucketName, region);
if (!bucket.getIndexes().containsKey(indexName)) {
throw new AwsException("NotFoundException", "The index " + indexName + " does not exist.", 404);
synchronized (lockFor(bucketName, region)) {
VectorBucket bucket = getVectorBucket(bucketName, region);
if (!bucket.getIndexes().containsKey(indexName)) {
throw new AwsException("NotFoundException", "The index " + indexName + " does not exist.", 404);
}
bucket.getIndexes().remove(indexName);
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Deleted Index: {0}", indexName);
}
bucket.getIndexes().remove(indexName);
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Deleted Index: {0}", indexName);
}
public void putVectors(String bucketName, String indexName, List<VectorData> vectors, String region) {
VectorBucket bucket = getVectorBucket(bucketName, region);
VectorIndex index = getIndex(bucketName, indexName, region);
synchronized (lockFor(bucketName, region)) {
VectorBucket bucket = getVectorBucket(bucketName, region);
VectorIndex index = getIndex(bucket, indexName);
for (VectorData v : vectors) {
if (v.getData() != null && v.getData().size() != index.getDimension()) {
throw new AwsException("ValidationException",
"Vector dimension " + v.getData().size() + " does not match index dimension " + index.getDimension(), 400);
for (VectorData v : vectors) {
if (v.getData() != null && v.getData().size() != index.getDimension()) {
throw new AwsException("ValidationException",
"Vector dimension " + v.getData().size() + " does not match index dimension " + index.getDimension(), 400);
}
index.getVectors().put(v.getKey(), v);
}
index.getVectors().put(v.getKey(), v);
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Put {0} vectors into index {1}", vectors.size(), indexName);
}
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Put {0} vectors into index {1}", vectors.size(), indexName);
}
public List<VectorData> getVectors(String bucketName, String indexName, List<String> keys, String region) {
@@ -254,13 +358,15 @@ public class S3VectorsService {
public record ListVectorsResult(List<VectorData> vectors, String nextToken) {}
public void deleteVectors(String bucketName, String indexName, List<String> keys, String region) {
VectorBucket bucket = getVectorBucket(bucketName, region);
VectorIndex index = getIndex(bucketName, indexName, region);
for (String key : keys) {
index.getVectors().remove(key);
synchronized (lockFor(bucketName, region)) {
VectorBucket bucket = getVectorBucket(bucketName, region);
VectorIndex index = getIndex(bucket, indexName);
for (String key : keys) {
index.getVectors().remove(key);
}
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Deleted {0} vectors from index {1}", keys.size(), indexName);
}
store.put(buildStorageKey(region, bucketName), bucket);
LOG.infov("Deleted {0} vectors from index {1}", keys.size(), indexName);
}
public List<QueryResult> queryVectors(String bucketName, String indexName, List<Float> queryVector,
@@ -272,6 +378,7 @@ public class S3VectorsService {
}
S3VectorsMetadataFilter metadataFilter = S3VectorsMetadataFilter.compile(filter);
validateFilterKeys(filter, index.getNonFilterableMetadataKeys());
String metric = index.getDistanceMetric() != null ? index.getDistanceMetric().toLowerCase() : "cosine";
List<QueryResult> results = new ArrayList<>();
@@ -297,6 +404,23 @@ public class S3VectorsService {
return results.stream().limit(topK).collect(Collectors.toList());
}
private static void validateFilterKeys(Object filter, List<String> nonFilterableKeys) {
if (!(filter instanceof Map<?, ?> clauses)) {
return;
}
for (Map.Entry<?, ?> clause : clauses.entrySet()) {
if ("$and".equals(clause.getKey()) || "$or".equals(clause.getKey())) {
if (clause.getValue() instanceof List<?> children) {
for (Object child : children) {
validateFilterKeys(child, nonFilterableKeys);
}
}
} else if (nonFilterableKeys.contains(clause.getKey())) {
throw new AwsException("ValidationException", "Cannot filter on non-filterable metadata key: " + clause.getKey(), 400);
}
}
}
private double calculateCosineSimilarity(List<Float> v1, List<Float> v2) {
if (v1 == null || v2 == null || v1.size() != v2.size()) return 0.0;
double dotProduct = 0.0;
@@ -2,11 +2,14 @@ package io.github.hectorvent.floci.services.s3vectors.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RegisterForReflection
public class VectorBucket {
private Instant creationTime;
private Map<String, String> tags = new ConcurrentHashMap<>();
private String vectorBucketName;
private String vectorBucketArn;
private Object encryptionConfiguration;
@@ -20,6 +23,12 @@ public class VectorBucket {
this.encryptionConfiguration = encryptionConfiguration;
}
public Instant getCreationTime() { return creationTime; }
public void setCreationTime(Instant creationTime) { this.creationTime = creationTime; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
public String getVectorBucketName() { return vectorBucketName; }
public void setVectorBucketName(String vectorBucketName) { this.vectorBucketName = vectorBucketName; }
@@ -2,14 +2,20 @@ package io.github.hectorvent.floci.services.s3vectors.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@RegisterForReflection
public class VectorIndex {
private Instant creationTime;
private Map<String, String> tags = new ConcurrentHashMap<>();
private String indexName;
private String indexArn;
private String vectorBucketName;
private Object encryptionConfiguration;
private List<String> nonFilterableMetadataKeys = List.of();
private int dimension;
private String dataType;
private String distanceMetric;
@@ -26,6 +32,12 @@ public class VectorIndex {
this.distanceMetric = distanceMetric;
}
public Instant getCreationTime() { return creationTime; }
public void setCreationTime(Instant creationTime) { this.creationTime = creationTime; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
public String getIndexName() { return indexName; }
public void setIndexName(String indexName) { this.indexName = indexName; }
@@ -35,6 +47,12 @@ public class VectorIndex {
public String getVectorBucketName() { return vectorBucketName; }
public void setVectorBucketName(String vectorBucketName) { this.vectorBucketName = vectorBucketName; }
public Object getEncryptionConfiguration() { return encryptionConfiguration; }
public void setEncryptionConfiguration(Object encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
public List<String> getNonFilterableMetadataKeys() { return nonFilterableMetadataKeys; }
public void setNonFilterableMetadataKeys(List<String> keys) { this.nonFilterableMetadataKeys = keys; }
public int getDimension() { return dimension; }
public void setDimension(int dimension) { this.dimension = dimension; }