Search in sources :

Example 1 with CheckpointedFunction

use of org.apache.flink.streaming.api.checkpoint.CheckpointedFunction in project flink by apache.

the class StreamingFunctionUtils method trySnapshotFunctionState.

private static boolean trySnapshotFunctionState(StateSnapshotContext context, OperatorStateBackend backend, Function userFunction) throws Exception {
    if (userFunction instanceof CheckpointedFunction) {
        ((CheckpointedFunction) userFunction).snapshotState(context);
        return true;
    }
    if (userFunction instanceof ListCheckpointed) {
        @SuppressWarnings("unchecked") List<Serializable> partitionableState = ((ListCheckpointed<Serializable>) userFunction).snapshotState(context.getCheckpointId(), context.getCheckpointTimestamp());
        // We are using JavaSerializer from the flink-runtime module here. This is very naughty
        // and
        // we shouldn't be doing it because ideally nothing in the API modules/connector depends
        // directly on flink-runtime. We are doing it here because we need to maintain backwards
        // compatibility with old state and because we will have to rework/remove this code
        // soon.
        ListStateDescriptor<Serializable> listStateDescriptor = new ListStateDescriptor<>(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME, new JavaSerializer<>());
        ListState<Serializable> listState = backend.getListState(listStateDescriptor);
        listState.clear();
        if (null != partitionableState) {
            try {
                for (Serializable statePartition : partitionableState) {
                    listState.add(statePartition);
                }
            } catch (Exception e) {
                listState.clear();
                throw new Exception("Could not write partitionable state to operator " + "state backend.", e);
            }
        }
        return true;
    }
    return false;
}
Also used : Serializable(java.io.Serializable) ListCheckpointed(org.apache.flink.streaming.api.checkpoint.ListCheckpointed) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) CheckpointedFunction(org.apache.flink.streaming.api.checkpoint.CheckpointedFunction)

Example 2 with CheckpointedFunction

use of org.apache.flink.streaming.api.checkpoint.CheckpointedFunction in project flink by apache.

the class StreamingFunctionUtils method tryRestoreFunction.

private static boolean tryRestoreFunction(StateInitializationContext context, Function userFunction) throws Exception {
    if (userFunction instanceof CheckpointedFunction) {
        ((CheckpointedFunction) userFunction).initializeState(context);
        return true;
    }
    if (context.isRestored() && userFunction instanceof ListCheckpointed) {
        @SuppressWarnings("unchecked") ListCheckpointed<Serializable> listCheckpointedFun = (ListCheckpointed<Serializable>) userFunction;
        // We are using JavaSerializer from the flink-runtime module here. This is very naughty
        // and
        // we shouldn't be doing it because ideally nothing in the API modules/connector depends
        // directly on flink-runtime. We are doing it here because we need to maintain backwards
        // compatibility with old state and because we will have to rework/remove this code
        // soon.
        ListStateDescriptor<Serializable> listStateDescriptor = new ListStateDescriptor<>(DefaultOperatorStateBackend.DEFAULT_OPERATOR_STATE_NAME, new JavaSerializer<>());
        ListState<Serializable> listState = context.getOperatorStateStore().getListState(listStateDescriptor);
        List<Serializable> list = new ArrayList<>();
        for (Serializable serializable : listState.get()) {
            list.add(serializable);
        }
        try {
            listCheckpointedFun.restoreState(list);
        } catch (Exception e) {
            throw new Exception("Failed to restore state to function: " + e.getMessage(), e);
        }
        return true;
    }
    return false;
}
Also used : Serializable(java.io.Serializable) ListCheckpointed(org.apache.flink.streaming.api.checkpoint.ListCheckpointed) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) ArrayList(java.util.ArrayList) CheckpointedFunction(org.apache.flink.streaming.api.checkpoint.CheckpointedFunction)

Aggregations

Serializable (java.io.Serializable)2 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)2 CheckpointedFunction (org.apache.flink.streaming.api.checkpoint.CheckpointedFunction)2 ListCheckpointed (org.apache.flink.streaming.api.checkpoint.ListCheckpointed)2 ArrayList (java.util.ArrayList)1