Search in sources :

Example 1 with StateTtlConfig

use of org.apache.flink.api.common.state.StateTtlConfig in project flink by apache.

the class SimpleStateRequestHandler method getMapState.

private MapState<ByteArrayWrapper, byte[]> getMapState(BeamFnApi.StateRequest request) throws Exception {
    BeamFnApi.StateKey.MultimapSideInput mapUserState = request.getStateKey().getMultimapSideInput();
    byte[] data = Base64.getDecoder().decode(mapUserState.getSideInputId());
    FlinkFnApi.StateDescriptor stateDescriptor = FlinkFnApi.StateDescriptor.parseFrom(data);
    String stateName = PYTHON_STATE_PREFIX + stateDescriptor.getStateName();
    StateDescriptor cachedStateDescriptor = stateDescriptorCache.get(stateName);
    MapStateDescriptor<ByteArrayWrapper, byte[]> mapStateDescriptor;
    if (cachedStateDescriptor instanceof MapStateDescriptor) {
        mapStateDescriptor = (MapStateDescriptor<ByteArrayWrapper, byte[]>) cachedStateDescriptor;
    } else if (cachedStateDescriptor == null) {
        mapStateDescriptor = new MapStateDescriptor<>(stateName, ByteArrayWrapperSerializer.INSTANCE, valueSerializer);
        if (stateDescriptor.hasStateTtlConfig()) {
            FlinkFnApi.StateDescriptor.StateTTLConfig stateTtlConfigProto = stateDescriptor.getStateTtlConfig();
            StateTtlConfig stateTtlConfig = ProtoUtils.parseStateTtlConfigFromProto(stateTtlConfigProto);
            mapStateDescriptor.enableTimeToLive(stateTtlConfig);
        }
        stateDescriptorCache.put(stateName, mapStateDescriptor);
    } else {
        throw new RuntimeException(String.format("State name corrupt detected: " + "'%s' is used both as MAP state and '%s' state at the same time.", stateName, cachedStateDescriptor.getType()));
    }
    byte[] windowBytes = mapUserState.getWindow().toByteArray();
    if (windowBytes.length != 0) {
        bais.setBuffer(windowBytes, 0, windowBytes.length);
        Object namespace = namespaceSerializer.deserialize(baisWrapper);
        return (MapState<ByteArrayWrapper, byte[]>) keyedStateBackend.getPartitionedState(namespace, namespaceSerializer, mapStateDescriptor);
    } else {
        return (MapState<ByteArrayWrapper, byte[]>) keyedStateBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, mapStateDescriptor);
    }
}
Also used : MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) MapState(org.apache.flink.api.common.state.MapState) ByteString(org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig) FlinkFnApi(org.apache.flink.fnexecution.v1.FlinkFnApi) ByteArrayWrapper(org.apache.flink.streaming.api.utils.ByteArrayWrapper) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) StateDescriptor(org.apache.flink.api.common.state.StateDescriptor)

Example 2 with StateTtlConfig

use of org.apache.flink.api.common.state.StateTtlConfig in project flink by apache.

the class SimpleStateRequestHandler method getListState.

