mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3tables): give each table a warehouse bucket and report its location (#8413)
GetTable and GetTableMetadataLocation never returned warehouseLocation, which the S3 Tables model marks required, so Spark's S3TablesCatalog had nowhere to write: CREATE TABLE failed with "Invalid S3 URI, cannot determine scheme: null/metadata/00000-...metadata.json". CreateTable now creates the table's warehouse, a bucket of its own named like AWS's (<uuid>--table-s3), and both responses report it; a table persisted before this gets one the first time it is read. An Iceberg client writes the table's files there and commits with UpdateTableMetadataLocation, which already existed. DeleteTable deletes the warehouse with its contents, as deleting an S3 Tables table deletes its data. GetTable also reports the required createdBy and modifiedBy.
This commit is contained in:
1 parent
2cb54dad8f
commit
b18feb39d1
7 files changed
+287
-12
No files matched your search
@@ -5,11 +5,13 @@ 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.StorageFactory;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3tables.model.S3Table;
|
||||
import io.floci.aws.services.s3tables.model.TableBucket;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -22,6 +24,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3TablesServiceTest {
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
@@ -29,17 +32,55 @@ class S3TablesServiceTest {
|
||||
private static final String BUCKET = "unit-test-table-bucket";
|
||||
|
||||
private S3TablesService service;
|
||||
private S3Service s3Service;
|
||||
private AccountAwareStorageBackend<TableBucket> store;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StorageFactory storageFactory = new StorageFactory(null, null) {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
|
||||
store = Mockito.spy(AccountAwareStorageBackend.<TableBucket>inMemory(ACCOUNT_ID));
|
||||
return (AccountAwareStorageBackend<V>) store;
|
||||
}
|
||||
};
|
||||
service = new S3TablesService(storageFactory, new RegionResolver(REGION, ACCOUNT_ID));
|
||||
s3Service = Mockito.mock(S3Service.class);
|
||||
Mockito.when(s3Service.listObjectsWithPrefixes(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyInt(),
|
||||
Mockito.any(), Mockito.any())).thenReturn(new S3Service.ListObjectsResult(List.of(), List.of(), false, null));
|
||||
Mockito.when(s3Service.listObjectVersions(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyInt(),
|
||||
Mockito.any(), Mockito.any())).thenReturn(new S3Service.ListVersionsResult(List.of(), List.of(), false, null, null));
|
||||
service = new S3TablesService(storageFactory, new RegionResolver(REGION, ACCOUNT_ID), s3Service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listingAndUpdatingATableFromBeforeWarehousesGiveItOne() {
|
||||
String arn = service.createTableBucket(BUCKET, null, null, Map.of(), REGION).getArn();
|
||||
service.createNamespace(arn, List.of("analytics"), REGION);
|
||||
S3Table legacy = createTable(arn, "analytics", "events");
|
||||
legacy.setWarehouseLocation(null);
|
||||
|
||||
S3Table listed = service.listTables(arn, "analytics", null, REGION).getFirst();
|
||||
listed.setWarehouseLocation(null);
|
||||
S3Table updated = service.updateTableMetadataLocation(arn, "analytics", "events", "s3://w/m.json",
|
||||
listed.getVersionToken(), REGION);
|
||||
|
||||
assertTrue(updated.getWarehouseLocation().matches("s3://[0-9a-f-]{36}--table-s3"), updated.getWarehouseLocation());
|
||||
Mockito.verify(s3Service, Mockito.times(3)).createBucket(Mockito.endsWith("--table-s3"), Mockito.eq(REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedSaveTakesTheNewTableAndItsWarehouseBackOut() {
|
||||
String arn = service.createTableBucket(BUCKET, null, null, Map.of(), REGION).getArn();
|
||||
service.createNamespace(arn, List.of("analytics"), REGION);
|
||||
Mockito.doThrow(new IllegalStateException("disk full")).when(store).put(Mockito.anyString(), Mockito.any());
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> createTable(arn, "analytics", "events"));
|
||||
|
||||
Mockito.verify(s3Service).deleteBucket(Mockito.endsWith("--table-s3"));
|
||||
Mockito.doCallRealMethod().when(store).put(Mockito.anyString(), Mockito.any());
|
||||
assertEquals(List.of(), service.listTables(arn, "analytics", null, REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
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.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketVersioningStatus;
|
||||
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* Every S3 Tables table has a warehouse, a bucket of its own that an Iceberg client such as Spark's
|
||||
* {@code S3TablesCatalog} writes the table's data and metadata files to before committing with
|
||||
* {@code UpdateTableMetadataLocation}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class S3TablesWarehouseIntegrationTest {
|
||||
|
||||
private static final String JSON = "application/json";
|
||||
|
||||
private String encodedBucketArn;
|
||||
private S3Client s3;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createTable() {
|
||||
String bucketName = "warehouse-" + System.nanoTime();
|
||||
String bucketArn = given().contentType(JSON).body("{\"name\": \"" + bucketName + "\"}")
|
||||
.when().put("/buckets").then().statusCode(200).extract().path("arn");
|
||||
encodedBucketArn = URLEncoder.encode(bucketArn, StandardCharsets.UTF_8);
|
||||
given().urlEncodingEnabled(false).contentType(JSON).body("{\"namespace\": [\"analytics\"]}")
|
||||
.when().put("/namespaces/" + encodedBucketArn).then().statusCode(200);
|
||||
given().urlEncodingEnabled(false).contentType(JSON).body("{\"name\": \"events\", \"format\": \"ICEBERG\"}")
|
||||
.when().put("/tables/" + encodedBucketArn + "/analytics").then().statusCode(200);
|
||||
s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), "000000000000", Region.US_EAST_1);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClient() {
|
||||
s3.close();
|
||||
}
|
||||
|
||||
private String warehouseLocation() {
|
||||
return given().urlEncodingEnabled(false)
|
||||
.when().get("/get-table?tableBucketARN=" + encodedBucketArn + "&namespace=analytics&name=events")
|
||||
.then().statusCode(200)
|
||||
.body("warehouseLocation", matchesPattern("s3://[0-9a-f-]{36}--table-s3"))
|
||||
.body("createdBy", equalTo("000000000000"))
|
||||
.body("modifiedBy", equalTo("000000000000"))
|
||||
.extract().path("warehouseLocation");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTablesWarehouseIsABucketItsMetadataLocationAlsoNames() {
|
||||
String warehouse = warehouseLocation();
|
||||
String bucket = warehouse.substring("s3://".length());
|
||||
|
||||
s3.putObject(r -> r.bucket(bucket).key("metadata/00000-a.metadata.json"), RequestBody.fromString("{}"));
|
||||
|
||||
given().urlEncodingEnabled(false)
|
||||
.when().get("/tables/" + encodedBucketArn + "/analytics/events/metadata-location")
|
||||
.then().statusCode(200)
|
||||
.body("warehouseLocation", equalTo(warehouse));
|
||||
assertEquals(1, s3.listObjectsV2(r -> r.bucket(bucket)).keyCount());
|
||||
assertEquals(warehouse, warehouseLocation(), "the warehouse stays the same across reads");
|
||||
}
|
||||
|
||||
@Test
|
||||
void warehousesAreNotListedAsGeneralPurposeBuckets() {
|
||||
String bucket = warehouseLocation().substring("s3://".length());
|
||||
|
||||
assertFalse(s3.listBuckets().buckets().stream().anyMatch(b -> b.name().equals(bucket)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingATableDeletesEveryVersionInAVersionedWarehouse() {
|
||||
String bucket = warehouseLocation().substring("s3://".length());
|
||||
s3.putBucketVersioning(r -> r.bucket(bucket).versioningConfiguration(v -> v.status(BucketVersioningStatus.ENABLED)));
|
||||
s3.putObject(r -> r.bucket(bucket).key("data/part-0.parquet"), RequestBody.fromString("v1"));
|
||||
s3.putObject(r -> r.bucket(bucket).key("data/part-0.parquet"), RequestBody.fromString("v2"));
|
||||
s3.deleteObject(r -> r.bucket(bucket).key("data/part-0.parquet"));
|
||||
|
||||
given().urlEncodingEnabled(false)
|
||||
.when().delete("/tables/" + encodedBucketArn + "/analytics/events")
|
||||
.then().statusCode(204);
|
||||
|
||||
assertThrows(NoSuchBucketException.class, () -> s3.headBucket(r -> r.bucket(bucket)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingATableDeletesItsWarehouse() {
|
||||
String bucket = warehouseLocation().substring("s3://".length());
|
||||
s3.putObject(r -> r.bucket(bucket).key("data/part-0.parquet"), RequestBody.fromString("x"));
|
||||
|
||||
given().urlEncodingEnabled(false)
|
||||
.when().delete("/tables/" + encodedBucketArn + "/analytics/events")
|
||||
.then().statusCode(204);
|
||||
|
||||
assertThrows(NoSuchBucketException.class, () -> s3.headBucket(r -> r.bucket(bucket)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user