KAFKA-20804: Cut lock contention in metadata add()#22869
Closed
1230fahid wants to merge 1 commit into
Closed
Conversation
This commit switches add() to use ConcurrentHashMap.replace() for known topics, avoiding the instance lock on every send(). New or evicted topics fall back to a synchronized block so map insert and newTopics bookkeeping remain atomic with retainTopic(). retainTopic() also switches to conditional remove(topic, expireMs) to prevent evicting a topic concurrently refreshed by the add() fast path.
1230fahid
marked this pull request as draft
July 19, 2026 00:40
1230fahid
marked this pull request as ready for review
July 19, 2026 00:41
Author
|
This is actually duplicate work for: #22810, so will close this PR. |
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
ProducerMetadata.add()is called on everyKafkaProducer.send()viawaitOnMetadata. Because it was fullysynchronized, all producer threadscontended on the same instance lock even for topics already in the metadata
cache.
Changes
topicsmap switched toConcurrentHashMap.add(String)andadd(Collection)now take a lock-free fast path viaConcurrentHashMap.replace()for already-known topics, covering the hotpath on every
send(). Only truly new (or concurrently evicted) topicsfall back to a
synchronizedblock, keeping the map insert andnewTopicsbookkeeping atomic with
retainTopic().retainTopic()switches from a plaintopics.remove(topic)to aconditional
topics.remove(topic, expireMs). This prevents a race whereretainTopic()reads a stale (expired) expiry,add()concurrentlyrefreshes it via
replace(), andretainTopic()then removes the freshlyrefreshed entry. The conditional remove loses the CAS if the value has
changed, leaving the refreshed entry intact.
Testing
testAddKnownTopicFastPathDoesNotAddToNewTopics: verifies the fast pathdoes not re-add a known topic to
newTopicsor trigger an update.testBatchAddAllKnownTopicsReturnsEmptyOptional/testBatchAddWithNewTopicReturnsPresentOptional: verifiesadd(Collection)fast/slow path return values.
testConcurrentAddOfSameNewTopicIsIdempotent: 20 threads racing to add thesame new topic and asserts
newTopicscontains it exactly once.testRetainTopicConditionalRemovePreservesRefreshedTopic: 200 iterations ofconcurrent
add()andretainTopic()racing at the expiry boundary andasserts the topic always survives when
add()has refreshed its expiry.