繁体   English   中英

kafka 批处理侦听器的正确错误处理

[英]Proper error handling for kafka batch listener

我正在研究错误处理实现并有一个问题。
让我解释一下问题:
我收到一批消息,我在 for 循环中对每个消息进行数据库查找,然后我需要收集列表中所有查找的对象,并使用此对象列表调用批量插入存储过程和批量更新存储过程。

现在让我们假设在查找过程中发生了一些异常。 我想重试这条消息。 对于这种情况,我尝试使用DefaultErrorHandler 但是有一个问题,根据文档,当抛出带有元素索引的 BatchListenerFailedException 时,它会提交索引之前记录的偏移量。 但正如我所说,我需要在查找后执行批量插入和更新,所以我不想在索引之前提交偏移量,这些记录还没有插入/更新到数据库中。

这是否意味着我唯一的选择是使用RetryingBatchErrorHandler每次重试整个批次? 我可以以某种方式继续处理不产生错误的消息吗?

此外,如果RetryingBatchErrorHandler是唯一的选择,我怎么能确定在长退避期(指数退避)的情况下,kafka 不会杀死我的消费者并且不会启动重新平衡?

我目前的实现:

RetryingBatchErrorHandler retryingBatchErrorHandler =
                new RetryingBatchErrorHandler(backoff,
                        (consumerRecord, e) ->
                                log.error("Backoff attempts exhausted for the record with offset={}, partition={}, value={}, offset committed.",
                consumerRecord.offset(), consumerRecord.partition(), consumerRecord.value()));

factory.setBatchErrorHandler(retryingBatchErrorHandler);

更新:请参阅 Artem 答案中的评论。
这就是如何将查找步骤包装到retryTemplate

LookedUpRequest lookedUpRequest = retryTemplate.execute(ctx -> {
   //Lookup step
   return lookup.process(request);
});

如果它失败,那么它将进一步为批处理错误处理程序抛出异常,其中RetryingBatchErrorHandler根据其策略重试批处理

查看它的 JavaDocs:

/**
 * A batch error handler that invokes the listener according to the supplied
 * {@link BackOff}. The consumer is paused/polled/resumed before each retry in order to
 * avoid a rebalance. If/when retries are exhausted, the provided
 * {@link ConsumerRecordRecoverer} is invoked for each record in the batch. If the
 * recoverer throws an exception, or the thread is interrupted while sleeping, seeks are
 * performed so that the batch will be redelivered on the next poll.
 *
 * @author Gary Russell
 * @since 2.3.7
 *
 */
public class RetryingBatchErrorHandler extends KafkaExceptionLogLevelAware
        implements ListenerInvokingBatchErrorHandler {

因此,没有重新平衡,因为消费者在两者之间暂停。

您可能需要考虑为您的查找部分缓存一些东西,这样您就不会在失败之前对那些您已经请求的记录施加压力。

您也可以考虑自己重试该查找。 请参阅RetryTemplate 但在这种情况下,您需要确保整个操作的时间不够长(请参阅max.poll.interval.ms )以使消费者离开其组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM