Skip to content

Commit

Permalink
fix/read after shutdown (#215)
Browse files Browse the repository at this point in the history
* (fix) read after db shutdown

* (fix) check state before access db

* (fix) by review comments
  • Loading branch information
fengjiachun authored and killme2008 committed Jul 25, 2019
1 parent 3367394 commit 03c4bf8
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand All @@ -272,6 +274,10 @@ private void openDB(final List<ColumnFamilyDescriptor> columnFamilyDescriptors)
this.defaultHandle = columnFamilyHandles.get(1);
}

private void checkState() {
Requires.requireNonNull(this.db, "DB not initialized or destroyed");
}

/**
* Execute write batch template.
*
Expand Down Expand Up @@ -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();
}
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 03c4bf8

Please sign in to comment.