eeriee opened a new pull request, #22810: URL: https://github.com/apache/kafka/pull/22810
## Summary add() is called on every send() via KafkaProducer.waitOnMetadata, so the synchronized method contends with the Sender thread's update()/ retainTopic() calls on every produced record even though the vast majority of calls are just refreshing an already-tracked topic's expiry. Switch the topics map to a ConcurrentHashMap and refresh known topics via a lock-free replace(), which only writes if the topic is still present. For a topic seen for the first time (or one concurrently evicted by retainTopic()), fall back to a synchronized block so the map insert and newTopics bookkeeping happen atomically with retainTopic() - otherwise retainTopic() could expire-and-remove the topic in the gap between the two, leaving it recorded in newTopics without the corresponding map entry, or silently re-inserting an evicted topic without the newTopics bookkeeping needed to trigger an immediate refresh. retainTopic() switches to a conditional remove(topic, expireMs) so a concurrent refresh of an existing topic in add() can't be undone by an eviction decision based on the expiry value read just before the refresh landed. ## Testing Adds ProducerMetadataTest#testRetainTopic for direct coverage of retainTopic()'s branches, and a JMH benchmark (ProducerMetadataAddBenchmark) comparing add() against a baseline mirroring the prior fully-synchronized implementation. With 8 threads concurrently refreshing a shared pool of 200 already-tracked topics: Benchmark Mode Cnt Score Error Units ProducerMetadataAddBenchmark.addExistingTopicFullySynchronized thrpt 5 6217.287 ± 1279.054 ops/ms ProducerMetadataAddBenchmark.addExistingTopicLockFreeHotPath thrpt 5 27232.344 ± 5404.836 ops/ms ~4.4x higher throughput under this contention pattern. -- 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]
