Search in sources :

Example 21 with DBOptions

use of org.rocksdb.DBOptions in project jstorm by alibaba.

the class RocksDBTest method visitorAccross.

public void visitorAccross() throws RocksDBException, InterruptedException {
    DBOptions dbOptions = null;
    TtlDB ttlDB = null;
    List<ColumnFamilyDescriptor> cfNames = new ArrayList<ColumnFamilyDescriptor>();
    List<ColumnFamilyHandle> columnFamilyHandleList = new ArrayList<ColumnFamilyHandle>();
    cfNames.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));
    cfNames.add(new ColumnFamilyDescriptor("new_cf".getBytes()));
    List<Integer> ttlValues = new ArrayList<Integer>();
    // new column family with 1 second ttl
    ttlValues.add(1);
    // Default column family with infinite lifetime
    ttlValues.add(0);
    try {
        System.out.println("Begin to open db");
        dbOptions = new DBOptions().setCreateMissingColumnFamilies(true).setCreateIfMissing(true);
        ttlDB = TtlDB.open(dbOptions, rootDir, cfNames, columnFamilyHandleList, ttlValues, false);
        System.out.println("Successfully open db " + rootDir);
        List<String> keys = new ArrayList<String>();
        keys.add("key");
        ttlDB.put("key".getBytes(), "key".getBytes());
        for (int i = 0; i < 2; i++) {
            String key = "key" + i;
            keys.add(key);
            ttlDB.put(columnFamilyHandleList.get(i), key.getBytes(), key.getBytes());
        }
        try {
            byte[] value = ttlDB.get("others".getBytes());
            if (value != null) {
                System.out.println("Raw get :" + new String(value));
            } else {
                System.out.println("No value of other");
            }
        } catch (Exception e) {
            System.out.println("Occur exception other");
        }
        for (String key : keys) {
            try {
                byte[] value = ttlDB.get(key.getBytes());
                if (value != null) {
                    System.out.println("Raw get :" + new String(value));
                } else {
                    System.out.println("No value of " + key);
                }
            } catch (Exception e) {
                System.out.println("Occur exception " + key + ", Raw");
            }
            for (int i = 0; i < 2; i++) {
                try {
                    byte[] value = ttlDB.get(columnFamilyHandleList.get(i), key.getBytes());
                    if (value != null) {
                        System.out.println("handler index" + i + " get :" + new String(value));
                    } else {
                        System.out.println("No value of index" + i + " get :" + key);
                    }
                } catch (Exception e) {
                    System.out.println("Occur exception " + key + ", handler index:" + i);
                }
            }
        }
    } finally {
        for (ColumnFamilyHandle columnFamilyHandle : columnFamilyHandleList) {
            columnFamilyHandle.dispose();
        }
        if (ttlDB != null) {
            ttlDB.close();
        }
        if (dbOptions != null) {
            dbOptions.dispose();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) RocksDBException(org.rocksdb.RocksDBException) DBOptions(org.rocksdb.DBOptions) TtlDB(org.rocksdb.TtlDB)

Example 22 with DBOptions

use of org.rocksdb.DBOptions in project kafka by apache.

the class RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapterTest method shouldLogWarningWhenSettingWalOptions.

@Test
public void shouldLogWarningWhenSettingWalOptions() throws Exception {
    try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter.class)) {
        final RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter adapter = new RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter(new DBOptions(), new ColumnFamilyOptions());
        for (final Method method : RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter.class.getDeclaredMethods()) {
            if (walRelatedMethods.contains(method.getName())) {
                method.invoke(adapter, getDBOptionsParameters(method.getParameterTypes()));
            }
        }
        final List<String> walOptions = Arrays.asList("walDir", "walFilter", "walRecoveryMode", "walBytesPerSync", "walSizeLimitMB", "manualWalFlush", "maxTotalWalSize", "walTtlSeconds");
        final Set<String> logMessages = appender.getEvents().stream().filter(e -> e.getLevel().equals("WARN")).map(LogCaptureAppender.Event::getMessage).collect(Collectors.toSet());
        walOptions.forEach(option -> assertThat(logMessages, hasItem(String.format("WAL is explicitly disabled by Streams in RocksDB. Setting option '%s' will be ignored", option))));
    }
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) DBOptions(org.rocksdb.DBOptions) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 23 with DBOptions

use of org.rocksdb.DBOptions in project kafka by apache.

the class RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapterTest method verifyColumnFamilyOptionsMethodCall.

private void verifyColumnFamilyOptionsMethodCall(final Method method) throws Exception {
    final ColumnFamilyOptions mockedColumnFamilyOptions = mock(ColumnFamilyOptions.class);
    final RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter optionsFacadeColumnFamilyOptions = new RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter(new DBOptions(), mockedColumnFamilyOptions);
    final Object[] parameters = getColumnFamilyOptionsParameters(method.getParameterTypes());
    try {
        reset(mockedColumnFamilyOptions);
        replay(mockedColumnFamilyOptions);
        method.invoke(optionsFacadeColumnFamilyOptions, parameters);
        verify();
        fail("Should have called ColumnFamilyOptions." + method.getName() + "()");
    } catch (final InvocationTargetException undeclaredMockMethodCall) {
        assertThat(undeclaredMockMethodCall.getCause(), instanceOf(AssertionError.class));
        assertThat(undeclaredMockMethodCall.getCause().getMessage().trim(), matchesPattern("Unexpected method call ColumnFamilyOptions\\." + method.getName() + "(.*)"));
    }
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) DBOptions(org.rocksdb.DBOptions) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with DBOptions

