gianm commented on a change in pull request #11115:
URL: https://github.com/apache/druid/pull/11115#discussion_r615069868



##########
File path: 
extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/HllSketchMergeBufferAggregatorHelper.java
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.query.aggregation.datasketches.hll;
+
+import org.apache.datasketches.hll.HllSketch;
+import org.apache.datasketches.hll.TgtHllType;
+import org.apache.datasketches.hll.Union;
+import org.apache.datasketches.memory.WritableMemory;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+
+public class HllSketchMergeBufferAggregatorHelper
+{
+  private final int lgK;
+  private final TgtHllType tgtHllType;
+  private final int size;
+  private final StripedBufferLock stripedLock = new StripedBufferLock();
+
+  /**
+   * Used by {@link #init(ByteBuffer, int)}. We initialize by copying a 
prebuilt empty Union image.
+   * {@link HllSketchBuildBufferAggregator} does something similar, but 
different enough that we don't share code. The
+   * "build" flavor uses {@link HllSketch} objects and the "merge" flavor uses 
{@link Union} objects.
+   */
+  private final byte[] emptyUnion;
+
+  public HllSketchMergeBufferAggregatorHelper(int lgK, TgtHllType tgtHllType, 
int size)
+  {
+    this.lgK = lgK;
+    this.tgtHllType = tgtHllType;
+    this.size = size;
+    this.emptyUnion = new byte[size];
+
+    //noinspection ResultOfObjectAllocationIgnored (Union writes to 
"emptyUnion" as a side effect of construction)
+    new Union(lgK, WritableMemory.wrap(emptyUnion));
+  }
+
+  /**
+   * Helper for implementing {@link 
org.apache.druid.query.aggregation.BufferAggregator#init} and
+   * {@link org.apache.druid.query.aggregation.VectorAggregator#init}.
+   */
+  public void init(final ByteBuffer buf, final int position)
+  {
+    // Copy prebuilt empty union object.
+    // Not necessary to cache a Union wrapper around the initialized memory, 
because:
+    //  - It is cheap to reconstruct by re-wrapping the memory in "aggregate" 
and "get".
+    //  - Unlike the HllSketch objects used by HllSketchBuildBufferAggregator, 
our Union objects never exceed the
+    //    max size and therefore do not need to be potentially moved in-heap.
+
+    final int oldPosition = buf.position();
+    try {
+      buf.position(position);
+      buf.put(emptyUnion);
+    }
+    finally {
+      buf.position(oldPosition);
+    }
+  }
+
+  /**
+   * Helper for implementing {@link 
org.apache.druid.query.aggregation.BufferAggregator#get} and
+   * {@link org.apache.druid.query.aggregation.VectorAggregator#get}.
+   *
+   * This method uses locks because it can be used during indexing,
+   * and Druid can call aggregate() and get() concurrently
+   * See https://github.com/druid-io/druid/pull/3956
+   */
+  public Object get(ByteBuffer buf, int position)
+  {
+    final WritableMemory mem = WritableMemory.wrap(buf, 
ByteOrder.LITTLE_ENDIAN).writableRegion(position, size);
+    final Lock lock = stripedLock.getLock(position).readLock();
+    lock.lock();

Review comment:
       See also: #11124, which was inspired by this thread and clears up some 
stuff.




-- 
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]



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

Reply via email to