From 42261d9ee47381e0fab36b741361d0152b856fdb Mon Sep 17 00:00:00 2001 From: "jiachun.fjc" Date: Sun, 21 Jul 2019 21:27:02 +0800 Subject: [PATCH 1/3] (fix) read after db shutdown --- .../alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java | 5 +++++ 1 file changed, 5 insertions(+) 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..65e455b6e 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 @@ -248,6 +248,7 @@ private boolean saveFirstLogIndex(final long firstLogIndex) { try { final byte[] vs = new byte[8]; Bits.putLong(vs, 0, firstLogIndex); + Requires.requireNonNull(this.db, "DB not initialized or destroyed."); this.db.put(this.confHandle, this.writeOptions, FIRST_LOG_IDX_KEY, vs); return true; } catch (final RocksDBException e) { @@ -321,6 +322,7 @@ public void shutdown() { this.writeOptions = null; this.defaultHandle = null; this.confHandle = null; + LOG.info("DB is destroyed."); } finally { this.writeLock.unlock(); } @@ -340,6 +342,7 @@ public long getFirstLogIndex() { if (this.hasLoadFirstLogIndex) { return this.firstLogIndex; } + Requires.requireNonNull(this.db, "DB not initialized or destroyed."); it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions); it.seekToFirst(); if (it.isValid()) { @@ -360,6 +363,7 @@ public long getFirstLogIndex() { @Override public long getLastLogIndex() { this.readLock.lock(); + Requires.requireNonNull(this.db, "DB not initialized or destroyed."); try (final RocksIterator it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions)) { it.seekToLast(); if (it.isValid()) { @@ -399,6 +403,7 @@ public LogEntry getEntry(final long index) { } protected byte[] getValueFromRocksDB(final byte[] keyBytes) throws RocksDBException { + Requires.requireNonNull(this.db, "DB not initialized or destroyed."); return this.db.get(this.defaultHandle, keyBytes); } From 7724aaae667018d72e7915ecb04c978e09483b9a Mon Sep 17 00:00:00 2001 From: "jiachun.fjc" Date: Mon, 22 Jul 2019 00:42:03 +0800 Subject: [PATCH 2/3] (fix) check state before access db --- .../sofa/jraft/storage/impl/RocksDBLogStorage.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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 65e455b6e..f7191c192 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,7 +249,7 @@ private boolean saveFirstLogIndex(final long firstLogIndex) { try { final byte[] vs = new byte[8]; Bits.putLong(vs, 0, firstLogIndex); - Requires.requireNonNull(this.db, "DB not initialized or destroyed."); + checkState(); this.db.put(this.confHandle, this.writeOptions, FIRST_LOG_IDX_KEY, vs); return true; } catch (final RocksDBException e) { @@ -273,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. * @@ -342,7 +347,7 @@ public long getFirstLogIndex() { if (this.hasLoadFirstLogIndex) { return this.firstLogIndex; } - Requires.requireNonNull(this.db, "DB not initialized or destroyed."); + checkState(); it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions); it.seekToFirst(); if (it.isValid()) { @@ -363,7 +368,7 @@ public long getFirstLogIndex() { @Override public long getLastLogIndex() { this.readLock.lock(); - Requires.requireNonNull(this.db, "DB not initialized or destroyed."); + checkState(); try (final RocksIterator it = this.db.newIterator(this.defaultHandle, this.totalOrderReadOptions)) { it.seekToLast(); if (it.isValid()) { @@ -403,7 +408,7 @@ public LogEntry getEntry(final long index) { } protected byte[] getValueFromRocksDB(final byte[] keyBytes) throws RocksDBException { - Requires.requireNonNull(this.db, "DB not initialized or destroyed."); + checkState(); return this.db.get(this.defaultHandle, keyBytes); } From 040075f46a09a7e35cb8ff79cdd1bdd2675eee24 Mon Sep 17 00:00:00 2001 From: "jiachun.fjc" Date: Wed, 24 Jul 2019 21:51:08 +0800 Subject: [PATCH 3/3] (fix) by review comments --- .../com/alipay/sofa/jraft/storage/impl/RocksDBLogStorage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f7191c192..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 @@ -327,7 +327,7 @@ public void shutdown() { this.writeOptions = null; this.defaultHandle = null; this.confHandle = null; - LOG.info("DB is destroyed."); + LOG.info("DB destroyed, the db path is: {}.", this.path); } finally { this.writeLock.unlock(); }