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

GH-4817 LMDB: Make close method of LmdbRecordIterator thread-safe. #4818

Merged
merged 1 commit into from
Nov 7, 2023
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 @@ -54,7 +54,7 @@ class LmdbRecordIterator implements RecordIterator {

private final int dbi;

private boolean closed = false;
private volatile boolean closed = false;

private final MDBVal keyData;

Expand All @@ -72,6 +72,8 @@ class LmdbRecordIterator implements RecordIterator {

private final StampedLock txnLock;

private final Thread ownerThread = Thread.currentThread();

LmdbRecordIterator(Pool pool, TripleIndex index, boolean rangeSearch, long subj, long pred, long obj,
long context, boolean explicit, Txn txnRef) throws IOException {
this.pool = pool;
Expand Down Expand Up @@ -140,7 +142,7 @@ public long[] next() throws IOException {
lastResult = mdb_cursor_get(cursor, keyData, valueData, MDB_SET_RANGE);
}
if (lastResult != 0) {
close();
closeInternal(false);
return null;
}
}
Expand Down Expand Up @@ -177,30 +179,45 @@ public long[] next() throws IOException {
return quad;
}
}
close();
closeInternal(false);
return null;
} finally {
txnLock.unlockRead(stamp);
}
}

@Override
public void close() throws IOException {
private void closeInternal(boolean maybeCalledAsync) {
if (!closed) {
long stamp;
if (maybeCalledAsync && ownerThread != Thread.currentThread()) {
stamp = txnLock.writeLock();
} else {
stamp = 0;
}
try {
mdb_cursor_close(cursor);
pool.free(keyData);
pool.free(valueData);
if (minKeyBuf != null) {
pool.free(minKeyBuf);
}
if (maxKey != null) {
pool.free(maxKeyBuf);
pool.free(maxKey);
if (!closed) {
mdb_cursor_close(cursor);
pool.free(keyData);
pool.free(valueData);
if (minKeyBuf != null) {
pool.free(minKeyBuf);
}
if (maxKey != null) {
pool.free(maxKeyBuf);
pool.free(maxKey);
}
}
} finally {
closed = true;
if (stamp != 0) {
txnLock.unlockWrite(stamp);
}
}
}
}

@Override
public void close() throws IOException {
closeInternal(true);
}
}
Loading