Lazy load native library in Statistics constructor. (#11953)

Summary:
Should fix https://github.com/facebook/rocksdb/issues/9667 and follow the same appropoach as https://github.com/facebook/rocksdb/issues/11919

Pull Request resolved: https://github.com/facebook/rocksdb/pull/11953

Reviewed By: hx235

Differential Revision: D50307456

Pulled By: ajkr

fbshipit-source-id: 43e7e671e8b04875185b38284cefd4c3e11981fa
This commit is contained in:
Radek Hubner 2023-10-18 11:01:04 -07:00 committed by Facebook GitHub Bot
parent d5bc30befa
commit 0bb3a26d89
3 changed files with 24 additions and 11 deletions

View File

@ -450,6 +450,7 @@ test: java java_test
run_test:
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner $(ALL_JAVA_TESTS)
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner org.rocksdb.StatisticsTest
run_plugin_test:
$(JAVA_CMD) $(JAVA_ARGS) -Djava.library.path=target -cp "$(MAIN_CLASSES):$(TEST_CLASSES):$(JAVA_TESTCLASSPATH):target/*" org.rocksdb.test.RocksJunitRunner $(ROCKSDB_PLUGIN_JAVA_TESTS)

View File

@ -14,7 +14,7 @@ import java.util.EnumSet;
public class Statistics extends RocksObject {
public Statistics() {
super(newStatistics());
super(newStatisticsInstance());
}
public Statistics(final Statistics otherStatistics) {
@ -22,7 +22,7 @@ public class Statistics extends RocksObject {
}
public Statistics(final EnumSet<HistogramType> ignoreHistograms) {
super(newStatistics(toArrayValues(ignoreHistograms)));
super(newStatisticsInstance(toArrayValues(ignoreHistograms)));
}
public Statistics(final EnumSet<HistogramType> ignoreHistograms, final Statistics otherStatistics) {
@ -134,8 +134,16 @@ public class Statistics extends RocksObject {
return toString(nativeHandle_);
}
private static long newStatisticsInstance() {
RocksDB.loadLibrary();
return newStatistics();
}
private static native long newStatistics();
private static native long newStatistics(final long otherStatisticsHandle);
private static long newStatisticsInstance(final byte[] ignoreHistograms) {
RocksDB.loadLibrary();
return newStatistics(ignoreHistograms);
}
private static native long newStatistics(final byte[] ignoreHistograms);
private static native long newStatistics(
final byte[] ignoreHistograms, final long otherStatisticsHandle);

View File

@ -5,24 +5,28 @@
package org.rocksdb;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
public class StatisticsTest {
@ClassRule
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
new RocksNativeLibraryResource();
@Rule
public TemporaryFolder dbFolder = new TemporaryFolder();
@Test
public void createStatistics() throws RocksDBException {
final Statistics statistics = new Statistics();
statistics.setStatsLevel(StatsLevel.EXCEPT_DETAILED_TIMERS);
final Statistics statisticsWithHistogramOptions =
new Statistics(EnumSet.of(HistogramType.DB_WRITE, HistogramType.COMPACTION_TIME));
statisticsWithHistogramOptions.reset();
}
@Test
public void statsLevel() throws RocksDBException {
final Statistics statistics = new Statistics();