Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/read after shutdown #215

Merged
merged 3 commits into from
Jul 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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