You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/06/05 00:54:00 UTC

[jira] [Commented] (KAFKA-6722) SensorAccess.getOrCreate should be more efficient

    [ https://issues.apache.org/jira/browse/KAFKA-6722?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16501095#comment-16501095 ] 

ASF GitHub Bot commented on KAFKA-6722:
---------------------------------------

wuqingjun closed pull request #5109: KAFKA-6722: SensorAccess.getOrCreate should be more efficient
URL: https://github.com/apache/kafka/pull/5109
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/clients/src/main/java/org/apache/kafka/common/record/FileRecords.java b/clients/src/main/java/org/apache/kafka/common/record/FileRecords.java
index e44d5d93be8..861537307bd 100644
--- a/clients/src/main/java/org/apache/kafka/common/record/FileRecords.java
+++ b/clients/src/main/java/org/apache/kafka/common/record/FileRecords.java
@@ -186,7 +186,7 @@ public void closeHandlers() throws IOException {
      */
     public boolean deleteIfExists() throws IOException {
         Utils.closeQuietly(channel, "FileChannel");
-        return Files.deleteIfExists(file.toPath());
+        return file.exists() ? file.delete() : false;
     }
 
     /**
diff --git a/core/src/main/scala/kafka/server/SensorAccess.scala b/core/src/main/scala/kafka/server/SensorAccess.scala
index 49e1fc00b8f..4a015c26393 100644
--- a/core/src/main/scala/kafka/server/SensorAccess.scala
+++ b/core/src/main/scala/kafka/server/SensorAccess.scala
@@ -31,19 +31,7 @@ class SensorAccess(lock: ReadWriteLock, metrics: Metrics) {
 
   def getOrCreate(sensorName: String, expirationTime: Long,
                   metricName: => MetricName, config: => Option[MetricConfig], measure: => MeasurableStat): Sensor = {
-    var sensor: Sensor = null
-
-    /* Acquire the read lock to fetch the sensor. It is safe to call getSensor from multiple threads.
-     * The read lock allows a thread to create a sensor in isolation. The thread creating the sensor
-     * will acquire the write lock and prevent the sensors from being read while they are being created.
-     * It should be sufficient to simply check if the sensor is null without acquiring a read lock but the
-     * sensor being present doesn't mean that it is fully initialized i.e. all the Metrics may not have been added.
-     * This read lock waits until the writer thread has released its lock i.e. fully initialized the sensor
-     * at which point it is safe to read
-     */
-    lock.readLock().lock()
-    try sensor = metrics.getSensor(sensorName)
-    finally lock.readLock().unlock()
+    var sensor: Sensor = metrics.getSensor(sensorName) 
 
     /* If the sensor is null, try to create it else return the existing sensor
      * The sensor can be null, hence the null checks
@@ -61,8 +49,9 @@ class SensorAccess(lock: ReadWriteLock, metrics: Metrics) {
         // ensure that we initialise `ClientSensors` with non-null parameters.
         sensor = metrics.getSensor(sensorName)
         if (sensor == null) {
-          sensor = metrics.sensor(sensorName, config.orNull, expirationTime)
-          sensor.add(metricName, measure)
+          var tempSensor: Sensor = metrics.sensor(sensorName, config.orNull, expirationTime)
+          tempSensor.add(metricName, measure)
+          sensor = tempSensor
         }
       } finally {
         lock.writeLock().unlock()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> SensorAccess.getOrCreate should be more efficient
> -------------------------------------------------
>
>                 Key: KAFKA-6722
>                 URL: https://issues.apache.org/jira/browse/KAFKA-6722
>             Project: Kafka
>          Issue Type: Improvement
>            Reporter: wade wu
>            Priority: Major
>
> In this source code: [https://github.com/Microsoft/kafka/blob/azpubsub-release-0-10-2/core/src/main/scala/kafka/server/SensorAccess.scala]
> The lock/unlock of read lock in getOrCreate() is not necessary, or it should be refactored. For each request from Producer, this read lock lock/unlock is called and lock/unlock, it is costing the time. 
> By using a temp variable, we can completely get rid of the read lock. 
> var sensor: Sensor = metrics.getSensor(sensorName)
> if (sensor == null) {
> lock.writeLock().lock()
> try
> {  var temp = new sensor(); tmp.add(metrics); sensor = temp; }
> catch()
> {...}
>  
> lock.writelock.unlock();
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)