This is an automated email from the ASF dual-hosted git repository.

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 9a281f8e1c0 Fix snapshot recovery when hard links are unsupported 
(#19236)
9a281f8e1c0 is described below

commit 9a281f8e1c062007233a89bd18a8e22372097fc2
Author: Hongzhi Gao <[email protected]>
AuthorDate: Fri Nov 13 11:14:41 2026 +0800

    Fix snapshot recovery when hard links are unsupported (#19236)
    
    * Fix snapshot recovery when hard links are unsupported
    
    * Declare directly used PowerMock test support dependency
---
 iotdb-core/datanode/pom.xml                        |  6 ++
 .../dataregion/snapshot/SnapshotLoader.java        | 11 +---
 .../snapshot/SnapshotLinkFallbackTest.java         | 72 ++++++++++++++++++++++
 pom.xml                                            |  6 ++
 4 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/iotdb-core/datanode/pom.xml b/iotdb-core/datanode/pom.xml
index 2dad06d8ba7..3a94fd45fbe 100644
--- a/iotdb-core/datanode/pom.xml
+++ b/iotdb-core/datanode/pom.xml
@@ -285,6 +285,12 @@
             <artifactId>powermock-api-mockito2</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.powermock</groupId>
+            <artifactId>powermock-api-support</artifactId>
+            <version>${powermock.version}</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.powermock</groupId>
             <artifactId>powermock-reflect</artifactId>
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLoader.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLoader.java
index dcf2246036c..f0fc34cf1e1 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLoader.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLoader.java
@@ -412,16 +412,7 @@ public class SnapshotLoader {
                 targetFile.getParentFile().getAbsolutePath()));
       }
 
-      try {
-        Files.createLink(targetFile.toPath(), file.toPath());
-        LOGGER.debug(StorageEngineMessages.CREATED_HARD_LINK, file, 
targetFile);
-        fileTarget.put(fileKey, finalDir);
-        return targetFile;
-      } catch (IOException e) {
-        LOGGER.info(StorageEngineMessages.CANNOT_CREATE_LINK_FALLBACK_COPY, 
file, targetFile);
-      }
-
-      Files.copy(file.toPath(), targetFile.toPath());
+      SnapshotFileUtils.createLinkOrCopy(targetFile.toPath(), file.toPath());
       fileTarget.put(fileKey, finalDir);
       return targetFile;
     } catch (Exception e) {
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLinkFallbackTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLinkFallbackTest.java
new file mode 100644
index 00000000000..52db16882f4
--- /dev/null
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotLinkFallbackTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.dataregion.snapshot;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", 
"javax.management.*"})
+@PrepareForTest({SnapshotFileUtils.class, SnapshotLoader.class})
+public class SnapshotLinkFallbackTest {
+  @Rule public TemporaryFolder folder = new TemporaryFolder();
+
+  @Test
+  public void testRecoveryWithoutLogCopiesWhenHardLinksAreUnsupported() throws 
Exception {
+    Path source = folder.newFile("source.tsfile").toPath();
+    Files.write(source, new byte[] {1, 2, 3});
+    File dataDir = folder.newFolder("data");
+    Map<String, String> targets = new HashMap<>();
+    SnapshotLoader loader = new SnapshotLoader(folder.getRoot().toString(), 
"root.sg", "0");
+    PowerMockito.replace(PowerMockito.method(Files.class, "createLink", 
Path.class, Path.class))
+        .with(
+            (proxy, method, arguments) -> {
+              throw new UnsupportedOperationException("Hard links 
unsupported");
+            });
+    File restored =
+        Whitebox.invokeMethod(
+            loader,
+            "createLinksFromSnapshotToSourceDir",
+            "sequence/root.sg/0/0",
+            source.toFile(),
+            targets,
+            "source",
+            dataDir.getAbsolutePath());
+    Files.delete(source);
+    assertArrayEquals(new byte[] {1, 2, 3}, 
Files.readAllBytes(restored.toPath()));
+    assertEquals(dataDir.getAbsolutePath(), targets.get("source"));
+  }
+}
diff --git a/pom.xml b/pom.xml
index 6286660c83c..abd01e78010 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1437,8 +1437,14 @@
                 -->
                 <argLine><![CDATA[
                         --add-opens=java.base/java.lang=ALL-UNNAMED
+                        --add-opens=java.base/java.lang.reflect=ALL-UNNAMED
                         --add-opens=java.base/java.util=ALL-UNNAMED
                         --add-opens=java.base/java.nio=ALL-UNNAMED
+                        --add-opens=java.base/java.nio.file=ALL-UNNAMED
+                        --add-opens=java.base/java.nio.channels=ALL-UNNAMED
+                        --add-opens=java.base/java.nio.channels.spi=ALL-UNNAMED
+                        --add-opens=java.base/sun.nio.fs=ALL-UNNAMED
+                        --add-opens=java.base/sun.nio.ch=ALL-UNNAMED
                         --add-opens=java.base/java.io=ALL-UNNAMED
                         --add-opens=java.base/java.net=ALL-UNNAMED
                         --add-opens=java.base/java.util.concurrent=ALL-UNNAMED

Reply via email to