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
@@ -127,6 +127,12 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
public static final String REDSHIFT_UNLOAD_STAGING_TAG_VALUE = "redshift-unload-staging";
|
||||
public static final String INTERNAL_BUCKET_TAG_KEY = "floci:internal";
|
||||
public static final String REDSHIFT_SPECTRUM_SCRATCH_TAG_VALUE = "redshift-spectrum-scratch";
|
||||
|
||||
/**
|
||||
* The suffix S3 Tables gives the bucket behind each table's warehouse. Such a bucket is the table's
|
||||
* storage, not a general purpose bucket, so it is left out of {@code ListBuckets} as on AWS.
|
||||
*/
|
||||
public static final String TABLE_WAREHOUSE_BUCKET_SUFFIX = "--table-s3";
|
||||
private static final String AUTHENTICATED_USERS_GROUP_URI = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers";
|
||||
private static final String LOG_DELIVERY_GROUP_URI = "http://acs.amazonaws.com/groups/s3/LogDelivery";
|
||||
private static final String LEGACY_ACCESS_KEY_ID = "test";
|
||||
@@ -669,6 +675,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
public List<Bucket> listBuckets() {
|
||||
return bucketStore.scan(key -> true).stream()
|
||||
.filter(bucket -> !isScratchBucket(bucket))
|
||||
.filter(bucket -> !bucket.getName().endsWith(TABLE_WAREHOUSE_BUCKET_SUFFIX))
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
@@ -280,8 +280,11 @@ public class S3TablesController {
|
||||
response.put("namespace", List.of(table.getNamespace()));
|
||||
response.put("versionToken", table.getVersionToken());
|
||||
putIfNotNull(response, "metadataLocation", table.getMetadataLocation());
|
||||
putIfNotNull(response, "warehouseLocation", table.getWarehouseLocation());
|
||||
response.put("createdAt", table.getCreatedAt());
|
||||
response.put("createdBy", table.getOwnerAccountId());
|
||||
response.put("modifiedAt", table.getModifiedAt());
|
||||
response.put("modifiedBy", table.getOwnerAccountId());
|
||||
response.put("ownerAccountId", table.getOwnerAccountId());
|
||||
response.put("format", table.getFormat());
|
||||
return response;
|
||||
@@ -291,6 +294,7 @@ public class S3TablesController {
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
putIfNotNull(response, "metadataLocation", table.getMetadataLocation());
|
||||
response.put("versionToken", table.getVersionToken());
|
||||
putIfNotNull(response, "warehouseLocation", table.getWarehouseLocation());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
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.s3.model.S3Object;
|
||||
import io.floci.aws.services.s3tables.model.Namespace;
|
||||
import io.floci.aws.services.s3tables.model.S3Table;
|
||||
import io.floci.aws.services.s3tables.model.TableBucket;
|
||||
@@ -25,14 +27,17 @@ import java.util.UUID;
|
||||
public class S3TablesService {
|
||||
private static final Logger LOG = Logger.getLogger(S3TablesService.class);
|
||||
private static final String BUCKET_PREFIX = "bucket/";
|
||||
private static final int LIST_PAGE_SIZE = 1000;
|
||||
|
||||
private final StorageBackend<String, TableBucket> store;
|
||||
private final RegionResolver regionResolver;
|
||||
private final S3Service s3Service;
|
||||
|
||||
@Inject
|
||||
public S3TablesService(StorageFactory factory, RegionResolver regionResolver) {
|
||||
public S3TablesService(StorageFactory factory, RegionResolver regionResolver, S3Service s3Service) {
|
||||
this.store = factory.create("s3tables", "s3tables.json", new TypeReference<Map<String, TableBucket>>() {});
|
||||
this.regionResolver = regionResolver;
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
public synchronized TableBucket createTableBucket(String name, Object encryptionConfiguration,
|
||||
@@ -122,13 +127,23 @@ public class S3TablesService {
|
||||
S3Table table = new S3Table(name, bucket.getArn() + "/table/" + name, namespaceName, format,
|
||||
versionToken(), metadataLocation(metadata), now(), regionResolver.getAccountId(), metadata,
|
||||
encryptionConfiguration, storageClassConfiguration, tags);
|
||||
table.setWarehouseLocation(createWarehouse(region));
|
||||
namespace.getTables().put(name, table);
|
||||
persist(bucket, region);
|
||||
try {
|
||||
persist(bucket, region);
|
||||
} catch (RuntimeException e) {
|
||||
namespace.getTables().remove(name);
|
||||
deleteWarehouse(table.getWarehouseLocation());
|
||||
throw e;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
public synchronized S3Table getTable(String tableBucketArn, String namespaceName, String name, String region) {
|
||||
return getTable(getTableBucket(tableBucketArn, region), namespaceName, name);
|
||||
TableBucket bucket = getTableBucket(tableBucketArn, region);
|
||||
S3Table table = getTable(bucket, namespaceName, name);
|
||||
withWarehouses(bucket, List.of(table), region);
|
||||
return table;
|
||||
}
|
||||
|
||||
public synchronized List<S3Table> listTables(String tableBucketArn, String namespaceName, String prefix, String region) {
|
||||
@@ -139,18 +154,96 @@ public class S3TablesService {
|
||||
} else {
|
||||
tables.addAll(getNamespace(bucket, namespaceName).getTables().values());
|
||||
}
|
||||
return tables.stream().filter(table -> prefix == null || table.getName().startsWith(prefix))
|
||||
List<S3Table> listed = tables.stream().filter(table -> prefix == null || table.getName().startsWith(prefix))
|
||||
.sorted(Comparator.comparing(S3Table::getName)).toList();
|
||||
withWarehouses(bucket, listed, region);
|
||||
return listed;
|
||||
}
|
||||
|
||||
public synchronized void deleteTable(String tableBucketArn, String namespaceName, String name, String region) {
|
||||
TableBucket bucket = getTableBucket(tableBucketArn, region);
|
||||
Namespace namespace = getNamespace(bucket, namespaceName);
|
||||
getTable(bucket, namespaceName, name);
|
||||
S3Table table = getTable(bucket, namespaceName, name);
|
||||
// The warehouse goes first: if emptying it fails, the table is still there to delete again.
|
||||
deleteWarehouse(table.getWarehouseLocation());
|
||||
namespace.getTables().remove(name);
|
||||
persist(bucket, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* back out.
|
||||
*/
|
||||
private void withWarehouses(TableBucket bucket, List<S3Table> tables, String region) {
|
||||
List<S3Table> backfilled = new ArrayList<>();
|
||||
for (S3Table table : tables) {
|
||||
if (table.getWarehouseLocation() == null) {
|
||||
table.setWarehouseLocation(createWarehouse(region));
|
||||
backfilled.add(table);
|
||||
}
|
||||
}
|
||||
if (backfilled.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
persist(bucket, region);
|
||||
} catch (RuntimeException e) {
|
||||
for (S3Table table : backfilled) {
|
||||
deleteWarehouse(table.getWarehouseLocation());
|
||||
table.setWarehouseLocation(null);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the bucket that holds a table's data and metadata files and returns its location, as
|
||||
* S3 Tables gives every table a warehouse of its own: an Iceberg client such as Spark's
|
||||
* {@code S3TablesCatalog} writes there and commits with {@code UpdateTableMetadataLocation}.
|
||||
*/
|
||||
private String createWarehouse(String region) {
|
||||
String name = UUID.randomUUID() + S3Service.TABLE_WAREHOUSE_BUCKET_SUFFIX;
|
||||
s3Service.createBucket(name, region);
|
||||
return "s3://" + name;
|
||||
}
|
||||
|
||||
/** Deletes a table's warehouse with everything in it, as deleting an S3 Tables table deletes its data. */
|
||||
private void deleteWarehouse(String warehouseLocation) {
|
||||
if (warehouseLocation == null || !warehouseLocation.startsWith("s3://")) {
|
||||
return;
|
||||
}
|
||||
String name = warehouseLocation.substring("s3://".length());
|
||||
try {
|
||||
String keyMarker = null;
|
||||
String versionIdMarker = null;
|
||||
do {
|
||||
S3Service.ListVersionsResult versions = s3Service.listObjectVersions(name, null, null, LIST_PAGE_SIZE,
|
||||
keyMarker, versionIdMarker);
|
||||
for (S3Object version : versions.versions()) {
|
||||
s3Service.deleteObject(name, version.getKey(), version.getVersionId());
|
||||
}
|
||||
keyMarker = versions.isTruncated() ? versions.nextKeyMarker() : null;
|
||||
versionIdMarker = versions.isTruncated() ? versions.nextVersionIdMarker() : null;
|
||||
} while (keyMarker != null);
|
||||
String continuation = null;
|
||||
do {
|
||||
S3Service.ListObjectsResult page = s3Service.listObjectsWithPrefixes(name, null, null, LIST_PAGE_SIZE,
|
||||
continuation, null);
|
||||
for (S3Object object : page.objects()) {
|
||||
s3Service.deleteObject(name, object.getKey());
|
||||
}
|
||||
continuation = page.isTruncated() ? page.nextContinuationToken() : null;
|
||||
} while (continuation != null);
|
||||
s3Service.deleteBucket(name);
|
||||
} catch (AwsException e) {
|
||||
if (!"NoSuchBucket".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
LOG.debugv("Warehouse bucket {0} was already gone", name);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized S3Table renameTable(String tableBucketArn, String namespaceName, String name, String newNamespaceName,
|
||||
String newName, String versionToken, String region) {
|
||||
TableBucket bucket = getTableBucket(tableBucketArn, region);
|
||||
@@ -186,6 +279,7 @@ public class S3TablesService {
|
||||
table.setMetadataLocation(metadataLocation);
|
||||
touch(table);
|
||||
persist(bucket, region);
|
||||
withWarehouses(bucket, List.of(table), region);
|
||||
return table;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ public class S3Table {
|
||||
private String format;
|
||||
private String versionToken;
|
||||
private String metadataLocation;
|
||||
private String warehouseLocation;
|
||||
private String createdAt;
|
||||
private String modifiedAt;
|
||||
private String ownerAccountId;
|
||||
@@ -58,6 +59,8 @@ public class S3Table {
|
||||
public void setVersionToken(String versionToken) { this.versionToken = versionToken; }
|
||||
public String getMetadataLocation() { return metadataLocation; }
|
||||
public void setMetadataLocation(String metadataLocation) { this.metadataLocation = metadataLocation; }
|
||||
public String getWarehouseLocation() { return warehouseLocation; }
|
||||
public void setWarehouseLocation(String warehouseLocation) { this.warehouseLocation = warehouseLocation; }
|
||||
public String getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
|
||||
public String getModifiedAt() { return modifiedAt; }
|
||||
|
||||
Reference in new issue
Block a user