leventov commented on a change in pull request #7938: locking in Theta sketch 
buffer aggregator
URL: https://github.com/apache/incubator-druid/pull/7938#discussion_r317986917
 
 

 ##########
 File path: 
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/theta/SketchBufferAggregator.java
 ##########
 @@ -54,32 +59,58 @@ public void init(ByteBuffer buf, int position)
     createNewUnion(buf, position, false);
   }
 
+  /**
+   * This method uses locks because it can be used during indexing,
+   * and Druid can call aggregate() and get() concurrently
+   * https://github.com/apache/incubator-druid/pull/3956
+   */
   @Override
   public void aggregate(ByteBuffer buf, int position)
   {
     Object update = selector.getObject();
     if (update == null) {
       return;
     }
-
-    Union union = getOrCreateUnion(buf, position);
-    SketchAggregator.updateUnion(union, update);
+    final Lock lock = 
stripedLock.getAt(StripedLockHelper.lockIndex(position)).writeLock();
+    lock.lock();
+    try {
+      Union union = getOrCreateUnion(buf, position);
+      SketchAggregator.updateUnion(union, update);
+    }
+    finally {
+      lock.unlock();
+    }
   }
 
+  /**
+   * This method uses locks because it can be used during indexing,
+   * and Druid can call aggregate() and get() concurrently
+   * https://github.com/apache/incubator-druid/pull/3956
+   * The returned sketch is a separate instance of Sketch
+   * representing the current state of the aggregation, and is not affected by 
consequent
+   * aggregate() calls
+   */
   @Override
   public Object get(ByteBuffer buf, int position)
   {
-    Int2ObjectMap<Union> unionMap = unions.get(buf);
-    Union union = unionMap != null ? unionMap.get(position) : null;
-    if (union == null) {
-      return SketchHolder.EMPTY;
+    final Lock lock = 
stripedLock.getAt(StripedLockHelper.lockIndex(position)).readLock();
+    lock.lock();
+    try {
+      Int2ObjectMap<Union> unionMap = unions.get(buf);
+      Union union = unionMap != null ? unionMap.get(position) : null;
+      if (union == null) {
+        return SketchHolder.EMPTY;
+      }
+      //the code below returns SetOp.getResult(true, null)
+      //"true" returns an ordered sketch but slower to compute than unordered 
sketch.
+      //however, advantage of ordered sketch is that they are faster to 
"union" later
+      //given that results from the aggregator will be combined further, it is 
better
+      //to return the ordered sketch here
+      return SketchHolder.of(union.getResult(true, null));
+    }
+    finally {
+      lock.unlock();
     }
-    //in the code below, I am returning SetOp.getResult(true, null)
-    //"true" returns an ordered sketch but slower to compute than unordered 
sketch.
-    //however, advantage of ordered sketch is that they are faster to "union" 
later
-    //given that results from the aggregator will be combined further, it is 
better
-    //to return the ordered sketch here
-    return SketchHolder.of(union.getResult(true, null));
   }
 
   private Union getOrCreateUnion(ByteBuffer buf, int position)
 
 Review comment:
   Please annotate `@GuardedBy("stripedLock")`. Also, annotate 
`createNewUnion()` method.

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to