java为什么flink 1.10.1在flink crashrestart与FsStateBackend一起启动后不加载存储状态
我正在使用Flink 1.10.1和FsStateBackend作为检查点的状态后端。我有一些有状态的操作,在应用程序运行期间(作为.jar应用程序而不是集群运行),它们按照预期工作,但是如果应用程序由于某种原因停止(或崩溃),应该存储在文件系统中的状态(带有检查点)不会被加载,函数也不会有任何以前的引用,然后我需要从数据库中加载信息,并将其保存为状态,以便再次处理这些以前的状态。必须有一种方法可以使用检查点和fsstatebend实现这一点,而不必读取数据库中的所有信息,只需从已存储的检查点重新加载此状态即可。这可能吗
下面是一些代码: 我的检查点配置
final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironment(8, GetConfiguration.getConfig());
final StateBackend stateBackend = new FsStateBackend(new Path("/some/path/checkpoints").toUri(), true);
env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
env.getCheckpointConfig().setMinPauseBetweenCheckpoints(5000);
env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
env.enableCheckpointing(30000, CheckpointingMode.EXACTLY_ONCE);
env.getCheckpointConfig().setCheckpointTimeout(60000);
env.getCheckpointConfig().setTolerableCheckpointFailureNumber(10);
env.getCheckpointConfig().setPreferCheckpointForRecovery(true);
env.setRestartStrategy(RestartStrategies.noRestart());
env.setStateBackend(stateBackend);
这就是我想要避免的例子:
public class EventCountMap extends RichMapFunction<Event, EventCounter> {
private static final MapStateDescriptor<String, Timestamp> descriptor = new MapStateDescriptor<>("previous_counter", String.class, Timestamp.class);
private static final EventCounter eventCounter = new EventCounter();
private MapState<String, Timestamp> previous_state;
private static final StateTtlConfig ttlConfig = StateTtlConfig
.newBuilder(org.apache.flink.api.common.time.Time.days(1))
.cleanupFullSnapshot()
.build();
@Override
public void open(Configuration parameters) {
descriptor.enableTimeToLive(ttlConfig);
previous_state = getRuntimeContext().getMapState(descriptor);
}
/*I want to avoid to call this function that load all events from db and pass them to the state to be used. This happens only once but there must be a efficient way to do this in flink.*/
private void mapRefueled() throws Exception {
Preconditions.checkNotNull(previous_state);
for (Map.Entry<String, Timestamp> map : StreamingJob.update_beh_count_ts.entrySet())
previous_state.put(map.getKey(), map.getValue());
StreamingJob.update_beh_count_ts.clear();
}
@Override
public EventCounter map(Event event) throws Exception {
/*Refuel map state in case of failures*/
if (!StreamingJob.update_beh_count_ts.isEmpty()) mapRefueled();
eventCounter.date = new Date(event.timestamp.getTime());
final String key_first = eventCounter.date.toString().concat("_ts_first");
final String key_last = eventCounter.date.toString().concat("_ts_last");
if (previous_state.contains(key_first) && previous_state.contains(key_last)) {
final Timestamp first = (previous_state.get(key_first).after(event.timestamp)) ? event.timestamp : previous_state.get(key_first);
final Timestamp last = (previous_state.get(key_last).before(event.timestamp)) ? event.timestamp : previous_state.get(key_last);
previous_state.put(key_first, first);
previous_state.put(key_last, last);
} else {
previous_state.put(key_first, event.timestamp);
previous_state.put(key_last, event.timestamp);
}
eventCounter.first_event = previous_state.get(key_first);
eventCounter.last_event = previous_state.get(key_last);
return eventCounter;
}
}
希望我能为你解释一下我需要做什么。 亲切的问候! 提前谢谢
# 1 楼答案
要在重新启动作业时加载检查点的状态,必须显式安排,否则将在不加载检查点的情况下重新运行作业
详情见Restore from a savepoint和Resuming from a retained checkpoint,但要点如下:
我猜这件事还没做完
就如何配置Flink群集进行自动恢复的最佳实践而言,这取决于您使用的是什么(纱线、Mesos、Kubernetes等)