Reduce lock contention in ProducerMetadata#add#22810
Open
eeriee wants to merge 1 commit into
Open
Conversation
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. The batch add(Collection<String>, long) overload is unaffected: it stays fully synchronized, remains mutually exclusive with retainTopic(), and only races benignly with the lock-free path (a last-write-wins refresh of the same timestamp semantics). It's used only by KafkaProducer#sendOffsetsToTransaction, not the per-record send() path, so it doesn't need the same treatment. 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.