Implement closeRequested

This commit is contained in:
Andrea Cavalli 2022-06-08 18:52:15 +02:00
parent 3be0d3710c
commit 563defb2ff

View File

@ -130,6 +130,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
private final ConcurrentHashMap<Long, Snapshot> snapshotsHandles = new ConcurrentHashMap<>(); private final ConcurrentHashMap<Long, Snapshot> snapshotsHandles = new ConcurrentHashMap<>();
private final AtomicLong nextSnapshotNumbers = new AtomicLong(1); private final AtomicLong nextSnapshotNumbers = new AtomicLong(1);
private final StampedLock closeLock = new StampedLock(); private final StampedLock closeLock = new StampedLock();
private volatile boolean closeRequested = false;
private volatile boolean closed = false; private volatile boolean closed = false;
@SuppressWarnings("SwitchStatementWithTooFewBranches") @SuppressWarnings("SwitchStatementWithTooFewBranches")
@ -189,6 +190,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
} }
} }
var rocksLogger = new RocksLog4jLogger(rocksdbOptions, logger);
this.persistentCaches = new HashMap<>(); this.persistentCaches = new HashMap<>();
for (Column column : columns) { for (Column column : columns) {
@ -361,7 +363,8 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
rocksdbOptions, rocksdbOptions,
databaseOptions.persistentCaches(), databaseOptions.persistentCaches(),
columnOptions.persistentCacheId(), columnOptions.persistentCacheId(),
refs refs,
rocksLogger
)); ));
columnFamilyOptions.setTableFormatConfig(tableOptions); columnFamilyOptions.setTableFormatConfig(tableOptions);
@ -560,7 +563,8 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
DBOptions rocksdbOptions, DBOptions rocksdbOptions,
List<it.cavallium.dbengine.rpc.current.data.PersistentCache> persistentCaches, List<it.cavallium.dbengine.rpc.current.data.PersistentCache> persistentCaches,
NullableString persistentCacheId, NullableString persistentCacheId,
RocksDBRefs refs) throws RocksDBException { RocksDBRefs refs,
RocksLog4jLogger rocksLogger) throws RocksDBException {
if (persistentCacheId.isEmpty()) { if (persistentCacheId.isEmpty()) {
return null; return null;
} }
@ -581,7 +585,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
var persistentCache = new PersistentCache(Env.getDefault(), var persistentCache = new PersistentCache(Env.getDefault(),
foundCache.path(), foundCache.path(),
foundCache.size(), foundCache.size(),
new RocksLog4jLogger(rocksdbOptions, logger), rocksLogger,
foundCache.optimizeForNvm() foundCache.optimizeForNvm()
); );
refs.track(persistentCache); refs.track(persistentCache);
@ -1057,14 +1061,13 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
List.of(Tag.of("db.name", dbName), Tag.of("db.statistics.name", tickerType.name())), List.of(Tag.of("db.name", dbName), Tag.of("db.statistics.name", tickerType.name())),
stats, stats,
statistics -> { statistics -> {
if (closed) { if (closeRequested || closed) return 0d;
return 0d; long closeReadLock = 0;
}
var closeReadLock = closeLock.readLock();
try { try {
if (closed) { closeReadLock = closeLock.tryReadLock(1, TimeUnit.SECONDS);
return 0d; } catch (InterruptedException ignored) {}
} try {
if (closeRequested || closed || closeReadLock == 0) return 0d;
return statistics.getTickerCount(tickerType); return statistics.getTickerCount(tickerType);
} finally { } finally {
closeLock.unlockRead(closeReadLock); closeLock.unlockRead(closeReadLock);
@ -1247,21 +1250,22 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
public Mono<MemoryStats> getMemoryStats() { public Mono<MemoryStats> getMemoryStats() {
return Mono return Mono
.fromCallable(() -> { .fromCallable(() -> {
if (closed) return null; if (closeRequested || closed) return null;
var closeReadLock = closeLock.readLock(); long closeReadLock = 0;
try { try {
if (!closed) { //noinspection BlockingMethodInNonBlockingContext
ensureOpen(); closeReadLock = closeLock.tryReadLock(1, TimeUnit.SECONDS);
return new MemoryStats(db.getAggregatedLongProperty("rocksdb.estimate-table-readers-mem"), } catch (InterruptedException ignored) {}
db.getAggregatedLongProperty("rocksdb.size-all-mem-tables"), try {
db.getAggregatedLongProperty("rocksdb.cur-size-all-mem-tables"), if (closeRequested || closed || closeReadLock == 0) return null;
db.getAggregatedLongProperty("rocksdb.estimate-num-keys"), ensureOpen();
db.getAggregatedLongProperty("rocksdb.block-cache-usage") / this.handles.size(), return new MemoryStats(db.getAggregatedLongProperty("rocksdb.estimate-table-readers-mem"),
db.getAggregatedLongProperty("rocksdb.block-cache-pinned-usage") / this.handles.size() db.getAggregatedLongProperty("rocksdb.size-all-mem-tables"),
); db.getAggregatedLongProperty("rocksdb.cur-size-all-mem-tables"),
} else { db.getAggregatedLongProperty("rocksdb.estimate-num-keys"),
return null; db.getAggregatedLongProperty("rocksdb.block-cache-usage") / this.handles.size(),
} db.getAggregatedLongProperty("rocksdb.block-cache-pinned-usage") / this.handles.size()
);
} finally { } finally {
closeLock.unlockRead(closeReadLock); closeLock.unlockRead(closeReadLock);
} }
@ -1394,23 +1398,24 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
public Mono<String> getRocksDBStats() { public Mono<String> getRocksDBStats() {
return Mono return Mono
.fromCallable(() -> { .fromCallable(() -> {
if (closed) return null; if (closeRequested || closed) return null;
var closeReadLock = closeLock.readLock(); long closeReadLock = 0;
try { try {
if (!closed) { //noinspection BlockingMethodInNonBlockingContext
ensureOpen(); closeReadLock = closeLock.tryReadLock(1, TimeUnit.SECONDS);
StringBuilder aggregatedStats = new StringBuilder(); } catch (InterruptedException ignored) {}
for (var entry : this.handles.entrySet()) { try {
aggregatedStats if (closeRequested || closed || closeReadLock == 0) return null;
.append(entry.getKey().name()) ensureOpen();
.append("\n") StringBuilder aggregatedStats = new StringBuilder();
.append(db.getProperty(entry.getValue(), "rocksdb.stats")) for (var entry : this.handles.entrySet()) {
.append("\n"); aggregatedStats
} .append(entry.getKey().name())
return aggregatedStats.toString(); .append("\n")
} else { .append(db.getProperty(entry.getValue(), "rocksdb.stats"))
return null; .append("\n");
} }
return aggregatedStats.toString();
} finally { } finally {
closeLock.unlockRead(closeReadLock); closeLock.unlockRead(closeReadLock);
} }
@ -1425,10 +1430,14 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
.fromIterable(handles.entrySet()) .fromIterable(handles.entrySet())
.flatMapSequential(handle -> Mono .flatMapSequential(handle -> Mono
.fromCallable(() -> { .fromCallable(() -> {
if (closed) return null; if (closeRequested || closed) return null;
var closeReadLock = closeLock.readLock(); long closeReadLock = 0;
try { try {
if (closed) return null; //noinspection BlockingMethodInNonBlockingContext
closeReadLock = closeLock.tryReadLock(1, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {}
try {
if (closeRequested || closed || closeReadLock == 0) return null;
ensureOpen(); ensureOpen();
return db.getPropertiesOfAllTables(handle.getValue()); return db.getPropertiesOfAllTables(handle.getValue());
} finally { } finally {
@ -1531,6 +1540,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase {
public Mono<Void> close() { public Mono<Void> close() {
return Mono return Mono
.<Void>fromCallable(() -> { .<Void>fromCallable(() -> {
closeRequested = true;
if (statistics != null) { if (statistics != null) {
statistics.close(); statistics.close();
statistics = null; statistics = null;