mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(s3tables): return the bucket and namespace ids and the namespace owner (#9917)
GetTableBucket, ListTableBuckets, GetNamespace, ListNamespaces, GetTable and ListTables now return tableBucketId and namespaceId where the S3 Tables model defines them, and GetNamespace and ListNamespaces return the required createdBy and ownerAccountId. New buckets and namespaces get a random id their children repeat; a bucket or namespace stored by an earlier Floci gets one derived from its bucket's ARN and its name, so every read returns the same id.
This commit is contained in:
1 parent
a7bb0b7ff8
commit
fcbab03dcd
10 files changed
+362
-11
No files matched your search
@@ -73,6 +73,7 @@ class S3TablesTest {
|
||||
GetTableBucketRequest.builder().tableBucketARN(bucketArn).build());
|
||||
assertThat(fetched.name()).isEqualTo(bucketName);
|
||||
assertThat(fetched.arn()).isEqualTo(bucketArn);
|
||||
assertThat(fetched.tableBucketId()).isNotBlank();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -109,15 +110,33 @@ class S3TablesTest {
|
||||
@Test
|
||||
@Order(3)
|
||||
void getsAndListsResourcesWithPrefixes() {
|
||||
GetTableBucketResponse bucket = client.getTableBucket(
|
||||
GetTableBucketRequest.builder().tableBucketARN(bucketArn).build());
|
||||
String bucketId = bucket.tableBucketId();
|
||||
assertThat(client.listTableBuckets(ListTableBucketsRequest.builder().prefix("table-bucket").build()).tableBuckets())
|
||||
.anyMatch(bucket -> bucketName.equals(bucket.name()));
|
||||
assertThat(client.getNamespace(GetNamespaceRequest.builder().tableBucketARN(bucketArn).namespace(NAMESPACE).build())
|
||||
.namespace()).containsExactly(NAMESPACE);
|
||||
.anyMatch(summary -> bucketName.equals(summary.name()) && bucketId.equals(summary.tableBucketId()));
|
||||
GetNamespaceResponse namespace = client.getNamespace(
|
||||
GetNamespaceRequest.builder().tableBucketARN(bucketArn).namespace(NAMESPACE).build());
|
||||
assertThat(namespace.namespace()).containsExactly(NAMESPACE);
|
||||
assertThat(namespace.namespaceId()).isNotBlank();
|
||||
assertThat(namespace.tableBucketId()).isEqualTo(bucketId);
|
||||
assertThat(namespace.ownerAccountId()).isEqualTo(bucket.ownerAccountId());
|
||||
assertThat(namespace.createdBy()).isEqualTo(bucket.ownerAccountId());
|
||||
assertThat(client.listNamespaces(ListNamespacesRequest.builder().tableBucketARN(bucketArn).prefix("anal").build())
|
||||
.namespaces()).anyMatch(namespace -> namespace.namespace().contains(NAMESPACE));
|
||||
.namespaces()).anyMatch(summary -> summary.namespace().contains(NAMESPACE)
|
||||
&& namespace.namespaceId().equals(summary.namespaceId())
|
||||
&& bucket.ownerAccountId().equals(summary.ownerAccountId())
|
||||
&& bucket.ownerAccountId().equals(summary.createdBy()));
|
||||
assertThat(client.getTable(GetTableRequest.builder()
|
||||
.tableBucketARN(bucketArn).namespace(NAMESPACE).name(tableName).build()))
|
||||
.satisfies(table -> {
|
||||
assertThat(table.namespaceId()).isEqualTo(namespace.namespaceId());
|
||||
assertThat(table.tableBucketId()).isEqualTo(bucketId);
|
||||
});
|
||||
assertThat(client.listTables(ListTablesRequest.builder().tableBucketARN(bucketArn).namespace(NAMESPACE)
|
||||
.prefix("events").build()).tables())
|
||||
.anyMatch(table -> tableName.equals(table.name()));
|
||||
.anyMatch(table -> tableName.equals(table.name()) && namespace.namespaceId().equals(table.namespaceId())
|
||||
&& bucketId.equals(table.tableBucketId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -30,6 +30,12 @@ a rename keeps it. `GetTable` takes either `tableArn` or the table bucket's
|
||||
ARN, namespace and name. A table created by an earlier Floci keeps the ARN it
|
||||
was stored with, which ends in its name.
|
||||
|
||||
Table buckets and namespaces get an id too, which `GetTableBucket`,
|
||||
`GetNamespace`, `GetTable` and the list operations return as `tableBucketId`
|
||||
and `namespaceId`. A bucket or namespace created by an earlier Floci gets one
|
||||
derived from its bucket's ARN and its name, so it reads the same id every time,
|
||||
after a restart too.
|
||||
|
||||
## Supported operations
|
||||
|
||||
| Area | Operations |
|
||||
|
||||
@@ -162,7 +162,7 @@ public class S3TablesController {
|
||||
@QueryParam("prefix") String prefix) {
|
||||
String arn = decode(tableBucketArn);
|
||||
List<Map<String, Object>> namespaces = service.listNamespaces(arn, prefix, region(headers)).stream()
|
||||
.map(namespace -> namespaceRepresentation(arn, namespace)).toList();
|
||||
.map(namespace -> namespaceSummary(arn, namespace)).toList();
|
||||
return Response.ok(Map.of("namespaces", namespaces)).build();
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ public class S3TablesController {
|
||||
public Response getNamespace(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
|
||||
@PathParam("namespace") String namespace) {
|
||||
String arn = decode(tableBucketArn);
|
||||
return Response.ok(namespaceRepresentation(arn, service.getNamespace(arn, namespace, region(headers)))).build();
|
||||
return Response.ok(namespaceSummary(arn, service.getNamespace(arn, namespace, region(headers)))).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@@ -329,6 +329,7 @@ public class S3TablesController {
|
||||
response.put("name", bucket.getName());
|
||||
response.put("createdAt", bucket.getCreatedAt());
|
||||
response.put("ownerAccountId", bucket.getOwnerAccountId());
|
||||
response.put("tableBucketId", bucket.getTableBucketId());
|
||||
putIfNotNull(response, "encryptionConfiguration", bucket.getEncryptionConfiguration());
|
||||
putIfNotNull(response, "storageClassConfiguration", bucket.getStorageClassConfiguration());
|
||||
return response;
|
||||
@@ -342,6 +343,15 @@ public class S3TablesController {
|
||||
return response;
|
||||
}
|
||||
|
||||
private Map<String, Object> namespaceSummary(String tableBucketArn, Namespace namespace) {
|
||||
Map<String, Object> response = namespaceRepresentation(tableBucketArn, namespace);
|
||||
response.put("createdBy", namespace.getOwnerAccountId());
|
||||
response.put("ownerAccountId", namespace.getOwnerAccountId());
|
||||
response.put("namespaceId", namespace.getNamespaceId());
|
||||
response.put("tableBucketId", namespace.getTableBucketId());
|
||||
return response;
|
||||
}
|
||||
|
||||
private Map<String, Object> tableRepresentation(S3Table table) {
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("name", table.getName());
|
||||
@@ -357,6 +367,8 @@ public class S3TablesController {
|
||||
response.put("modifiedBy", table.getOwnerAccountId());
|
||||
response.put("ownerAccountId", table.getOwnerAccountId());
|
||||
response.put("format", table.getFormat());
|
||||
response.put("namespaceId", table.getNamespaceId());
|
||||
response.put("tableBucketId", table.getTableBucketId());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
@@ -32,6 +33,7 @@ public class S3TablesService {
|
||||
private static final Logger LOG = Logger.getLogger(S3TablesService.class);
|
||||
private static final String BUCKET_PREFIX = "bucket/";
|
||||
private static final String TABLE_INFIX = "/table/";
|
||||
private static final String NAMESPACE_INFIX = "/namespace/";
|
||||
private static final int LIST_PAGE_SIZE = 1000;
|
||||
private static final int MAX_TAGS = 50;
|
||||
private static final int TAG_KEY_MAX = 128;
|
||||
@@ -65,6 +67,7 @@ public class S3TablesService {
|
||||
}
|
||||
TableBucket bucket = new TableBucket(name, arn, now(), regionResolver.getAccountId(),
|
||||
encryptionConfiguration, storageClassConfiguration, tags);
|
||||
bucket.setTableBucketId(newId());
|
||||
store.put(key, bucket);
|
||||
LOG.infov("Created S3 Tables bucket {0} in region {1}", name, region);
|
||||
return bucket;
|
||||
@@ -78,7 +81,7 @@ public class S3TablesService {
|
||||
if (!bucket.getArn().equals(tableBucketArn)) {
|
||||
throw notFound("The table bucket does not exist.");
|
||||
}
|
||||
return bucket;
|
||||
return withIds(bucket);
|
||||
}
|
||||
|
||||
public synchronized Object getTableBucketEncryption(String tableBucketArn, String region) {
|
||||
@@ -90,6 +93,7 @@ public class S3TablesService {
|
||||
return store.scan(key -> key.startsWith(region + "::")).stream()
|
||||
.filter(bucket -> prefix == null || bucket.getName().startsWith(prefix))
|
||||
.sorted(Comparator.comparing(TableBucket::getName))
|
||||
.map(this::withIds)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -137,6 +141,9 @@ public class S3TablesService {
|
||||
throw conflict("The namespace already exists.");
|
||||
}
|
||||
Namespace value = new Namespace(namespaceName, now());
|
||||
value.setNamespaceId(newId());
|
||||
value.setTableBucketId(bucket.getTableBucketId());
|
||||
value.setOwnerAccountId(regionResolver.getAccountId());
|
||||
bucket.getNamespaces().put(namespaceName, value);
|
||||
persist(bucket, region);
|
||||
return value;
|
||||
@@ -181,6 +188,8 @@ public class S3TablesService {
|
||||
S3Table table = new S3Table(name, bucket.getArn() + TABLE_INFIX + UUID.randomUUID(), namespaceName, format,
|
||||
versionToken(), metadataLocation(metadata), now(), regionResolver.getAccountId(), metadata,
|
||||
encryptionConfiguration, storageClassConfiguration, tags);
|
||||
table.setNamespaceId(namespace.getNamespaceId());
|
||||
table.setTableBucketId(bucket.getTableBucketId());
|
||||
table.setWarehouseLocation(createWarehouse(region));
|
||||
namespace.getTables().put(name, table);
|
||||
try {
|
||||
@@ -257,8 +266,8 @@ public class S3TablesService {
|
||||
|
||||
/**
|
||||
* The ARN of the table a request names by its table bucket's ARN, its namespace and its name, as IAM asks for it
|
||||
* before the handler runs; empty when there is no such table. Reads only: a table without a warehouse yet keeps
|
||||
* it so.
|
||||
* before the handler runs; empty when there is no such table. Saves nothing: a table without a warehouse yet
|
||||
* keeps it so, and ids a bucket stored before ids existed lacks are filled in memory only.
|
||||
*/
|
||||
public synchronized Optional<String> tableArn(String tableBucketArn, String namespaceName, String name,
|
||||
String region) {
|
||||
@@ -330,6 +339,39 @@ public class S3TablesService {
|
||||
persist(bucket, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives a table bucket, and each namespace and table in it, persisted before they had ids the ids they would
|
||||
* keep: derived from the bucket's ARN and the namespace's name rather than minted, so every read gives the same
|
||||
* one, after a restart too, whether or not a later save stores it. Such a namespace takes its bucket's owner as
|
||||
* its own. A background flush can save a bucket while this fills it, so every read walks the whole bucket, even
|
||||
* one that has its id, and fills only what is missing.
|
||||
*/
|
||||
private TableBucket withIds(TableBucket bucket) {
|
||||
if (bucket.getTableBucketId() == null) {
|
||||
bucket.setTableBucketId(derivedId(bucket.getArn()));
|
||||
}
|
||||
for (Namespace namespace : bucket.getNamespaces().values()) {
|
||||
if (namespace.getNamespaceId() == null) {
|
||||
namespace.setNamespaceId(derivedId(bucket.getArn() + NAMESPACE_INFIX + namespace.getName()));
|
||||
}
|
||||
if (namespace.getTableBucketId() == null) {
|
||||
namespace.setTableBucketId(bucket.getTableBucketId());
|
||||
}
|
||||
if (namespace.getOwnerAccountId() == null) {
|
||||
namespace.setOwnerAccountId(bucket.getOwnerAccountId());
|
||||
}
|
||||
for (S3Table table : namespace.getTables().values()) {
|
||||
if (table.getNamespaceId() == null) {
|
||||
table.setNamespaceId(namespace.getNamespaceId());
|
||||
}
|
||||
if (table.getTableBucketId() == null) {
|
||||
table.setTableBucketId(bucket.getTableBucketId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return bucket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives each table persisted before tables had a warehouse one of its own, so every response
|
||||
* that reports {@code warehouseLocation} has one. A save that fails takes the new warehouses
|
||||
@@ -420,6 +462,7 @@ public class S3TablesService {
|
||||
}
|
||||
sourceNamespace.getTables().remove(name);
|
||||
table.setNamespace(targetNamespaceName);
|
||||
table.setNamespaceId(targetNamespace.getNamespaceId());
|
||||
table.setName(targetName);
|
||||
touch(table);
|
||||
targetNamespace.getTables().put(targetName, table);
|
||||
@@ -729,6 +772,8 @@ public class S3TablesService {
|
||||
|
||||
private String now() { return Instant.now().toString(); }
|
||||
private String versionToken() { return UUID.randomUUID().toString(); }
|
||||
private String newId() { return UUID.randomUUID().toString(); }
|
||||
private String derivedId(String seed) { return UUID.nameUUIDFromBytes(seed.getBytes(StandardCharsets.UTF_8)).toString(); }
|
||||
private AwsException badRequest(String message) { return new AwsException("BadRequestException", message, 400); }
|
||||
private AwsException conflict(String message) { return new AwsException("ConflictException", message, 409); }
|
||||
private AwsException notFound(String message) { return new AwsException("NotFoundException", message, 404); }
|
||||
|
||||
@@ -8,6 +8,9 @@ import java.util.Map;
|
||||
@RegisterForReflection
|
||||
public class Namespace {
|
||||
private String name;
|
||||
private String namespaceId;
|
||||
private String tableBucketId;
|
||||
private String ownerAccountId;
|
||||
private String createdAt;
|
||||
private Map<String, S3Table> tables = new LinkedHashMap<>();
|
||||
|
||||
@@ -21,6 +24,12 @@ public class Namespace {
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getNamespaceId() { return namespaceId; }
|
||||
public void setNamespaceId(String namespaceId) { this.namespaceId = namespaceId; }
|
||||
public String getTableBucketId() { return tableBucketId; }
|
||||
public void setTableBucketId(String tableBucketId) { this.tableBucketId = tableBucketId; }
|
||||
public String getOwnerAccountId() { return ownerAccountId; }
|
||||
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
public Map<String, S3Table> getTables() { return tables; }
|
||||
|
||||
@@ -10,6 +10,8 @@ public class S3Table {
|
||||
private String name;
|
||||
private String arn;
|
||||
private String namespace;
|
||||
private String namespaceId;
|
||||
private String tableBucketId;
|
||||
private String format;
|
||||
private String versionToken;
|
||||
private String metadataLocation;
|
||||
@@ -53,6 +55,10 @@ public class S3Table {
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
public String getNamespace() { return namespace; }
|
||||
public void setNamespace(String namespace) { this.namespace = namespace; }
|
||||
public String getNamespaceId() { return namespaceId; }
|
||||
public void setNamespaceId(String namespaceId) { this.namespaceId = namespaceId; }
|
||||
public String getTableBucketId() { return tableBucketId; }
|
||||
public void setTableBucketId(String tableBucketId) { this.tableBucketId = tableBucketId; }
|
||||
public String getFormat() { return format; }
|
||||
public void setFormat(String format) { this.format = format; }
|
||||
public String getVersionToken() { return versionToken; }
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.Map;
|
||||
public class TableBucket {
|
||||
private String name;
|
||||
private String arn;
|
||||
private String tableBucketId;
|
||||
private String createdAt;
|
||||
private String ownerAccountId;
|
||||
private Object encryptionConfiguration;
|
||||
@@ -40,6 +41,8 @@ public class TableBucket {
|
||||
public void setName(String name) { this.name = name; }
|
||||
public String getArn() { return arn; }
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
public String getTableBucketId() { return tableBucketId; }
|
||||
public void setTableBucketId(String tableBucketId) { this.tableBucketId = tableBucketId; }
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
public String getOwnerAccountId() { return ownerAccountId; }
|
||||
@@ -63,6 +66,7 @@ public class TableBucket {
|
||||
public TableBucket withMetricsConfigurationEnabled(boolean enabled) {
|
||||
TableBucket copy = new TableBucket(name, arn, createdAt, ownerAccountId, encryptionConfiguration,
|
||||
storageClassConfiguration, tags);
|
||||
copy.setTableBucketId(tableBucketId);
|
||||
copy.setResourcePolicy(resourcePolicy);
|
||||
copy.setMaintenanceConfigurations(maintenanceConfigurations);
|
||||
copy.setNamespaces(namespaces);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.s3tables;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
@@ -8,15 +9,26 @@ import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3tables.S3TablesClient;
|
||||
import software.amazon.awssdk.services.s3tables.model.GetNamespaceResponse;
|
||||
import software.amazon.awssdk.services.s3tables.model.GetTableBucketResponse;
|
||||
import software.amazon.awssdk.services.s3tables.model.GetTableResponse;
|
||||
import software.amazon.awssdk.services.s3tables.model.NamespaceSummary;
|
||||
import software.amazon.awssdk.services.s3tables.model.TableBucketSummary;
|
||||
import software.amazon.awssdk.services.s3tables.model.TableSummary;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.floci.aws.testing.S3TablesRequests.s3tables;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.everyItem;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@@ -34,6 +46,8 @@ class S3TablesIntegrationTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String BUCKET_ARN = "arn:aws:s3tables:%s:%s:bucket/%s"
|
||||
.formatted(REGION, ACCOUNT_ID, BUCKET_NAME);
|
||||
private static final Pattern UUID_FORM =
|
||||
Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
|
||||
private static final String ENCODED_BUCKET_ARN = URLEncoder.encode(BUCKET_ARN, StandardCharsets.UTF_8);
|
||||
|
||||
private String versionToken;
|
||||
@@ -75,7 +89,8 @@ class S3TablesIntegrationTest {
|
||||
.statusCode(200)
|
||||
.contentType(containsString(JSON_CONTENT_TYPE))
|
||||
.body("namespace", contains(NAMESPACE))
|
||||
.body("tableBucketARN", equalTo(BUCKET_ARN));
|
||||
.body("tableBucketARN", equalTo(BUCKET_ARN))
|
||||
.body("$", not(hasKey("namespaceId")));
|
||||
|
||||
versionToken = s3tables()
|
||||
.urlEncodingEnabled(false)
|
||||
@@ -266,6 +281,38 @@ class S3TablesIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void sdkReadsTheIdsOfTheBucketAndNamespaceAndTheNamespaceOwnerOnEveryOperationThatReturnsThem() {
|
||||
try (S3TablesClient client = AwsTestClients.create(S3TablesClient.builder(), ACCOUNT_ID, Region.of(REGION))) {
|
||||
GetTableBucketResponse bucket = client.getTableBucket(request -> request.tableBucketARN(BUCKET_ARN));
|
||||
GetNamespaceResponse namespace = client.getNamespace(request -> request.tableBucketARN(BUCKET_ARN)
|
||||
.namespace(NAMESPACE));
|
||||
GetTableResponse table = client.getTable(request -> request.tableBucketARN(BUCKET_ARN)
|
||||
.namespace(NAMESPACE).name(TABLE_NAME));
|
||||
TableBucketSummary listedBucket = client.listTableBuckets(request -> request.prefix(BUCKET_NAME))
|
||||
.tableBuckets().stream().filter(summary -> BUCKET_NAME.equals(summary.name())).findFirst()
|
||||
.orElseThrow();
|
||||
NamespaceSummary listedNamespace = client.listNamespaces(request -> request.tableBucketARN(BUCKET_ARN))
|
||||
.namespaces().stream().filter(summary -> summary.namespace().equals(List.of(NAMESPACE)))
|
||||
.findFirst().orElseThrow();
|
||||
TableSummary listedTable = client.listTables(request -> request.tableBucketARN(BUCKET_ARN)).tables()
|
||||
.stream().filter(summary -> TABLE_NAME.equals(summary.name())
|
||||
&& summary.namespace().equals(List.of(NAMESPACE)))
|
||||
.findFirst().orElseThrow();
|
||||
|
||||
assertThat(bucket.tableBucketId(), matchesPattern(UUID_FORM));
|
||||
assertThat(namespace.namespaceId(), matchesPattern(UUID_FORM));
|
||||
assertThat(namespace.namespaceId(), not(equalTo(bucket.tableBucketId())));
|
||||
assertThat(List.of(listedBucket.tableBucketId(), namespace.tableBucketId(), table.tableBucketId(),
|
||||
listedNamespace.tableBucketId(), listedTable.tableBucketId()), everyItem(equalTo(bucket.tableBucketId())));
|
||||
assertThat(List.of(table.namespaceId(), listedNamespace.namespaceId(), listedTable.namespaceId()),
|
||||
everyItem(equalTo(namespace.namespaceId())));
|
||||
assertThat(List.of(namespace.createdBy(), namespace.ownerAccountId(), listedNamespace.createdBy(),
|
||||
listedNamespace.ownerAccountId()), everyItem(equalTo(ACCOUNT_ID)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void refusesTagsOnAnUnknownResourceReservedKeysAndMalformedBodies() {
|
||||
s3tables()
|
||||
.urlEncodingEnabled(false)
|
||||
|
||||
@@ -56,9 +56,11 @@ class S3TablesMetricsServiceTest {
|
||||
service.createTable(arn, "analytics", "events", "ICEBERG", null, null, null, Map.of(), REGION);
|
||||
service.putTableBucketPolicy(arn, "{\"Statement\":[]}", REGION);
|
||||
service.putTableBucketMaintenance(arn, "icebergUnreferencedFileRemoval", Map.of("status", "enabled"), REGION);
|
||||
String bucketId = service.getTableBucket(arn, REGION).getTableBucketId();
|
||||
service.putTableBucketMetricsConfiguration(arn, REGION);
|
||||
service.deleteTableBucketMetricsConfiguration(arn, REGION);
|
||||
TableBucket after = service.getTableBucket(arn, REGION);
|
||||
assertEquals(bucketId, after.getTableBucketId());
|
||||
assertEquals(encryption, after.getEncryptionConfiguration());
|
||||
assertEquals(storageClass, after.getStorageClassConfiguration());
|
||||
assertEquals(Map.of("owner", "audit"), after.getTags());
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
package io.floci.aws.services.s3tables;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
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.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3tables.model.Namespace;
|
||||
import io.floci.aws.services.s3tables.model.S3Table;
|
||||
import io.floci.aws.services.s3tables.model.TableBucket;
|
||||
import io.floci.aws.testing.StorageTestBackends;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -28,6 +39,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -44,6 +56,7 @@ class S3TablesServiceTest {
|
||||
+ "or equal to 1]";
|
||||
private static final String VALUE_LENGTH = "Value at 'tags' failed to satisfy constraint: Map value must satisfy "
|
||||
+ "constraint: [Member must have length less than or equal to 256]";
|
||||
private static final Pattern UUID_FORM = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
|
||||
|
||||
private S3TablesService service;
|
||||
private S3Service s3Service;
|
||||
@@ -220,6 +233,106 @@ class S3TablesServiceTest {
|
||||
assertEquals(renamed, service.getTable(arn, "reporting", "daily_events", REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bucketsNamespacesAndTablesCarryMintedIdsThatTheirChildrenRepeat() {
|
||||
TableBucket bucket = createBucket();
|
||||
String arn = bucket.getArn();
|
||||
Namespace analytics = service.createNamespace(arn, List.of("analytics"), REGION);
|
||||
Namespace staging = service.createNamespace(arn, List.of("staging"), REGION);
|
||||
S3Table table = createTable(arn, "analytics", "events");
|
||||
|
||||
assertTrue(UUID_FORM.matcher(bucket.getTableBucketId()).matches(), bucket.getTableBucketId());
|
||||
assertTrue(UUID_FORM.matcher(analytics.getNamespaceId()).matches(), analytics.getNamespaceId());
|
||||
assertNotEquals(analytics.getNamespaceId(), staging.getNamespaceId());
|
||||
assertEquals(bucket.getTableBucketId(), analytics.getTableBucketId());
|
||||
assertEquals(ACCOUNT_ID, analytics.getOwnerAccountId());
|
||||
assertEquals(analytics.getNamespaceId(), table.getNamespaceId());
|
||||
assertEquals(bucket.getTableBucketId(), table.getTableBucketId());
|
||||
assertEquals(bucket.getTableBucketId(), service.listTableBuckets(null, REGION).getFirst().getTableBucketId());
|
||||
assertEquals(List.of(analytics.getNamespaceId(), staging.getNamespaceId()),
|
||||
service.listNamespaces(arn, null, REGION).stream().map(Namespace::getNamespaceId).toList());
|
||||
|
||||
S3Table moved = service.renameTable(arn, "analytics", "events", "staging", null, table.getVersionToken(), REGION);
|
||||
|
||||
assertEquals(staging.getNamespaceId(), moved.getNamespaceId());
|
||||
assertEquals(staging.getNamespaceId(), service.listTables(arn, null, null, REGION).getFirst().getNamespaceId());
|
||||
service.deleteTable(arn, "staging", "events", REGION);
|
||||
service.deleteNamespace(arn, "staging", REGION);
|
||||
assertNotEquals(staging.getNamespaceId(),
|
||||
service.createNamespace(arn, List.of("staging"), REGION).getNamespaceId());
|
||||
}
|
||||
|
||||
/**
|
||||
* A bucket, namespace and table stored before they had ids read the same ids on every read and after a restart,
|
||||
* whether or not anything saved them in between, and a namespace created in that bucket repeats its id. The
|
||||
* namespace, stored without an owner too, reads its bucket's.
|
||||
*/
|
||||
@Test
|
||||
void resourcesStoredBeforeIdsReadTheSameIdsEveryTimeAndAfterARestart(@TempDir Path directory) throws Exception {
|
||||
Path snapshot = directory.resolve("s3tables.json");
|
||||
Path unsaved = directory.resolve("unsaved.json");
|
||||
String arn = "arn:aws:s3tables:" + REGION + ":" + ACCOUNT_ID + ":bucket/" + BUCKET;
|
||||
seed(snapshot, legacyBucket(arn), false);
|
||||
Files.copy(snapshot, unsaved);
|
||||
|
||||
StorageBackend<String, TableBucket> first = openSnapshot(snapshot);
|
||||
List<String> ids;
|
||||
try {
|
||||
S3TablesService reader = persistentService(first);
|
||||
ids = ids(reader, arn);
|
||||
assertEquals(ids, ids(reader, arn));
|
||||
assertTrue(ids.stream().allMatch(id -> UUID_FORM.matcher(id).matches()), ids.toString());
|
||||
assertNotEquals(ids.get(0), ids.get(1));
|
||||
assertEquals(ids.get(0), reader.createNamespace(arn, List.of("staging"), REGION).getTableBucketId());
|
||||
assertEquals(ACCOUNT_ID, reader.getNamespace(arn, "analytics", REGION).getOwnerAccountId());
|
||||
} finally {
|
||||
StorageTestBackends.close(first);
|
||||
}
|
||||
|
||||
for (Path restarted : List.of(snapshot, unsaved)) {
|
||||
StorageBackend<String, TableBucket> backend = openSnapshot(restarted);
|
||||
try {
|
||||
S3TablesService reader = persistentService(backend);
|
||||
assertEquals(ids, ids(reader, arn), restarted.toString());
|
||||
assertEquals(ACCOUNT_ID, reader.getNamespace(arn, "analytics", REGION).getOwnerAccountId());
|
||||
} finally {
|
||||
StorageTestBackends.close(backend);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A background flush can save a bucket that has its derived id before its namespace and table have theirs. That
|
||||
* bucket reads the ids and the owner that the same bucket saved with no ids reads.
|
||||
*/
|
||||
@Test
|
||||
void bucketSavedWithItsIdButNotItsNamespacesAndTablesReadsTheIdsOfOneSavedWithNone(@TempDir Path directory)
|
||||
throws Exception {
|
||||
String arn = "arn:aws:s3tables:" + REGION + ":" + ACCOUNT_ID + ":bucket/" + BUCKET;
|
||||
Path none = directory.resolve("none.json");
|
||||
Path partial = directory.resolve("partial.json");
|
||||
seed(none, legacyBucket(arn), false);
|
||||
List<String> expected;
|
||||
StorageBackend<String, TableBucket> unmigrated = openSnapshot(none);
|
||||
try {
|
||||
expected = ids(persistentService(unmigrated), arn);
|
||||
} finally {
|
||||
StorageTestBackends.close(unmigrated);
|
||||
}
|
||||
TableBucket bucket = legacyBucket(arn);
|
||||
bucket.setTableBucketId(expected.getFirst());
|
||||
seed(partial, bucket, true);
|
||||
|
||||
StorageBackend<String, TableBucket> backend = openSnapshot(partial);
|
||||
try {
|
||||
S3TablesService reader = persistentService(backend);
|
||||
assertEquals(expected, ids(reader, arn));
|
||||
assertEquals(ACCOUNT_ID, reader.getNamespace(arn, "analytics", REGION).getOwnerAccountId());
|
||||
} finally {
|
||||
StorageTestBackends.close(backend);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTripsPoliciesAndTypedMaintenanceConfigurations() {
|
||||
String arn = createBucketWithNamespace("analytics");
|
||||
@@ -403,6 +516,94 @@ class S3TablesServiceTest {
|
||||
assertEquals(expectedMessage, exception.getMessage());
|
||||
}
|
||||
|
||||
/** The bucket's id, the namespace's, and the ids its table repeats, as GetTableBucket, GetNamespace and GetTable read them. */
|
||||
private static List<String> ids(S3TablesService reader, String arn) {
|
||||
Namespace namespace = reader.getNamespace(arn, "analytics", REGION);
|
||||
S3Table table = reader.getTable(arn, "analytics", "events", REGION);
|
||||
return List.of(reader.getTableBucket(arn, REGION).getTableBucketId(), namespace.getNamespaceId(),
|
||||
namespace.getTableBucketId(), table.getNamespaceId(), table.getTableBucketId());
|
||||
}
|
||||
|
||||
/** Saves {@code bucket} to {@code snapshot}, then rewrites it as {@link #withoutIdKeys} does. */
|
||||
private static void seed(Path snapshot, TableBucket bucket, boolean keepBucketId) throws IOException {
|
||||
StorageBackend<String, TableBucket> backend = openSnapshot(snapshot);
|
||||
persisted(backend).put(REGION + "::" + BUCKET, bucket);
|
||||
StorageTestBackends.close(backend);
|
||||
withoutIdKeys(snapshot, keepBucketId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites {@code snapshot} as a Floci from before ids wrote it: without the id keys, not with null ones, and
|
||||
* without a namespace's owner. A bucket is the object holding {@code namespaces}, a namespace the one holding
|
||||
* {@code tables}, and a table the one holding {@code versionToken}. With {@code keepBucketId} the bucket keeps
|
||||
* its id, as a flush in the middle of filling them would save it.
|
||||
*/
|
||||
private static void withoutIdKeys(Path snapshot, boolean keepBucketId) throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root = mapper.readTree(snapshot.toFile());
|
||||
List<JsonNode> pending = new ArrayList<>(List.of(root));
|
||||
while (!pending.isEmpty()) {
|
||||
JsonNode node = pending.removeLast();
|
||||
if (node instanceof ObjectNode object && !(keepBucketId && object.has("namespaces"))) {
|
||||
object.remove(List.of("tableBucketId", "namespaceId"));
|
||||
if (object.has("tables")) {
|
||||
object.remove("ownerAccountId");
|
||||
}
|
||||
}
|
||||
node.forEach(pending::add);
|
||||
}
|
||||
mapper.writeValue(snapshot.toFile(), root);
|
||||
String written = Files.readString(snapshot);
|
||||
List<JsonNode> buckets = root.findParents("namespaces");
|
||||
List<JsonNode> namespaces = root.findParents("tables");
|
||||
List<JsonNode> tables = root.findParents("versionToken");
|
||||
assertFalse(buckets.isEmpty() || namespaces.isEmpty() || tables.isEmpty(), written);
|
||||
for (JsonNode bucket : buckets) {
|
||||
assertEquals(keepBucketId, bucket.has("tableBucketId"), written);
|
||||
}
|
||||
for (JsonNode namespace : namespaces) {
|
||||
assertFalse(namespace.has("namespaceId") || namespace.has("tableBucketId")
|
||||
|| namespace.has("ownerAccountId"), written);
|
||||
}
|
||||
for (JsonNode table : tables) {
|
||||
assertFalse(table.has("namespaceId") || table.has("tableBucketId"), written);
|
||||
}
|
||||
}
|
||||
|
||||
private static TableBucket legacyBucket(String arn) {
|
||||
TableBucket bucket = new TableBucket(BUCKET, arn, "2026-01-01T00:00:00Z", ACCOUNT_ID, null, null, Map.of());
|
||||
Namespace namespace = new Namespace("analytics", "2026-01-01T00:00:00Z");
|
||||
S3Table table = new S3Table("events", arn + "/table/" + UUID.randomUUID(), "analytics", "ICEBERG",
|
||||
UUID.randomUUID().toString(), null, "2026-01-01T00:00:00Z", ACCOUNT_ID, null, null, null, Map.of());
|
||||
table.setWarehouseLocation("s3://legacy--table-s3");
|
||||
namespace.getTables().put("events", table);
|
||||
bucket.getNamespaces().put("analytics", namespace);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static StorageBackend<String, TableBucket> openSnapshot(Path snapshot) {
|
||||
StorageBackend<String, TableBucket> backend = StorageTestBackends.create("persistent", snapshot, null,
|
||||
new TypeReference<Map<String, TableBucket>>() {}, 60_000);
|
||||
backend.load();
|
||||
return backend;
|
||||
}
|
||||
|
||||
private static AccountAwareStorageBackend<TableBucket> persisted(StorageBackend<String, TableBucket> backend) {
|
||||
return new AccountAwareStorageBackend<>(backend, null, ACCOUNT_ID);
|
||||
}
|
||||
|
||||
private S3TablesService persistentService(StorageBackend<String, TableBucket> backend) {
|
||||
StorageFactory factory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) persisted(backend);
|
||||
}
|
||||
};
|
||||
return new S3TablesService(factory, new RegionResolver(REGION, ACCOUNT_ID), s3Service);
|
||||
}
|
||||
|
||||
private TableBucket createBucket() {
|
||||
return service.createTableBucket(BUCKET, null, null, Map.of(), REGION);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user