use of org.rocksdb.DBOptions in project kafka by apache.

the class RocksDBTimestampedStoreTest method shouldOpenExistingStoreInRegularMode.

@Test
public void shouldOpenExistingStoreInRegularMode() throws Exception {
    // prepare store
    rocksDBStore.init((StateStoreContext) context, rocksDBStore);
    rocksDBStore.put(new Bytes("key".getBytes()), "timestamped".getBytes());
    rocksDBStore.close();
    // re-open store
    try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(RocksDBTimestampedStore.class)) {
        rocksDBStore.init((StateStoreContext) context, rocksDBStore);
        assertThat(appender.getMessages(), hasItem("Opening store " + DB_NAME + " in regular mode"));
    } finally {
        rocksDBStore.close();
    }
    // verify store
    final DBOptions dbOptions = new DBOptions();
    final ColumnFamilyOptions columnFamilyOptions = new ColumnFamilyOptions();
    final List<ColumnFamilyDescriptor> columnFamilyDescriptors = asList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, columnFamilyOptions), new ColumnFamilyDescriptor("keyValueWithTimestamp".getBytes(StandardCharsets.UTF_8), columnFamilyOptions));
    final List<ColumnFamilyHandle> columnFamilies = new ArrayList<>(columnFamilyDescriptors.size());
    RocksDB db = null;
    ColumnFamilyHandle noTimestampColumnFamily = null, withTimestampColumnFamily = null;
    try {
        db = RocksDB.open(dbOptions, new File(new File(context.stateDir(), "rocksdb"), DB_NAME).getAbsolutePath(), columnFamilyDescriptors, columnFamilies);
        noTimestampColumnFamily = columnFamilies.get(0);
        withTimestampColumnFamily = columnFamilies.get(1);
        assertThat(db.get(noTimestampColumnFamily, "key".getBytes()), new IsNull<>());
        assertThat(db.getLongProperty(noTimestampColumnFamily, "rocksdb.estimate-num-keys"), is(0L));
        assertThat(db.get(withTimestampColumnFamily, "key".getBytes()).length, is(11));
        assertThat(db.getLongProperty(withTimestampColumnFamily, "rocksdb.estimate-num-keys"), is(1L));
    } finally {
        // Order of closing must follow: ColumnFamilyHandle > RocksDB > DBOptions > ColumnFamilyOptions
        if (noTimestampColumnFamily != null) {
            noTimestampColumnFamily.close();
        }
        if (withTimestampColumnFamily != null) {
            withTimestampColumnFamily.close();
        }
        if (db != null) {
            db.close();
        }
        dbOptions.close();
        columnFamilyOptions.close();
    }
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) Bytes(org.apache.kafka.common.utils.Bytes) RocksDB(org.rocksdb.RocksDB) ArrayList(java.util.ArrayList) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) DBOptions(org.rocksdb.DBOptions) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) File(java.io.File) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) Test(org.junit.Test)

Example 25 with DBOptions

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

the class RocksDBResourceContainer method getDbOptions.

/**
 * Gets the RocksDB {@link DBOptions} to be used for RocksDB instances.
 */
public DBOptions getDbOptions() {
    // initial options from common profile
    DBOptions opt = createBaseCommonDBOptions();
    handlesToClose.add(opt);
    // load configurable options on top of pre-defined profile
    setDBOptionsFromConfigurableOptions(opt);
    // add user-defined options factory, if specified
    if (optionsFactory != null) {
        opt = optionsFactory.createDBOptions(opt, handlesToClose);
    }
    // add necessary default options
    opt = opt.setCreateIfMissing(true);
    // if sharedResources is non-null, use the write buffer manager from it.
    if (sharedResources != null) {
        opt.setWriteBufferManager(sharedResources.getResourceHandle().getWriteBufferManager());
    }
    return opt;
}
Also used : DBOptions(org.rocksdb.DBOptions)

Aggregations

DBOptions (org.rocksdb.DBOptions)29 ColumnFamilyOptions (org.rocksdb.ColumnFamilyOptions)19 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)10 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)9 File (java.io.File)7 RocksDB (org.rocksdb.RocksDB)7 RocksDBException (org.rocksdb.RocksDBException)5 WriteOptions (org.rocksdb.WriteOptions)5 IOException (java.io.IOException)4 FlushOptions (org.rocksdb.FlushOptions)4 Options (org.rocksdb.Options)3 RocksDbOptionsFactory (com.alibaba.jstorm.cache.rocksdb.RocksDbOptionsFactory)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 JobID (org.apache.flink.api.common.JobID)2