Flink Statefun 并发状态更新

Flink Statefun concurrent state update

我正在尝试使用 apache flink 有状态函数来实现消息传递场景。 我的状态之一能够通过提供给 MatchBinder 的两个不同函数进行更新。这两个函数基本上检查当前状态并相应地更新状态。

What happens if these two functions are called concurrently for the same key?

MatchBinder 基本上是一种编写单个 StateFun 函数的简便方法,它通过首先匹配传入消息的类型(或属性)来开始执行。它基本上是一种 避免 编写如下代码的方法:

...
if (message instanceof A) {
  handleA((A) message);
} else if (message instanceof B) {
  handleB((B) message);
}
...

因此实际上,尽管您为每个绑定案例提供了“不同的”Java 函数,但调用的是同一个 StateFun 函数,并且会选择正确的绑定案例。

Is there a queue mechanism for stateful functions called for the same key?

是的,StateFun 函数将按地址顺序调用。当一个函数应用于特定地址时,不会同时应用该地址的其他消息。由于将 Apache Flink 作为实际运行时,这几乎是免费的。

Can we lock the state access/update for sequential access ?

每个地址的状态访问和修改是原子的和顺序的。