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

lhotari pushed a commit to branch branch-4.17
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git

commit a64227c0a9123cfcc7003783805585e0692e263a
Author: Lari Hotari <lhot...@users.noreply.github.com>
AuthorDate: Wed Apr 2 10:43:00 2025 +0300

    Improve locating config files (#4560)
    
    (cherry picked from commit f34455b2dafb7ce164b5f81db8b252a7b5cd1a7d)
---
 .../bookie/storage/ldb/KeyValueStorageRocksDB.java |  3 +-
 .../bookkeeper/conf/ServerConfiguration.java       | 46 +++++++++++++---------
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
index 22c5bc75db..c9e72847fb 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java
@@ -40,6 +40,7 @@ import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
 import 
org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorageFactory.DbConfigType;
 import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.commons.lang3.StringUtils;
 import org.rocksdb.BlockBasedTableConfig;
 import org.rocksdb.BloomFilter;
 import org.rocksdb.Cache;
@@ -131,7 +132,7 @@ public class KeyValueStorageRocksDB implements 
KeyValueStorage {
             dbFilePath = conf.getDefaultRocksDBConf();
         }
         log.info("Searching for a RocksDB configuration file in {}", 
dbFilePath);
-        if (Paths.get(dbFilePath).toFile().exists()) {
+        if (StringUtils.isNotBlank(dbFilePath) && 
Paths.get(dbFilePath).toFile().exists()) {
             log.info("Found a RocksDB configuration file and using it to 
initialize the RocksDB");
             db = initializeRocksDBWithConfFile(basePath, subPath, 
dbConfigType, conf, readOnly, dbFilePath);
         } else {
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
index d9f1c545d9..b77e0dc713 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java
@@ -25,6 +25,7 @@ import com.google.common.collect.Lists;
 import java.io.File;
 import java.net.URL;
 import java.util.concurrent.TimeUnit;
+import lombok.SneakyThrows;
 import org.apache.bookkeeper.bookie.FileChannelProvider;
 import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
 import org.apache.bookkeeper.bookie.LedgerStorage;
@@ -4073,12 +4074,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured default rocksdb conf.
      */
     public String getDefaultRocksDBConf() {
-        String defaultPath = "conf/default_rocksdb.conf";
-        URL defURL = getClass().getClassLoader().getResource(defaultPath);
-        if (defURL != null) {
-            defaultPath = defURL.getPath();
-        }
-        return getString(DEFAULT_ROCKSDB_CONF, defaultPath);
+        return getString(DEFAULT_ROCKSDB_CONF, 
getDefaultFilePath("conf/default_rocksdb.conf"));
     }
 
     /**
@@ -4097,12 +4093,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured entry Location rocksdb conf.
      */
     public String getEntryLocationRocksdbConf() {
-        String defaultPath = "conf/entry_location_rocksdb.conf";
-        URL defURL = getClass().getClassLoader().getResource(defaultPath);
-        if (defURL != null) {
-            defaultPath = defURL.getPath();
-        }
-        return getString(ENTRY_LOCATION_ROCKSDB_CONF, defaultPath);
+        return getString(ENTRY_LOCATION_ROCKSDB_CONF, 
getDefaultFilePath("conf/entry_location_rocksdb.conf"));
     }
 
     /**
@@ -4121,12 +4112,7 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
      * @return String configured ledger metadata rocksdb conf.
      */
     public String getLedgerMetadataRocksdbConf() {
-        String defaultPath = "conf/ledger_metadata_rocksdb.conf";
-        URL defURL = getClass().getClassLoader().getResource(defaultPath);
-        if (defURL != null) {
-            defaultPath = defURL.getPath();
-        }
-        return getString(LEDGER_METADATA_ROCKSDB_CONF, defaultPath);
+        return getString(LEDGER_METADATA_ROCKSDB_CONF, 
getDefaultFilePath("conf/ledger_metadata_rocksdb.conf"));
     }
 
     /**
@@ -4179,4 +4165,28 @@ public class ServerConfiguration extends 
AbstractConfiguration<ServerConfigurati
     public long getMaxBatchReadSize() {
         return this.getLong(MAX_BATCH_READ_SIZE, DEFAULT_MAX_BATCH_READ_SIZE);
     }
+
+    /**
+     * Retrieves the default file path for the specified file name.
+     * This method prioritizes a file available in the classpath, which is 
often used in testing scenarios.
+     * If the file is not found in the classpath, the original file name is 
returned.
+     *
+     * @param fileName the name of the file for which to retrieve the path.
+     * @return the path of the file if found in the classpath, otherwise the 
input file name.
+     */
+    @SneakyThrows
+    private String getDefaultFilePath(String fileName) {
+        // Attempt to locate the file in the classpath, used mainly for 
testing purposes.
+        URL resourceURL = getClass().getClassLoader().getResource(fileName);
+        if (resourceURL != null && "file".equals(resourceURL.getProtocol())) {
+            // Convert the URL to a File object using toURI() for proper URL 
decoding
+            // and platform specific file path handling (such as on Windows OS)
+            File file = new File(resourceURL.toURI());
+            if (file.exists()) {
+                return file.getAbsolutePath();
+            }
+        }
+        // Return the original file name if no path was found in the classpath
+        return fileName;
+    }
 }

Reply via email to