在 kafka 本地状态存储/变更日志中的保留时间

mme*_*sen 3 apache-kafka apache-kafka-streams

我使用 Kafka 和 Kafka Streams 作为 Spring Cloud Stream 的一部分。在我的 Kafka Streams 应用程序中流动的数据正在按特定时间窗口聚合和具体化:

Materialized<String, ErrorScore, WindowStore<Bytes, byte[]>> oneHour = Materialized.as("one-hour-store");
    oneHour.withLoggingEnabled(topicConfig);
    events
            .map(getStringSensorMeasurementKeyValueKeyValueMapper())
            .groupByKey()
            .windowedBy(TimeWindows.of(oneHourStore.getTimeUnit()))
            .reduce((aggValue, newValue) -> getMaxErrorScore(aggValue, newValue),
                    (oneHour));
Run Code Online (Sandbox Code Playgroud)

按照设计,正在具体化的信息也由变更日志主题支持。

我们的应用程序还有一个 rest 端点,它将像这样查询 statestore:

 ReadOnlyWindowStore<String, Double> windowStore =  queryableStoreRegistry.getQueryableStoreType("one-hour-store", QueryableStoreTypes.windowStore());
 WindowStoreIterator<ErrorScore> iter = windowStore.fetch(key, from, to);
Run Code Online (Sandbox Code Playgroud)

查看创建的更改日志主题的设置,它显示:

min.insync.replicas 1
cleanup.policy delete
retention.ms 5259600000
retention.bytes -1
Run Code Online (Sandbox Code Playgroud)

我认为当地的州商店至少会将信息保留 61 天(~2 个月)。然而,似乎只有最后一天的数据保留在存储中。

什么可能导致数据这么快被删除?

使用解决方案更新 Kafka Streams 2.0.1 版不包含 Materialized.withRetention 方法。对于这个特定版本,我能够使用以下解决我问题的代码设置状态存储的保留时间:

TimeWindows timeWindows = TimeWindows.of(windowSizeMs);
    timeWindows.until(retentionMs);
Run Code Online (Sandbox Code Playgroud)

使我的代码写成:

...

.groupByKey()
        .windowedBy(timeWindows)
        .reduce((aggValue, newValue) -> getMaxErrorScore(aggValue, newValue),
                (oneHour));
...
Run Code Online (Sandbox Code Playgroud)

Mat*_*Sax 5

对于 windowed KTables,有本地保留时间和 changlog 保留时间。您可以通过设置本地存储保留时间Materialized.withRetentionTime(...)-- 默认值为 24 小时。

对于较旧的 Kafka 版本,本地存储保留时间通过Windows#until().

如果创建了新应用程序,则创建的变更日志主题的保留时间与本地存储保留时间相同。但是,如果您手动增加日志保留时间,这不会影响您的商店保留时间,但您需要相应地更新您的代码。当更改日志主题已经存在时也是如此:如果您更改本地存储保留时间,则更改日志主题配置不会自动更新。

也有一个 Jira:https : //issues.apache.org/jira/browse/KAFKA-7591