This is an automated email from the ASF dual-hosted git repository. lhotari pushed a commit to branch branch-4.15 in repository https://gitbox.apache.org/repos/asf/bookkeeper.git
commit fc079939769398d8a15e71db8747a2af795ca0e6 Author: Lari Hotari <[email protected]> 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 ee2c38609f..9cade1264f 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; @@ -128,7 +129,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 aab3fd5976..d3e688413f 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 @@ -28,6 +28,7 @@ import io.netty.util.internal.PlatformDependent; 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; @@ -3943,12 +3944,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")); } /** @@ -3967,12 +3963,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")); } /** @@ -3991,12 +3982,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")); } /** @@ -4051,4 +4037,28 @@ public class ServerConfiguration extends AbstractConfiguration<ServerConfigurati public int getMaxOperationNumbersInSingleRocksDBBatch() { return getInt(MAX_OPERATION_NUMBERS_IN_SINGLE_ROCKSDB_WRITE_BATCH, 100000); } + + /** + * 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; + } }
