use of org.apache.flink.streaming.api.checkpoint.ListCheckpointed 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;
}
use of org.apache.flink.streaming.api.checkpoint.ListCheckpointed 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;
}
Aggregations