Search in sources :

Example 6 with RocksDB

use of org.rocksdb.RocksDB in project flink by apache.

the class RocksDBStateBackendTest method verifyRocksObjectsReleased.

private void verifyRocksObjectsReleased() {
    //Ensure every RocksObject was closed exactly once
    for (RocksObject rocksCloseable : allCreatedCloseables) {
        verify(rocksCloseable, times(1)).close();
    }
    assertNotNull(null, keyedStateBackend.db);
    RocksDB spyDB = keyedStateBackend.db;
    verify(spyDB, times(1)).getSnapshot();
    verify(spyDB, times(1)).releaseSnapshot(any(Snapshot.class));
    keyedStateBackend.dispose();
    verify(spyDB, times(1)).close();
    assertEquals(null, keyedStateBackend.db);
}
Also used : Snapshot(org.rocksdb.Snapshot) RocksDB(org.rocksdb.RocksDB) RocksObject(org.rocksdb.RocksObject)

Example 7 with RocksDB

use of org.rocksdb.RocksDB in project flink by apache.

the class RocksDBStateBackendTest method testReleasingSnapshotAfterBackendClosed.

@Test
public void testReleasingSnapshotAfterBackendClosed() throws Exception {
    setupRocksKeyedStateBackend();
    RunnableFuture<KeyGroupsStateHandle> snapshot = keyedStateBackend.snapshot(0L, 0L, testStreamFactory, CheckpointOptions.forFullCheckpoint());
    RocksDB spyDB = keyedStateBackend.db;
    verify(spyDB, times(1)).getSnapshot();
    verify(spyDB, times(0)).releaseSnapshot(any(Snapshot.class));
    this.keyedStateBackend.dispose();
    verify(spyDB, times(1)).close();
    assertEquals(null, keyedStateBackend.db);
    //Ensure every RocksObjects not closed yet
    for (RocksObject rocksCloseable : allCreatedCloseables) {
        verify(rocksCloseable, times(0)).close();
    }
    snapshot.cancel(true);
    //Ensure every RocksObjects was closed exactly once
    for (RocksObject rocksCloseable : allCreatedCloseables) {
        verify(rocksCloseable, times(1)).close();
    }
}
Also used : Snapshot(org.rocksdb.Snapshot) RocksDB(org.rocksdb.RocksDB) RocksObject(org.rocksdb.RocksObject) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) Test(org.junit.Test)

Example 8 with RocksDB

use of org.rocksdb.RocksDB in project flink by apache.

the class ListViaRangeSpeedMiniBenchmark method main.

public static void main(String[] args) throws Exception {
    final File rocksDir = new File("/tmp/rdb");
    FileUtils.deleteDirectory(rocksDir);
    final Options options = new Options().setCompactionStyle(CompactionStyle.LEVEL).setLevelCompactionDynamicLevelBytes(true).setIncreaseParallelism(4).setUseFsync(false).setMaxOpenFiles(-1).setDisableDataSync(true).setCreateIfMissing(true).setMergeOperator(new StringAppendOperator());
    final WriteOptions write_options = new WriteOptions().setSync(false).setDisableWAL(true);
    final RocksDB rocksDB = RocksDB.open(options, rocksDir.getAbsolutePath());
    final String key = "key";
    final String value = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ7890654321";
    final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
    final byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
    final byte[] keyTemplate = Arrays.copyOf(keyBytes, keyBytes.length + 4);
    final Unsafe unsafe = MemoryUtils.UNSAFE;
    final long offset = unsafe.arrayBaseOffset(byte[].class) + keyTemplate.length - 4;
    final int num = 50000;
    System.out.println("begin insert");
    final long beginInsert = System.nanoTime();
    for (int i = 0; i < num; i++) {
        unsafe.putInt(keyTemplate, offset, i);
        rocksDB.put(write_options, keyTemplate, valueBytes);
    }
    final long endInsert = System.nanoTime();
    System.out.println("end insert - duration: " + ((endInsert - beginInsert) / 1_000_000) + " ms");
    final byte[] resultHolder = new byte[num * valueBytes.length];
    final long beginGet = System.nanoTime();
    final RocksIterator iterator = rocksDB.newIterator();
    int pos = 0;
    // seek to start
    unsafe.putInt(keyTemplate, offset, 0);
    iterator.seek(keyTemplate);
    // mark end
    unsafe.putInt(keyTemplate, offset, -1);
    // iterate
    while (iterator.isValid()) {
        byte[] currKey = iterator.key();
        if (samePrefix(keyBytes, currKey)) {
            byte[] currValue = iterator.value();
            System.arraycopy(currValue, 0, resultHolder, pos, currValue.length);
            pos += currValue.length;
            iterator.next();
        } else {
            break;
        }
    }
    final long endGet = System.nanoTime();
    System.out.println("end get - duration: " + ((endGet - beginGet) / 1_000_000) + " ms");
}
Also used : Options(org.rocksdb.Options) WriteOptions(org.rocksdb.WriteOptions) WriteOptions(org.rocksdb.WriteOptions) RocksDB(org.rocksdb.RocksDB) StringAppendOperator(org.rocksdb.StringAppendOperator) Unsafe(sun.misc.Unsafe) RocksIterator(org.rocksdb.RocksIterator) File(java.io.File)

Aggregations

RocksDB (org.rocksdb.RocksDB)8 File (java.io.File)4 RocksObject (org.rocksdb.RocksObject)3 Snapshot (org.rocksdb.Snapshot)3 ArrayList (java.util.ArrayList)2 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)2 Test (org.junit.Test)2 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)2 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)2 Options (org.rocksdb.Options)2 StringAppendOperator (org.rocksdb.StringAppendOperator)2 WriteOptions (org.rocksdb.WriteOptions)2 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Properties (java.util.Properties)1 Random (java.util.Random)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)1 ColumnFamilyOptions (org.rocksdb.ColumnFamilyOptions)1