forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment with RocksDB object owning iterators
In an analogous way to owning column family handles, and closing them when the DB itself is closed, we make iterators be “child” objects of the DB. Iterator lifecycle is generally shorter than DB, so we give them a lambda to remove themselves from the DB on iterator close(). Add a few tests to check that iterators which remain open when the DB is requested to close, are now closed when the DB is closed. TODO - other DB subclasses need to add iterators to the list TODO - there are cases where isOwningHandle() should not throw an exception; we need a less sledgehammer way of implementing this TODO - fix the current Java tests TODO - the atomic improvement in facebook#5234
- Loading branch information
1 parent
6f9d826
commit 932fbfe
Showing
5 changed files
with
133 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.rocksdb; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.nio.file.Paths; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class IteratorClosedDBTest { | ||
|
||
@ClassRule | ||
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = | ||
new RocksNativeLibraryResource(); | ||
|
||
@Rule | ||
public TemporaryFolder dbFolder = new TemporaryFolder(); | ||
|
||
@Test | ||
public void resourceIterators() throws RocksDBException { | ||
try (Options options = new Options().setCreateIfMissing(true); RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | ||
|
||
byte[] key = {0x1}; | ||
byte[] value = {0x2}; | ||
db.put(key, value); | ||
|
||
try (RocksIterator it = db.newIterator()) { | ||
it.seekToFirst(); | ||
assertThat(it.key()).isEqualTo(key); | ||
assertThat(it.value()).isEqualTo(value); | ||
|
||
it.next(); | ||
assertThat(it.isValid()).isFalse(); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void ownedIterators() throws RocksDBException { | ||
try (Options options = new Options().setCreateIfMissing(true); RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) { | ||
|
||
byte[] key = {0x1}; | ||
byte[] value = {0x2}; | ||
db.put(key, value); | ||
|
||
RocksIterator it = db.newIterator(); | ||
it.seekToFirst(); | ||
assertThat(it.key()).isEqualTo(key); | ||
assertThat(it.value()).isEqualTo(value); | ||
|
||
it.next(); | ||
assertThat(it.isValid()).isFalse(); | ||
} //iterator is still open when we close the DB, C++ assertion in DEBUG_LEVEL=1 | ||
} | ||
|
||
@Test | ||
public void shouldCrashJavaRocks() throws RocksDBException { | ||
try (Options options = new Options().setCreateIfMissing(true)) { | ||
RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); | ||
byte[] key = {0x1}; | ||
byte[] value = {0x2}; | ||
db.put(key, value); | ||
|
||
RocksIterator it = db.newIterator(); | ||
assertThat(it.isValid()).isFalse(); | ||
it.seekToFirst(); | ||
assertThat(it.isValid()).isTrue(); | ||
|
||
// Exception here (assertion failure in C++) - when built with DEBUG_LEVEL=1 | ||
// Outstanding iterator has a reference to the column family which is being closed | ||
db.close(); | ||
|
||
assertThat(it.isValid()).isFalse(); | ||
it.close(); | ||
} | ||
} | ||
} |