本文介绍了KafkaStreams EXACTLY_ONCE 保证 - 跳过 kafka 偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spark 2.2.0 和 kafka 0.10 spark-streaming 库来读取充满 Kafka-Streams scala 应用程序的主题.Kafka Broker 版本为 0.11,Kafka-streams 版本为 0.11.0.2.

I'm using Spark 2.2.0 and kafka 0.10 spark-streaming library to read from topic filled with Kafka-Streams scala application. Kafka Broker version is 0.11 and Kafka-streams version is 0.11.0.2.

当我在 Kafka-Stream 应用中设置 EXACTLY_ONCE 保证时:

When i set EXACTLY_ONCE guarantee in Kafka-Stream app:

 p.put(StreamsConfig.PROCESSING_GUARANTEE_CONFIG, StreamsConfig.EXACTLY_ONCE)

我在 Spark 中遇到此错误:

i get this error in Spark:

java.lang.AssertionError: assertion failed: Got wrong record for spark-executor-<group.id> <topic> 0 even after seeking to offset 24
at scala.Predef$.assert(Predef.scala:170)
at org.apache.spark.streaming.kafka010.CachedKafkaConsumer.get(CachedKafkaConsumer.scala:85)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.next(KafkaRDD.scala:223)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.next(KafkaRDD.scala:189)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.foreach(KafkaRDD.scala:189)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:59)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:104)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:48)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:310)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.to(KafkaRDD.scala:189)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:302)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.toBuffer(KafkaRDD.scala:189)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:289)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.toArray(KafkaRDD.scala:189)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:936)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$13.apply(RDD.scala:936)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2062)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:2062)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:108)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:335)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

如果没有设置 EXACTLY_ONCE 属性,它就可以正常工作.

If EXACTLY_ONCE property is not set, it works just fine.

编辑 1:填充了 kafka-streams 应用程序的主题(恰好启用后)具有错误的结束偏移量.当我运行 kafka.tools.GetOffsetShell 时,它给出了结束偏移 18,但在主题中只有 12 条消息(保留被禁用).当禁用恰好一次保证时,这些偏移量是匹配的.我试图根据 this,但问题仍然存在.

EDIT 1:Topic filled with kafka-streams app(exactly once enabled) has wrong ending offset. When i run kafka.tools.GetOffsetShell, it gives ending offset 18, but in topic there are just 12 messages (retention is disabled). When exactly once guarantee is disabled, these offsets are matching. I tried to reset kafka-streams according to this, but problem still remains.

编辑 2:当我使用 --print-offsets 选项运行 SimpleConsumerShell 时,输出如下:

EDIT 2:When i run SimpleConsumerShell with --print-offsets option, output is folowing:

next offset = 1
{"timestamp": 149583551238149, "data": {...}}
next offset = 2
{"timestamp": 149583551238149, "data": {...}}
next offset = 4
{"timestamp": 149583551238149, "data": {...}}
next offset = 5
{"timestamp": 149583551238149, "data": {...}}
next offset = 7
{"timestamp": 149583551238149, "data": {...}}
next offset = 8
{"timestamp": 149583551238149, "data": {...}}
...

当启用一次交付保证时,显然会跳过一些偏移量.

Some offsets are apparently skipped when exactly-once dellivery guarantee is enabled.

有什么想法吗?什么会导致这种情况?谢谢!

Any thoughts? What can cause this? Thanks!

推荐答案

我发现偏移间隙是 Kafka(版本 >= 0.11)中的预期行为,这些是由提交/中止事务标记引起的.

I found that offset gaps are expected behavior in Kafka (version >= 0.11), these are caused by commit/abort transaction markers.

有关 kafka 事务和控制消息的更多信息此处:

More info about kafka transactions and control messages here:

这些事务标记不暴露给应用程序,而是消费者在 read_committed 模式下使用它来过滤来自中止的事务并且不返回作为打开的一部分的消息事务(即那些在日志中但没有与它们关联的交易标记).

这里.

Kafka 事务是在 Kafka 0.11 中引入的,所以我假设 spark-streaming-kafka 库 0.10 不兼容这种消息格式,并且尚未实现新版本的 spark-streaming-kafka.

Kafka transactions were introduced in Kafka 0.11, so I assume that spark-streaming-kafka library 0.10 is not compatible with this message format, and newer version of spark-streaming-kafka is not yet implemented.

这篇关于KafkaStreams EXACTLY_ONCE 保证 - 跳过 kafka 偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:13