1

在某些情况下(非常罕见,但确实有)我收到了重复文件,即使所有内容都配置为具有高耐用性并且我们只使用一次配置。

请检查下面导致此问题的应用程序上下文和测试场景。

卡夫卡集群设置

3 个 Kafka 代理(host1 上 1 个,host2 上 2 个,host3 上 3 个)

3 个 Zookeeper 实例(host1 上 1 个,host2 上 2 个,host3 上 3 个)

卡夫卡配置


    broker.id=1,2,3

    num.network.threads=2

    num.io.threads=8

    socket.send.buffer.bytes=102400

    socket.receive.buffer.bytes=102400

    socket.request.max.bytes=104857600

    log.dirs=/home/kafka/logs/kafka

    min.insync.replicas=3

    transaction.state.log.min.isr=3

    default.replication.factor=3

    log.retention.minutes=600

    log.segment.bytes=1073741824

    log.retention.check.interval.ms=300000

    zookeeper.connect=host1:2181,host2:2181,host3:2181

    zookeeper.connection.timeout.ms=6000

    group.initial.rebalance.delay.ms=1000

    log.message.timestamp.type=LogAppendTime

    delete.topic.enable=true

    auto.create.topics.enable=false

    unclean.leader.election.enable=false

ZooKeeper 配置


    tickTime=2000

    dataDir=/home/kafka/logs/zk

    clientPort=2181

    maxClientCnxns=0

    initLimit=5

    syncLimit=2

    server.1=host1:2888:3888

    server.2=host2:2888:3888

    server.3=host3:2888:3888

    autopurge.snapRetainCount=3

    autopurge.purgeInterval=24

Kafka 内部主题描述

Topic:__transaction_state       PartitionCount:50       ReplicationFactor:3     Configs:segment.bytes=104857600,unclean.leader.election.enable=false,compression.type=uncompressed,cleanup.policy=compact,min.insync.replicas=3
      Topic: __transaction_state     Partition: 0   Leader: 1       Replicas: 3,2,1 Isr: 1,2,3
​
Topic:__consumer_offsets       PartitionCount:50       ReplicationFactor:3     Configs:segment.bytes=104857600,unclean.leader.election.enable=false,min.insync.replicas=3,cleanup.policy=compact,compression.type=producer
      Topic: __consumer_offsets       Partition: 0   Leader: 1       Replicas: 3,2,1 Isr: 1,2,3

应用主题


    Topic input-event
    Topic:input-event     PartitionCount:3       ReplicationFactor:3   Configs:retention.ms=28800001,unclean.leader.election.enable=false,min.insync.replicas=3,message.timestamp.difference.max.ms=28800000
          Topic: input-event     Partition: 0   Leader: 1       Replicas: 1,2,3 Isr: 1,2,3
          Topic: input-event     Partition: 1   Leader: 2       Replicas: 2,3,1 Isr: 1,2,3
          Topic: input-event     Partition: 2   Leader: 3       Replicas: 3,1,2 Isr: 1,2,3

    Topic output-event
    Topic:output-event       PartitionCount:3       ReplicationFactor:3   Configs:retention.ms=28800001,unclean.leader.election.enable=false,min.insync.replicas=3,message.timestamp.difference.max.ms=28800000
          Topic: output-event       Partition: 0   Leader: 2       Replicas: 2,3,1 Isr: 1,2,3
          Topic: output-event       Partition: 1   Leader: 3       Replicas: 3,1,2 Isr: 1,2,3
          Topic: output-event       Partition: 2   Leader: 1       Replicas: 1,2,3 Isr: 1,2,3

