Search in sources :

Example 6 with WriteOptions

use of org.rocksdb.WriteOptions in project samza by apache.

the class TestLargeMessageSafeKeyValueStores method setup.

@Before
public void setup() {
    KeyValueStore<byte[], byte[]> kvStore;
    switch(typeOfStore) {
        case "inmemory":
            {
                kvStore = new InMemoryKeyValueStore(keyValueStoreMetrics);
                break;
            }
        case "rocksdb":
            {
                kvStore = new RocksDbKeyValueStore(dir, new org.rocksdb.Options().setCreateIfMissing(true).setCompressionType(org.rocksdb.CompressionType.SNAPPY_COMPRESSION), new MapConfig(), false, storeName, new WriteOptions(), new FlushOptions(), keyValueStoreMetrics);
                break;
            }
        default:
            throw new IllegalArgumentException("Type of store undefined: " + typeOfStore);
    }
    MessageCollector collector = envelope -> {
        int messageLength = ((byte[]) envelope.getMessage()).length;
        if (messageLength > maxMessageSize) {
            throw new SamzaException("Logged store message size " + messageLength + " for store " + storeName + " was larger than the maximum allowed message size " + maxMessageSize + ".");
        }
    };
    loggedStore = new LoggedStore<>(kvStore, systemStreamPartition, collector, loggedStoreMetrics);
    switch(storeConfig) {
        case "serde":
            {
                KeyValueStore<byte[], byte[]> largeMessageSafeStore = new LargeMessageSafeStore(loggedStore, storeName, dropLargeMessage, maxMessageSize);
                store = new SerializedKeyValueStore<>(largeMessageSafeStore, stringSerde, stringSerde, serializedKeyValueStoreMetrics);
                break;
            }
        case "cache-then-serde":
            {
                KeyValueStore<byte[], byte[]> toBeSerializedStore = loggedStore;
                if (dropLargeMessage) {
                    toBeSerializedStore = new LargeMessageSafeStore(loggedStore, storeName, dropLargeMessage, maxMessageSize);
                }
                KeyValueStore<String, String> serializedStore = new SerializedKeyValueStore<>(toBeSerializedStore, stringSerde, stringSerde, serializedKeyValueStoreMetrics);
                store = new CachedStore<>(serializedStore, cacheSize, batchSize, cachedStoreMetrics);
                break;
            }
        // large messages are expected and StorageConfig.DISALLOW_LARGE_MESSAGES is true.
        case "serde-then-cache":
            {
                KeyValueStore<byte[], byte[]> cachedStore = new CachedStore<>(loggedStore, cacheSize, batchSize, cachedStoreMetrics);
                KeyValueStore<byte[], byte[]> largeMessageSafeStore = new LargeMessageSafeStore(cachedStore, storeName, dropLargeMessage, maxMessageSize);
                store = new SerializedKeyValueStore<>(largeMessageSafeStore, stringSerde, stringSerde, serializedKeyValueStoreMetrics);
                break;
            }
        default:
            throw new IllegalArgumentException("Store config undefined: " + storeConfig);
    }
    store = new NullSafeKeyValueStore<>(store);
}
Also used : Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) FlushOptions(org.rocksdb.FlushOptions) Random(java.util.Random) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) StringSerde(org.apache.samza.serializers.StringSerde) MessageCollector(org.apache.samza.task.MessageCollector) After(org.junit.After) MapConfig(org.apache.samza.config.MapConfig) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) Int(scala.Int) Collection(java.util.Collection) Partition(org.apache.samza.Partition) Test(org.junit.Test) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) WriteOptions(org.rocksdb.WriteOptions) File(java.io.File) SamzaException(org.apache.samza.SamzaException) List(java.util.List) InMemoryKeyValueStore(org.apache.samza.storage.kv.inmemory.InMemoryKeyValueStore) Assert(org.junit.Assert) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) InMemoryKeyValueStore(org.apache.samza.storage.kv.inmemory.InMemoryKeyValueStore) FlushOptions(org.rocksdb.FlushOptions) SamzaException(org.apache.samza.SamzaException) WriteOptions(org.rocksdb.WriteOptions) MessageCollector(org.apache.samza.task.MessageCollector) MapConfig(org.apache.samza.config.MapConfig) InMemoryKeyValueStore(org.apache.samza.storage.kv.inmemory.InMemoryKeyValueStore) Before(org.junit.Before)

Example 7 with WriteOptions

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

