yuqi1129 commented on code in PR #11354:
URL: https://github.com/apache/gravitino/pull/11354#discussion_r3392623764


##########
common/src/main/java/org/apache/gravitino/utils/FetchFileUtils.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.annotation.Nullable;
+import org.apache.commons.io.FileUtils;
+
+/**
+ * Fetches a file referenced by a URI to a local destination. Supports {@code 
file}, {@code http},
+ * {@code https}, {@code ftp} and {@code hdfs} schemes. This is the single 
shared implementation
+ * used by the job manager and the Kerberos clients of the Hive, Iceberg, 
Hadoop and Paimon
+ * catalogs.
+ *
+ * <p>The {@code hdfs} scheme is resolved reflectively against {@code
+ * org.apache.hadoop.fs.FileSystem} so that this class can live in the {@code 
common} module, which
+ * does not declare a compile-time dependency on Hadoop. Callers that never 
use {@code hdfs} URIs
+ * (for example the job manager and the Paimon catalog) simply pass {@code 
null} for the Hadoop
+ * configuration.
+ */
+public final class FetchFileUtils {
+
+  /** The server configuration that controls unsafe remote URI blocking. */
+  public static final String BLOCK_UNSAFE_REMOTE_URI_CONFIG = 
"gravitino.blockUnsafeRemoteUri";
+
+  /**
+   * Per-destination lock map used to serialize concurrent symlink creation 
for the same destination
+   * file. Keyed by the normalized absolute destination path string to avoid 
races caused by
+   * different path spellings referring to the same file. Entries should be 
removed via {@link
+   * #removeLock(File)} when the destination file is deleted, so the map size 
stays bounded by the
+   * number of live consumers.
+   */
+  private static final ConcurrentHashMap<String, Object> SYMLINK_LOCKS = new 
ConcurrentHashMap<>();
+
+  private static volatile boolean blockUnsafeRemoteUri = true;
+
+  private FetchFileUtils() {}
+
+  /**
+   * Sets whether remote URIs that resolve to unsafe addresses should be 
blocked.
+   *
+   * @param blockUnsafeRemoteUri whether to block unsafe remote URIs
+   */
+  public static void setBlockUnsafeRemoteUri(boolean blockUnsafeRemoteUri) {
+    FetchFileUtils.blockUnsafeRemoteUri = blockUnsafeRemoteUri;
+  }
+
+  /**
+   * Removes the per-destination lock entry for the given file. Should be 
called when the
+   * destination file is deleted (for example on a Kerberos client {@code 
close()}) to prevent
+   * unbounded map growth.
+   *
+   * @param destFile the destination file whose lock entry should be removed
+   */
+  public static void removeLock(File destFile) {
+    
SYMLINK_LOCKS.remove(destFile.toPath().toAbsolutePath().normalize().toString());
+  }
+
+  /**
+   * Fetches the file referenced by {@code fileUri} into {@code destFile}.
+   *
+   * @param fileUri the source URI; a missing scheme is treated as {@code file}
+   * @param destFile the local destination file
+   * @param timeoutMs the connect/read timeout in milliseconds, applied to 
remote (http/https/ftp)
+   *     downloads
+   * @param hadoopConf an {@code org.apache.hadoop.conf.Configuration} 
instance, required only for
+   *     the {@code hdfs} scheme; may be {@code null} when no {@code hdfs} URI 
is fetched
+   * @return the absolute path of {@code destFile}
+   * @throws IOException if the file cannot be fetched
+   */
+  public static String fetchFileFromUri(
+      String fileUri, File destFile, int timeoutMs, @Nullable Object 
hadoopConf)
+      throws IOException {
+    try {
+      URI uri = new URI(fileUri);
+      String scheme = Optional.ofNullable(uri.getScheme()).orElse("file");
+
+      switch (scheme) {
+        case "http":
+        case "https":
+        case "ftp":
+          RemoteUriValidator.validate(
+              uri,
+              blockUnsafeRemoteUri,
+              String.format("'%s' to false", BLOCK_UNSAFE_REMOTE_URI_CONFIG));
+          FileUtils.copyURLToFile(uri.toURL(), destFile, timeoutMs, timeoutMs);
+          break;
+
+        case "file":
+          linkLocalFile(uri, destFile);
+          break;
+
+        case "hdfs":
+          copyHdfsFileToLocal(uri, destFile, hadoopConf);
+          break;
+
+        default:
+          throw new IllegalArgumentException(
+              String.format("The scheme '%s' is not supported", scheme));
+      }
+
+      return destFile.getAbsolutePath();
+    } catch (URISyntaxException ue) {
+      throw new IllegalArgumentException("The uri of file has the wrong 
format", ue);
+    }
+  }
+
+  private static void linkLocalFile(URI uri, File destFile) throws IOException 
{
+    Path srcPath = new File(uri.getPath()).toPath().normalize();
+    if (!Files.exists(srcPath)) {
+      throw new IOException(
+          String.format("Source file does not exist: %s", 
srcPath.toAbsolutePath()));
+    }
+
+    Path destPath = destFile.toPath().toAbsolutePath().normalize();
+    Object lock = SYMLINK_LOCKS.computeIfAbsent(destPath.toString(), k -> new 
Object());
+    synchronized (lock) {
+      // Skip if the symlink already points to the correct target.
+      if (Files.isSymbolicLink(destPath)
+          && Files.readSymbolicLink(destPath).normalize().equals(srcPath)) {
+        return;
+      }
+      // Replace via a temporary symlink + rename to minimize the window where 
the destination path
+      // is absent (which could otherwise cause a concurrent reader, e.g. 
loginUserFromKeytab, to
+      // fail). REPLACE_EXISTING is used here; on common local filesystems 
(ext4, xfs, APFS) a
+      // same-directory rename is effectively atomic at the OS level.
+      Path tmpPath = destPath.resolveSibling(destPath.getFileName() + 
".symlink.tmp");
+      Files.deleteIfExists(tmpPath);
+      Files.createSymbolicLink(tmpPath, srcPath);
+      Files.move(tmpPath, destPath, StandardCopyOption.REPLACE_EXISTING);
+    }
+  }

Review Comment:
   Changed as suggested.



-- 
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