fix: archive Glue table versions (#1233)

This commit is contained in:
David Phillips authored and GitHub committed 2026-06-10 21:33:19 -05:00
1 parent d96f65afca
commit 27943b35c3
4 files changed
+101 -22

No files matched your search

@@ -88,11 +88,14 @@ public class GlueJsonHandler {
case "UpdateTable" -> {
String dbName = request.get("DatabaseName").asText();
Table table = mapper.treeToValue(request.get("TableInput"), Table.class);
glueService.updateTable(dbName, table, request.path("VersionId").asText(null));
glueService.updateTable(dbName, table, request.path("VersionId").asText(null),
request.path("SkipArchive").asBoolean(false));
yield Response.ok().build();
}
case "GetTableVersions" -> {
yield Response.ok(Map.of("TableVersions", glueService.getTableVersions())).build();
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
yield Response.ok(Map.of("TableVersions", glueService.getTableVersions(dbName, tableName))).build();
}
case "DeleteTable" -> {
String dbName = request.get("DatabaseName").asText();
@@ -41,6 +41,7 @@ public class GlueService {
private final StorageBackend<String, Database> databaseStore;
private final StorageBackend<String, Table> tableStore;
private final StorageBackend<String, Table> tableVersionStore;
private final StorageBackend<String, Partition> partitionStore;
private final StorageBackend<String, UserDefinedFunction> functionStore;
private final GlueSchemaRegistryService schemaRegistryService;
@@ -52,6 +53,7 @@ public class GlueService {
RegionResolver regionResolver) {
this.databaseStore = storageFactory.create("glue", "databases.json", new TypeReference<>() {});
this.tableStore = storageFactory.create("glue", "tables.json", new TypeReference<>() {});
this.tableVersionStore = storageFactory.create("glue", "table_versions.json", new TypeReference<>() {});
this.partitionStore = storageFactory.create("glue", "partitions.json", new TypeReference<>() {});
this.functionStore = storageFactory.create("glue", "functions.json", new TypeReference<>() {});
this.schemaRegistryService = schemaRegistryService;
@@ -60,12 +62,14 @@ public class GlueService {
GlueService(StorageBackend<String, Database> databaseStore,
StorageBackend<String, Table> tableStore,
StorageBackend<String, Table> tableVersionStore,
StorageBackend<String, Partition> partitionStore,
StorageBackend<String, UserDefinedFunction> functionStore,
GlueSchemaRegistryService schemaRegistryService,
RegionResolver regionResolver) {
this.databaseStore = databaseStore;
this.tableStore = tableStore;
this.tableVersionStore = tableVersionStore;
this.partitionStore = partitionStore;
this.functionStore = functionStore;
this.schemaRegistryService = schemaRegistryService;
@@ -151,11 +155,7 @@ public class GlueService {
return resolved;
}
public void updateTable(String databaseName, Table table) {
updateTable(databaseName, table, null);
}
public synchronized void updateTable(String databaseName, Table table, String versionId) {
public synchronized void updateTable(String databaseName, Table table, String versionId, boolean skipArchive) {
Database database = getDatabase(databaseName);
String key = tableKey(databaseName, table.getName());
Table existing = tableStore.get(key)
@@ -165,6 +165,10 @@ public class GlueService {
throw new AwsException("ConcurrentModificationException",
"Update table failed due to concurrent modifications.", 400);
}
if (!skipArchive) {
tableVersionStore.put(tableVersionKey(existing.getDatabaseName(), existing.getName(), existing.getVersionId()),
copyTable(existing));
}
validateSchemaReference(table);
table.setName(normalizeName(table.getName()));
table.setDatabaseName(database.getName());
@@ -175,13 +179,27 @@ public class GlueService {
LOG.infov("Updated Glue Table: {0}.{1}", databaseName, table.getName());
}
public List<Map<String, Object>> getTableVersions() {
return List.of();
public List<Map<String, Object>> getTableVersions(String databaseName, String tableName) {
Table current = getTable(databaseName, tableName);
String prefix = tableKey(databaseName, tableName) + ":";
List<Table> versions = new ArrayList<>();
versions.add(current);
versions.addAll(tableVersionStore.scan(k -> k.startsWith(prefix)).stream()
.sorted(Comparator.comparing(GlueService::versionIdAsLong).reversed())
.toList());
return versions.stream()
.map(table -> Map.<String, Object>of(
"Table", withResolvedSchemaReference(table),
"VersionId", table.getVersionId()))
.toList();
}
public void deleteTable(String databaseName, String tableName) {
String key = tableKey(databaseName, tableName);
tableStore.delete(key);
tableVersionStore.keys().stream()
.filter(versionKey -> versionKey.startsWith(key + ":"))
.forEach(tableVersionStore::delete);
partitionStore.scan(k -> k.startsWith(key + ":")).forEach(p -> {
partitionStore.delete(key + ":" + String.join(",", p.getValues()));
});
@@ -336,6 +354,10 @@ public class GlueService {
return normalizeName(databaseName) + ":" + normalizeName(tableName);
}
private static String tableVersionKey(String databaseName, String tableName, String versionId) {
return tableKey(databaseName, tableName) + ":" + versionId;
}
private static String normalizeName(String name) {
return name.toLowerCase(Locale.ROOT);
}
@@ -412,6 +434,10 @@ public class GlueService {
}
}
private static long versionIdAsLong(Table table) {
return Long.parseLong(table.getVersionId());
}
private static StorageDescriptor copyStorageDescriptor(StorageDescriptor source) {
if (source == null) {
return null;