Avoid SIGSEGV when using/closing rocksIterator on different threads

This commit is contained in:
Andrea Cavalli 2023-07-28 19:22:42 +02:00
parent e83270906e
commit a4c322e96a

View File

@ -37,7 +37,7 @@ public class RocksIteratorObj extends SimpleResource {
this.iterNextTime = iteratorMetrics.iterNextTime(); this.iterNextTime = iteratorMetrics.iterNextTime();
} }
public void seek(ByteBuffer seekBuf) throws RocksDBException { public synchronized void seek(ByteBuffer seekBuf) throws RocksDBException {
ensureOpen(); ensureOpen();
startedIterSeek.increment(); startedIterSeek.increment();
try { try {
@ -48,7 +48,7 @@ public class RocksIteratorObj extends SimpleResource {
rocksIterator.status(); rocksIterator.status();
} }
public void seek(byte[] seekArray) throws RocksDBException { public synchronized void seek(byte[] seekArray) throws RocksDBException {
ensureOpen(); ensureOpen();
startedIterSeek.increment(); startedIterSeek.increment();
try { try {
@ -59,7 +59,7 @@ public class RocksIteratorObj extends SimpleResource {
rocksIterator.status(); rocksIterator.status();
} }
public void seekToFirst() throws RocksDBException { public synchronized void seekToFirst() throws RocksDBException {
ensureOpen(); ensureOpen();
startedIterSeek.increment(); startedIterSeek.increment();
try { try {
@ -70,7 +70,7 @@ public class RocksIteratorObj extends SimpleResource {
rocksIterator.status(); rocksIterator.status();
} }
public void seekToLast() throws RocksDBException { public synchronized void seekToLast() throws RocksDBException {
ensureOpen(); ensureOpen();
startedIterSeek.increment(); startedIterSeek.increment();
try { try {
@ -84,7 +84,7 @@ public class RocksIteratorObj extends SimpleResource {
/** /**
* Useful for reverse iterations * Useful for reverse iterations
*/ */
public void seekFrom(Buf key) { public synchronized void seekFrom(Buf key) {
ensureOpen(); ensureOpen();
var keyArray = LLUtils.asArray(key); var keyArray = LLUtils.asArray(key);
rocksIterator.seekForPrev(keyArray); rocksIterator.seekForPrev(keyArray);
@ -95,7 +95,7 @@ public class RocksIteratorObj extends SimpleResource {
/** /**
* Useful for forward iterations * Useful for forward iterations
*/ */
public void seekTo(Buf key) { public synchronized void seekTo(Buf key) {
ensureOpen(); ensureOpen();
var keyArray = LLUtils.asArray(key); var keyArray = LLUtils.asArray(key);
startedIterSeek.increment(); startedIterSeek.increment();
@ -105,19 +105,19 @@ public class RocksIteratorObj extends SimpleResource {
this.seekingTo = keyArray; this.seekingTo = keyArray;
} }
public boolean isValid() { public synchronized boolean isValid() {
ensureOpen(); ensureOpen();
return rocksIterator.isValid(); return rocksIterator.isValid();
} }
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
public int key(ByteBuffer buffer) { public synchronized int key(ByteBuffer buffer) {
ensureOpen(); ensureOpen();
return rocksIterator.key(buffer); return rocksIterator.key(buffer);
} }
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
public int value(ByteBuffer buffer) { public synchronized int value(ByteBuffer buffer) {
ensureOpen(); ensureOpen();
return rocksIterator.value(buffer); return rocksIterator.value(buffer);
} }
@ -125,7 +125,7 @@ public class RocksIteratorObj extends SimpleResource {
/** /**
* The returned buffer may change when calling next() or when the iterator is not valid anymore * The returned buffer may change when calling next() or when the iterator is not valid anymore
*/ */
public byte[] key() { public synchronized byte[] key() {
ensureOpen(); ensureOpen();
return rocksIterator.key(); return rocksIterator.key();
} }
@ -133,7 +133,7 @@ public class RocksIteratorObj extends SimpleResource {
/** /**
* The returned buffer may change when calling next() or when the iterator is not valid anymore * The returned buffer may change when calling next() or when the iterator is not valid anymore
*/ */
public byte[] value() { public synchronized byte[] value() {
ensureOpen(); ensureOpen();
return rocksIterator.value(); return rocksIterator.value();
} }
@ -153,11 +153,10 @@ public class RocksIteratorObj extends SimpleResource {
} }
public void next() throws RocksDBException { public void next() throws RocksDBException {
ensureOpen();
next(true); next(true);
} }
public void next(boolean traceStats) { public synchronized void next(boolean traceStats) {
ensureOpen(); ensureOpen();
if (traceStats) { if (traceStats) {
startedIterNext.increment(); startedIterNext.increment();
@ -169,11 +168,10 @@ public class RocksIteratorObj extends SimpleResource {
} }
public void prev() throws RocksDBException { public void prev() throws RocksDBException {
ensureOpen();
prev(true); prev(true);
} }
public void prev(boolean traceStats) { public synchronized void prev(boolean traceStats) {
ensureOpen(); ensureOpen();
if (traceStats) { if (traceStats) {
startedIterNext.increment(); startedIterNext.increment();
@ -185,7 +183,7 @@ public class RocksIteratorObj extends SimpleResource {
} }
@Override @Override
protected void onClose() { protected synchronized void onClose() {
if (rocksIterator != null) { if (rocksIterator != null) {
rocksIterator.close(); rocksIterator.close();
} }