You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ju...@apache.org on 2015/10/16 18:47:13 UTC

kafka git commit: KAFKA-2419 - Fix to prevent background thread from getting created when not required

Repository: kafka
Updated Branches:
  refs/heads/trunk 7efa12705 -> aa66b42da


KAFKA-2419 - Fix to prevent background thread from getting created when not required

See here for more discussion: https://issues.apache.org/jira/browse/KAFKA-2419
Basically, the fix involves adding a param to Metrics to indicate if it is capable of metric cleanup or not.

Author: Aditya Auradkar <aa...@aauradka-mn1.linkedin.biz>

Reviewers: Jun Rao <ju...@gmail.com>

Closes #323 from auradkar/KAFKA-2419-fix


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/aa66b42d
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/aa66b42d
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/aa66b42d

Branch: refs/heads/trunk
Commit: aa66b42daca0791840da3c77d47c96cf98f19ef2
Parents: 7efa127
Author: Aditya Auradkar <aa...@aauradka-mn1.linkedin.biz>
Authored: Fri Oct 16 09:47:04 2015 -0700
Committer: Jun Rao <ju...@gmail.com>
Committed: Fri Oct 16 09:47:04 2015 -0700

----------------------------------------------------------------------
 .../apache/kafka/common/metrics/Metrics.java    | 51 ++++++++++++++------
 .../kafka/common/metrics/MetricsTest.java       |  2 +-
 .../main/scala/kafka/server/KafkaServer.scala   |  2 +-
 3 files changed, 39 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/aa66b42d/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java
index be744ab..6281640 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java
@@ -66,6 +66,7 @@ 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());
@@ -73,6 +74,7 @@ public class Metrics implements Closeable {
 
     /**
      * 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);
@@ -80,7 +82,7 @@ public class Metrics implements Closeable {
 
     /**
      * 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) {
@@ -88,12 +90,24 @@ public class Metrics implements Closeable {
     }
 
     /**
-     * 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<>();
@@ -102,13 +116,20 @@ public class Metrics implements Closeable {
         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;
+        }
     }
 
     /**
@@ -308,11 +329,13 @@ public class Metrics implements Closeable {
      */
     @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)

http://git-wip-us.apache.org/repos/asf/kafka/blob/aa66b42d/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
----------------------------------------------------------------------
diff --git a/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java b/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
index 9058387..bd84ebe 100644
--- a/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
@@ -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);
     }
 
     @After

http://git-wip-us.apache.org/repos/asf/kafka/blob/aa66b42d/core/src/main/scala/kafka/server/KafkaServer.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/kafka/server/KafkaServer.scala b/core/src/main/scala/kafka/server/KafkaServer.scala
index e2c8f48..99a3c12 100755
--- a/core/src/main/scala/kafka/server/KafkaServer.scala
+++ b/core/src/main/scala/kafka/server/KafkaServer.scala
@@ -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)