应用程序消费者属性


    o.a.k.clients.consumer.ConsumerConfig : ConsumerConfig values:
                  auto.commit.interval.ms = 5000
                  auto.offset.reset = earliest
                  bootstrap.servers = [host1:9092, host2:9092, host3:9092]
                  check.crcs = true
                  client.id =
                  connections.max.idle.ms = 540000
                  default.api.timeout.ms = 60000
                  enable.auto.commit = false
                  exclude.internal.topics = true
                  fetch.max.bytes = 134217728
                  fetch.max.wait.ms = 500
                  fetch.min.bytes = 1
                  group.id = groupId
                  heartbeat.interval.ms = 3000
                  interceptor.classes = []
                  internal.leave.group.on.close = true
                  isolation.level = read_committed
                  key.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer
                  max.partition.fetch.bytes = 134217728
                  max.poll.interval.ms = 300000
                  max.poll.records = 1
                  metadata.max.age.ms = 300000
                  metric.reporters = []
                  metrics.num.samples = 2
                  metrics.recording.level = INFO
                  metrics.sample.window.ms = 30000
                  partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
                  receive.buffer.bytes = 65536
                  reconnect.backoff.max.ms = 1000
                  reconnect.backoff.ms = 1000
                  request.timeout.ms = 30000
                  retry.backoff.ms = 1000
                  sasl.client.callback.handler.class = null
                  sasl.jaas.config = null
                  sasl.kerberos.kinit.cmd = /usr/bin/kinit
                  sasl.kerberos.min.time.before.relogin = 60000
                  sasl.kerberos.service.name = null
                  sasl.kerberos.ticket.renew.jitter = 0.05
                  sasl.kerberos.ticket.renew.window.factor = 0.8
                  sasl.login.callback.handler.class = null
                  sasl.login.class = null
                  sasl.login.refresh.buffer.seconds = 300
                  sasl.login.refresh.min.period.seconds = 60
                  sasl.login.refresh.window.factor = 0.8
                  sasl.login.refresh.window.jitter = 0.05
                  sasl.mechanism = GSSAPI
                  security.protocol = PLAINTEXT
                  send.buffer.bytes = 131072
                  session.timeout.ms = 10000
                  ssl.cipher.suites = null
                  ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
                  ssl.endpoint.identification.algorithm = https
                  ssl.key.password = null
                  ssl.keymanager.algorithm = SunX509
                  ssl.keystore.location = null
                  ssl.keystore.password = null
                  ssl.keystore.type = JKS
                  ssl.protocol = TLS
                  ssl.provider = null
                  ssl.secure.random.implementation = null
                  ssl.trustmanager.algorithm = PKIX
                  ssl.truststore.location = null
                  ssl.truststore.password = null
                  ssl.truststore.type = JKS
                  value.deserializer = class org.apache.kafka.common.serialization.ByteArrayDeserializer

应用程序生产者属性

    bootstrapServers = "host1, host2, host3"
    transactionIdPrefix = "my-producer-"${instance}"
    "enable.idempotence" = "true"
    "acks" = "all"
    "retries" = "2147483647"
    "transaction.timeout.ms" = "10000"
    "max.in.flight.requests.per.connection" = "1"
    "reconnect.backoff.max.ms" = "1000"
    "reconnect.backoff.ms" = "1000"
    "retry.backoff.ms" = "1000"

应用程序处理提交

使用 KafkaTransactionManager,我们启动事务,使用 KafkaTemplate 将消息写入输出主题,并发送消费者偏移量(spring-kafka 2.2.8.RELEASE)。

测试预期/实际

  • 向输入主题写入 32,000 条消息

  • 启动 3 个应用实例

  • 开始一一处理消息(max.poll.records = 1)

  • 在处理过程中,并行发送 SIGKILL (kill -9) 到 host1 和 host2 Kafka Brokers 50 次。

  • 等待 60 秒

  • 在处理过程中,向 host1 和 host3 Kafka Brokers 并行发送 SIGKILL (kill -9) 50 次。

  • 等待 60 秒

  • 在处理过程中,向 host2 和 host3 Kafka Brokers 并行发送 SIGKILL (kill -9) 50 次。

期望有 32,000 条消息到输出主题,但是,有时我们实际上最终会得到重复(至少一条)。

有时我们最终会收到 32,000 条消息,并且一切正常。

4

1 回答 1

0

该问题与在 topic.partition 级别未正确设置事务 id 并且我们有两个生产者为同一分区写入相同消息两次这一事实有关。

这是一本好书: https ://tgrez.github.io/posts/2019-04-13-kafka-transactions.html

于 2020-03-02T15:48:29.167 回答