Apache flink Flink RichParallelSourceFunction-close()与cancel()的比较

Apache flink Flink RichParallelSourceFunction-close()与cancel()的比较,apache-flink,flink-streaming,Apache Flink,Flink Streaming,我正在实现一个RichParallelSourceFunction,它通过SFTP读取文件。RichParallelSourceFunction从SourceFunction继承cancel(),从RichFunction()继承close()。据我所知,cancel()和close()都是在源代码被删除之前被调用的。因此,在这两种方法中,我必须添加逻辑来停止读取文件的无休止循环 当我将源代码的并行度设置为1并从IDE运行Flink作业时,Flink运行时在调用start()之后立即调用stop

我正在实现一个RichParallelSourceFunction,它通过SFTP读取文件。RichParallelSourceFunction从SourceFunction继承cancel(),从RichFunction()继承close()。据我所知,cancel()和close()都是在源代码被删除之前被调用的。因此,在这两种方法中,我必须添加逻辑来停止读取文件的无休止循环

当我将源代码的并行度设置为1并从IDE运行Flink作业时,Flink运行时在调用start()之后立即调用stop(),整个作业都停止了。我没想到会这样

当我将源代码的并行度设置为1,并在集群中运行Flink作业时,该作业照常运行。 如果我将源代码的并行性保留为默认值(在我的示例4中),那么作业将照常运行

使用Flink 1.7


公共类SftpSource
扩展了RichParallelSourceFunction
{
专用最终SftpConnection mConnection;
私有布尔mSourceIsRunning;
@凌驾
公共void open(配置参数)引发异常
{
mConnection.open();
}
@凌驾
公众假期结束()
{
mSourceIsRunning=false;
}
@凌驾
公共作废运行(SourceContext文本)
{
while(mSourceIsRunning)
{
已同步(aContext.getCheckpointLock())
{
//使用mConnection
//aContext.collect()。。。
}
尝试
{
睡眠(1000);
}
捕获(中断异常ie)
{
warn(“线程错误:{}”,即.getMessage());
}
}
mConnection.close();
}
@凌驾
公开作废取消()
{
mSourceIsRunning=false;
}
}
所以我有解决办法,问题更多的是关于理论。如果并行度为1,并且作业从IDE(即从命令行)运行,为什么会调用close()?
另外,在RichParallelSourceFunction中是否执行close()和cancel()操作?我认为Javadoc不仅仅是不言自明的:

Gracefully Stopping Functions
Functions may additionally implement the {@link org.apache.flink.api.common.functions.StoppableFunction} interface. "Stopping" a function, in contrast to "canceling" means a graceful exit that leaves the state and the emitted elements in a consistent state.
--

--


注意,您可以取消SourceFunction,但停止SourceContext

我认为javadocs不仅仅是不言自明的:

Gracefully Stopping Functions
Functions may additionally implement the {@link org.apache.flink.api.common.functions.StoppableFunction} interface. "Stopping" a function, in contrast to "canceling" means a graceful exit that leaves the state and the emitted elements in a consistent state.
--

--

注意,您可以取消SourceFunction,但停止SourceContext

如果并行度为1,并且作业是从 IDE

close
在上次调用主要工作方法(如map或join)后调用。此方法可用于清理工作。 它将被称为独立于并行中定义的数字

另外,在RichParallelSourceFunction中close()和cancel()是否也执行相同的操作

他们不是同一件事,看看是怎么描述的

Cancels the source. Most sources will have a while loop inside the run(SourceContext) method. The implementation needs to ensure that the source will break out of that loop after this method is called.

以下链接可能有助于您了解任务生命周期:

如果并行度为1,并且作业是从 IDE

close
在上次调用主要工作方法(如map或join)后调用。此方法可用于清理工作。 它将被称为独立于并行中定义的数字

另外,在RichParallelSourceFunction中close()和cancel()是否也执行相同的操作

他们不是同一件事,看看是怎么描述的

Cancels the source. Most sources will have a while loop inside the run(SourceContext) method. The implementation needs to ensure that the source will break out of that loop after this method is called.

以下链接可能有助于您了解任务生命周期:
我在代码中发现了一个bug。这是解决办法

public void open(Configuration parameters) throws Exception
{
    mConnection.open();
    mSourceIsRunning = true;
}

现在,直到我决定停止工作流时才调用close(),在这种情况下,首先调用cancel(),然后调用close()。我仍然想知道并行性是如何影响行为的。

我在代码中发现了一个bug。这是解决办法

public void open(Configuration parameters) throws Exception
{
    mConnection.open();
    mSourceIsRunning = true;
}

现在,直到我决定停止工作流时才调用close(),在这种情况下,首先调用cancel(),然后调用close()。我仍然想知道并行性是如何影响行为的。

谢谢您对cancel()的解释。结合Ricardo Alvaro Lohmann的输入,它给出了close()和cancel()之间区别的答案。感谢对cancel()的解释。结合Ricardo Alvaro Lohmann的输入,它给出了close()和cancel()之间差异的答案。选择此选项是因为:-关于close()的解释+close()和cancel()的比较+morsik的输入回答close()和cancel()之间的差异-关于任务生命周期的链接提供了更多的答案,也许它帮助我找到了我的错误选择,因为:-关于close()的解释+close()和cancel()的比较+来自morsik的输入回答close()和cancel()之间的差异-关于任务生命周期的链接提供了更多答案,也许它帮助我找到了我的bug