KAFKA - What is reason for getting ProducerFencedException during producer.send

后端 未结 4 594
执笔经年
执笔经年 2021-01-13 19:56

Trying to load around 50K messages into KAFKA topic. In the beginning of few runs getting below exception but not all the time.

org.apache.kafka.common.Kafka         


        
4条回答
  •  离开以前
    2021-01-13 20:55

    I write a unit test to reproduce this, from this piece of Java code, you can easily understand how this happen by two same tansactional id.

      @Test
      public void SendOffset_TwoProducerDuplicateTrxId_ThrowException() {
        // create two producer with same transactional id
        Producer producer1 = KafkaBuilder.buildProducer(trxId, servers);
        Producer producer2 = KafkaBuilder.buildProducer(trxId, servers);
    
        offsetMap.put(new TopicPartition(topic, 0), new OffsetAndMetadata(1000));
    
        // initial and start two transactions
        sendOffsetBegin(producer1);
        sendOffsetBegin(producer2);
    
        try {
          // when commit first transaction it expected to throw exception
          sendOffsetEnd(producer1);
    
          // it expects not run here
          Assert.assertTrue(false);
        } catch (Throwable t) {
          // it expects to catch the exception
          Assert.assertTrue(t instanceof ProducerFencedException);
        }
      }
    
      private void sendOffsetBegin(Producer producer) {
        producer.initTransactions();
        producer.beginTransaction();
        producer.sendOffsetsToTransaction(offsetMap, consumerGroup);
      }
    
      private void sendOffsetEnd(Producer producer) {
        producer.commitTransaction();
      }
    

提交回复
热议问题