最近在做kafka性能测试,发现随着partition的数量增加,producer的吞吐量增大,但当partition增长到一定数量时吞吐量又有所下降。这在我看来是十分奇怪的,producer的吞吐量为什么会和partition数量相关,producer的吞吐量不应该只和producer有关吗。为此我翻看了下源码,找到了为何partition数量增大,吞吐量会增大的原因。
for (Node node : nodes) {
int size = 0;
List<PartitionInfo> parts = cluster.partitionsForNode(node.id());
List<ProducerBatch> ready = new ArrayList<>();
/* to make starvation less likely this loop doesn't start at 0 */
int start = drainIndex = drainIndex % parts.size();
do {
PartitionInfo part = parts.get(drainIndex);
TopicPartition tp = new TopicPartition(part.topic(), part.partition());
// Only proceed if the partition has no in-flight batches.
if (!isMuted(tp, now)) {
Deque<ProducerBatch> deque = getDeque(tp);
if (deque != null) {
synchronized (deque) {