Search in sources :

Example 1 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by apache.

the class ListStateDescriptorTest method testValueStateDescriptorLazySerializer.

@Test
public void testValueStateDescriptorLazySerializer() throws Exception {
    // some different registered value
    ExecutionConfig cfg = new ExecutionConfig();
    cfg.registerKryoType(TaskInfo.class);
    ListStateDescriptor<Path> descr = new ListStateDescriptor<Path>("testName", Path.class);
    try {
        descr.getSerializer();
        fail("should cause an exception");
    } catch (IllegalStateException ignored) {
    }
    descr.initializeSerializerUnlessSet(cfg);
    assertNotNull(descr.getSerializer());
    assertTrue(descr.getSerializer() instanceof ListSerializer);
    assertNotNull(descr.getElementSerializer());
    assertTrue(descr.getElementSerializer() instanceof KryoSerializer);
    assertTrue(((KryoSerializer<?>) descr.getElementSerializer()).getKryo().getRegistration(TaskInfo.class).getId() > 0);
}
Also used : Path(org.apache.flink.core.fs.Path) TaskInfo(org.apache.flink.api.common.TaskInfo) ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer) Test(org.junit.Test)

Example 2 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by apache.

the class HeapListState method getSerializedValue.

@Override
public byte[] getSerializedValue(final byte[] serializedKeyAndNamespace, final TypeSerializer<K> safeKeySerializer, final TypeSerializer<N> safeNamespaceSerializer, final TypeSerializer<List<V>> safeValueSerializer) throws Exception {
    Preconditions.checkNotNull(serializedKeyAndNamespace);
    Preconditions.checkNotNull(safeKeySerializer);
    Preconditions.checkNotNull(safeNamespaceSerializer);
    Preconditions.checkNotNull(safeValueSerializer);
    Tuple2<K, N> keyAndNamespace = KvStateSerializer.deserializeKeyAndNamespace(serializedKeyAndNamespace, safeKeySerializer, safeNamespaceSerializer);
    List<V> result = stateTable.get(keyAndNamespace.f0, keyAndNamespace.f1);
    if (result == null) {
        return null;
    }
    final TypeSerializer<V> dupSerializer = ((ListSerializer<V>) safeValueSerializer).getElementSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputViewStreamWrapper view = new DataOutputViewStreamWrapper(baos);
    // write the same as RocksDB writes lists, with one ',' separator
    for (int i = 0; i < result.size(); i++) {
        dupSerializer.serialize(result.get(i), view);
        if (i < result.size() - 1) {
            view.writeByte(',');
        }
    }
    view.flush();
    return baos.toByteArray();
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by apache.

the class RocksDBListState method migrateSerializedValue.

@Override
public void migrateSerializedValue(DataInputDeserializer serializedOldValueInput, DataOutputSerializer serializedMigratedValueOutput, TypeSerializer<List<V>> priorSerializer, TypeSerializer<List<V>> newSerializer) throws StateMigrationException {
    Preconditions.checkArgument(priorSerializer instanceof ListSerializer);
    Preconditions.checkArgument(newSerializer instanceof ListSerializer);
    TypeSerializer<V> priorElementSerializer = ((ListSerializer<V>) priorSerializer).getElementSerializer();
    TypeSerializer<V> newElementSerializer = ((ListSerializer<V>) newSerializer).getElementSerializer();
    try {
        while (serializedOldValueInput.available() > 0) {
            V element = ListDelimitedSerializer.deserializeNextElement(serializedOldValueInput, priorElementSerializer);
            newElementSerializer.serialize(element, serializedMigratedValueOutput);
            if (serializedOldValueInput.available() > 0) {
                serializedMigratedValueOutput.write(DELIMITER);
            }
        }
    } catch (Exception e) {
        throw new StateMigrationException("Error while trying to migrate RocksDB list state.", e);
    }
}
Also used : ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) StateMigrationException(org.apache.flink.util.StateMigrationException) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) IOException(java.io.IOException) StateMigrationException(org.apache.flink.util.StateMigrationException) RocksDBException(org.rocksdb.RocksDBException)

Example 4 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by apache.

the class RocksDbTtlCompactFiltersManager method configCompactFilter.