private ListState<byte[]> getListState(BeamFnApi.StateRequest request) throws Exception {
    BeamFnApi.StateKey.BagUserState bagUserState = request.getStateKey().getBagUserState();
    byte[] data = Base64.getDecoder().decode(bagUserState.getUserStateId());
    FlinkFnApi.StateDescriptor stateDescriptor = FlinkFnApi.StateDescriptor.parseFrom(data);
    String stateName = PYTHON_STATE_PREFIX + stateDescriptor.getStateName();
    ListStateDescriptor<byte[]> listStateDescriptor;
    StateDescriptor cachedStateDescriptor = stateDescriptorCache.get(stateName);
    if (cachedStateDescriptor instanceof ListStateDescriptor) {
        listStateDescriptor = (ListStateDescriptor<byte[]>) cachedStateDescriptor;
    } else if (cachedStateDescriptor == null) {
        listStateDescriptor = new ListStateDescriptor<>(stateName, valueSerializer);
        if (stateDescriptor.hasStateTtlConfig()) {
            FlinkFnApi.StateDescriptor.StateTTLConfig stateTtlConfigProto = stateDescriptor.getStateTtlConfig();
            StateTtlConfig stateTtlConfig = ProtoUtils.parseStateTtlConfigFromProto(stateTtlConfigProto);
            listStateDescriptor.enableTimeToLive(stateTtlConfig);
        }
        stateDescriptorCache.put(stateName, listStateDescriptor);
    } else {
        throw new RuntimeException(String.format("State name corrupt detected: " + "'%s' is used both as LIST state and '%s' state at the same time.", stateName, cachedStateDescriptor.getType()));
    }
    byte[] windowBytes = bagUserState.getWindow().toByteArray();
    if (windowBytes.length != 0) {
        bais.setBuffer(windowBytes, 0, windowBytes.length);
        Object namespace = namespaceSerializer.deserialize(baisWrapper);
        return (ListState<byte[]>) keyedStateBackend.getPartitionedState(namespace, namespaceSerializer, listStateDescriptor);
    } else {
        return (ListState<byte[]>) keyedStateBackend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, listStateDescriptor);
    }
}
Also used : ListState(org.apache.flink.api.common.state.ListState) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ByteString(org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig) FlinkFnApi(org.apache.flink.fnexecution.v1.FlinkFnApi) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) StateDescriptor(org.apache.flink.api.common.state.StateDescriptor)

Example 3 with StateTtlConfig

use of org.apache.flink.api.common.state.StateTtlConfig in project flink by apache.

the class ProtoUtilsTest method testParseStateTtlConfigFromProto.

@Test
public void testParseStateTtlConfigFromProto() {
    FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies cleanupStrategiesProto = FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.newBuilder().setIsCleanupInBackground(true).addStrategies(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.MapStrategiesEntry.newBuilder().setStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.Strategies.FULL_STATE_SCAN_SNAPSHOT).setEmptyStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.EmptyCleanupStrategy.EMPTY_STRATEGY)).addStrategies(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.MapStrategiesEntry.newBuilder().setStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.Strategies.INCREMENTAL_CLEANUP).setIncrementalCleanupStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.IncrementalCleanupStrategy.newBuilder().setCleanupSize(10).setRunCleanupForEveryRecord(true).build())).addStrategies(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.MapStrategiesEntry.newBuilder().setStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.Strategies.ROCKSDB_COMPACTION_FILTER).setRocksdbCompactFilterCleanupStrategy(FlinkFnApi.StateDescriptor.StateTTLConfig.CleanupStrategies.RocksdbCompactFilterCleanupStrategy.newBuilder().setQueryTimeAfterNumEntries(1000).build())).build();
    FlinkFnApi.StateDescriptor.StateTTLConfig stateTTLConfigProto = FlinkFnApi.StateDescriptor.StateTTLConfig.newBuilder().setTtl(Time.of(1000, TimeUnit.MILLISECONDS).toMilliseconds()).setUpdateType(FlinkFnApi.StateDescriptor.StateTTLConfig.UpdateType.OnCreateAndWrite).setStateVisibility(FlinkFnApi.StateDescriptor.StateTTLConfig.StateVisibility.NeverReturnExpired).setCleanupStrategies(cleanupStrategiesProto).build();
    StateTtlConfig stateTTLConfig = ProtoUtils.parseStateTtlConfigFromProto(stateTTLConfigProto);
    assertEquals(stateTTLConfig.getUpdateType(), StateTtlConfig.UpdateType.OnCreateAndWrite);
    assertEquals(stateTTLConfig.getStateVisibility(), StateTtlConfig.StateVisibility.NeverReturnExpired);
    assertEquals(stateTTLConfig.getTtl(), Time.milliseconds(1000));
    assertEquals(stateTTLConfig.getTtlTimeCharacteristic(), StateTtlConfig.TtlTimeCharacteristic.ProcessingTime);
    StateTtlConfig.CleanupStrategies cleanupStrategies = stateTTLConfig.getCleanupStrategies();
    assertTrue(cleanupStrategies.isCleanupInBackground());
    assertTrue(cleanupStrategies.inFullSnapshot());
    StateTtlConfig.IncrementalCleanupStrategy incrementalCleanupStrategy = cleanupStrategies.getIncrementalCleanupStrategy();
    assertNotNull(incrementalCleanupStrategy);
    assertEquals(incrementalCleanupStrategy.getCleanupSize(), 10);
    assertTrue(incrementalCleanupStrategy.runCleanupForEveryRecord());
    StateTtlConfig.RocksdbCompactFilterCleanupStrategy rocksdbCompactFilterCleanupStrategy = cleanupStrategies.getRocksdbCompactFilterCleanupStrategy();
    assertNotNull(rocksdbCompactFilterCleanupStrategy);
    assertEquals(rocksdbCompactFilterCleanupStrategy.getQueryTimeAfterNumEntries(), 1000);
}
Also used : FlinkFnApi(org.apache.flink.fnexecution.v1.FlinkFnApi) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig) Test(org.junit.Test)

