Skip to content

Commit f34455b

Browse files
authored
Improve locating config files (#4560)
1 parent 4d8b927 commit f34455b

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
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: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import com.google.common.collect.Lists;
2525
import java.io.File;
2626
import java.net.URL;
27-
import java.nio.file.Paths;
2827
import java.util.concurrent.TimeUnit;
28+
import lombok.SneakyThrows;
2929
import org.apache.bookkeeper.bookie.FileChannelProvider;
3030
import org.apache.bookkeeper.bookie.InterleavedLedgerStorage;
3131
import org.apache.bookkeeper.bookie.LedgerStorage;
@@ -4080,8 +4080,7 @@ public boolean isSkipReplayJournalInvalidRecord() {
40804080
* @return String configured default rocksdb conf.
40814081
*/
40824082
public String getDefaultRocksDBConf() {
4083-
String filePath = getFilePath("conf/default_rocksdb.conf");
4084-
return getString(DEFAULT_ROCKSDB_CONF, filePath);
4083+
return getString(DEFAULT_ROCKSDB_CONF, getDefaultFilePath("conf/default_rocksdb.conf"));
40854084
}
40864085

40874086
/**
@@ -4100,8 +4099,7 @@ public ServerConfiguration setDefaultRocksDBConf(String defaultRocksdbConf) {
41004099
* @return String configured entry Location rocksdb conf.
41014100
*/
41024101
public String getEntryLocationRocksdbConf() {
4103-
String filePath = getFilePath("conf/entry_location_rocksdb.conf");
4104-
return getString(ENTRY_LOCATION_ROCKSDB_CONF, filePath);
4102+
return getString(ENTRY_LOCATION_ROCKSDB_CONF, getDefaultFilePath("conf/entry_location_rocksdb.conf"));
41054103
}
41064104

41074105
/**
@@ -4120,8 +4118,7 @@ public ServerConfiguration setEntryLocationRocksdbConf(String entryLocationRocks
41204118
* @return String configured ledger metadata rocksdb conf.
41214119
*/
41224120
public String getLedgerMetadataRocksdbConf() {
4123-
String filePath = getFilePath("conf/ledger_metadata_rocksdb.conf");
4124-
return getString(LEDGER_METADATA_ROCKSDB_CONF, filePath);
4121+
return getString(LEDGER_METADATA_ROCKSDB_CONF, getDefaultFilePath("conf/ledger_metadata_rocksdb.conf"));
41254122
}
41264123

41274124
/**
@@ -4176,16 +4173,26 @@ public long getMaxBatchReadSize() {
41764173
}
41774174

41784175
/**
4179-
* Get the path of a file from resources.
4176+
* Retrieves the default file path for the specified file name.
4177+
* This method prioritizes a file available in the classpath, which is often used in testing scenarios.
4178+
* If the file is not found in the classpath, the original file name is returned.
41804179
*
4181-
* @param fileName the name of the file to get the path for.
4182-
* @return String the absolute path of the file.
4180+
* @param fileName the name of the file for which to retrieve the path.
4181+
* @return the path of the file if found in the classpath, otherwise the input file name.
41834182
*/
4184-
private String getFilePath(String fileName) {
4183+
@SneakyThrows
4184+
private String getDefaultFilePath(String fileName) {
4185+
// Attempt to locate the file in the classpath, used mainly for testing purposes.
41854186
URL resourceURL = getClass().getClassLoader().getResource(fileName);
4186-
if (resourceURL != null) {
4187-
return Paths.get(resourceURL.getPath()).toString();
4187+
if (resourceURL != null && "file".equals(resourceURL.getProtocol())) {
4188+
// Convert the URL to a File object using toURI() for proper URL decoding
4189+
// and platform specific file path handling (such as on Windows OS)
4190+
File file = new File(resourceURL.toURI());
4191+
if (file.exists()) {
4192+
return file.getAbsolutePath();
4193+
}
41884194
}
4189-
return "";
4195+
// Return the original file name if no path was found in the classpath
4196+
return fileName;
41904197
}
41914198
}

0 commit comments

Comments
 (0)