jt2594838 commented on code in PR #18224:
URL: https://github.com/apache/iotdb/pull/18224#discussion_r3593820467


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/util/LoadUtil.java:
##########
@@ -183,32 +186,91 @@ public static boolean loadFilesToActiveDir(
     final Map<String, String> attributes = 
appendCurrentUserIfAbsent(loadAttributes);
     final File targetDir = 
ActiveLoadPathHelper.resolveTargetDir(targetFilePath, attributes);
 
+    final List<File> sourceFiles = new ArrayList<>(files.size());
     for (final String file : files) {
-      loadTsFileAsyncToTargetDir(targetDir, new File(file), isDeleteAfterLoad);
+      sourceFiles.add(new File(file));
     }
+    sourceFiles.sort(Comparator.comparing(LoadUtil::isTsFile));
+    transferFilesToActiveDir(targetDir, sourceFiles, isDeleteAfterLoad);
     return true;
   }
 
-  private static void loadTsFileAsyncToTargetDir(
-      final File targetDir, final File file, final boolean isDeleteAfterLoad) 
throws IOException {
-    if (!file.exists()) {
+  static void transferFilesToActiveDir(
+      final File targetDir, final List<File> sourceFiles, final boolean 
isDeleteAfterLoad)
+      throws IOException {
+    final List<File> existingSourceFiles = new ArrayList<>(sourceFiles.size());
+    for (final File sourceFile : sourceFiles) {
+      if (sourceFile.exists()) {
+        existingSourceFiles.add(sourceFile);
+      }
+    }
+    if (existingSourceFiles.isEmpty()) {
       return;
     }
-    if (!targetDir.exists() && !targetDir.mkdirs()) {
-      if (!targetDir.exists()) {
-        throw new IOException(
-            StorageEngineMessages.FAILED_TO_CREATE_TARGET_DIR + 
targetDir.getAbsolutePath());
+
+    final File transferDir = new File(targetDir, UUID.randomUUID().toString());
+    try {
+      Files.createDirectories(transferDir.toPath());
+      for (final File sourceFile : existingSourceFiles) {
+        final File targetFile = new File(transferDir, sourceFile.getName());
+        RetryUtils.retryOnException(
+            () -> {
+              transferFile(sourceFile, targetFile, isDeleteAfterLoad);
+              return null;
+            });
+      }
+    } catch (final IOException | RuntimeException e) {
+      
org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(transferDir, 
true);

Review Comment:
   Mind this



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/util/LoadUtil.java:
##########
@@ -183,32 +186,91 @@ public static boolean loadFilesToActiveDir(
     final Map<String, String> attributes = 
appendCurrentUserIfAbsent(loadAttributes);
     final File targetDir = 
ActiveLoadPathHelper.resolveTargetDir(targetFilePath, attributes);
 
+    final List<File> sourceFiles = new ArrayList<>(files.size());
     for (final String file : files) {
-      loadTsFileAsyncToTargetDir(targetDir, new File(file), isDeleteAfterLoad);
+      sourceFiles.add(new File(file));
     }
+    sourceFiles.sort(Comparator.comparing(LoadUtil::isTsFile));
+    transferFilesToActiveDir(targetDir, sourceFiles, isDeleteAfterLoad);
     return true;
   }
 
-  private static void loadTsFileAsyncToTargetDir(
-      final File targetDir, final File file, final boolean isDeleteAfterLoad) 
throws IOException {
-    if (!file.exists()) {
+  static void transferFilesToActiveDir(
+      final File targetDir, final List<File> sourceFiles, final boolean 
isDeleteAfterLoad)
+      throws IOException {
+    final List<File> existingSourceFiles = new ArrayList<>(sourceFiles.size());
+    for (final File sourceFile : sourceFiles) {
+      if (sourceFile.exists()) {
+        existingSourceFiles.add(sourceFile);
+      }
+    }
+    if (existingSourceFiles.isEmpty()) {
       return;
     }
-    if (!targetDir.exists() && !targetDir.mkdirs()) {
-      if (!targetDir.exists()) {
-        throw new IOException(
-            StorageEngineMessages.FAILED_TO_CREATE_TARGET_DIR + 
targetDir.getAbsolutePath());
+
+    final File transferDir = new File(targetDir, UUID.randomUUID().toString());
+    try {
+      Files.createDirectories(transferDir.toPath());
+      for (final File sourceFile : existingSourceFiles) {
+        final File targetFile = new File(transferDir, sourceFile.getName());
+        RetryUtils.retryOnException(
+            () -> {
+              transferFile(sourceFile, targetFile, isDeleteAfterLoad);
+              return null;
+            });
+      }
+    } catch (final IOException | RuntimeException e) {
+      
org.apache.iotdb.commons.utils.FileUtils.deleteFileOrDirectory(transferDir, 
true);
+      throw e;
+    }
+
+    if (isDeleteAfterLoad) {
+      deleteSourceFiles(existingSourceFiles);
+    }
+  }
+
+  private static void transferFile(
+      final File sourceFile, final File targetFile, final boolean useHardLink) 
throws IOException {
+    Exception linkException = null;
+    if (useHardLink) {
+      try {
+        Files.createLink(targetFile.toPath(), sourceFile.toPath());
+        return;
+      } catch (final IOException | UnsupportedOperationException | 
SecurityException e) {
+        linkException = e;
+      }
+    }
+
+    try {
+      Files.copy(
+          sourceFile.toPath(),
+          targetFile.toPath(),
+          StandardCopyOption.REPLACE_EXISTING,
+          StandardCopyOption.COPY_ATTRIBUTES);
+    } catch (final IOException e) {
+      if (linkException != null) {
+        e.addSuppressed(linkException);
+      }
+      throw e;
+    }
+  }
+
+  private static void deleteSourceFiles(final List<File> sourceFiles) {
+    for (final File sourceFile : sourceFiles) {
+      try {
+        RetryUtils.retryOnException(
+            () -> {
+              Files.deleteIfExists(sourceFile.toPath());
+              return null;
+            });
+      } catch (final Exception e) {
+        LOGGER.warn(StorageEngineMessages.FAILED_TO_DELETE_FILE_OR_DIR, 
sourceFile, e);
       }
     }
-    RetryUtils.retryOnException(
-        () -> {
-          if (isDeleteAfterLoad) {
-            moveFileWithMD5Check(file, targetDir);
-          } else {
-            copyFileWithMD5Check(file, targetDir);
-          }
-          return null;
-        });
+  }
+
+  private static boolean isTsFile(final File file) {
+    return 
file.getAbsolutePath().equals(getTsFilePath(file.getAbsolutePath()));
   }

Review Comment:
   Why not use endsWith?



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/load/util/LoadUtilTest.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.storageengine.load.util;
+
+import 
org.apache.iotdb.db.storageengine.dataregion.modification.ModificationFile;
+import 
org.apache.iotdb.db.storageengine.dataregion.modification.v1.ModificationFileV1;
+import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.List;
+
+public class LoadUtilTest {
+
+  private File tempDir;
+  private File sourceDir;
+  private File targetDir;
+
+  @Before
+  public void setUp() throws Exception {
+    tempDir = Files.createTempDirectory("load-util").toFile();
+    sourceDir = new File(tempDir, "source");
+    targetDir = new File(tempDir, "target");
+    Assert.assertTrue(sourceDir.mkdirs());
+    Assert.assertTrue(targetDir.mkdirs());
+  }
+
+  @After
+  public void tearDown() {
+    deleteRecursively(tempDir);
+  }
+
+  @Test
+  public void 
testTransferFilesKeepsCompanionsTogetherAndDeletesSourcesAfterHandoff()
+      throws Exception {
+    final List<File> sourceFiles = createTsFileAndCompanions();
+    for (final File sourceFile : sourceFiles) {
+      Files.write(
+          new File(targetDir, sourceFile.getName()).toPath(),
+          "existing".getBytes(StandardCharsets.UTF_8));
+    }
+
+    LoadUtil.transferFilesToActiveDir(targetDir, sourceFiles, true);
+
+    final File[] transferDirs = targetDir.listFiles(File::isDirectory);
+    Assert.assertNotNull(transferDirs);
+    Assert.assertEquals(1, transferDirs.length);
+    for (final File sourceFile : sourceFiles) {
+      Assert.assertFalse(sourceFile.exists());
+      final File transferredFile = new File(transferDirs[0], 
sourceFile.getName());
+      Assert.assertTrue(transferredFile.exists());
+      Assert.assertArrayEquals(
+          sourceFile.getName().getBytes(StandardCharsets.UTF_8),
+          Files.readAllBytes(transferredFile.toPath()));
+      Assert.assertArrayEquals(
+          "existing".getBytes(StandardCharsets.UTF_8),
+          Files.readAllBytes(new File(targetDir, 
sourceFile.getName()).toPath()));
+    }

Review Comment:
   What is the point of the "existing" 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