the class RocksDBResourceContainer method getWriteOptions.

/**
 * Gets the RocksDB {@link WriteOptions} to be used for write operations.
 */
public WriteOptions getWriteOptions() {
    // Disable WAL by default
    WriteOptions opt = new WriteOptions().setDisableWAL(true);
    handlesToClose.add(opt);
    // add user-defined options factory, if specified
    if (optionsFactory != null) {
        opt = optionsFactory.createWriteOptions(opt, handlesToClose);
    }
    return opt;
}
Also used : WriteOptions(org.rocksdb.WriteOptions)

Example 8 with WriteOptions

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

the class RocksDBWriteBatchWrapperTest method testWriteBatchWrapperFlushAfterMemorySizeExceed.

/**
 * Tests that {@link RocksDBWriteBatchWrapper} flushes after the memory consumed exceeds the
 * preconfigured value.
 */
@Test
public void testWriteBatchWrapperFlushAfterMemorySizeExceed() throws Exception {
    try (RocksDB db = RocksDB.open(folder.newFolder().getAbsolutePath());
        WriteOptions options = new WriteOptions().setDisableWAL(true);
        ColumnFamilyHandle handle = db.createColumnFamily(new ColumnFamilyDescriptor("test".getBytes()));
        RocksDBWriteBatchWrapper writeBatchWrapper = new RocksDBWriteBatchWrapper(db, options, 200, 50)) {
        long initBatchSize = writeBatchWrapper.getDataSize();
        byte[] dummy = new byte[6];
        ThreadLocalRandom.current().nextBytes(dummy);
        // will add 1 + 1 + 1 + 6 + 1 + 6 = 16 bytes for each KV
        // format is [handleType|kvType|keyLen|key|valueLen|value]
        // more information please ref write_batch.cc in RocksDB
        writeBatchWrapper.put(handle, dummy, dummy);
        assertEquals(initBatchSize + 16, writeBatchWrapper.getDataSize());
        writeBatchWrapper.put(handle, dummy, dummy);
        assertEquals(initBatchSize + 32, writeBatchWrapper.getDataSize());
        writeBatchWrapper.put(handle, dummy, dummy);
        // will flush all, then an empty write batch
        assertEquals(initBatchSize, writeBatchWrapper.getDataSize());
    }
}
Also used : WriteOptions(org.rocksdb.WriteOptions) RocksDB(org.rocksdb.RocksDB) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) Test(org.junit.Test)

Example 9 with WriteOptions

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

the class RocksDBResource method before.

@Override
protected void before() throws Throwable {
    this.temporaryFolder = new TemporaryFolder();
    this.temporaryFolder.create();
    final File rocksFolder = temporaryFolder.newFolder();
    this.dbOptions = optionsFactory.createDBOptions(new DBOptions().setUseFsync(false).setInfoLogLevel(InfoLogLevel.HEADER_LEVEL).setStatsDumpPeriodSec(0), handlesToClose).setCreateIfMissing(true);
    this.columnFamilyOptions = optionsFactory.createColumnOptions(new ColumnFamilyOptions(), handlesToClose);
    this.writeOptions = new WriteOptions();
    this.writeOptions.disableWAL();
    this.readOptions = new ReadOptions();
    this.columnFamilyHandles = new ArrayList<>(1);
    this.rocksDB = RocksDB.open(dbOptions, rocksFolder.getAbsolutePath(), Collections.singletonList(new ColumnFamilyDescriptor("default".getBytes(), columnFamilyOptions)), columnFamilyHandles);
    this.batchWrapper = new RocksDBWriteBatchWrapper(rocksDB, writeOptions);
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) WriteOptions(org.rocksdb.WriteOptions) ReadOptions(org.rocksdb.ReadOptions) TemporaryFolder(org.junit.rules.TemporaryFolder) DBOptions(org.rocksdb.DBOptions) ColumnFamilyDescriptor(org.rocksdb.ColumnFamilyDescriptor) File(java.io.File)

Example 10 with WriteOptions

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

the class RocksDBStore method openDB.

