diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java index 953e4cc06..68c711813 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java @@ -196,6 +196,7 @@ private boolean initAndLoad(final ConfigurationManager confManager) throws Rocks public static final byte[] FIRST_LOG_IDX_KEY = Utils.getBytes("meta/firstLogIndex"); private void load(final ConfigurationManager confManager) { + checkState(); try (final RocksIterator it = this.db.newIterator(this.confHandle, this.totalOrderReadOptions)) { it.seekToFirst(); while (it.isValid()) { @@ -248,6 +249,7 @@ private boolean saveFirstLogIndex(final long firstLogIndex) { try { final byte[] vs = new byte[8]; Bits.putLong(vs, 0, firstLogIndex); + checkState(); this.db.put(this.confHandle, this.writeOptions, FIRST_LOG_IDX_KEY, vs); return true; } catch (final RocksDBException e) { @@ -272,6 +274,10 @@ private void openDB(final List columnFamilyDescriptors) this.defaultHandle = columnFamilyHandles.get(1); } + private void checkState() { + Requires.requireNonNull(this.db, "DB not initialized or destroyed"); + } + /** * Execute write batch template. * @@ -321,6 +327,7 @@ public void shutdown() { this.writeOptions = null; this.defaultHandle = null; this.confHandle = null; + LOG.info("DB destroyed, the db path is: {}.", this.path); } finally { this.writeLock.unlock(); } @@ -340,6 +347,7 @@ public long getFirstLogIndex() { if (this.hasLoadFirstLogIndex) { return this.firstLogIndex; } + checkState(); it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions); it.seekToFirst(); if (it.isValid()) { @@ -360,6 +368,7 @@ public long getFirstLogIndex() { @Override public long getLastLogIndex() { this.readLock.lock(); + checkState(); try (final RocksIterator it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions)) { it.seekToLast(); if (it.isValid()) { @@ -399,6 +408,7 @@ public LogEntry getEntry(final long index) { } protected byte[] getValueFromRocksDB(final byte[] keyBytes) throws RocksDBException { + checkState(); return this.db.get(this.defaultHandle, keyBytes); }