-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-20804: Reduce lock contention in ProducerMetadata#add #22810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eeriee
wants to merge
2
commits into
apache:trunk
Choose a base branch
from
eeriee:producer-metadata-sync
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
88 changes: 88 additions & 0 deletions
88
jmh-benchmarks/src/main/java/org/apache/kafka/jmh/producer/ProducerMetadataAddBenchmark.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.kafka.jmh.producer; | ||
|
|
||
| import org.apache.kafka.clients.producer.internals.ProducerMetadata; | ||
| import org.apache.kafka.common.internals.ClusterResourceListeners; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.apache.kafka.common.utils.internals.LogContext; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Benchmarks {@link ProducerMetadata#add} refreshing an already-tracked topic's expiry. Every | ||
| * {@code send()} call does this refresh, so with many application threads sharing one producer, | ||
| * it's on the hot path and contends across threads for a topic that's virtually always already | ||
| * tracked. This benchmark simulates that: many threads concurrently refreshing a shared pool of | ||
| * already-registered topics. | ||
| * | ||
| * To measure the effect of a change to {@code add()}, run this benchmark before and after the | ||
| * change and compare the throughput, rather than hand-mirroring the old implementation here | ||
| * where it could drift out of sync with reality. | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| @Fork(value = 1) | ||
| @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| @Threads(8) | ||
| public class ProducerMetadataAddBenchmark { | ||
|
|
||
| private static final int TOPIC_COUNT = 200; | ||
| private static final long METADATA_IDLE_MS = TimeUnit.MINUTES.toMillis(5); | ||
|
|
||
| private String[] topics; | ||
| private ProducerMetadata metadata; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| topics = new String[TOPIC_COUNT]; | ||
| for (int i = 0; i < TOPIC_COUNT; i++) { | ||
| topics[i] = "topic-" + i; | ||
| } | ||
|
|
||
| metadata = new ProducerMetadata(100L, 1000L, TimeUnit.MINUTES.toMillis(5), METADATA_IDLE_MS, | ||
| new LogContext(), new ClusterResourceListeners(), Time.SYSTEM); | ||
|
|
||
| long nowMs = Time.SYSTEM.milliseconds(); | ||
| for (String topic : topics) { | ||
| metadata.add(topic, nowMs); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void addExistingTopic() { | ||
| String topic = topics[ThreadLocalRandom.current().nextInt(TOPIC_COUNT)]; | ||
| metadata.add(topic, Time.SYSTEM.milliseconds()); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this check is because the overloaded
add(Collection<String> topics, long nowMs)might have mutatedtopics?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the same old behavior moving to a sync block, yeah the overloaded add or the same add method can be called to update topic concurrently.