You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "jtuglu1 (via GitHub)" <gi...@apache.org> on 2025/12/05 07:28:06 UTC
[PR] Vectorize SpectatorHistogram (druid)
jtuglu1 opened a new pull request, #18813:
URL: https://github.com/apache/druid/pull/18813
### Description
Vectorizes SpectatorHistogram.
#### Performance
TODO:
```
SpectatorHistogramAggregatorBenchmark.queryQueryableIndex long1 1000000 false avgt 2 3916.783 us/op
SpectatorHistogramAggregatorBenchmark.queryQueryableIndex long1 1000000 true avgt 2 212.739 us/op
```
<!--
In each section, please describe design decisions made, including:
- Choice of algorithms
- Behavioral aspects. What configuration values are acceptable? How are corner cases and error conditions handled, such as when there are insufficient resources?
- Class organization and design (how the logic is split between classes, inheritance, composition, design patterns)
- Method organization and design (how the logic is split between methods, parameters and return types)
- Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
-->
<!-- It's good to describe an alternative design (or mention an alternative name) for every design (or naming) decision point and compare the alternatives with the designs that you've implemented (or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
<!-- If there was a discussion of the design of the feature implemented in this PR elsewhere (e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list), link to that discussion from this PR description and explain what have changed in your final design compared to your original proposal or the consensus version in the end of the discussion. If something hasn't changed since the original discussion, you can omit a detailed discussion of those aspects of the design here, perhaps apart from brief mentioning for the sake of readability of this PR description. -->
<!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
#### Release note
<!-- Give your best effort to summarize your changes in a couple of sentences aimed toward Druid users.
If your change doesn't have end user impact, you can skip this section.
For tips about how to write a good release note, see [Release notes](https://github.com/apache/druid/blob/master/CONTRIBUTING.md#release-notes).
-->
<hr>
##### Key changed/added classes in this PR
* `MyFoo`
* `OurBar`
* `TheirBaz`
<hr>
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
This PR has:
- [ ] been self-reviewed.
- [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
- [ ] added documentation for new or modified features or behaviors.
- [ ] a release note entry in the PR description.
- [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
- [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
- [ ] added integration tests.
- [ ] been tested in a test Druid cluster.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2601184289
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(buffer);
+ if (map == null) {
+ return null;
+ }
+ return map.get(position);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram cache for the given buffer
+ */
+ public Int2ObjectMap<SpectatorHistogram> get(final ByteBuffer buffer)
+ {
+ return histogramCache.get(buffer);
+ }
+
+ public float getFloat(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ public long getLong(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ /**
+ * Resets the helper by clearing the buffer/histogram cache.
+ */
+ public void close()
+ {
+ histogramCache.clear();
+ }
+
+ /**
+ * Move histogram located at {@param oldBuffer} in position {@param oldPosition} to {@param newBuffer} in position {@param newPosition}.
+ */
+ public void relocate(int oldPosition, int newPosition, ByteBuffer oldBuffer, ByteBuffer newBuffer)
+ {
+ final SpectatorHistogram histogram = histogramCache.get(oldBuffer).get(oldPosition);
+ addToCache(newBuffer, newPosition, histogram);
+
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(oldBuffer);
+ map.remove(oldPosition);
+ if (map.isEmpty()) {
+ histogramCache.remove(oldBuffer);
+ }
+ }
+
+ private void addToCache(final ByteBuffer buffer, final int position, final SpectatorHistogram histogram)
+ {
+ Int2ObjectMap<SpectatorHistogram> map = histogramCache.computeIfAbsent(
Review Comment:
VectorAggregators are not required to be thread-safe.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on PR #18813:
URL: https://github.com/apache/druid/pull/18813#issuecomment-3628416415
@clintropolis do you know why many `VectorAggregator` implementations:
- [NullableNumericVectorAggregator](https://github.com/apache/druid/blob/-/processing/src/main/java/org/apache/druid/query/aggregation/NullableNumericVectorAggregator.java#L116-L117)
- [DoubleHllSketchBuildVectorProcessor](https://github.com/apache/druid/blob/master/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/vector/DoubleHllSketchBuildVectorProcessor.java#L62-L66)
use the `rows` index to check whether the row being accessed is `null`, but [ApproximateHistogramVectorAggregator](https://github.com/apache/druid/blob/-/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramVectorAggregator.java#L75-L78) does not index into this null vector to fetch that information (and instead uses rows directly)?.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600712935
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
I think that seems reasonable – it should get cleaned up both at relocate/end. Not sure if we can do a read prior to any writes being committed into the histogram? cc @bsyk
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2605019071
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
cc @clintropolis if you have any thoughts, otherwise I'm inclined to leave as-is.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2605018844
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
I'd prefer to stick with the current behavior (to ensure backwards compatibility) (in other words, don't change behavior from buffer aggregator). The other classes which use this sort of bytemap approach evict in a similar fashion.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jasonk000 (via GitHub)" <gi...@apache.org>.
jasonk000 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2603669578
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramVectorizedAggregator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorObjectSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during compact/re-ingest/query time when aggregating against other Spectator histogram objects (COMPLEX type).
+*/
+public class SpectatorHistogramVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorObjectSelector selector;
+
+ public SpectatorHistogramVectorizedAggregator(VectorObjectSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+ if (histogram == null) {
+ return;
+ }
+
+ for (int i = startRow; i < endRow; ++i) {
+ Object other = vector[i];
+ if (other != null) {
+ innerAggregator.merge(histogram, other);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer);
Review Comment:
I think this might return null still, in theory?
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600773552
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
Review Comment:
I just followed the naming of the [interface](https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/query/aggregation/VectorAggregator.java#L70) – will keep as-is for the sake of uniforimity.
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
Review Comment:
I just followed the naming of the [interface](https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/query/aggregation/VectorAggregator.java#L70) – will keep as-is for the sake of uniformity.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2601184289
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(buffer);
+ if (map == null) {
+ return null;
+ }
+ return map.get(position);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram cache for the given buffer
+ */
+ public Int2ObjectMap<SpectatorHistogram> get(final ByteBuffer buffer)
+ {
+ return histogramCache.get(buffer);
+ }
+
+ public float getFloat(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ public long getLong(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ /**
+ * Resets the helper by clearing the buffer/histogram cache.
+ */
+ public void close()
+ {
+ histogramCache.clear();
+ }
+
+ /**
+ * Move histogram located at {@param oldBuffer} in position {@param oldPosition} to {@param newBuffer} in position {@param newPosition}.
+ */
+ public void relocate(int oldPosition, int newPosition, ByteBuffer oldBuffer, ByteBuffer newBuffer)
+ {
+ final SpectatorHistogram histogram = histogramCache.get(oldBuffer).get(oldPosition);
+ addToCache(newBuffer, newPosition, histogram);
+
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(oldBuffer);
+ map.remove(oldPosition);
+ if (map.isEmpty()) {
+ histogramCache.remove(oldBuffer);
+ }
+ }
+
+ private void addToCache(final ByteBuffer buffer, final int position, final SpectatorHistogram histogram)
+ {
+ Int2ObjectMap<SpectatorHistogram> map = histogramCache.computeIfAbsent(
Review Comment:
VectorAggregators are not required to be thread-safe (see [here](https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/query/aggregation/VectorAggregator.java#L34-L35)), however, the BufferAggregator may be made unsafe due it sharing some common helper methods. I'll double-check those.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600737163
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+
+ final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer);
+ for (int i = 0; i < numRows; ++i) {
+ int rowIndex = rows != null ? rows[i] : i;
+ if (!hasNulls || !isNull[rowIndex]) {
+ int position = positions[i] + positionOffset;
+ innerAggregator.merge(histMap.get(position), vector[rowIndex]);
Review Comment:
I had thought about this. I think `positions[]` might not be deterministic enough for that to matter and we'd likely spend more time branching on the cached value than not
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jasonk000 (via GitHub)" <gi...@apache.org>.
jasonk000 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600426132
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
I see this is in the previous code. Regardless, this state seems odd. Should we actively remove it from the buffer if we're never going to expose it?
##########
benchmarks/src/test/java/org/apache/druid/benchmark/SpectatorHistogramAggregatorBenchmark.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.benchmark;
+
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.benchmark.query.QueryBenchmarkUtil;
+import org.apache.druid.jackson.DefaultObjectMapper;
+import org.apache.druid.java.util.common.FileUtils;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.java.util.common.guava.Sequence;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.FinalizeResultsQueryRunner;
+import org.apache.druid.query.Query;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryPlus;
+import org.apache.druid.query.QueryRunner;
+import org.apache.druid.query.QueryRunnerFactory;
+import org.apache.druid.query.QueryToolChest;
+import org.apache.druid.query.Result;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import org.apache.druid.query.context.ResponseContext;
+import org.apache.druid.query.spec.MultipleIntervalSegmentSpec;
+import org.apache.druid.query.spec.QuerySegmentSpec;
+import org.apache.druid.query.timeseries.TimeseriesQuery;
+import org.apache.druid.query.timeseries.TimeseriesQueryEngine;
+import org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest;
+import org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory;
+import org.apache.druid.query.timeseries.TimeseriesResultValue;
+import org.apache.druid.segment.IncrementalIndexSegment;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.IndexMergerV9;
+import org.apache.druid.segment.IndexSpec;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexSegment;
+import org.apache.druid.segment.column.ColumnConfig;
+import org.apache.druid.segment.generator.DataGenerator;
+import org.apache.druid.segment.generator.GeneratorBasicSchemas;
+import org.apache.druid.segment.generator.GeneratorSchemaInfo;
+import org.apache.druid.segment.incremental.AppendableIndexSpec;
+import org.apache.druid.segment.incremental.IncrementalIndex;
+import org.apache.druid.segment.incremental.OnheapIncrementalIndex;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.spectator.histogram.SpectatorHistogramAggregatorFactory;
+import org.apache.druid.spectator.histogram.SpectatorHistogramModule;
+import org.apache.druid.timeline.SegmentId;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Benchmark to compare performance of SpectatorHistogram aggregator with and without vectorization.
+ */
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = {
+ "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
+ "--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED",
+ "--add-opens=java.base/java.nio=ALL-UNNAMED",
+ "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED",
+ "--add-opens=java.base/jdk.internal.ref=ALL-UNNAMED",
+ "--add-opens=java.base/java.io=ALL-UNNAMED",
+ "--add-opens=java.base/java.lang=ALL-UNNAMED",
+ "--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED"
+})
+@Warmup(iterations = 10)
+@Measurement(iterations = 25)
+public class SpectatorHistogramAggregatorBenchmark
+{
+ private static final Logger log = new Logger(SpectatorHistogramAggregatorBenchmark.class);
+ private static final int RNG_SEED = 9999;
+ private static final IndexMergerV9 INDEX_MERGER_V9;
+ private static final IndexIO INDEX_IO;
+ public static final ObjectMapper JSON_MAPPER;
+
+ static {
+ JSON_MAPPER = new DefaultObjectMapper();
+ // Register the SpectatorHistogram Jackson modules and serde
+ SpectatorHistogramModule module = new SpectatorHistogramModule();
+ for (Module jacksonModule : module.getJacksonModules()) {
+ JSON_MAPPER.registerModule(jacksonModule);
+ }
+ SpectatorHistogramModule.registerSerde();
+
+ INDEX_IO = new IndexIO(
+ JSON_MAPPER,
+ new ColumnConfig()
+ {
+ }
+ );
+ INDEX_MERGER_V9 = new IndexMergerV9(JSON_MAPPER, INDEX_IO, OffHeapMemorySegmentWriteOutMediumFactory.instance());
+ }
+
+ @Param({"1000000"})
+ private int rowsPerSegment;
+
+ @Param({"false", "true"})
+ private String vectorize;
+
+ @Param({"long1"})
+ private String metricName;
+
+ private AppendableIndexSpec appendableIndexSpec;
+ private AggregatorFactory spectatorHistogramFactory;
+ private DataGenerator generator;
+ private QueryRunnerFactory factory;
+ private GeneratorSchemaInfo schemaInfo;
+ private TimeseriesQuery query;
+
+ /**
+ * Setup everything common for benchmarking both the incremental-index and the queryable-index.
+ */
+ @Setup
+ public void setup()
+ {
+ log.info("SETUP CALLED AT " + System.currentTimeMillis());
+
+ schemaInfo = GeneratorBasicSchemas.SCHEMA_MAP.get("basic");
+
+ spectatorHistogramFactory = new SpectatorHistogramAggregatorFactory("spectatorHistogram", metricName);
+
+ QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(
+ Collections.singletonList(schemaInfo.getDataInterval())
+ );
+
+ generator = new DataGenerator(
+ schemaInfo.getColumnSchemas(),
+ RNG_SEED,
+ schemaInfo.getDataInterval(),
+ rowsPerSegment
+ );
+
+ query = Druids.newTimeseriesQueryBuilder()
+ .dataSource("blah")
+ .granularity(Granularities.ALL)
+ .intervals(intervalSpec)
+ .aggregators(Collections.singletonList(spectatorHistogramFactory))
+ .descending(false)
+ .build();
+
+ factory = new TimeseriesQueryRunnerFactory(
+ new TimeseriesQueryQueryToolChest(),
+ new TimeseriesQueryEngine(),
+ QueryBenchmarkUtil.NOOP_QUERYWATCHER
+ );
+ }
+
+ /**
+ * Setup/teardown everything specific for benchmarking the incremental-index.
+ */
+ @State(Scope.Benchmark)
+ public static class IncrementalIndexState
+ {
+ IncrementalIndex incIndex;
+
+ @Setup(Level.Invocation)
+ public void setup(SpectatorHistogramAggregatorBenchmark global)
+ {
+ global.appendableIndexSpec = new OnheapIncrementalIndex.Spec();
+ incIndex = global.makeIncIndex(global.spectatorHistogramFactory);
+ global.generator.addToIndex(incIndex, global.rowsPerSegment);
+ }
+
+ @TearDown(Level.Invocation)
+ public void tearDown()
+ {
+ if (incIndex != null) {
+ incIndex.close();
+ }
+ }
+ }
+
+ /**
+ * Setup/teardown everything specific for benchmarking the queryable-index.
+ */
+ @State(Scope.Benchmark)
+ public static class QueryableIndexState
+ {
+ private File qIndexesDir;
+ private QueryableIndex qIndex;
+
+ @Setup
+ public void setup(SpectatorHistogramAggregatorBenchmark global) throws IOException
+ {
+ global.appendableIndexSpec = new OnheapIncrementalIndex.Spec();
+
+ IncrementalIndex incIndex = global.makeIncIndex(global.spectatorHistogramFactory);
+ global.generator.addToIndex(incIndex, global.rowsPerSegment);
+
+ qIndexesDir = FileUtils.createTempDir();
+ log.info("Using temp dir: " + qIndexesDir.getAbsolutePath());
+
+ File indexFile = INDEX_MERGER_V9.persist(
+ incIndex,
+ qIndexesDir,
+ IndexSpec.getDefault(),
+ null
+ );
+ incIndex.close();
+
+ qIndex = INDEX_IO.loadIndex(indexFile);
+ }
+
+ @TearDown
+ public void tearDown()
+ {
+ if (qIndex != null) {
+ qIndex.close();
+ }
+ if (qIndexesDir != null) {
+ qIndexesDir.delete();
+ }
+ }
+ }
+
+ private IncrementalIndex makeIncIndex(AggregatorFactory metric)
+ {
+ return appendableIndexSpec.builder()
+ .setSimpleTestingIndexSchema(metric)
+ .setMaxRowCount(rowsPerSegment)
+ .build();
+ }
+
+ private static <T> List<T> runQuery(
+ QueryRunnerFactory factory,
+ QueryRunner runner,
+ Query<T> query,
+ String vectorize
+ )
+ {
+ QueryToolChest toolChest = factory.getToolchest();
+ QueryRunner<T> theRunner = new FinalizeResultsQueryRunner<>(
+ toolChest.mergeResults(toolChest.preMergeQueryDecoration(runner)),
+ toolChest
+ );
+
+ final QueryPlus<T> queryToRun = QueryPlus.wrap(
+ query.withOverriddenContext(
+ ImmutableMap.of(
+ QueryContexts.VECTORIZE_KEY, vectorize,
+ QueryContexts.VECTORIZE_VIRTUAL_COLUMNS_KEY, vectorize
+ )
+ )
+ );
+ Sequence<T> queryResult = theRunner.run(queryToRun, ResponseContext.createEmpty());
+ return queryResult.toList();
+ }
+
+ @Benchmark
+ @BenchmarkMode(Mode.AverageTime)
+ @OutputTimeUnit(TimeUnit.MICROSECONDS)
+ public void queryIncrementalIndex(Blackhole blackhole, IncrementalIndexState state)
+ {
+ QueryRunner<Result<TimeseriesResultValue>> runner = QueryBenchmarkUtil.makeQueryRunner(
+ factory,
+ SegmentId.dummy("incIndex"),
+ new IncrementalIndexSegment(state.incIndex, SegmentId.dummy("incIndex"))
+ );
+
+ List<Result<TimeseriesResultValue>> results = runQuery(factory, runner, query, vectorize);
+ blackhole.consume(results);
+ }
+
+ @Benchmark
+ @BenchmarkMode(Mode.AverageTime)
+ @OutputTimeUnit(TimeUnit.MICROSECONDS)
+ public void queryQueryableIndex(Blackhole blackhole, QueryableIndexState state)
+ {
+ QueryRunner<Result<TimeseriesResultValue>> runner = QueryBenchmarkUtil.makeQueryRunner(
+ factory,
+ SegmentId.dummy("qIndex"),
+ new QueryableIndexSegment(state.qIndex, SegmentId.dummy("qIndex"))
+ );
+
+ List<Result<TimeseriesResultValue>> results = runQuery(factory, runner, query, vectorize);
+ blackhole.consume(results);
+ }
+
+ public static void main(String[] args) throws RunnerException
+ {
+ Options opt = new OptionsBuilder()
+ .include(SpectatorHistogramAggregatorBenchmark.class.getSimpleName())
+ .forks(1)
+ .warmupIterations(1)
+ .measurementIterations(2)
Review Comment:
A main is likely not required since we can use a compiled jmh-jar. If it is, then these numbers should be higher by default, I doubt there is enough time here for them to consistently settle down (warmup) and mitigate any variance (fork + measurement).
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(buffer);
+ if (map == null) {
+ return null;
+ }
+ return map.get(position);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram cache for the given buffer
+ */
+ public Int2ObjectMap<SpectatorHistogram> get(final ByteBuffer buffer)
+ {
+ return histogramCache.get(buffer);
+ }
+
+ public float getFloat(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ public long getLong(final ByteBuffer buffer, final int position)
+ {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+
+ /**
+ * Resets the helper by clearing the buffer/histogram cache.
+ */
+ public void close()
+ {
+ histogramCache.clear();
+ }
+
+ /**
+ * Move histogram located at {@param oldBuffer} in position {@param oldPosition} to {@param newBuffer} in position {@param newPosition}.
+ */
+ public void relocate(int oldPosition, int newPosition, ByteBuffer oldBuffer, ByteBuffer newBuffer)
+ {
+ final SpectatorHistogram histogram = histogramCache.get(oldBuffer).get(oldPosition);
+ addToCache(newBuffer, newPosition, histogram);
+
+ final Int2ObjectMap<SpectatorHistogram> map = histogramCache.get(oldBuffer);
+ map.remove(oldPosition);
+ if (map.isEmpty()) {
+ histogramCache.remove(oldBuffer);
+ }
+ }
+
+ private void addToCache(final ByteBuffer buffer, final int position, final SpectatorHistogram histogram)
+ {
+ Int2ObjectMap<SpectatorHistogram> map = histogramCache.computeIfAbsent(
Review Comment:
does this need to be concurrent-capable?
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
Review Comment:
I don't understand the significance of this comment. Do we expect concurrent read/write of the underlying Int2ObjectMap? (if so, does it need synchronization?).
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
Review Comment:
style: would `int[] rows` as `int[] rowMap` or `int[] rowIndexMap` be better?
##########
extensions-contrib/spectator-histogram/src/test/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelperTest.java:
##########
@@ -0,0 +1,326 @@
+/*
+ * 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.spectator.histogram;
+
+import com.netflix.spectator.api.histogram.PercentileBuckets;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.testing.InitializedNullHandlingTest;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+public class SpectatorHistogramAggregateHelperTest extends InitializedNullHandlingTest
+{
+ private static final int POSITION = 0;
+ private static final int POSITION_2 = 1;
+
+ private SpectatorHistogramAggregateHelper helper;
+ private ByteBuffer buffer;
+
+ @Before
+ public void setUp()
+ {
+ helper = new SpectatorHistogramAggregateHelper();
+ buffer = ByteBuffer.allocate(1024);
+ }
+
+ @After
+ public void tearDown()
+ {
+ helper.close();
+ }
+
+ @Test
+ public void testInitCreatesEmptyHistogram()
+ {
+ helper.init(buffer, POSITION);
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testMergeWithSpectatorHistogramObject()
+ {
+ SpectatorHistogram other = new SpectatorHistogram();
+ other.add(PercentileBuckets.indexOf(100), 5L);
+ other.add(PercentileBuckets.indexOf(200), 3L);
+
+ helper.init(buffer, POSITION);
+ SpectatorHistogram histogram = helper.get(buffer, POSITION);
+ helper.merge(histogram, other);
+
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(other, result);
+ }
+
+ @Test
+ public void testMergeWithLongValues()
+ {
+ helper.init(buffer, POSITION);
+ SpectatorHistogram histogram = helper.get(buffer, POSITION);
+ helper.merge(histogram, 100L);
+ helper.merge(histogram, 200L);
+ helper.merge(histogram, 100L);
+
+ SpectatorHistogram expected = new SpectatorHistogram();
+ expected.insert(100L);
+ expected.insert(200L);
+ expected.insert(100L);
+
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
+ public void testMergeWithSpectatorHistogram()
+ {
+ SpectatorHistogram histogram = new SpectatorHistogram();
+ histogram.add(PercentileBuckets.indexOf(100), 5L);
+
+ SpectatorHistogram other = new SpectatorHistogram();
+ other.add(PercentileBuckets.indexOf(100), 3L);
+ other.add(PercentileBuckets.indexOf(200), 2L);
+
+ helper.merge(histogram, other);
+
+ SpectatorHistogram expected = new SpectatorHistogram();
+ expected.add(PercentileBuckets.indexOf(100), 8L);
+ expected.add(PercentileBuckets.indexOf(200), 2L);
+
+ Assert.assertEquals(expected, histogram);
+ }
+
+ @Test
+ public void testMergeWithNumber()
+ {
+ SpectatorHistogram histogram = new SpectatorHistogram();
+
+ helper.merge(histogram, 100);
+ helper.merge(histogram, 200L);
+ helper.merge(histogram, 300.0);
+
+ SpectatorHistogram expected = new SpectatorHistogram();
+ expected.insert(100);
+ expected.insert(200L);
+ expected.insert(300.0);
+
+ Assert.assertEquals(expected, histogram);
Review Comment:
I'm not sure this assert is testing anything. Probably the `expected` should be written with all-primitives.
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
Review Comment:
(.. refer above, I think this will end up allocating on `SpectatorHistogram::insert` as there is no primitive `long` implementation).
##########
benchmarks/src/test/java/org/apache/druid/benchmark/SpectatorHistogramAggregatorBenchmark.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.benchmark;
+
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.benchmark.query.QueryBenchmarkUtil;
+import org.apache.druid.jackson.DefaultObjectMapper;
+import org.apache.druid.java.util.common.FileUtils;
+import org.apache.druid.java.util.common.granularity.Granularities;
+import org.apache.druid.java.util.common.guava.Sequence;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.query.Druids;
+import org.apache.druid.query.FinalizeResultsQueryRunner;
+import org.apache.druid.query.Query;
+import org.apache.druid.query.QueryContexts;
+import org.apache.druid.query.QueryPlus;
+import org.apache.druid.query.QueryRunner;
+import org.apache.druid.query.QueryRunnerFactory;
+import org.apache.druid.query.QueryToolChest;
+import org.apache.druid.query.Result;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import org.apache.druid.query.context.ResponseContext;
+import org.apache.druid.query.spec.MultipleIntervalSegmentSpec;
+import org.apache.druid.query.spec.QuerySegmentSpec;
+import org.apache.druid.query.timeseries.TimeseriesQuery;
+import org.apache.druid.query.timeseries.TimeseriesQueryEngine;
+import org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest;
+import org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory;
+import org.apache.druid.query.timeseries.TimeseriesResultValue;
+import org.apache.druid.segment.IncrementalIndexSegment;
+import org.apache.druid.segment.IndexIO;
+import org.apache.druid.segment.IndexMergerV9;
+import org.apache.druid.segment.IndexSpec;
+import org.apache.druid.segment.QueryableIndex;
+import org.apache.druid.segment.QueryableIndexSegment;
+import org.apache.druid.segment.column.ColumnConfig;
+import org.apache.druid.segment.generator.DataGenerator;
+import org.apache.druid.segment.generator.GeneratorBasicSchemas;
+import org.apache.druid.segment.generator.GeneratorSchemaInfo;
+import org.apache.druid.segment.incremental.AppendableIndexSpec;
+import org.apache.druid.segment.incremental.IncrementalIndex;
+import org.apache.druid.segment.incremental.OnheapIncrementalIndex;
+import org.apache.druid.segment.writeout.OffHeapMemorySegmentWriteOutMediumFactory;
+import org.apache.druid.spectator.histogram.SpectatorHistogramAggregatorFactory;
+import org.apache.druid.spectator.histogram.SpectatorHistogramModule;
+import org.apache.druid.timeline.SegmentId;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Benchmark to compare performance of SpectatorHistogram aggregator with and without vectorization.
+ */
+@State(Scope.Benchmark)
+@Fork(value = 1, jvmArgs = {
+ "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
+ "--add-exports=java.base/jdk.internal.ref=ALL-UNNAMED",
+ "--add-opens=java.base/java.nio=ALL-UNNAMED",
+ "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED",
+ "--add-opens=java.base/jdk.internal.ref=ALL-UNNAMED",
+ "--add-opens=java.base/java.io=ALL-UNNAMED",
+ "--add-opens=java.base/java.lang=ALL-UNNAMED",
+ "--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED"
+})
+@Warmup(iterations = 10)
+@Measurement(iterations = 25)
+public class SpectatorHistogramAggregatorBenchmark
+{
+ private static final Logger log = new Logger(SpectatorHistogramAggregatorBenchmark.class);
+ private static final int RNG_SEED = 9999;
+ private static final IndexMergerV9 INDEX_MERGER_V9;
+ private static final IndexIO INDEX_IO;
+ public static final ObjectMapper JSON_MAPPER;
+
+ static {
+ JSON_MAPPER = new DefaultObjectMapper();
+ // Register the SpectatorHistogram Jackson modules and serde
+ SpectatorHistogramModule module = new SpectatorHistogramModule();
+ for (Module jacksonModule : module.getJacksonModules()) {
+ JSON_MAPPER.registerModule(jacksonModule);
+ }
+ SpectatorHistogramModule.registerSerde();
+
+ INDEX_IO = new IndexIO(
+ JSON_MAPPER,
+ new ColumnConfig()
+ {
+ }
+ );
+ INDEX_MERGER_V9 = new IndexMergerV9(JSON_MAPPER, INDEX_IO, OffHeapMemorySegmentWriteOutMediumFactory.instance());
+ }
+
+ @Param({"1000000"})
+ private int rowsPerSegment;
+
+ @Param({"false", "true"})
+ private String vectorize;
+
+ @Param({"long1"})
+ private String metricName;
+
+ private AppendableIndexSpec appendableIndexSpec;
+ private AggregatorFactory spectatorHistogramFactory;
+ private DataGenerator generator;
+ private QueryRunnerFactory factory;
+ private GeneratorSchemaInfo schemaInfo;
+ private TimeseriesQuery query;
+
+ /**
+ * Setup everything common for benchmarking both the incremental-index and the queryable-index.
+ */
+ @Setup
+ public void setup()
+ {
+ log.info("SETUP CALLED AT " + System.currentTimeMillis());
Review Comment:
What's the purpose of this during the JMH run?
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogram.java:
##########
@@ -276,7 +276,7 @@ byte[] toBytes()
return Arrays.copyOf(buffer.array(), buffer.position());
}
- void insert(Number num)
+ public void insert(Number num)
Review Comment:
We should add another `public void insert(long value)` to avoid the implicit Long/Number allocation.
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
Review Comment:
It looks like `innerAggregator.get()` can return null, in which case the next line, `if (histo.isEmpty()) {` might NPE.
##########
extensions-contrib/spectator-histogram/src/test/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelperTest.java:
##########
@@ -0,0 +1,326 @@
+/*
+ * 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.spectator.histogram;
+
+import com.netflix.spectator.api.histogram.PercentileBuckets;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.testing.InitializedNullHandlingTest;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+public class SpectatorHistogramAggregateHelperTest extends InitializedNullHandlingTest
+{
+ private static final int POSITION = 0;
+ private static final int POSITION_2 = 1;
+
+ private SpectatorHistogramAggregateHelper helper;
+ private ByteBuffer buffer;
+
+ @Before
+ public void setUp()
+ {
+ helper = new SpectatorHistogramAggregateHelper();
+ buffer = ByteBuffer.allocate(1024);
+ }
+
+ @After
+ public void tearDown()
+ {
+ helper.close();
+ }
+
+ @Test
+ public void testInitCreatesEmptyHistogram()
+ {
+ helper.init(buffer, POSITION);
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.isEmpty());
+ }
+
+ @Test
+ public void testMergeWithSpectatorHistogramObject()
+ {
+ SpectatorHistogram other = new SpectatorHistogram();
+ other.add(PercentileBuckets.indexOf(100), 5L);
+ other.add(PercentileBuckets.indexOf(200), 3L);
+
+ helper.init(buffer, POSITION);
+ SpectatorHistogram histogram = helper.get(buffer, POSITION);
+ helper.merge(histogram, other);
+
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(other, result);
+ }
+
+ @Test
+ public void testMergeWithLongValues()
+ {
+ helper.init(buffer, POSITION);
+ SpectatorHistogram histogram = helper.get(buffer, POSITION);
+ helper.merge(histogram, 100L);
+ helper.merge(histogram, 200L);
+ helper.merge(histogram, 100L);
+
+ SpectatorHistogram expected = new SpectatorHistogram();
+ expected.insert(100L);
+ expected.insert(200L);
+ expected.insert(100L);
+
+ SpectatorHistogram result = helper.get(buffer, POSITION);
+ Assert.assertNotNull(result);
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
+ public void testMergeWithSpectatorHistogram()
+ {
+ SpectatorHistogram histogram = new SpectatorHistogram();
+ histogram.add(PercentileBuckets.indexOf(100), 5L);
+
+ SpectatorHistogram other = new SpectatorHistogram();
+ other.add(PercentileBuckets.indexOf(100), 3L);
+ other.add(PercentileBuckets.indexOf(200), 2L);
+
+ helper.merge(histogram, other);
+
+ SpectatorHistogram expected = new SpectatorHistogram();
+ expected.add(PercentileBuckets.indexOf(100), 8L);
+ expected.add(PercentileBuckets.indexOf(200), 2L);
+
+ Assert.assertEquals(expected, histogram);
+ }
+
+ @Test
+ public void testMergeWithNumber()
+ {
+ SpectatorHistogram histogram = new SpectatorHistogram();
+
+ helper.merge(histogram, 100);
+ helper.merge(histogram, 200L);
+ helper.merge(histogram, 300.0);
Review Comment:
I think this `300.0` is implicitly being cast to Number? Might be clearer as `Float.valueOf(300)`.
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
Review Comment:
nit: SpectatorHistogram `or Number`
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+
+ final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer);
+ for (int i = 0; i < numRows; ++i) {
+ int rowIndex = rows != null ? rows[i] : i;
+ if (!hasNulls || !isNull[rowIndex]) {
+ int position = positions[i] + positionOffset;
+ innerAggregator.merge(histMap.get(position), vector[rowIndex]);
Review Comment:
perf: Are we likely in practice to see long runs where `position` ends up loading the same map repeatedly? Not sure if worth caching this or how likely this occurrence is.
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
Review Comment:
style: personally I think this (and other examples) would be more readable as
```
boolean rowIsNull = hasNulls && isNull[i];
if (!rowIsNull) {
...
}
```
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on PR #18813:
URL: https://github.com/apache/druid/pull/18813#issuecomment-3629463400
@jasonk000 one note: I will not be leaving the JMH in the final PR (this is because benchmarks are intended to be Druid core pieces, and not extensions).
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jasonk000 (via GitHub)" <gi...@apache.org>.
jasonk000 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2603669578
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramVectorizedAggregator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorObjectSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during compact/re-ingest/query time when aggregating against other Spectator histogram objects (COMPLEX type).
+*/
+public class SpectatorHistogramVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorObjectSelector selector;
+
+ public SpectatorHistogramVectorizedAggregator(VectorObjectSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+ if (histogram == null) {
+ return;
+ }
+
+ for (int i = startRow; i < endRow; ++i) {
+ Object other = vector[i];
+ if (other != null) {
+ innerAggregator.merge(histogram, other);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer);
Review Comment:
I think this might return null, in theory?
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2605020392
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramVectorizedAggregator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorObjectSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during compact/re-ingest/query time when aggregating against other Spectator histogram objects (COMPLEX type).
+*/
+public class SpectatorHistogramVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorObjectSelector selector;
+
+ public SpectatorHistogramVectorizedAggregator(VectorObjectSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+ if (histogram == null) {
+ return;
+ }
+
+ for (int i = startRow; i < endRow; ++i) {
+ Object other = vector[i];
+ if (other != null) {
+ innerAggregator.merge(histogram, other);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
+ {
+ Object[] vector = selector.getObjectVector();
+ if (vector == null) {
+ return;
+ }
+
+ final Int2ObjectMap<SpectatorHistogram> histMap = innerAggregator.get(buffer);
Review Comment:
In theory, yes. I'm fairly certain this is an invariant but I'll include for good measure.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600499262
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
Review Comment:
I believe VectorAggregators are not required to be thread-safe by contract, so I didn't modify it.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "bsyk (via GitHub)" <gi...@apache.org>.
bsyk commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2603348117
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogram.java:
##########
@@ -276,7 +276,7 @@ byte[] toBytes()
return Arrays.copyOf(buffer.array(), buffer.position());
}
- void insert(Number num)
+ public void insert(Number num)
Review Comment:
Minor: This could call the other insert method now.
`this.insert(num.longValue())`
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "gianm (via GitHub)" <gi...@apache.org>.
gianm commented on PR #18813:
URL: https://github.com/apache/druid/pull/18813#issuecomment-4000853456
> @gianm @clintropolis do you know why many `VectorAggregator` implementations(2 examples below):
>
> * [NullableNumericVectorAggregator](https://github.com/apache/druid/blob/-/processing/src/main/java/org/apache/druid/query/aggregation/NullableNumericVectorAggregator.java#L116-L117)
>
> * [DoubleHllSketchBuildVectorProcessor](https://github.com/apache/druid/blob/master/extensions-core/datasketches/src/main/java/org/apache/druid/query/aggregation/datasketches/hll/vector/DoubleHllSketchBuildVectorProcessor.java#L62-L66)
>
>
> use the `rows` index to check whether the row being accessed is `null`, but [ApproximateHistogramVectorAggregator](https://github.com/apache/druid/blob/-/extensions-core/histogram/src/main/java/org/apache/druid/query/aggregation/histogram/ApproximateHistogramVectorAggregator.java#L75-L78) does not index into this null vector with the `rows` index to fetch that information (and instead uses `i` directly)?.
>
> in other words: using `selector.getNullVector()[i]` vs `selector.getNullVector()[rows != null ? rows[i] : i]`?
It looks like a bug in `ApproximateHistogramVectorAggregator` to me.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600499262
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
Review Comment:
I believe VectorAggregators are not required to be thread-safe by contract, so I didn't modify the logic to be so.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600712935
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
I think that seems reasonable – it should get cleaned up both at relocate/end. Not sure if we can do a read prior to any writes being committed into the histogram.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on PR #18813:
URL: https://github.com/apache/druid/pull/18813#issuecomment-3615970688
Test failure is unrelated and being fixed in https://github.com/apache/druid/pull/18814
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2601403891
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramAggregateHelper.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import org.apache.druid.java.util.common.IAE;
+
+import java.nio.ByteBuffer;
+import java.util.IdentityHashMap;
+
+/**
+ * Helper class for Spectator histogram implementations of {@link org.apache.druid.query.aggregation.BufferAggregator} and {@link org.apache.druid.query.aggregation.VectorAggregator}.
+ */
+public class SpectatorHistogramAggregateHelper
+{
+ private final IdentityHashMap<ByteBuffer, Int2ObjectMap<SpectatorHistogram>> histogramCache = new IdentityHashMap<>();
+
+ public void init(ByteBuffer buffer, int position)
+ {
+ SpectatorHistogram emptyCounts = new SpectatorHistogram();
+ addToCache(buffer, position, emptyCounts);
+ }
+
+ /**
+ * Merge obj ({@link SpectatorHistogram} or {@link Object}) into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, Object obj)
+ {
+ if (obj instanceof SpectatorHistogram) {
+ SpectatorHistogram other = (SpectatorHistogram) obj;
+ current.merge(other);
+ } else if (obj instanceof Number) {
+ current.insert((Number) obj);
+ } else {
+ throw new IAE(
+ "Expected a Number, but received [%s] of type [%s]",
+ obj,
+ obj.getClass()
+ );
+ }
+ }
+
+ /**
+ * Merge {@param value} into {@param current}.
+ */
+ public void merge(SpectatorHistogram current, long value)
+ {
+ current.insert(value);
+ }
+
+ /**
+ * Fetches the SpectatorHistogram at the given buffer/position pair in the cache
+ */
+ public SpectatorHistogram get(final ByteBuffer buffer, final int position)
+ {
+ // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
+ // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
Review Comment:
Also worth noting this pattern is repeated in `TDigestSketchBufferAggregator` and `DDSketchBufferAggregator`, which are required to be thread-safe.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2600773552
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
Review Comment:
I just followed the naming of the [interface](https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/query/aggregation/VectorAggregator.java#L70).
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramNumericVectorizedAggregator.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.spectator.histogram;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+/*
+ Aggregator used during ingestion time when aggregating against numeric values.
+*/
+public class SpectatorHistogramNumericVectorizedAggregator implements VectorAggregator
+{
+ private final SpectatorHistogramAggregateHelper innerAggregator = new SpectatorHistogramAggregateHelper();
+ private final VectorValueSelector selector;
+
+ public SpectatorHistogramNumericVectorizedAggregator(VectorValueSelector selector)
+ {
+ this.selector = selector;
+ }
+
+ @Override
+ public void init(ByteBuffer buffer, int position)
+ {
+ // Map buf to a new histogram
+ innerAggregator.init(buffer, position);
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int position, int startRow, int endRow)
+ {
+ boolean[] isNull = selector.getNullVector();
+ long[] vector = selector.getLongVector();
+ boolean hasNulls = isNull != null;
+ final SpectatorHistogram histogram = innerAggregator.get(buffer, position);
+
+ for (int i = startRow; i < endRow; ++i) {
+ if (!hasNulls || !isNull[i]) {
+ innerAggregator.merge(histogram, vector[i]);
+ }
+ }
+ }
+
+ @Override
+ public void aggregate(ByteBuffer buffer, int numRows, int[] positions, @Nullable int[] rows, int positionOffset)
Review Comment:
I just followed the naming of the [interface](https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/query/aggregation/VectorAggregator.java#L70) – will update.
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "jtuglu1 (via GitHub)" <gi...@apache.org>.
jtuglu1 merged PR #18813:
URL: https://github.com/apache/druid/pull/18813
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org
Re: [PR] Vectorize SpectatorHistogram (druid)
Posted by "bsyk (via GitHub)" <gi...@apache.org>.
bsyk commented on code in PR #18813:
URL: https://github.com/apache/druid/pull/18813#discussion_r2603421707
##########
extensions-contrib/spectator-histogram/src/main/java/org/apache/druid/spectator/histogram/SpectatorHistogramBufferAggregator.java:
##########
@@ -62,70 +57,41 @@ public void aggregate(ByteBuffer buffer, int position)
if (obj == null) {
return;
}
- SpectatorHistogram counts = histogramCache.get(buffer).get(position);
- if (obj instanceof SpectatorHistogram) {
- SpectatorHistogram other = (SpectatorHistogram) obj;
- counts.merge(other);
- } else if (obj instanceof Number) {
- counts.insert((Number) obj);
- } else {
- throw new IAE(
- "Expected a number or a long[], but received [%s] of type [%s]",
- obj,
- obj.getClass()
- );
- }
+ SpectatorHistogram counts = innerAggregator.get(buffer, position);
+ innerAggregator.merge(counts, obj);
}
@Override
public Object get(final ByteBuffer buffer, final int position)
{
- // histogramCache is an IdentityHashMap where the reference of buffer is used for equality checks.
- // So the returned object isn't impacted by the changes in the buffer object made by concurrent threads.
-
- SpectatorHistogram spectatorHistogram = histogramCache.get(buffer).get(position);
- if (spectatorHistogram.isEmpty()) {
+ SpectatorHistogram histo = innerAggregator.get(buffer, position);
+ if (histo.isEmpty()) {
Review Comment:
This was likely being defensive or copied from another example. There's no harm in returning the empty histogram, it will happily merge with others. It would be a waste of a few bytes of buffer if we had any of these.
Could these be empty when merging query results? It's possible a segment has no results for a query. Would that return nothing, or an empty aggregation?
--
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.
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org