如何使应用程序从由请求超时引起的reactor kafka的retrieablecommitfailedexception中自我恢复?

muk1a3rh  于 2021-06-04  发布在  Kafka
关注(0)|答案(0)|浏览(440)

我有一个Kafka处理器是这样定义的。

import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.common.serialization.StringDeserializer
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
import reactor.core.publisher.Mono
import reactor.core.scheduler.Schedulers
import reactor.kafka.receiver.KafkaReceiver
import reactor.kafka.receiver.ReceiverOptions
import reactor.kafka.receiver.ReceiverRecord
import reactor.kotlin.core.publisher.toMono
import reactor.util.retry.Retry
import java.time.Duration
import java.util.*

@Component
class KafkaProcessor {
    private val logger = LoggerFactory.getLogger(javaClass)

    private val consumerProps = hashMapOf(
        ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::javaClass,
        ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to StringDeserializer::javaClass,
        ConsumerConfig.GROUP_ID_CONFIG to "groupId",
        ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest",
        ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to "localhost:9092"
    )

    private val receiverOptions = ReceiverOptions.create<String, String>(consumerProps)
        .subscription(Collections.singleton("some-topic"))
        .commitInterval(Duration.ofSeconds(1))
        .commitBatchSize(1000)
        .maxCommitAttempts(1)

    private val kafkaReceiver: KafkaReceiver<String, String> = KafkaReceiver.create(receiverOptions)

    @Bean
    fun processKafkaMessages(): Unit {
        kafkaReceiver.receive()
            .groupBy { m -> m.receiverOffset().topicPartition() }
            .flatMap { partitionFlux ->
                partitionFlux.publishOn(Schedulers.elastic())
                    .concatMap { receiverRecord ->
                        processRecord(receiverRecord)
                            .map { it.receiverOffset().acknowledge() }
                    }
            }
            .retryWhen(
                Retry.backoff(3, Duration.ofSeconds(1))
                    .maxBackoff(Duration.ofSeconds(3))
                    .doBeforeRetry { rs ->
                        logger.warn("Retrying: ${rs.totalRetries() + 1}/3 due to ${rs.failure()}")
                    }
                    .onRetryExhaustedThrow { _, u ->
                        logger.error("All ${u.totalRetries() + 1} attempts failed with the last exception: ${u.failure()}")
                        u.failure()
                    }
            )
            .subscribe()
    }

    private fun processRecord(record: ReceiverRecord<String, String>): Mono<ReceiverRecord<String, String>> {
        return record.toMono()
    }
}

有时候,我会犯这个错误。

org.apache.kafka.clients.consumer.RetriableCommitFailedException: Offset commit failed with a retriable exception. You should retry committing the latest consumed offsets.
Caused by: org.apache.kafka.common.errors.TimeoutException: The request timed out.

第一次重试是这样的。

Retrying: 1/3 due to org.apache.kafka.clients.consumer.RetriableCommitFailedException: Offset commit failed with a retriable exception. You should retry committing the latest consumed offsets

第二个和第三个看起来像这样。

Retrying: 2/3 due to reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable

Retrying: 3/3 due to reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable

当所有3次重试都结束后,消息如下所示。

All 4 attempts failed with the last exception: reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable

当我得到那个错误时,我需要重新启动应用程序,以便重新连接到kafka代理并提交记录。
我知道 maxCommitAttempts1 意味着一旦它撞到了 RetriableCommitFailedException ,它将不再重试。我以为 retryWhen 我在最后加上的条款 processKafkaMessages() 函数将执行此操作,以便管道可以自行恢复。
我定的原因是 maxCommitAttempts 因为它没有这里讨论的带退避的重试,默认的100次最大提交尝试在10毫秒内完成。
问题是,对于自动提交,我应该如何正确地使用backoff进行重试?有没有可能使用 EmbeddedKafka ?
语言: Kotlin React堆Kafka图书馆: io.projectreactor.kafka:reactor-kafka:1.2.2.RELEASE

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题