Example 4 with StateTtlConfig

use of org.apache.flink.api.common.state.StateTtlConfig 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 StateTtlConfig

use of org.apache.flink.api.common.state.StateTtlConfig in project flink by apache.

the class MiniBatchGroupAggFunction method open.

@Override
public void open(ExecutionContext ctx) throws Exception {
    super.open(ctx);
    // instantiate function
    StateTtlConfig ttlConfig = createTtlConfig(stateRetentionTime);
    function = genAggsHandler.newInstance(ctx.getRuntimeContext().getUserCodeClassLoader());
    function.open(new PerKeyStateDataViewStore(ctx.getRuntimeContext(), ttlConfig));
    // instantiate equaliser
    equaliser = genRecordEqualiser.newInstance(ctx.getRuntimeContext().getUserCodeClassLoader());
    InternalTypeInfo<RowData> accTypeInfo = InternalTypeInfo.ofFields(accTypes);
    ValueStateDescriptor<RowData> accDesc = new ValueStateDescriptor<>("accState", accTypeInfo);
    if (ttlConfig.isEnabled()) {
        accDesc.enableTimeToLive(ttlConfig);
    }
    accState = ctx.getRuntimeContext().getState(accDesc);
    inputRowSerializer = InternalSerializers.create(inputType);
    resultRow = new JoinedRowData();
}
Also used : ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) RowData(org.apache.flink.table.data.RowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) StateTtlConfig(org.apache.flink.api.common.state.StateTtlConfig) PerKeyStateDataViewStore(org.apache.flink.table.runtime.dataview.PerKeyStateDataViewStore)

Aggregations

StateTtlConfig (org.apache.flink.api.common.state.StateTtlConfig)14 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)7 RowData (org.apache.flink.table.data.RowData)5 PerKeyStateDataViewStore (org.apache.flink.table.runtime.dataview.PerKeyStateDataViewStore)5 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)4 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)4 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)4 StateDescriptor (org.apache.flink.api.common.state.StateDescriptor)3 FlinkFnApi (org.apache.flink.fnexecution.v1.FlinkFnApi)3 ByteString (org.apache.beam.vendor.grpc.v1p26p0.com.google.protobuf.ByteString)2 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)1 ListState (org.apache.flink.api.common.state.ListState)1 MapState (org.apache.flink.api.common.state.MapState)1 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)1 ListSerializer (org.apache.flink.api.common.typeutils.base.ListSerializer)1 Transformation (org.apache.flink.api.dag.Transformation)1 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)1 RegisteredKeyValueStateBackendMetaInfo (org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 PrintSinkFunction (org.apache.flink.streaming.api.functions.sink.PrintSinkFunction)1