Skip to content
Closed
Show file tree
Hide file tree
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
51 changes: 37 additions & 14 deletions clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,48 @@ public class Metrics implements Closeable {

/**
* Create a metrics repository with no metric reporters and default configuration.
* Expiration of Sensors is disabled.
*/
public Metrics() {
this(new MetricConfig());
}

/**
* Create a metrics repository with no metric reporters and default configuration.
* Expiration of Sensors is disabled.
*/
public Metrics(Time time) {
this(new MetricConfig(), new ArrayList<MetricsReporter>(0), time);
}

/**
* Create a metrics repository with no reporters and the given default config. This config will be used for any
* metric that doesn't override its own config.
* metric that doesn't override its own config. Expiration of Sensors is disabled.
* @param defaultConfig The default config to use for all metrics that don't override their config
*/
public Metrics(MetricConfig defaultConfig) {
this(defaultConfig, new ArrayList<MetricsReporter>(0), new SystemTime());
}

/**
* Create a metrics repository with a default config and the given metric reporters
* Create a metrics repository with a default config and the given metric reporters.
* Expiration of Sensors is disabled.
* @param defaultConfig The default config
* @param reporters The metrics reporters
* @param time The time instance to use with the metrics
*/
public Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, Time time) {
this(defaultConfig, reporters, time, false);
}

/**
* Create a metrics repository with a default config, given metric reporters and the ability to expire eligible sensors
* @param defaultConfig The default config
* @param reporters The metrics reporters
* @param time The time instance to use with the metrics
* @param enableExpiration true if the metrics instance can garbage collect inactive sensors, false otherwise
*/
public Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, Time time, boolean enableExpiration) {
this.config = defaultConfig;
this.sensors = new CopyOnWriteMap<>();
this.metrics = new CopyOnWriteMap<>();
Expand All @@ -102,13 +116,20 @@ public Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, Time
this.time = time;
for (MetricsReporter reporter : reporters)
reporter.init(new ArrayList<KafkaMetric>());
this.metricsScheduler = new ScheduledThreadPoolExecutor(1);
// Creating a daemon thread to not block shutdown
this.metricsScheduler.setThreadFactory(new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return Utils.newThread("SensorExpiryThread", runnable, true);
}
});

// Create the ThreadPoolExecutor only if expiration of Sensors is enabled.
if (enableExpiration) {
this.metricsScheduler = new ScheduledThreadPoolExecutor(1);
// Creating a daemon thread to not block shutdown
this.metricsScheduler.setThreadFactory(new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return Utils.newThread("SensorExpiryThread", runnable, true);
}
});
this.metricsScheduler.scheduleAtFixedRate(new ExpireSensorTask(), 30, 30, TimeUnit.SECONDS);
} else {
this.metricsScheduler = null;
}
}

/**
Expand Down Expand Up @@ -308,11 +329,13 @@ Map<Sensor, List<Sensor>> childrenSensors() {
*/
@Override
public void close() {
this.metricsScheduler.shutdown();
try {
this.metricsScheduler.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
// ignore and continue shutdown
if (this.metricsScheduler != null) {
this.metricsScheduler.shutdown();
try {
this.metricsScheduler.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
// ignore and continue shutdown
}
}

for (MetricsReporter reporter : this.reporters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class MetricsTest {

@Before
public void setup() {
this.metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), time);
this.metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), time, true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@auradkar Just a tip (no need to change, up to you): you can use Collections.singletonList() instead of Arrays.asList when dealing with only a single element collection. ;-)

}

@After
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/kafka/server/KafkaServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class KafkaServer(val config: KafkaConfig, time: Time = SystemTime, threadNamePr

val canStartup = isStartingUp.compareAndSet(false, true)
if (canStartup) {
metrics = new Metrics(metricConfig, reporters, kafkaMetricsTime)
metrics = new Metrics(metricConfig, reporters, kafkaMetricsTime, true)

brokerState.newState(Starting)

Expand Down