Uploaded image for project: 'Kafka'
  1. Kafka
  2. KAFKA-8877

Race condition on partition counter

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.2.1
    • None
    • producer
    • None

    Description

      In the method:

      org.apache.kafka.clients.producer.internals.DefaultPartitioner#nextValue

      private int nextValue(String topic) {
          AtomicInteger counter = topicCounterMap.get(topic);
          if (null == counter) {
              counter = new AtomicInteger(ThreadLocalRandom.current().nextInt());
              AtomicInteger currentCounter = topicCounterMap.putIfAbsent(topic, counter);
              if (currentCounter != null) {
                  counter = currentCounter;
              }
          }
          return counter.getAndIncrement();
      }
      

      the counter might be created multiple times instead of once.

      I propose to replace it with something like topicCounterMap.compute(topic, _ -> ... (init the counter once per topic))  ** 

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            olkuznsmith Oleg Kuznetsov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: