roryqi commented on code in PR #15004:
URL: https://github.com/apache/gravitino/pull/15004#discussion_r4598963262


##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergTableLocationUtils.java:
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.iceberg.common.utils;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import javax.annotation.Nullable;
+
+/** Location comparisons and JDBC lookup patterns for Iceberg's standard 
metadata layout. */
+public final class IcebergTableLocationUtils {
+  private static final String METADATA_DIRECTORY = "/metadata/";
+
+  private IcebergTableLocationUtils() {}
+
+  /**
+   * Normalizes trailing separators, local URI forms and equivalent S3 schemes.
+   *
+   * @param location storage location, possibly absent
+   * @return normalized location, or an empty string for an absent location
+   */
+  public static String normalize(@Nullable String location) {
+    if (location == null) {
+      return "";
+    }
+    String value = location.trim();
+    if (value.startsWith("s3a://") || value.startsWith("s3n://")) {
+      value = "s3://" + value.substring(6);
+    } else if (value.startsWith("file:")) {
+      String path = value.substring(5);
+      while (path.startsWith("//")) {
+        path = path.substring(1);
+      }
+      value = "file:" + (path.startsWith("/") ? path : "/" + path);
+    } else if (value.startsWith("/")) {
+      value = "file:" + value;
+    }
+    while (value.endsWith("/") && !value.endsWith(":/")) {
+      value = value.substring(0, value.length() - 1);
+    }
+    return value;

Review Comment:
   Fixed in 
[b687414](https://github.com/apache/gravitino/commit/b687414faffa3c74aa42c33ca811b199c9e9d7d6).
 Filesystem dot segments and repeated separators are normalized before 
lookup/comparison. The JDBC query includes filesystem pointer candidates so 
stored aliases are also compared, without opening their metadata files. 
Object-store keys retain their literal segments.
   
   Added real SQLite/local-file regressions, an authorization-interceptor 
denial test, and checks that S3/GCS key semantics remain unchanged.



##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java:
##########
@@ -245,6 +246,19 @@ public UpdateNamespacePropertiesResponse 
updateNamespaceProperties(
 
   public LoadTableResponse registerTable(Namespace namespace, 
RegisterTableRequest request) {
     validateTableLocation(request.metadataLocation());
+    Catalog loadedCatalog = getCatalog();
+    // Subclasses can enforce additional registration invariants through the 
original API.
+    if (request instanceof RegisterTableRequestWithMetadata
+        && loadedCatalog.getClass() == 
JdbcCatalogWithMetadataLocationSupport.class) {

Review Comment:
   Fixed in 
[b687414](https://github.com/apache/gravitino/commit/b687414faffa3c74aa42c33ca811b199c9e9d7d6).
 Embedded table location and write-path validation now runs before backend 
registration, for both preloaded snapshots and ordinary requests. Custom 
registration hooks remain in place. Remote-storage view registration now 
validates the embedded view location and write paths as well.
   
   Added rejection tests for non-JDBC catalogs, JDBC subclasses, and views, 
plus coverage preserving valid custom registration hooks.



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/dispatcher/RegisterTableSourceResolver.java:
##########
@@ -53,22 +56,47 @@ public static List<TableIdentifier> findSourceTables(
       TableIdentifier target,
       String metadataLocation,
       TableMetadata metadata) {
+    return findOverlappingTables(catalogWrapper, target, metadata.location());

Review Comment:
   Restored exact current-pointer matching in 
[b687414](https://github.com/apache/gravitino/commit/b687414faffa3c74aa42c33ca811b199c9e9d7d6),
 including URI aliases, within the same JDBC candidate query. A real SQLite 
regression with a separate metadata directory verifies that the source is found 
without namespace traversal or reads of other tables' metadata.
   
   This fixes the reproduced current-pointer registration regression. As you 
noted, it does not provide complete custom-layout overlap protection for 
create/set-location. That broader support remains outside this PR's 
standard-layout scope and is explicitly documented; this finding is only 
partially addressed.



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/dispatcher/RegisterTableSourceResolver.java:
##########
@@ -82,63 +110,42 @@ private static void collectMatchingTables(
       return;
     }
     for (Namespace child : children) {
-      collectMatchingTables(
-          catalogWrapper, child, target, uuid, tableLocation, 
metadataLocation, matches);
+      collectMatchingTables(catalogWrapper, child, target, tableLocation, 
matches);
     }
     for (TableIdentifier identifier : tables) {
       if (identifier.equals(target)) {
         continue;
       }
-      if (matchesSource(catalogWrapper, identifier, uuid, tableLocation, 
metadataLocation)) {
+      if (matchesSource(catalogWrapper, identifier, tableLocation)) {
         matches.add(identifier);
       }
     }
   }
 
   private static boolean matchesSource(
-      IcebergCatalogWrapper catalogWrapper,
-      TableIdentifier identifier,
-      String uuid,
-      String tableLocation,
-      String metadataLocation) {
+      IcebergCatalogWrapper catalogWrapper, TableIdentifier identifier, String 
tableLocation) {
     Optional<String> currentMeta = 
catalogWrapper.getTableMetadataLocation(identifier);
-    if (currentMeta.isPresent() && 
normalizePath(currentMeta.get()).equals(metadataLocation)) {
-      return true;
+    if (currentMeta.isPresent()) {
+      return matchesPointer(currentMeta.get(), tableLocation);
     }
     try {
       TableMetadata existing = catalogWrapper.loadTableMetadata(identifier);
-      if (uuid != null && uuid.equals(existing.uuid())) {
-        return true;
-      }
-      return tableLocation != null
-          && !tableLocation.isEmpty()
-          && tableLocation.equals(normalizePath(existing.location()));
-    } catch (NoSuchTableException e) {
-      // Only a table that no longer exists is unrelated. Any other failure to 
read its metadata
-      // must not be mistaken for unrelated metadata, which would skip source 
authorization.
+      return IcebergTableLocationUtils.overlaps(tableLocation, 
existing.location());
+    } catch (RuntimeException e) {

Review Comment:
   Fixed in 
[b687414](https://github.com/apache/gravitino/commit/b687414faffa3c74aa42c33ca811b199c9e9d7d6).
 The fallback now skips only NoSuchTableException; other metadata/storage 
failures propagate and block the operation.
   
   Added resolver coverage and an actual authorization-interceptor regression 
verifying that a source read failure never reaches MethodInvocation.proceed(). 
This restores failure handling on the existing fallback without adding scans or 
metadata reads to the standard-layout JDBC path.



##########
iceberg/iceberg-common/src/main/java/org/apache/iceberg/MetastoreRegisterTableUtils.java:
##########
@@ -96,5 +98,49 @@ public static Table registerTable(
         catalog.metricsReporter());
   }
 
+  /**
+   * Registers a metadata snapshot already read and authorized by the caller, 
without reopening its
+   * file. Existing destination metadata is still read for an atomic overwrite.
+   *
+   * @param catalog destination catalog
+   * @param identifier destination table
+   * @param metadata snapshot read from the requested metadata file
+   * @param overwrite whether the destination can be replaced
+   * @param metadataLocationOverwrite atomic pointer replacement
+   * @return registered table
+   */
+  public static Table registerTable(
+      BaseMetastoreCatalog catalog,
+      TableIdentifier identifier,
+      TableMetadata metadata,
+      boolean overwrite,
+      MetadataLocationOverwrite metadataLocationOverwrite) {
+    Preconditions.checkArgument(
+        identifier != null && catalog.isValidIdentifier(identifier),
+        "Invalid identifier: %s",
+        identifier);
+    Preconditions.checkArgument(
+        metadata.metadataFileLocation() != null,
+        "Registered metadata must retain its file location");
+    TableOperations ops = catalog.newTableOps(identifier);
+    TableMetadata base = ops.current();
+    if (base == null) {
+      ops.commit(null, metadata);
+    } else {
+      if (!overwrite) {
+        throw new AlreadyExistsException("Table already exists: %s", 
identifier);
+      }
+      Preconditions.checkArgument(
+          Objects.equals(base.uuid(), metadata.uuid()),
+          "Cannot overwrite a table with a different table UUID");
+      metadataLocationOverwrite.overwrite(

Review Comment:
   Fixed in 
[b687414](https://github.com/apache/gravitino/commit/b687414faffa3c74aa42c33ca811b199c9e9d7d6)
 by replacing the new Table-returning overload with a dedicated void 
registerTableWithMetadata operation. It commits the authorized snapshot without 
exposing cached pre-overwrite TableOperations. The existing String-based API is 
unchanged.
   
   The regression advances the destination from snapshot A to B, registers A 
with overwrite, and checks that the response and a fresh catalog load reflect 
A. It also verifies that registration does not reopen the authorized metadata 
file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to