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

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


The following commit(s) were added to refs/heads/master by this push:
     new 15d87f53bb7 HDDS-17155. NativeLibraryLoader falls back to 
java.io.tmpdir when the working directory is not writable (#11636)
15d87f53bb7 is described below

commit 15d87f53bb771d7a3bc6fdb6aaa0596cb6ee62f8
Author: Siyao Meng <[email protected]>
AuthorDate: Thu Oct 22 02:55:44 2026 -0700

    HDDS-17155. NativeLibraryLoader falls back to java.io.tmpdir when the 
working directory is not writable (#11636)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../hadoop/hdds/utils/NativeLibraryLoader.java     | 21 ++++++++++++-----
 .../hadoop/hdds/utils/TestNativeLibraryLoader.java | 26 ++++++++++++++++++----
 2 files changed, 37 insertions(+), 10 deletions(-)

diff --git 
a/hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/NativeLibraryLoader.java
 
b/hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/NativeLibraryLoader.java
index 39bb0b3ca56..7eb10a8b18f 100644
--- 
a/hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/NativeLibraryLoader.java
+++ 
b/hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/NativeLibraryLoader.java
@@ -29,7 +29,6 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import org.apache.commons.io.FileUtils;
@@ -165,13 +164,23 @@ private Pair<Optional<File>, List<File>> 
copyResourceFromJarToTemp(final String
         return Pair.of(Optional.empty(), null);
       }
 
-      final String nativeLibDir =
-          Objects.nonNull(getSystemProperty(NATIVE_LIB_TMP_DIR)) ?
-              getSystemProperty(NATIVE_LIB_TMP_DIR) : "";
-      final File dir = new File(nativeLibDir).getAbsoluteFile();
+      final String nativeLibDir = getSystemProperty(NATIVE_LIB_TMP_DIR);
+      final boolean useWorkingDir = nativeLibDir == null || 
nativeLibDir.isEmpty();
+      final File dir = new File(useWorkingDir ? getSystemProperty("user.dir") 
: nativeLibDir).getAbsoluteFile();
 
       // create a temporary dir to copy the library to
-      final Path tempPath = Files.createTempDirectory(dir.toPath(), 
libraryName);
+      Path tempPath;
+      try {
+        tempPath = Files.createTempDirectory(dir.toPath(), libraryName);
+      } catch (IOException e) {
+        if (!useWorkingDir) {
+          throw e;
+        }
+        // The working directory may not be writable, fall back to 
java.io.tmpdir.
+        LOG.info("Unable to extract {} under working directory {}, using 
java.io.tmpdir instead: {}",
+            libraryName, dir, e.toString());
+        tempPath = Files.createTempDirectory(libraryName);
+      }
       final File tempDir = tempPath.toFile();
       if (!tempDir.exists()) {
         return Pair.of(Optional.empty(), null);
diff --git 
a/hadoop-hdds/rocks-native/src/test/java/org/apache/hadoop/hdds/utils/TestNativeLibraryLoader.java
 
b/hadoop-hdds/rocks-native/src/test/java/org/apache/hadoop/hdds/utils/TestNativeLibraryLoader.java
index b8ac1c132e0..88181603866 100644
--- 
a/hadoop-hdds/rocks-native/src/test/java/org/apache/hadoop/hdds/utils/TestNativeLibraryLoader.java
+++ 
b/hadoop-hdds/rocks-native/src/test/java/org/apache/hadoop/hdds/utils/TestNativeLibraryLoader.java
@@ -25,6 +25,7 @@
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.CALLS_REAL_METHODS;
 import static org.mockito.Mockito.anyString;
+import static org.mockito.Mockito.eq;
 import static org.mockito.Mockito.mockStatic;
 import static org.mockito.Mockito.same;
 
@@ -41,6 +42,7 @@
 import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
 import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.mockito.MockedStatic;
 
@@ -56,6 +58,19 @@ private static Stream<String> 
nativeLibraryDirectoryLocations() {
     return Stream.of("", tempDir.toAbsolutePath().toString(), null);
   }
 
+  private static Stream<Arguments> dummyLibraryLocations() {
+    String workingDir = System.getProperty("user.dir");
+    // A regular file cannot be used as a parent directory, so extraction 
under it fails.
+    String unwritableDir = 
tempDir.resolve("not-a-dir").toAbsolutePath().toString();
+    String javaTmpDir = System.getProperty("java.io.tmpdir");
+    return Stream.of(
+        Arguments.of("", workingDir, workingDir),
+        Arguments.of(null, workingDir, workingDir),
+        Arguments.of(tempDir.toAbsolutePath().toString(), workingDir, 
tempDir.toAbsolutePath().toString()),
+        Arguments.of(null, unwritableDir, javaTmpDir),
+        Arguments.of("", unwritableDir, javaTmpDir));
+  }
+
   @EnabledIfSystemProperty(named = ROCKS_TOOLS_NATIVE_PROPERTY, matches = 
"true")
   @ParameterizedTest
   @MethodSource("nativeLibraryDirectoryLocations")
@@ -73,14 +88,17 @@ public void testNativeLibraryLoader(String 
nativeLibraryDirectoryLocation) throw
   }
 
   @ParameterizedTest
-  @MethodSource("nativeLibraryDirectoryLocations")
-  public void testDummyLibrary(String nativeLibraryDirectoryLocation) {
+  @MethodSource("dummyLibraryLocations")
+  public void testDummyLibrary(String nativeLibraryDirectoryLocation, String 
workingDir, String expectedDir)
+      throws Exception {
     Map<String, Boolean> libraryLoadedMap = new HashMap<>();
     NativeLibraryLoader loader = new NativeLibraryLoader(libraryLoadedMap);
     try (MockedStatic<NativeLibraryLoader> mockedNativeLibraryLoader = 
mockStatic(NativeLibraryLoader.class,
         CALLS_REAL_METHODS)) {
       mockedNativeLibraryLoader.when(() -> 
NativeLibraryLoader.getSystemProperty(same(NATIVE_LIB_TMP_DIR)))
           .thenReturn(nativeLibraryDirectoryLocation);
+      mockedNativeLibraryLoader.when(() -> 
NativeLibraryLoader.getSystemProperty(eq("user.dir")))
+          .thenReturn(workingDir);
       
mockedNativeLibraryLoader.when(NativeLibraryLoader::getInstance).thenReturn(loader);
       // Mocking to force copy random bytes to create a lib file to
       // nativeLibraryDirectoryLocation. But load library will fail.
@@ -88,8 +106,8 @@ public void testDummyLibrary(String 
nativeLibraryDirectoryLocation) {
           .thenReturn(new ByteArrayInputStream(new byte[]{0, 1, 2, 3}));
       String dummyLibraryName = "dummy_lib";
       List<String> dependencies = Arrays.asList("dep1", "dep2");
-      File absDir = new File(nativeLibraryDirectoryLocation == null ? "" : 
nativeLibraryDirectoryLocation)
-          .getAbsoluteFile();
+      FileUtils.touch(tempDir.resolve("not-a-dir").toFile());
+      File absDir = new File(expectedDir).getAbsoluteFile();
 
       NativeLibraryLoader.getInstance().loadLibrary(dummyLibraryName, 
dependencies);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to