Flink(五)--DataStream的Checkpoints和Savepoints

一、Checkpoints检查点机制

Flink中基于异步轻量级的分布式快照技术提供了Checkpoints容错机制。快照产生过程非常轻量,高频率创建对Flink任务性能影响相对较小。

Checkpoint配置信息:

(1)Checkpoint开启和时间间隔指定

开启检查点并指定检查点时间间隔为1000ms,如果状态比较大,建议适当增加该值

env.enableCheckpointing(1000);

(2)设置状态存储后端,支持内存、文件系统和 RocksDB。这里选择文件系统

env.setStateBackend(new FsStateBackend(path));

(3)exactly-once和at-least-once语义选择

可以选择exactly-once语义保证整个应用内端到端的数据一致性,这种情况适合于数据要求比较高,不允许出现丢数据或者数据重复,同时,Flink的性能相对较弱,而at-least-once语义更合适于时延和吞吐量要求非常高但对数据的一致性要求不高的场景。默认情况下使用的是exactly-once模式。

env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

(4)Checkpoint超时时间

超时时间指定了每次Checkpoint执行过程中的上限时间,一旦Checkpoint执行时间超过该阈值,Flink将会中断Checkpoint过程,并按照超时处理。默认为10分钟

env.getCheckpointConfig().setCheckpointTimeout(60000);

(5)检查点之间最小时间间隔

该参数的主要目的是设定两个Checkpoint之间的最小时间间隔,防止出现例如状态数据过大而导致Checkpoint执行时间过长,从而导致Checkpoint积压过多。

env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);

(6)最大并行执行的检查点数量

根据指定的数量可以同时触发多个Checkpoint

env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);

(7)外部检查点

设定周期性的外部检查点,然后将状态数据持久化到外部系统中,使用这种方式不会在任务正常停止的过程中清理掉检查点数据。

 env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

(8)failOnCheckPointingErrors

failOnCheckPointingErrors参数决定了在Checkpoint执行过程中,如果出现失败或者错误时,任务是否同时被关闭,默认值是True。

env.getCheckpointConfig().setFailOnCheckpointingErrors(false);

Checkpoints实例:

public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);

        String path = "file:///opt/checkpoints";
        env.setStateBackend(new FsStateBackend(path));
        env.enableCheckpointing(1000);
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointTimeout(60000);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
        env.getCheckpointConfig().setFailOnCheckpointingErrors(false);

        Properties props = new Properties();
        props.put("bootstrap.servers", "kafka:9092");
        props.put("group.id", "test_flink");

        DataStream<String> input = env.addSource(new FlinkKafkaConsumer010<String>
                ("test", new SimpleStringSchema(), props));

        SingleOutputStreamOperator<Tuple2<String, Integer>> map = input.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] arr = value.split(",");
                return new Tuple2<String, Integer>(arr[0], Integer.valueOf(arr[1]));
            }
        });
        WindowedStream<Tuple2<String, Integer>, Tuple, TimeWindow> window = map.keyBy(0).window(ProcessingTimeSessionWindows.withGap(Time.seconds(100)));
        SingleOutputStreamOperator<Tuple2<String, Integer>> process1 = window.process(new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple, TimeWindow>() {

            @Override
            public void process(Tuple tuple, Context context, Iterable<Tuple2<String, Integer>> elements, Collector<Tuple2<String, Integer>> out) throws Exception {
                int sum = 0;
                String key = "";
                Iterator<Tuple2<String, Integer>> iterator = elements.iterator();
                while (iterator.hasNext()){
                    Tuple2<String,Integer> value = iterator.next();
                    key = value.f0;
                    sum += value.f1;
                }
                out.collect(new Tuple2<String,Integer>(key,sum));
            }
        });
        process1.print();

        env.execute("Test Checkpoints");
    }

代码逻辑很简单,把100秒窗口内的数据,以第一个字段做key聚合,算出值的和,然后输出。打包放到集群上。启动flink集群,然后运行程序:

[root@kafka bin]# ./start-cluster.sh
[root@kafka bin]# ./flink run -c com.lishaokai.api.CheckpointsTest /opt/my_flink-2.0.jar

kakfa输入:

然后,等待日志输出:

OK。功能正常。然后再次输入

取消任务:

在/opt/checkpoints目录下发现检查点。

重启任务:

[root@kafka bin]# ./flink run -s /opt/checkpoints/c9dd58e3db9c9800bdc4e927b372bf4c/chk-264 -c com.lishaokai.api.CheckpointsTest /opt/my_flink-2.0.jar

等待一段时间,发现程序正确输出:

二、Savepoints机制

Savepoints是检查点的一种特殊实现,需要用户手动触发Checkpoint,主要用来帮助用户在升级和维护集群过程中保存系统中的状态数据。

关闭掉checkpoint功能,还是原来的处理逻辑。

kafka输入:

在web页面上找到jobID.

[root@kafka bin]# ./flink savepoint 5063a58111e39116fd4009b5144ee1c8 file:///opt/savepoint

在对应目录下会生成文件夹

重启程序:

[root@kafka bin]# ./flink run -s /opt/savepoint/savepoint-5063a5-876a6386d8a0 -c com.lishaokai.api.CheckpointsTest /opt/my_flink-2.0.jar 

日志输出:

Savepoints功能演示完成。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值