Skip to content

Commit 5e59636

Browse files
author
xiaohongbo
committed
fall back to load snapshot from file system when external paimon table
1 parent 4e934f4 commit 5e59636

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

paimon-core/src/main/java/org/apache/paimon/catalog/CatalogUtils.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
package org.apache.paimon.catalog;
2020

2121
import org.apache.paimon.CoreOptions;
22+
import org.apache.paimon.Snapshot;
2223
import org.apache.paimon.TableType;
2324
import org.apache.paimon.fs.FileIO;
2425
import org.apache.paimon.fs.Path;
2526
import org.apache.paimon.manifest.PartitionEntry;
2627
import org.apache.paimon.options.Options;
2728
import org.apache.paimon.partition.Partition;
29+
import org.apache.paimon.rest.exceptions.NotImplementedException;
2830
import org.apache.paimon.schema.Schema;
2931
import org.apache.paimon.schema.SchemaManager;
3032
import org.apache.paimon.schema.TableSchema;
@@ -323,6 +325,18 @@ private static List<Pair<Table, TableSnapshot>> toTableAndSnapshots(
323325
snapshot = optional.get();
324326
}
325327
} catch (Catalog.TableNotExistException ignored) {
328+
} catch (NotImplementedException e) {
329+
// does not support load external paimon table snapshot from rest server
330+
// construct TableSnapshot from local snapshot and table scan
331+
if (table instanceof FileStoreTable) {
332+
FileStoreTable fileStoreTable = (FileStoreTable) table;
333+
Snapshot lastSnapshot = table.latestSnapshot().orElse(null);
334+
if (lastSnapshot != null) {
335+
snapshot = tableSnapshotIdFromFileSystem(fileStoreTable, lastSnapshot);
336+
}
337+
} else {
338+
throw new NotImplementedException(e.getMessage());
339+
}
326340
}
327341
}
328342
tableAndSnapshots.add(Pair.of(table, snapshot));
@@ -427,4 +441,29 @@ private static IcebergTable toIcebergTable(
427441
.comment(schema.comment())
428442
.build();
429443
}
444+
445+
private static TableSnapshot tableSnapshotIdFromFileSystem(
446+
FileStoreTable fileStoreTable, Snapshot snapshot) {
447+
Long totalRecordCount = snapshot.totalRecordCount();
448+
long recordCount = totalRecordCount != null ? totalRecordCount : 0L;
449+
long fileSizeInBytes = 0L;
450+
long fileCount = 0L;
451+
long lastFileCreationTime = 0L;
452+
453+
List<PartitionEntry> partitionEntries =
454+
fileStoreTable.newSnapshotReader().withSnapshot(snapshot).partitionEntries();
455+
if (partitionEntries != null) {
456+
for (PartitionEntry entry : partitionEntries) {
457+
if (entry != null) {
458+
fileSizeInBytes += entry.fileSizeInBytes();
459+
fileCount += entry.fileCount();
460+
lastFileCreationTime =
461+
Math.max(lastFileCreationTime, entry.lastFileCreationTime());
462+
}
463+
}
464+
}
465+
466+
return new TableSnapshot(
467+
snapshot, recordCount, fileSizeInBytes, fileCount, lastFileCreationTime);
468+
}
430469
}

paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogServer.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,19 @@ private MockResponse getDataTokenHandle(Identifier tableIdentifier) throws Excep
683683
}
684684

685685
private MockResponse snapshotHandle(Identifier identifier) throws Exception {
686+
if (!tableMetadataStore.containsKey(identifier.getFullName())) {
687+
throw new Catalog.TableNotExistException(identifier);
688+
}
689+
TableMetadata tableMetadata = tableMetadataStore.get(identifier.getFullName());
690+
if (tableMetadata.isExternal()) {
691+
ErrorResponse response =
692+
new ErrorResponse(
693+
ErrorResponse.RESOURCE_TYPE_TABLE,
694+
identifier.getFullName(),
695+
"external paimon table does not support get table snapshot in rest server",
696+
501);
697+
return mockResponse(response, 404);
698+
}
686699
RESTResponse response;
687700
Optional<TableSnapshot> snapshotOptional =
688701
Optional.ofNullable(tableLatestSnapshotStore.get(identifier.getFullName()));
@@ -714,17 +727,7 @@ private MockResponse listSnapshots(Identifier identifier) throws Exception {
714727
}
715728

716729
private MockResponse loadSnapshot(Identifier identifier, String version) throws Exception {
717-
if (!tableMetadataStore.containsKey(identifier.getFullName())) {
718-
throw new Catalog.TableNotExistException(identifier);
719-
}
720-
TableMetadata tableMetadata = tableMetadataStore.get(identifier.getFullName());
721-
if (tableMetadata.isExternal()) {
722-
new ErrorResponse(
723-
ErrorResponse.RESOURCE_TYPE_TABLE,
724-
identifier.getFullName(),
725-
"external paimon table does not support load snapshot in rest server",
726-
403);
727-
}
730+
728731
FileStoreTable table = (FileStoreTable) catalog.getTable(identifier);
729732
SnapshotManager snapshotManager = table.snapshotManager();
730733
Snapshot snapshot = null;
@@ -2095,7 +2098,7 @@ private MockResponse commitSnapshot(
20952098
ErrorResponse.RESOURCE_TYPE_TABLE,
20962099
identifier.getFullName(),
20972100
"external paimon table does not support commit in rest server",
2098-
403);
2101+
501);
20992102
}
21002103
FileStoreTable table = getFileTable(identifier);
21012104
if (!tableId.equals(table.catalogEnvironment().uuid())) {

0 commit comments

Comments
 (0)