要想使用Operator State(non-keyed state),可以实现CheckpointedFunction接口实现一个有状态的函数。
关键点:
1. CheckpointedFunction是stateful transformation functions的核心接口,用于跨stream维护state。虽然有更轻量级的接口存在(假如不实现该接口,代替方案,比如operator state可以实现ListCheckpointed,已经废弃;keyed state可以用RuntimeContext,而RuntimeContext出现在RichFunction中,所以可以实现RichFunction),但是该接口为管理keyed state和operator state提供了最大的灵活性。
2. snapshotState是在执行checkpoint的时候会被调用;initializeState是在每次用户定义的function初始化的时候(第一次初始化或者从前一次checkpoint recover的时候)被调用,该方法不仅可以用来初始化state,还可以用于处理state recovery的逻辑。
3. 对于manageed operator state,目前仅仅支持list-style的形式,即要求state是serializable objects的List结构,方便在rescale的时候进行redistributed;关于redistribution schemes的模式目前有两种,分别是Even-split redistribution(在restore/redistribution的时候每个operator仅仅得到整个state的sublist,即多parallel下)及Union redistribution(在restore/redistribution的时候每个operator得到整个state的完整list,状态值比较大时可能会报内存错误或rpc帧过大)
CheckpointedFunction提供了两个函数:
void snapshotState(FunctionSnapshotContext context) throws Exception; void initializeState(FunctionInitializationContext context) throws Exception;
FunctionSnapshotContext继承了ManagedSnapshotContext接口,它定义了getCheckpointId、getCheckpointTimestamp方法;FunctionInitializationContext继承了ManagedInitializationContext接口,它定义了isRestored、getOperatorStateStore、getKeyedStateStore方法,可以用来判断是否是在前一次execution的snapshot中restored,以及获取OperatorStateStore、KeyedStateStore对象。



