luoluoyuyu commented on code in PR #19258:
URL: https://github.com/apache/iotdb/pull/19258#discussion_r4560876871


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/tsfile/PipeTsFileDirPathMapping.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.iotdb.db.pipe.resource.tsfile;
+
+import org.apache.iotdb.commons.conf.IoTDBConstant;
+import org.apache.iotdb.db.i18n.DataNodePipeMessages;
+
+import org.apache.tsfile.utils.FSUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Resolves the directory that holds the hardlinked or copied files of a 
TsFile.
+ *
+ * <p>The directory is always a subdirectory of the data directory that 
contains the TsFile, i.e.
+ * {@code 
<dataDir>/<pipe_hardlink_base_dir_name>/<pipe_hardlink_tsfile_dir_name>[/<pipeName>]}.
+ * Since a hardlink can only be created on the same file system as the source 
file, this keeps the
+ * hardlink on the same disk as the TsFile. IoTDB may place the TsFiles of one 
DataRegion on
+ * different disks, because every new TsFile is allocated by {@code 
TierManager}/{@code
+ * FolderManager} according to {@code dn_multi_dir_strategy}. Therefore the 
target directory can not
+ * be chosen per DataRegion or per pipe, but must be chosen per TsFile.
+ *
+ * <p>TsFiles on non-local storage (HDFS, object storage) can not be 
hardlinked nor copied by the
+ * local file system APIs. They are returned as is, i.e., pipe reads them in 
place instead of
+ * pinning a hardlink.
+ */
+class PipeTsFileDirPathMapping {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PipeTsFileDirPathMapping.class);
+
+  /** Canonical path of every local data directory -> canonical path of its 
pipe base directory. */
+  private final Map<String, String> dataDirPathToPipeBaseDirPath = new 
LinkedHashMap<>();
+
+  private final String pipeHardlinkBaseDirName;
+  private final String pipeHardlinkTsFileDirName;
+
+  PipeTsFileDirPathMapping(
+      final @Nonnull String[] localDataDirs,
+      final @Nonnull String pipeHardlinkBaseDirName,
+      final @Nonnull String pipeHardlinkTsFileDirName) {
+    this.pipeHardlinkBaseDirName = pipeHardlinkBaseDirName;
+    this.pipeHardlinkTsFileDirName = pipeHardlinkTsFileDirName;
+
+    for (final String dataDir : localDataDirs) {
+      final String canonicalDataDir = canonicalPath(dataDir);
+      dataDirPathToPipeBaseDirPath.put(
+          canonicalDataDir, canonicalDataDir + File.separator + 
pipeHardlinkBaseDirName);
+    }
+    LOGGER.info(
+        
DataNodePipeMessages.LOG_PIPE_HARDLINK_DIRECTORIES_ARE_RESOLVED_AS_ARG_1DAB3D0F,
+        dataDirPathToPipeBaseDirPath);
+  }
+
+  /**
+   * Given a TsFile, a .resource file or a .mod file, returns the hardlink or 
the copied file in the
+   * pipe directory on the same disk.
+   *
+   * <p>If the given file is not on a local file system, it is returned as is.
+   */
+  File getHardlinkOrCopiedFile(final File file, final @Nullable String 
pipeName)
+      throws IOException {
+    if (!isLocalFile(file)) {
+      return file;
+    }
+    // The file has already been moved into a pipe directory, e.g., the 
historical extractor pins
+    // the pipe file of another pipe. It must not be linked or copied again.
+    if (isInPipeTsFileDir(file)) {
+      return file;

Review Comment:
   Two more hardening changes went into 83e2cc18 while reviewing this area:
   
   - A pipe file which has already been left in a pipe directory (e.g. by a 
previous process, outside every configured data directory) is now resolved from 
the `<pipe base>/<tsfile dir>` layout instead of the data-directory anchors, 
because a pipe directory may be named exactly like the pipe base directory 
(`pipe/tsfile/pipe/...`). Before, such a file was resolved into 
`pipe/tsfile/pipe/tsfile/pipe/...` and nested on every pin. Covered by 
`testFileInAnExistingPipeDirIsResolvedFromTheLayout`.
   - `createHardLinkOrCopy` returns the target as is when the source and the 
target are the same file, instead of letting `Files.createLink` fail with 
`FileAlreadyExistsException` and then deleting the target (which is the source) 
before the copy. Covered by 
`testExistingPipeFileIsReusedInsteadOfCopiedOntoItself`.



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