public void configCompactFilter(@Nonnull StateDescriptor<?, ?> stateDesc, TypeSerializer<?> stateSerializer) {
    StateTtlConfig ttlConfig = stateDesc.getTtlConfig();
    if (ttlConfig.isEnabled() && ttlConfig.getCleanupStrategies().inRocksdbCompactFilter()) {
        FlinkCompactionFilterFactory compactionFilterFactory = compactionFilterFactories.get(stateDesc.getName());
        Preconditions.checkNotNull(compactionFilterFactory);
        long ttl = ttlConfig.getTtl().toMilliseconds();
        StateTtlConfig.RocksdbCompactFilterCleanupStrategy rocksdbCompactFilterCleanupStrategy = ttlConfig.getCleanupStrategies().getRocksdbCompactFilterCleanupStrategy();
        Preconditions.checkNotNull(rocksdbCompactFilterCleanupStrategy);
        long queryTimeAfterNumEntries = rocksdbCompactFilterCleanupStrategy.getQueryTimeAfterNumEntries();
        FlinkCompactionFilter.Config config;
        if (stateDesc instanceof ListStateDescriptor) {
            TypeSerializer<?> elemSerializer = ((ListSerializer<?>) stateSerializer).getElementSerializer();
            int len = elemSerializer.getLength();
            if (len > 0) {
                config = FlinkCompactionFilter.Config.createForFixedElementList(ttl, queryTimeAfterNumEntries, // plus one byte for list element delimiter
                len + 1);
            } else {
                config = FlinkCompactionFilter.Config.createForList(ttl, queryTimeAfterNumEntries, new ListElementFilterFactory<>(elemSerializer.duplicate()));
            }
        } else if (stateDesc instanceof MapStateDescriptor) {
            config = FlinkCompactionFilter.Config.createForMap(ttl, queryTimeAfterNumEntries);
        } else {
            config = FlinkCompactionFilter.Config.createForValue(ttl, queryTimeAfterNumEntries);
        }
        compactionFilterFactory.configure(config);
    }
}
Also used : FlinkCompactionFilterFactory(org.rocksdb.FlinkCompactionFilter.FlinkCompactionFilterFactory) ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig) FlinkCompactionFilter(org.rocksdb.FlinkCompactionFilter)

Example 5 with ListSerializer

use of org.apache.flink.api.common.typeutils.base.ListSerializer in project flink by apache.

the class StreamingRuntimeContextTest method testListStateInstantiation.

@Test
public void testListStateInstantiation() throws Exception {
    final ExecutionConfig config = new ExecutionConfig();
    config.registerKryoType(Path.class);
    final AtomicReference<Object> descriptorCapture = new AtomicReference<>();
    StreamingRuntimeContext context = createRuntimeContext(descriptorCapture, config);
    ListStateDescriptor<TaskInfo> descr = new ListStateDescriptor<>("name", TaskInfo.class);
    context.getListState(descr);
    ListStateDescriptor<?> descrIntercepted = (ListStateDescriptor<?>) descriptorCapture.get();
    TypeSerializer<?> serializer = descrIntercepted.getSerializer();
    // check that the Path class is really registered, i.e., the execution config was applied
    assertTrue(serializer instanceof ListSerializer);
    TypeSerializer<?> elementSerializer = descrIntercepted.getElementSerializer();
    assertTrue(elementSerializer instanceof KryoSerializer);
    assertTrue(((KryoSerializer<?>) elementSerializer).getKryo().getRegistration(Path.class).getId() > 0);
}
Also used : Path(org.apache.flink.core.fs.Path) ListSerializer(org.apache.flink.api.common.typeutils.base.ListSerializer) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer) TaskInfo(org.apache.flink.api.common.TaskInfo) Test(org.junit.Test)

Aggregations

ListSerializer (org.apache.flink.api.common.typeutils.base.ListSerializer)9 KryoSerializer (org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)5 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)4 Test (org.junit.Test)4 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)3 TaskInfo (org.apache.flink.api.common.TaskInfo)2 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)2 Path (org.apache.flink.core.fs.Path)2 Kryo (com.esotericsoftware.kryo.Kryo)1 JavaSerializer (com.esotericsoftware.kryo.serializers.JavaSerializer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 StateTtlConfig (org.apache.flink.api.common.state.StateTtlConfig)1 LongSerializer (org.apache.flink.api.common.typeutils.base.LongSerializer)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1 InternalMapState (org.apache.flink.runtime.state.internal.InternalMapState)1 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)1