Pankraz76 commented on code in PR #21149:
URL: https://github.com/apache/kafka/pull/21149#discussion_r2664959761
##########
clients/src/main/java/org/apache/kafka/clients/producer/RoundRobinPartitioner.java:
##########
@@ -64,7 +64,11 @@ public int partition(String topic, Object key, byte[]
keyBytes, Object value, by
}
private int nextValue(String topic) {
- AtomicInteger counter = topicCounterMap.computeIfAbsent(topic, k ->
new AtomicInteger(0));
+ AtomicInteger counter = topicCounterMap.get(topic);
+ if(counter == null) {
+ counter = new AtomicInteger(0);
+ AtomicInteger counter = topicCounterMap.putIfAbsent(topic,
counter);
+ }
return counter.getAndIncrement();
}
Review Comment:
```suggestion
return topicCounterMap.computeIfAbsent(topic, k -> new
AtomicInteger(0)).getAndIncrement();
```
why not it doing this way? I would also assume that the lambda is only
executed if its actually going to get called in the case ob Absent:
---
This calls the lambda **only when the key is absent**. Let me break down how
`computeIfAbsent()` works:
## How `computeIfAbsent()` works:
1. **Checks if the key exists** in the map first
2. **Only if the key is absent**, it executes the lambda function to compute
the value
3. **If the key already exists**, it returns the existing value without
executing the lambda
---
**So to answer your question directly:** The lambda is called **only when
the key is absent**, not every time.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]