-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Thread safe reads for aggregators in IncrementalIndex #3956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
processing/src/test/java/io/druid/segment/data/ThreadSafetyAssertingAggregatorFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /* | ||
| * Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. Metamarkets 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 io.druid.segment.data; | ||
|
|
||
| import io.druid.query.aggregation.Aggregator; | ||
| import io.druid.query.aggregation.AggregatorFactory; | ||
| import io.druid.query.aggregation.BufferAggregator; | ||
| import io.druid.segment.ColumnSelectorFactory; | ||
| import org.junit.Assert; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * An AggregatorFactory that asserts thread safety. | ||
| * If the delegate aggregator factory is accessed in a thread unsafe manner throws AssertionError during read. | ||
| */ | ||
| public class ThreadSafetyAssertingAggregatorFactory extends AggregatorFactory | ||
| { | ||
| private final AggregatorFactory delegate; | ||
|
|
||
| public ThreadSafetyAssertingAggregatorFactory(AggregatorFactory delegate) | ||
| { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public Aggregator factorize(ColumnSelectorFactory metricFactory) | ||
| { | ||
| final Aggregator delegate1 = delegate.factorize(metricFactory); | ||
| final Aggregator delegate2 = delegate.factorize(metricFactory); | ||
| return new Aggregator() | ||
| { | ||
| @Override | ||
| public void aggregate() | ||
| { | ||
| delegate1.aggregate(); | ||
| Thread.yield(); | ||
| delegate2.aggregate(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() | ||
| { | ||
| delegate1.reset(); | ||
| Thread.yield(); | ||
| delegate2.reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object get() | ||
| { | ||
| Object o1 = delegate1.get(); | ||
| Thread.yield(); | ||
| Object o2 = delegate2.get(); | ||
| Assert.assertEquals("Unsafe Call to aggregator.get()", o1, o2); | ||
| return o1; | ||
| } | ||
|
|
||
| @Override | ||
| public float getFloat() | ||
| { | ||
| float o1 = delegate1.getFloat(); | ||
| Thread.yield(); | ||
| float o2 = delegate2.getFloat(); | ||
| Assert.assertTrue("Unsafe Call to aggregator.get()", o1 == o2); | ||
| return o1; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| { | ||
| delegate1.close(); | ||
| delegate2.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getLong() | ||
| { | ||
| long o1 = delegate1.getLong(); | ||
| Thread.yield(); | ||
| long o2 = delegate2.getLong(); | ||
| Assert.assertEquals("Unsafe Call to aggregator.get()", o1, o2); | ||
| return o1; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) | ||
| { | ||
| final BufferAggregator delegate1 = delegate.factorizeBuffered(metricFactory); | ||
| final BufferAggregator delegate2 = delegate.factorizeBuffered(metricFactory); | ||
| final int intermediateSize = delegate.getMaxIntermediateSize(); | ||
| return new BufferAggregator() | ||
| { | ||
| @Override | ||
| public void init(ByteBuffer buf, int position) | ||
| { | ||
| delegate1.init(buf, position); | ||
| delegate2.init(buf, position + intermediateSize); | ||
| } | ||
|
|
||
| @Override | ||
| public void aggregate(ByteBuffer buf, int position) | ||
| { | ||
| delegate1.aggregate(buf, position); | ||
| Thread.yield(); | ||
| delegate2.aggregate(buf, position + intermediateSize); | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(ByteBuffer buf, int position) | ||
| { | ||
| Object o1 = delegate1.get(buf, position); | ||
| Thread.yield(); | ||
| Object o2 = delegate2.get(buf, position + intermediateSize); | ||
| Assert.assertEquals("Unsafe Call to aggregator.get()", o1, o2); | ||
| return o1; | ||
| } | ||
|
|
||
| @Override | ||
| public float getFloat(ByteBuffer buf, int position) | ||
| { | ||
| float o1 = delegate1.getFloat(buf, position); | ||
| Thread.yield(); | ||
| float o2 = delegate2.getFloat(buf, position + intermediateSize); | ||
| Assert.assertTrue("Unsafe Call to aggregator.get()", o1 == o2); | ||
| return o1; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| { | ||
| delegate1.close(); | ||
| delegate2.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getLong(ByteBuffer buf, int position) | ||
| { | ||
| long o1 = delegate1.getLong(buf, position); | ||
| Thread.yield(); | ||
| long o2 = delegate2.getLong(buf, position + intermediateSize); | ||
| Assert.assertEquals("Unsafe Call to aggregator.get()", o1, o2); | ||
| return o1; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public Comparator getComparator() | ||
| { | ||
| return delegate.getComparator(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object combine(Object lhs, Object rhs) | ||
| { | ||
| return delegate.combine(lhs, rhs); | ||
| } | ||
|
|
||
| @Override | ||
| public AggregatorFactory getCombiningFactory() | ||
| { | ||
| return delegate.getCombiningFactory(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<AggregatorFactory> getRequiredColumns() | ||
| { | ||
| return delegate.getRequiredColumns(); | ||
| } | ||
|
|
||
| @Override | ||
| public Object deserialize(Object object) | ||
| { | ||
| return delegate.deserialize(object); | ||
| } | ||
|
|
||
| @Override | ||
| public Object finalizeComputation(Object object) | ||
| { | ||
| return delegate.finalizeComputation(object); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() | ||
| { | ||
| return delegate.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> requiredFields() | ||
| { | ||
| return delegate.requiredFields(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getTypeName() | ||
| { | ||
| return delegate.getTypeName(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getMaxIntermediateSize() | ||
| { | ||
| return 2 * delegate.getMaxIntermediateSize(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] getCacheKey() | ||
| { | ||
| return delegate.getCacheKey(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we replace yield with Thread.sleep(1), the issue is reproduced more frequently but this slows down the test considerably as it is done for every aggregate call.