World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#14302] fix(iceberg-rest): Return 400 when register metadata location does not exist (#14628)
MaSai3f53a0ee16b9
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.javaindex 1b5312ad7e..36ed1f09b5 100644--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java@@ -333,10 +333,15 @@ public class IcebergCatalogWrapper implements AutoCloseable {** @param metadataLocation metadata file location* @return parsed table metadata+ * @throws BadRequestException if no metadata file exists at the location*/public TableMetadata loadTableMetadataFromLocation(String metadataLocation) {try (FileIO fileIO = CatalogUtil.loadFileIO(fileIOImpl(), fileIOProperties(), null)) {return TableMetadataParser.read(fileIO, metadataLocation);+ } catch (NotFoundException e) {+ // The location comes from the request, so a missing file is a client error. 404 is reserved+ // by the REST spec for a namespace or table that does not exist.+ throw new BadRequestException("Metadata location %s does not exist", metadataLocation);}}@@ -449,7 +454,14 @@ public class IcebergCatalogWrapper implements AutoCloseable {public LoadViewResponse registerView(Namespace namespace, RegisterViewRequest request) {request.validate();- return CatalogHandlers.registerView(getViewCatalog(), namespace, request);+ try {+ return CatalogHandlers.registerView(getViewCatalog(), namespace, request);+ } catch (NotFoundException e) {+ // As in registerTable: the metadata location comes from the request.+ throw new BadRequestException(+ "Cannot register view %s: metadata location %s does not exist",+ request.name(), request.metadataLocation());+ }}public LoadViewResponse updateView(TableIdentifier viewIdentifier, UpdateTableRequest request) {diff --git a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.javaindex e0dfe9dd98..ca0122231b 100644--- a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java+++ b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java@@ -56,7 +56,9 @@ import org.apache.iceberg.jdbc.JdbcCatalog;import org.apache.iceberg.rest.requests.CreateNamespaceRequest;import org.apache.iceberg.rest.requests.CreateTableRequest;import org.apache.iceberg.rest.requests.ImmutableRegisterTableRequest;+import org.apache.iceberg.rest.requests.ImmutableRegisterViewRequest;import org.apache.iceberg.rest.requests.RegisterTableRequest;+import org.apache.iceberg.rest.requests.RegisterViewRequest;import org.apache.iceberg.rest.requests.UpdateTableRequest;import org.apache.iceberg.rest.responses.LoadViewResponse;import org.apache.iceberg.types.Types;@@ -313,6 +315,39 @@ public class TestIcebergCatalogWrapper {}}+ @Test+ public void testRegisterViewWithMissingMetadataIsBadRequest(@TempDir Path warehouseDir)+ throws Exception {+ try (IcebergCatalogWrapper wrapper =+ new IcebergCatalogWrapper(+ new IcebergConfig(Map.of(IcebergConstants.CATALOG_BACKEND, "memory")))) {+ Namespace namespace = Namespace.of("db");+ wrapper.createNamespace(CreateNamespaceRequest.builder().withNamespace(namespace).build());+ RegisterViewRequest request =+ ImmutableRegisterViewRequest.builder()+ .name("ghost")+ .metadataLocation(warehouseDir.resolve("missing/v1.metadata.json").toUri().toString())+ .build();+ BadRequestException e =+ Assertions.assertThrows(+ BadRequestException.class, () -> wrapper.registerView(namespace, request));+ Assertions.assertTrue(e.getMessage().contains("does not exist"), e.getMessage());+ }+ }++ @Test+ public void testLoadTableMetadataFromMissingLocationIsBadRequest(@TempDir Path warehouseDir)+ throws Exception {+ try (IcebergCatalogWrapper wrapper =+ new IcebergCatalogWrapper(new IcebergConfig(sqliteConfig(warehouseDir)))) {+ String location = warehouseDir.resolve("missing/v1.metadata.json").toUri().toString();+ BadRequestException e =+ Assertions.assertThrows(+ BadRequestException.class, () -> wrapper.loadTableMetadataFromLocation(location));+ Assertions.assertTrue(e.getMessage().contains("does not exist"), e.getMessage());+ }+ }+@ParameterizedTest@ValueSource(booleans = {true, false})void testPurgeDeletesWhatCanBeReadWhenAManifestListIsMissing(