Skip to content

Reduce lock contention in ProducerMetadata#add#22810

Open
eeriee wants to merge 1 commit into
apache:trunkfrom
eeriee:producer-metadata-sync
Open

Reduce lock contention in ProducerMetadata#add#22810
eeriee wants to merge 1 commit into
apache:trunkfrom
eeriee:producer-metadata-sync

Conversation

@eeriee

@eeriee eeriee commented Jul 10, 2026

Copy link
Copy Markdown

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant