Kafka-streams:windows 保留期 memory/performance 影响

Kafka-streams: windows retention period memory/performance impact

我们在 kafka-streams 中进行了开发,它产生了一种时间 window 聚合类型:

selectKey().groupByKey().aggregate()

然后使用

TimeWindows.of().until()

我的主要问题是如果不使用 until 会发生什么,假设我们有 1 分钟 windows 并且由于某些不可预见的原因,一个新事件从 1 周前到达,应用程序是否从一开始就保存所有 windows 状态?这不会导致内存消耗过大吗?恢复适当的 window?

我们最近遇到了同样的问题。可以在这个 kafka 流的源文件中找到答案:

https://github.com/apache/kafka/blob/1.1/streams/src/main/java/org/apache/kafka/streams/kstream/Windows.java

其中包含:

static final long DEFAULT_MAINTAIN_DURATION_MS = 24 * 60 * 60 * 1000L; // one day

因此,如果不指定 until() 设置,您的 windowed 状态存储将默认保留一天(下限)的记录。

您问题的另一部分是:对于迟到的事件,超过 window 过期的时间会怎样?该答案在开发人员指南中:

In the Kafka Streams DSL users can specify a retention period for the window. This allows Kafka Streams to retain old window buckets for a period of time in order to wait for the late arrival of records whose timestamps fall within the window interval. If a record arrives after the retention period has passed, the record cannot be processed and is dropped.

将这些信息拼凑在一起表明,如果您未在 windowed 流中指定 until() 设置,则记录将至少保留一天,并且到达的记录超过迟到一天将被删除。