Search in sources :

Example 1 with RocksStateKeysIterator

use of org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator in project flink by apache.

the class RocksDBRocksStateKeysIteratorTest method testIteratorHelper.

<K> void testIteratorHelper(TypeSerializer<K> keySerializer, int maxKeyGroupNumber, Function<Integer, K> getKeyFunc) throws Exception {
    String testStateName = "aha";
    String namespace = "ns";
    try (RocksDBKeyedStateBackendTestFactory factory = new RocksDBKeyedStateBackendTestFactory()) {
        RocksDBKeyedStateBackend<K> keyedStateBackend = factory.create(tmp, keySerializer, maxKeyGroupNumber);
        ValueState<String> testState = keyedStateBackend.getPartitionedState(namespace, StringSerializer.INSTANCE, new ValueStateDescriptor<>(testStateName, String.class));
        // insert record
        for (int i = 0; i < 1000; ++i) {
            keyedStateBackend.setCurrentKey(getKeyFunc.apply(i));
            testState.update(String.valueOf(i));
        }
        DataOutputSerializer outputStream = new DataOutputSerializer(8);
        boolean ambiguousKeyPossible = CompositeKeySerializationUtils.isAmbiguousKeyPossible(keySerializer, StringSerializer.INSTANCE);
        CompositeKeySerializationUtils.writeNameSpace(namespace, StringSerializer.INSTANCE, outputStream, ambiguousKeyPossible);
        byte[] nameSpaceBytes = outputStream.getCopyOfBuffer();
        // already created with the state, should be closed with the backend
        ColumnFamilyHandle handle = keyedStateBackend.getColumnFamilyHandle(testStateName);
        try (RocksIteratorWrapper iterator = RocksDBOperationUtils.getRocksIterator(keyedStateBackend.db, handle, keyedStateBackend.getReadOptions());
            RocksStateKeysIterator<K> iteratorWrapper = new RocksStateKeysIterator<>(iterator, testStateName, keySerializer, keyedStateBackend.getKeyGroupPrefixBytes(), ambiguousKeyPossible, nameSpaceBytes)) {
            iterator.seekToFirst();
            // valid record
            List<Integer> fetchedKeys = new ArrayList<>(1000);
            while (iteratorWrapper.hasNext()) {
                fetchedKeys.add(Integer.parseInt(iteratorWrapper.next().toString()));
            }
            fetchedKeys.sort(Comparator.comparingInt(a -> a));
            Assert.assertEquals(1000, fetchedKeys.size());
            for (int i = 0; i < 1000; ++i) {
                Assert.assertEquals(i, fetchedKeys.get(i).intValue());
            }
        }
    }
}
Also used : TypeSerializer(org.apache.flink.api.common.typeutils.TypeSerializer) CompositeKeySerializationUtils(org.apache.flink.runtime.state.CompositeKeySerializationUtils) RocksStateKeysIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) Test(org.junit.Test) StringSerializer(org.apache.flink.api.common.typeutils.base.StringSerializer) Function(java.util.function.Function) ArrayList(java.util.ArrayList) IntSerializer(org.apache.flink.api.common.typeutils.base.IntSerializer) List(java.util.List) Rule(org.junit.Rule) ValueState(org.apache.flink.api.common.state.ValueState) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) Assert(org.junit.Assert) Comparator(java.util.Comparator) TemporaryFolder(org.junit.rules.TemporaryFolder) DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) RocksStateKeysIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator) ArrayList(java.util.ArrayList) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle)

Example 2 with RocksStateKeysIterator

use of org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator in project flink by apache.

the class RocksDBKeyedStateBackend method getKeys.

@SuppressWarnings("unchecked")
@Override
public <N> Stream<K> getKeys(String state, N namespace) {
    RocksDbKvStateInfo columnInfo = kvStateInformation.get(state);
    if (columnInfo == null || !(columnInfo.metaInfo instanceof RegisteredKeyValueStateBackendMetaInfo)) {
        return Stream.empty();
    }
    RegisteredKeyValueStateBackendMetaInfo<N, ?> registeredKeyValueStateBackendMetaInfo = (RegisteredKeyValueStateBackendMetaInfo<N, ?>) columnInfo.metaInfo;
    final TypeSerializer<N> namespaceSerializer = registeredKeyValueStateBackendMetaInfo.getNamespaceSerializer();
    final DataOutputSerializer namespaceOutputView = new DataOutputSerializer(8);
    boolean ambiguousKeyPossible = CompositeKeySerializationUtils.isAmbiguousKeyPossible(getKeySerializer(), namespaceSerializer);
    final byte[] nameSpaceBytes;
    try {
        CompositeKeySerializationUtils.writeNameSpace(namespace, namespaceSerializer, namespaceOutputView, ambiguousKeyPossible);
        nameSpaceBytes = namespaceOutputView.getCopyOfBuffer();
    } catch (IOException ex) {
        throw new FlinkRuntimeException("Failed to get keys from RocksDB state backend.", ex);
    }
    RocksIteratorWrapper iterator = RocksDBOperationUtils.getRocksIterator(db, columnInfo.columnFamilyHandle, readOptions);
    iterator.seekToFirst();
    final RocksStateKeysIterator<K> iteratorWrapper = new RocksStateKeysIterator<>(iterator, state, getKeySerializer(), keyGroupPrefixBytes, ambiguousKeyPossible, nameSpaceBytes);
    Stream<K> targetStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(iteratorWrapper, Spliterator.ORDERED), false);
    return targetStream.onClose(iteratorWrapper::close);
}
Also used : DataOutputSerializer(org.apache.flink.core.memory.DataOutputSerializer) RocksStateKeysIterator(org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator) IOException(java.io.IOException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) RegisteredKeyValueStateBackendMetaInfo(org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo)

Aggregations

RocksStateKeysIterator (org.apache.flink.contrib.streaming.state.iterator.RocksStateKeysIterator)2 DataOutputSerializer (org.apache.flink.core.memory.DataOutputSerializer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Function (java.util.function.Function)1 ValueState (org.apache.flink.api.common.state.ValueState)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 TypeSerializer (org.apache.flink.api.common.typeutils.TypeSerializer)1 IntSerializer (org.apache.flink.api.common.typeutils.base.IntSerializer)1 StringSerializer (org.apache.flink.api.common.typeutils.base.StringSerializer)1 CompositeKeySerializationUtils (org.apache.flink.runtime.state.CompositeKeySerializationUtils)1 RegisteredKeyValueStateBackendMetaInfo (org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo)1 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)1 Assert (org.junit.Assert)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 TemporaryFolder (org.junit.rules.TemporaryFolder)1 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)1