Skip to content
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

GH-2943: Fix KT.clusterId() for concurrency #2944

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;

import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -118,6 +120,8 @@ public class KafkaTemplate<K, V> implements KafkaOperations<K, V>, ApplicationCo

private final Map<String, String> micrometerTags = new HashMap<>();

private final Lock clusterIdLock = new ReentrantLock();

private String beanName = "kafkaTemplate";

private ApplicationContext applicationContext;
Expand Down Expand Up @@ -501,7 +505,15 @@ else if (this.micrometerEnabled) {
@Nullable
private String clusterId() {
if (this.kafkaAdmin != null && this.clusterId == null) {
this.clusterId = this.kafkaAdmin.clusterId();
this.clusterIdLock.lock();
try {
if (this.clusterId == null) {
this.clusterId = this.kafkaAdmin.clusterId();
}
}
finally {
this.clusterIdLock.unlock();
}
}
return this.clusterId;
}
Expand Down