twilliamson opened a new pull request, #18049:
URL: https://github.com/apache/druid/pull/18049

   When `RowCombiningTimeAndDimsIterator.moveToNext() is merging indexes, if 
`MergingRowIterator.hasTimeAndDimsChangedSinceMark()` is `false`, then 
`AggregateCombiner.reset()` will be called through the call sequence 
`moveToNext()` -> `combineToCurrentTimeAndDims()` -> `resetCombinedMetrics()`, 
if `soleCurrentPointSourceOriginalIteratorIndex >= 0`. When exactly is all that 
the case? I'm not sure. But the net result was occasional over-counting, often 
wildly so, since `reset()` wasn't resetting anything, just combining whatever 
it had with the reset value.
   
   Fixes #17822.
   
   ### Description
   
   The `AggregateCombiner.reset()` JavaDoc says:
   > Resets this AggregateCombiner's state value to the value of the given 
selector, e. g. after calling this method
   > combiner.get*() should return the same value as selector.get*().
   
   And indeed most implementors are doing this, e.g.:
   - `DoubleSumAggregateCombiner` -> `sum = selector.getDouble();`
   - `TDigestSketchAggregatorFactory` -> `combined = null; fold(selector);`
   - `HllSketchAggregatorFactory` -> `union.reset(); fold(selector);`
   - `ApproximateHistogramAggregatorFactory` -> `ApproximateHistogram first = 
(ApproximateHistogram) selector.getObject(); combined.copy(first);`
   
   However, `FixedBucketsHistogramAggregatorFactory` is not meeting this 
contract: it's `reset()` method is identical to it's `fold()` method:
   ```
   FixedBucketsHistogram first = (FixedBucketsHistogram) selector.getObject();
   combined.combineHistogram(first);
   ```
   
   That means when `reset()` is called, it's merging the current state into the 
desired state, leading to overcounts. The code path that calls `reset()` is 
very infrequently hit: it's only used when merging multiple indexes and even 
then only when the grouping key values across the indexes are equal. The net 
result is the overcounts rarely show up, but when they do, they can cause 
results with wildly-high overcounts.
   
   This PR updates the `reset()` implementation to actually meet the contract. 
It adds a `reset()` method to `FixedBucketsHistogram`, and calls this before 
calling `fold()`.
   
   Other approaches considered were adding a `copy()` method, and making the 
`combined` member variable non-final. In the case of `copy()`, it would need to 
handle the non-count member variables (which aren't currently `final` but 
could/should be). In the case of a non-final `combined` member variable, I was 
nervous about changing the lifetime of any of the objects: if 
`selector.getObject()` is later mutated or there's someone else with storing 
the reference returned by `AggregateCombiner.getObject()`, then changing what 
objects are referenced could cause issues. That's very unlikely, but `reset()` 
seemed simple enough and didn't have these concerns. The main downside is if 
new member variables are added, we need to remember to update `reset()`. Given 
the rate of change of this class, that's unlikely to happen.
   
   #### Release note
   Fixed: FixedBucketsHistogram no longer occasionally introduces large 
overcounts in rollup results
   
   <hr>
   
   ##### Key changed classes in this PR
    * `FixedBucketsHistogram`
    * `FixedBucketsHistogramAggregatorFactory`
   
   <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:
   
   - [x] been self-reviewed.
      - [x] 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.)
   - [x] added documentation for new or modified features or behaviors.
   - [x] a release note entry in the PR description.
   - [x] 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)
   - [x] added comments explaining the "why" and the intent of the code 
wherever would not be obvious for an unfamiliar reader.
   - [x] 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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to