Hi, In our project we recently upgraded from Flin...
# troubleshooting
l
Hi, In our project we recently upgraded from Flink 1.9 to 1.20. After the upgrade we saw a problem of two subtasks (in a task that reads several Kafka topics) out of four were not producing any records. We are reading three different topics each having two partitions. All of the partitions are producing messages continuously. This was fixed by changing slot count from four to two. I am glad to have this fixed but I was left wondering why. Moreover, what would be a reasonable "algorithm" of setting the slot count in general? Eg. "set slot count to partition count of the topic that has the most partitions".
a
Hi, Let me explain what happened in your case. In newer versions of Flink, the data source design uses a component called SplitEnumerator that's responsible for distributing splits (partitions) to slots (Readers). Looking at the code, the assignment process uses this key logic:
Copy code
static int getSplitOwner(TopicPartition tp, int numReaders) {
        int startIndex = ((tp.topic().hashCode() * 31) & 0x7FFFFFFF) % numReaders;

        // here, the assumption is that the id of Kafka partitions are always ascending
        // starting from 0, and therefore can be used directly as the offset clockwise from the
        // start index
        return (startIndex + tp.partition()) % numReaders;
    }
Your case hit an unlucky scenario where the hash of all your topic names modulo numReaders (4) resulted in the same starting index (likely 0). After that, partitions are distributed in a round-robin fashion from this starting point. So, with 4 slots: • All topics started distribution from slot 0 • Partition 0 of each topic went to slot 0 • Partition 1 of each topic went to slot 1 • Slots 2 and 3 got nothing This is why reducing to 2 slots fixed the issue - even with the same starting point, the round-robin distribution covered all slots evenly. You can retry using a prime number for slot count can help reduce the likelihood of uneven distribution! (2,3,5,7,
l
Thanks @Aly Ayman! I made a quick test. It would seems that it results in an even distribution using the actual topic names we used. Am I doing something wrong or could there be another explanation for this behaviour?
a
Let’s take a step back. How did you determine that the partitions were assigned to just two slots? Was it from the logs?
l
In Flink UI, Running Jobs -> job -> Kafka source task -> Sub tasks. There we saw 2/4 sub-tasks not producing records and watermarks not progressing
a
Check the flink job logs at startup You will find something like assigning partition number x to slot number y or task number y. This will help verify if the partitions are being correctly assigned to slots. Don't rely solely on the subtask UI view, as it can be misleading. I suspect that all the partitions you believe are unassigned to slots 3 and 4 are actually residing on the same Kafka broker Restart the kafka broker if applicable or reassign those partitions to different broker will help identifying the problem
Let me know how it goes 😊
l
Copy code
INFO org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase - Consumer subtask 2 will start reading the following 2 partitions from the committed group offsets in Kafka: [KafkaTopicPartition{topic='TOPIC_A', partition=1}, KafkaTopicPartition{topic='TOPIC_B', partition=1}]
INFO org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase - Consumer subtask 0 will start reading the following 1 partitions from the committed group offsets in Kafka: [KafkaTopicPartition{topic='TOPIC_C', partition=0}]
INFO org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase - Consumer subtask 1 will start reading the following 3 partitions from the committed group offsets in Kafka: [KafkaTopicPartition{topic='TOPIC_A', partition=0}, KafkaTopicPartition{topic='TOPIC_C', partition=1}, KafkaTopicPartition{topic='TOPIC_B', partition=0}]
INFO org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase - Consumer subtask 3 initially has no partitions to read from.
image.png
BTW There was an error in the screenshots I sent last week. I re-ran the above test and it matches the log lines above. So your original theory is right.