在 flink 自定义源的任务线程中完成睡眠时,槽的上下文切换如何发生?

How does context switch of slot happens when a sleep is done in the task thread of a flink custom source?

我有一个自定义源,它每 x 分钟生成一些事件。我引用了 this 文件,我的代码如下所示

public class PeriodicSourceGenerator extends RichParallelSourceFunction<GenericMetric> {
    private transient AtomicBoolean isRunning;
    @Override
    public void open(final Configuration c) throws Exception {
        isRunning = new AtomicBoolean(true);
    }
    @Override
    public void run(SourceContext<GenericMetric> ctx) throws Exception {
        while (isRunning.get()) {
            //noinspection BusyWait
            Thread.sleep(300000); // 5 mins
            final long ts = System.getCurrentTimeMillis();
            final MetricStore.MetricPoint mp = new MetricStore.MetricPoint(ts, 1, -1);
            synchronized (ctx.getCheckpointLock()) {
                ctx.collectWithTimestamp(new GenericMetric(mk, MetricName.vRNI_internal_droppedTx_flow_absolute_latest_number, mp), ts);
                ctx.collectWithTimestamp(new GenericMetric(mk, MetricName.vRNI_internal_droppedRx_flow_absolute_latest_number, mp), ts);
            }
        }
        logger.info("Job cancelled. Shutting Down Periodic Source Generator");
    }

    @Override
    public void cancel() {
        isRunning.set(false);
    }
}

我在单个 flink 作业中 运行 多个管道,如下所示。

我 运行 flink 启用了默认的运算符链接和插槽共享。我所有的操作员都具有相同的并行度,30,我有 5 个任务管理器,因此每个任务管理器有 6 个槽。

有人可以告诉我 PeriodicSourceGenerator 管道中的睡眠将如何影响 Collection Source 管道吗?我的理解是,睡眠将使 PeriodicSource 生成器管道上下文由 Collection Source 管道切换,并且整个插槽不会暂停 5 分钟。我的理解正确吗?

睡在一个操作员身上不会暂停整个槽——只是暂停包含该操作员的任务。在这种情况下,休眠 PeriodicSourceGenerator 不会影响 Collection Source 管道,因为这些管道未连接。

一般来说,您应该避免在主任务线程中休眠(或阻塞)。这会产生负面后果,例如阻止整个作业的检查点。在这种特定情况下,可以按照您的方式睡觉:即在检查点锁之外。