如何优雅的关闭kafka spark streaming应用

引入问题

    1.在spark streaming 中,如果消息抓取速度大于消费速度,这时候队列会有积压,如果这时候关闭了spark App,会导致队列中数据丢失

    2.spark.streaming.stopGracefullyOnShutdown   这个参数在单机环境上有用,但是在yarn 集群上仍然没有用,即使yarn kill 是发送kill -15 信号量

 

1.初步思路

    通过监控共享变量的形式, 由应用自身,去调用 JavaStreamingContext.stop(true,true),

    使用自己编写的特定的脚本, 启动和关闭spark APP

2.共享变量的选择

    a.redis

    b.zookeeper

    c.hdfs

3.具体实施

    这里采用hdfs

monitor(groupId, path,()-> JavaStreamingContext.stop(true,true)).start();
private static Thread monitor(String groupId, String dirPath,Runnable task) {
    Thread t = new Thread(() -> {
        String path;
        if (dirPath.charAt(dirPath.length() - 1) == '/') {
            path = dirPath + groupId;
        } else {
            path = dirPath + "/" + groupId;
        }
        while (true) {
            String cmd = "hdfs dfs -ls " + path;
            try {
                Thread.sleep(5000L);
                Process process = Runtime.getRuntime().exec(cmd);
                InputStream in = process.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
                String line = br.readLine();
                if (line == null || !line.contains(path)) {
                    logger.warn("flag file not found |stop Spark App by monitor thread !", line);
                    task.run();
                }
            } catch (InterruptedException e) {
                logger.error("monitor thread has been interrupt | {}", e.getMessage());
            } catch (Exception e) {
                logger.error("get flag file failed from hdfs | {}", cmd);
            }
        }
    }, groupId + "monitor_thread");
    t.setDaemon(true);
    return t;
}

 

转载于:https://my.oschina.net/ktlb/blog/897641

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值