Skip to content

Commit 282eef9

Browse files
authored
MINOR: Add DCL to improve performance (#22098)
Synchronized method will cause the process lock here every time, it would be better to lock when we actually need to create the meter, otherwise we can just return the existed value. Reviewers: Ken Huang <s7133700@gmail.com>, Chia-Ping Tsai <chia7712@gmail.com>
1 parent f1f0d97 commit 282eef9

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

  • clients/src/main/java/org/apache/kafka/common/metrics
  • server-common/src/main/java/org/apache/kafka/server/util/timer

clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public Sensor sensor(String name, Sensor.RecordingLevel recordingLevel, Sensor..
369369
* @param parents The parent sensors
370370
* @return The sensor that is created
371371
*/
372-
public synchronized Sensor sensor(String name, MetricConfig config, Sensor... parents) {
372+
public Sensor sensor(String name, MetricConfig config, Sensor... parents) {
373373
return this.sensor(name, config, Sensor.RecordingLevel.INFO, parents);
374374
}
375375

@@ -383,7 +383,7 @@ public synchronized Sensor sensor(String name, MetricConfig config, Sensor... pa
383383
* @param parents The parent sensors
384384
* @return The sensor that is created
385385
*/
386-
public synchronized Sensor sensor(String name, MetricConfig config, Sensor.RecordingLevel recordingLevel, Sensor... parents) {
386+
public Sensor sensor(String name, MetricConfig config, Sensor.RecordingLevel recordingLevel, Sensor... parents) {
387387
return sensor(name, config, Long.MAX_VALUE, recordingLevel, parents);
388388
}
389389

@@ -398,18 +398,23 @@ public synchronized Sensor sensor(String name, MetricConfig config, Sensor.Recor
398398
* @param recordingLevel The recording level.
399399
* @return The sensor that is created
400400
*/
401-
public synchronized Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel recordingLevel, Sensor... parents) {
401+
public Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel recordingLevel, Sensor... parents) {
402402
Sensor s = getSensor(name);
403403
if (s == null) {
404-
s = new Sensor(this, name, parents, config == null ? this.config : config, time, inactiveSensorExpirationTimeSeconds, recordingLevel);
405-
this.sensors.put(name, s);
406-
if (parents != null) {
407-
for (Sensor parent : parents) {
408-
List<Sensor> children = childrenSensors.computeIfAbsent(parent, k -> new ArrayList<>());
409-
children.add(s);
404+
synchronized (this) {
405+
s = getSensor(name);
406+
if (s == null) {
407+
s = new Sensor(this, name, parents, config == null ? this.config : config, time, inactiveSensorExpirationTimeSeconds, recordingLevel);
408+
this.sensors.put(name, s);
409+
if (parents != null) {
410+
for (Sensor parent : parents) {
411+
List<Sensor> children = childrenSensors.computeIfAbsent(parent, k -> new ArrayList<>());
412+
children.add(s);
413+
}
414+
}
415+
log.trace("Added sensor with name {}", name);
410416
}
411417
}
412-
log.trace("Added sensor with name {}", name);
413418
}
414419
return s;
415420
}
@@ -424,7 +429,7 @@ public synchronized Sensor sensor(String name, MetricConfig config, long inactiv
424429
* @param parents The parent sensors
425430
* @return The sensor that is created
426431
*/
427-
public synchronized Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor... parents) {
432+
public Sensor sensor(String name, MetricConfig config, long inactiveSensorExpirationTimeSeconds, Sensor... parents) {
428433
return this.sensor(name, config, inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel.INFO, parents);
429434
}
430435

server-common/src/main/java/org/apache/kafka/server/util/timer/TimingWheel.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,19 @@ public class TimingWheel {
128128
}
129129
}
130130

131-
private synchronized void addOverflowWheel() {
131+
private void addOverflowWheel() {
132132
if (overflowWheel == null) {
133-
overflowWheel = new TimingWheel(
134-
interval,
135-
wheelSize,
136-
currentTimeMs,
137-
taskCounter,
138-
queue
139-
);
133+
synchronized (this) {
134+
if (overflowWheel == null) {
135+
overflowWheel = new TimingWheel(
136+
interval,
137+
wheelSize,
138+
currentTimeMs,
139+
taskCounter,
140+
queue
141+
);
142+
}
143+
}
140144
}
141145
}
142146

0 commit comments

Comments
 (0)