首页 文章

Kafka 保留政策无法按预期工作

提问于
浏览
2

我想为我们的一些用例实现数据重放,为此,我需要使用 Kafka 保留策略(我正在使用 join,我需要窗口时间准确)。 P.S。我正在使用 Kafka 版 0.10.1.1

我正在将数据发送到这样的主题:

kafkaProducer.send(
                    new ProducerRecord<>(kafkaTopic, 0, (long) r.get("date_time") ,r.get(keyFieldName).toString(), r)
            );

我创建这样的主题:

kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic myTopic
kafka-topics --zookeeper localhost --alter --topic myTopic --config retention.ms=172800000 kafka-topics --zookeeper localhost --alter --topic myTopic --config segment.ms=172800000

因此,通过上述设置,我应该将我的主题的保留时间设置为 48 小时。

我扩展TimestampExtractor以记录每条消息的实际时间。

public class ConsumerRecordOrWallclockTimestampExtractor implements TimestampExtractor {
    private static final Logger LOG = LoggerFactory.getLogger(ConsumerRecordOrWallclockTimestampExtractor.class);
    @Override
    public long extract(ConsumerRecord<Object, Object> consumerRecord) {
        LOG.info("TIMESTAMP : " + consumerRecord.timestamp() + " - Human readable : " + new Date(consumerRecord.timestamp()));
        return consumerRecord.timestamp() >= 0.1 ? consumerRecord.timestamp() : System.currentTimeMillis();
    }
}

为了测试,我已经向我的主题发送了 4 条消息,我得到了这 4 条日志消息。

2017-02-28 10:23:39 INFO ConsumerRecordOrWallclockTimestampExtractor:21 - TIMESTAMP:1488295086292 Human readble -Tue Feb 28 10:18:06 EST 2017
2017-02-28 10:24:01 INFO ConsumerRecordOrWallclockTimestampExtractor:21 - TIMESTAMP:1483272000000 Human readble -Sun Jan 01 07:00:00 EST 2017
2017-02-28 10:26:11 INFO ConsumerRecordOrWallclockTimestampExtractor:21 - TIMESTAMP:1485820800000 Human readble -Mon Jan 30 19:00:00 EST 2017
2017-02-28 10:27:22 INFO ConsumerRecordOrWallclockTimestampExtractor:21 - TIMESTAMP:1488295604411 人类可读-Tue 2 月 28 日 10:26:44 EST 2017

因此基于卡夫卡的保留政策我希望看到我的两条消息在 5 分钟后得到 purged/deleted(因为它们是 1 月 1 日和 1 月 30 日的第 2 和第 3 消息)。但我试着消耗我的主题一个小时,每次我消耗我的主题我得到了所有 4 条消息。

kafka-avro-console-consumer --zookeeper localhost:2181 --from-beginning --topic myTopic

我的 Kafka 配置是这样的:

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

我做错了什么或者我错过了什么?

1 回答

  • 11

    Kafka 通过删除日志段来实施其保留策略。 Kafka 从不删除活动段,该段是将附加发送到分区的新消息的段。卡夫卡只删除旧段。当新消息发送到分区时,Kafka 将活动段滚动到旧段中

    • 带有新消息的活动段的大小将超过log.segment.bytes,或

    • 活动段中第一条消息的时间戳早于log.roll.ms(默认为 7 天)

    因此,在您的示例中,您必须在 2017 年 2 月 28 日美国东部时间 2017 年之后等待 7 天,发送新消息,然后将删除所有 4 条初始消息。

相关问题