@SuppressWarnings("unchecked")
void openDB(final Map<String, Object> configs, final File stateDir) {
    // initialize the default rocksdb options
    final DBOptions dbOptions = new DBOptions();
    final ColumnFamilyOptions columnFamilyOptions = new ColumnFamilyOptions();
    userSpecifiedOptions = new RocksDBGenericOptionsToDbOptionsColumnFamilyOptionsAdapter(dbOptions, columnFamilyOptions);
    final BlockBasedTableConfigWithAccessibleCache tableConfig = new BlockBasedTableConfigWithAccessibleCache();
    cache = new LRUCache(BLOCK_CACHE_SIZE);
    tableConfig.setBlockCache(cache);
    tableConfig.setBlockSize(BLOCK_SIZE);
    filter = new BloomFilter();
    tableConfig.setFilterPolicy(filter);
    userSpecifiedOptions.optimizeFiltersForHits();
    userSpecifiedOptions.setTableFormatConfig(tableConfig);
    userSpecifiedOptions.setWriteBufferSize(WRITE_BUFFER_SIZE);
    userSpecifiedOptions.setCompressionType(COMPRESSION_TYPE);
    userSpecifiedOptions.setCompactionStyle(COMPACTION_STYLE);
    userSpecifiedOptions.setMaxWriteBufferNumber(MAX_WRITE_BUFFERS);
    userSpecifiedOptions.setCreateIfMissing(true);
    userSpecifiedOptions.setErrorIfExists(false);
    userSpecifiedOptions.setInfoLogLevel(InfoLogLevel.ERROR_LEVEL);
    // this is the recommended way to increase parallelism in RocksDb
    // note that the current implementation of setIncreaseParallelism affects the number
    // of compaction threads but not flush threads (the latter remains one). Also,
    // the parallelism value needs to be at least two because of the code in
    // https://github.com/facebook/rocksdb/blob/62ad0a9b19f0be4cefa70b6b32876e764b7f3c11/util/options.cc#L580
    // subtracts one from the value passed to determine the number of compaction threads
    // (this could be a bug in the RocksDB code and their devs have been contacted).
    userSpecifiedOptions.setIncreaseParallelism(Math.max(Runtime.getRuntime().availableProcessors(), 2));
    wOptions = new WriteOptions();
    wOptions.setDisableWAL(true);
    fOptions = new FlushOptions();
    fOptions.setWaitForFlush(true);
    final Class<RocksDBConfigSetter> configSetterClass = (Class<RocksDBConfigSetter>) configs.get(StreamsConfig.ROCKSDB_CONFIG_SETTER_CLASS_CONFIG);
    if (configSetterClass != null) {
        configSetter = Utils.newInstance(configSetterClass);
        configSetter.setConfig(name, userSpecifiedOptions, configs);
    }
    dbDir = new File(new File(stateDir, parentDir), name);
    try {
        Files.createDirectories(dbDir.getParentFile().toPath());
        Files.createDirectories(dbDir.getAbsoluteFile().toPath());
    } catch (final IOException fatal) {
        throw new ProcessorStateException(fatal);
    }
    // Setup statistics before the database is opened, otherwise the statistics are not updated
    // with the measurements from Rocks DB
    maybeSetUpStatistics(configs);
    openRocksDB(dbOptions, columnFamilyOptions);
    open = true;
    addValueProvidersToMetricsRecorder();
}
Also used : IOException(java.io.IOException) FlushOptions(org.rocksdb.FlushOptions) BloomFilter(org.rocksdb.BloomFilter) ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) WriteOptions(org.rocksdb.WriteOptions) LRUCache(org.rocksdb.LRUCache) RocksDBConfigSetter(org.apache.kafka.streams.state.RocksDBConfigSetter) DBOptions(org.rocksdb.DBOptions) File(java.io.File) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

Aggregations

WriteOptions (org.rocksdb.WriteOptions)23 File (java.io.File)11 FlushOptions (org.rocksdb.FlushOptions)9 Options (org.rocksdb.Options)9 Test (org.junit.Test)7 RocksDB (org.rocksdb.RocksDB)6 MapConfig (org.apache.samza.config.MapConfig)5 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)5 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)5 IOException (java.io.IOException)4 ColumnFamilyDescriptor (org.rocksdb.ColumnFamilyDescriptor)4 ColumnFamilyOptions (org.rocksdb.ColumnFamilyOptions)4 RocksDBException (org.rocksdb.RocksDBException)4 WriteBatch (org.rocksdb.WriteBatch)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 RocksDBConfigSetter (org.apache.kafka.streams.state.RocksDBConfigSetter)3 Config (org.apache.samza.config.Config)3 BlockBasedTableConfig (org.rocksdb.BlockBasedTableConfig)3 DBOptions (org.rocksdb.DBOptions)3 ReadOptions (org.rocksdb.ReadOptions)3