Skip to content

Commit a18dc64

Browse files
committed
Improve locating config files (#4560)
(cherry picked from commit f34455b)
1 parent b00d2e0 commit a18dc64

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.concurrent.TimeUnit;
4141
import org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorageFactory.DbConfigType;
4242
import org.apache.bookkeeper.conf.ServerConfiguration;
43+
import org.apache.commons.lang3.StringUtils;
4344
import org.rocksdb.BlockBasedTableConfig;
4445
import org.rocksdb.BloomFilter;
4546
import org.rocksdb.Cache;
@@ -131,7 +132,7 @@ public KeyValueStorageRocksDB(String basePath, String subPath, DbConfigType dbCo
131132
dbFilePath = conf.getDefaultRocksDBConf();
132133
}
133134
log.info("Searching for a RocksDB configuration file in {}", dbFilePath);
134-
if (Paths.get(dbFilePath).toFile().exists()) {
135+
if (StringUtils.isNotBlank(dbFilePath) && Paths.get(dbFilePath).toFile().exists()) {
135136
log.info("Found a RocksDB configuration file and using it to initialize the RocksDB");
136137
db = initializeRocksDBWithConfFile(basePath, subPath, dbConfigType, conf, readOnly, dbFilePath);
137138
} else {

bookkeeper-server/src/main/java/org/apache/bookkeeper/conf/ServerConfiguration.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.net.URL;
2727
import java.util.concurrent.TimeUnit;
28+
import lombok.SneakyThrows;
2829
import org.apache.bookkeeper.bookie.FileChannelProvider;
2930
import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
3031
import org.apache.bookkeeper.bookie.LedgerStorage;
@@ -4047,12 +4048,7 @@ public boolean isSkipReplayJournalInvalidRecord() {
40474048
* @return String configured default rocksdb conf.
40484049
*/
40494050
public String getDefaultRocksDBConf() {
4050-
String defaultPath = "conf/default_rocksdb.conf";
4051-
URL defURL = getClass().getClassLoader().getResource(defaultPath);
4052-
if (defURL != null) {
4053-
defaultPath = defURL.getPath();
4054-
}
4055-
return getString(DEFAULT_ROCKSDB_CONF, defaultPath);
4051+
return getString(DEFAULT_ROCKSDB_CONF, getDefaultFilePath("conf/default_rocksdb.conf"));
40564052
}
40574053

40584054
/**
@@ -4071,12 +4067,7 @@ public ServerConfiguration setDefaultRocksDBConf(String defaultRocksdbConf) {
40714067
* @return String configured entry Location rocksdb conf.
40724068
*/
40734069
public String getEntryLocationRocksdbConf() {
4074-
String defaultPath = "conf/entry_location_rocksdb.conf";
4075-
URL defURL = getClass().getClassLoader().getResource(defaultPath);
4076-
if (defURL != null) {
4077-
defaultPath = defURL.getPath();
4078-
}
4079-
return getString(ENTRY_LOCATION_ROCKSDB_CONF, defaultPath);
4070+
return getString(ENTRY_LOCATION_ROCKSDB_CONF, getDefaultFilePath("conf/entry_location_rocksdb.conf"));
40804071
}
40814072

40824073
/**
@@ -4095,12 +4086,7 @@ public ServerConfiguration setEntryLocationRocksdbConf(String entryLocationRocks
40954086
* @return String configured ledger metadata rocksdb conf.
40964087
*/
40974088
public String getLedgerMetadataRocksdbConf() {
4098-
String defaultPath = "conf/ledger_metadata_rocksdb.conf";
4099-
URL defURL = getClass().getClassLoader().getResource(defaultPath);
4100-
if (defURL != null) {
4101-
defaultPath = defURL.getPath();
4102-
}
4103-
return getString(LEDGER_METADATA_ROCKSDB_CONF, defaultPath);
4089+
return getString(LEDGER_METADATA_ROCKSDB_CONF, getDefaultFilePath("conf/ledger_metadata_rocksdb.conf"));
41044090
}
41054091

41064092
/**
@@ -4133,4 +4119,28 @@ public ServerConfiguration setOperationMaxNumbersInSingleRocksDBWriteBatch(int m
41334119
public int getMaxOperationNumbersInSingleRocksDBBatch() {
41344120
return getInt(MAX_OPERATION_NUMBERS_IN_SINGLE_ROCKSDB_WRITE_BATCH, 100000);
41354121
}
4122+
4123+
/**
4124+
* Retrieves the default file path for the specified file name.
4125+
* This method prioritizes a file available in the classpath, which is often used in testing scenarios.
4126+
* If the file is not found in the classpath, the original file name is returned.
4127+
*
4128+
* @param fileName the name of the file for which to retrieve the path.
4129+
* @return the path of the file if found in the classpath, otherwise the input file name.
4130+
*/
4131+
@SneakyThrows
4132+
private String getDefaultFilePath(String fileName) {
4133+
// Attempt to locate the file in the classpath, used mainly for testing purposes.
4134+
URL resourceURL = getClass().getClassLoader().getResource(fileName);
4135+
if (resourceURL != null && "file".equals(resourceURL.getProtocol())) {
4136+
// Convert the URL to a File object using toURI() for proper URL decoding
4137+
// and platform specific file path handling (such as on Windows OS)
4138+
File file = new File(resourceURL.toURI());
4139+
if (file.exists()) {
4140+
return file.getAbsolutePath();
4141+
}
4142+
}
4143+
// Return the original file name if no path was found in the classpath
4144+
return fileName;
4145+
}
41364146
}

0 commit comments

Comments
 (0)