Flink 中的一把锁


点击箭头处“蓝色字”,关注我们哦!!

那把锁

锁用于多线程安全场景下,在Flink中存在一把锁,被用于数据处理线程、定时器调用线程、checkpoint线程。在StreamTask中定义了一个Object对象lock,通过使用synchronized方式进行同步,在task的初始化过程中该对象传给了SystemProcessingTimeService、StreamInputProcessor、StreamTwoInputProcessor。

数据处理线程

这里所说的数据处理线程表示正常的数据处理流程,可以认为就是processElement处理过程,StreamInputProcessor/StreamTwoInputProcessor主要工作就是读取数据然后调用对应的operator处理读取到的数据也就是调用processElement方法:

StreamRecord<IN> record = recordOrMark.asRecord();
            synchronized (lock) {
              numRecordsIn.inc();
              streamOperator.setKeyContextElement1(record);
              streamOperator.processElement(record);
            }

通过源码可以发现每次调用processElement之前都会使用synchronized锁住lock,然后才能进行后续的处理。

定时器调用线程

Flink中有一个很重要的功能那就是定时器,窗口触发需要定时器、用户自定义注册定时器需要定时器,但是定时器又可以按照时间属性分为两种:事件时间语义下watermark推进触发的定时器、处理时间语义下定时调度的定时器。

watermark也是作为一种StreamElement在管道中流动,在watermark向前推进时也是需要获得锁才能触发定时器:

public void handleWatermark(Watermark watermark) {
      try {
        synchronized (lock) {
          watermarkGauge.setCurrentWatermark(watermark.getTimestamp());
          operator.processWatermark(watermark);
        }
      } catch (Exception e) {
        throw new RuntimeException("Exception occurred while processing valve output watermark: ", e);
      }
    }

同样在处理时间触发的定时器也是需要获得锁才能执行:

    //SystemProcessingTimeServuce中
    public void run() {
      synchronized (lock) {
        try {
          if (serviceStatus.get() == STATUS_ALIVE) {
            target.onProcessingTime(timestamp);
          }
        } catch (Throwable t) {
          TimerException asyncException = new TimerException(t);
          exceptionHandler.handleAsyncException("Caught exception while processing timer.", asyncException);
        }
      }
    }

定时触发为什么需要锁?在processElement中可能会操作状态、在定时回调onTimer中也可能会操作状态,那么状态就是作为共享数据,为了保证数据的一致性,所以这里加了锁。

checkpoint线程

checkpoint是由jobmaster协调完成的,会定时向source端发送barrier标记然后在数据流中流动,checkpoint是为了对状态某一个时间点的备份,同样与processElement存在状态数据的竞争,为了保证数据的一致性,在checkpoint过程中会存在锁竞争:

//StreamTask中performCheckpoint方法
synchronized (lock) {
      if (isRunning) {
        operatorChain.prepareSnapshotPreBarrier(checkpointMetaData.getCheckpointId());


        // Step (2): Send the checkpoint barrier downstream
        operatorChain.broadcastCheckpointBarrier(
            checkpointMetaData.getCheckpointId(),
            checkpointMetaData.getTimestamp(),
            checkpointOptions);


        // Step (3): Take the state snapshot. This should be largely asynchronous, to not
        //           impact progress of the streaming topology
        checkpointState(checkpointMetaData, checkpointOptions, checkpointMetrics);
        return true;
      }
      else {
      .....


      }
    }

—END—

关注回复Flink

获取更多系列

原创不易,好看,就点个"在看"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值