forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Different accept methods to make MultiBucketConsumer thread safe
- Loading branch information
1 parent
9507e6d
commit 5153c45
Showing
3 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
|
||
package org.opensearch.search.aggregations.bucket; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.Repeat; | ||
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | ||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.document.SortedNumericDocValuesField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
|
@@ -41,6 +43,7 @@ | |
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.store.Directory; | ||
import org.opensearch.common.breaker.CircuitBreaker; | ||
import org.opensearch.common.breaker.CircuitBreakingException; | ||
import org.opensearch.index.mapper.NumberFieldMapper; | ||
import org.opensearch.indices.breaker.NoneCircuitBreakerService; | ||
import org.opensearch.search.aggregations.AggregatorFactories; | ||
|
@@ -51,9 +54,15 @@ | |
import org.opensearch.search.internal.SearchContext; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.opensearch.search.aggregations.MultiBucketConsumerService.DEFAULT_MAX_BUCKETS; | ||
|
||
@ThreadLeakScope(ThreadLeakScope.Scope.NONE) | ||
public class BucketsAggregatorTests extends AggregatorTestCase { | ||
|
||
public BucketsAggregator buildMergeAggregator() throws IOException { | ||
|
@@ -139,4 +148,88 @@ public void testBucketMergeAndDelete() throws IOException { | |
assertEquals(mergeAggregator.getDocCounts().get(i), i == 5 ? sum : 0); | ||
} | ||
} | ||
|
||
@Repeat(iterations = 1000, useConstantSeed = true) | ||
This comment has been minimized.
Sorry, something went wrong.
sohami
Collaborator
|
||
public void testMultiConsumerAcceptOne() throws Exception { | ||
CircuitBreaker breaker = mock(CircuitBreaker.class); | ||
MultiBucketConsumerService.MultiBucketConsumer multiBucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer( | ||
DEFAULT_MAX_BUCKETS, | ||
breaker | ||
); | ||
|
||
when(breaker.addEstimateBytesAndMaybeBreak(0, "allocated_buckets")).thenThrow(CircuitBreakingException.class); | ||
int numberOfThreads = 5; | ||
ExecutorService service = Executors.newFixedThreadPool(10); | ||
CountDownLatch latch = new CountDownLatch(numberOfThreads); | ||
long start = System.currentTimeMillis(); | ||
for (int i = 0; i < numberOfThreads; i++) { | ||
service.submit(() -> { | ||
try { | ||
multiBucketConsumer.accept1(0); | ||
} catch (CircuitBreakingException e) { | ||
System.out.println("CircuitBreaker was thrown"); | ||
} | ||
latch.countDown(); | ||
}); | ||
} | ||
long end = System.currentTimeMillis(); | ||
This comment has been minimized.
Sorry, something went wrong.
sohami
Collaborator
|
||
latch.await(); | ||
System.out.println("DEBUG: accept1 took " + (end - start) + " MilliSeconds"); | ||
} | ||
|
||
@Repeat(iterations = 1000, useConstantSeed = true) | ||
public void testMultiConsumerAcceptFunctionTwo() throws InterruptedException { | ||
CircuitBreaker breaker = mock(CircuitBreaker.class); | ||
MultiBucketConsumerService.MultiBucketConsumer multiBucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer( | ||
DEFAULT_MAX_BUCKETS, | ||
breaker | ||
); | ||
|
||
when(breaker.addEstimateBytesAndMaybeBreak(0, "allocated_buckets")).thenThrow(CircuitBreakingException.class); | ||
int numberOfThreads = 5; | ||
ExecutorService service = Executors.newFixedThreadPool(10); | ||
CountDownLatch latch = new CountDownLatch(numberOfThreads); | ||
long start = System.currentTimeMillis(); | ||
for (int i = 0; i < numberOfThreads; i++) { | ||
service.submit(() -> { | ||
try { | ||
multiBucketConsumer.accept2(0); | ||
} catch (CircuitBreakingException e) { | ||
System.out.println("CircuitBreaker was thrown"); | ||
} | ||
latch.countDown(); | ||
}); | ||
} | ||
long end = System.currentTimeMillis(); | ||
latch.await(); | ||
System.out.println("DEBUG: accept2 took " + (end - start) + " MilliSeconds"); | ||
} | ||
|
||
@Repeat(iterations = 1000, useConstantSeed = true) | ||
public void testMultiConsumerAcceptFunctionThree() throws InterruptedException { | ||
CircuitBreaker breaker = mock(CircuitBreaker.class); | ||
MultiBucketConsumerService.MultiBucketConsumer multiBucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer( | ||
DEFAULT_MAX_BUCKETS, | ||
breaker | ||
); | ||
|
||
when(breaker.addEstimateBytesAndMaybeBreak(0, "allocated_buckets")).thenThrow(CircuitBreakingException.class); | ||
int numberOfThreads = 5; | ||
ExecutorService service = Executors.newFixedThreadPool(10); | ||
CountDownLatch latch = new CountDownLatch(numberOfThreads); | ||
long start = System.currentTimeMillis(); | ||
for (int i = 0; i < numberOfThreads; i++) { | ||
service.submit(() -> { | ||
try { | ||
multiBucketConsumer.accept3(0); | ||
} catch (CircuitBreakingException e) { | ||
System.out.println("CircuitBreaker was thrown"); | ||
} | ||
latch.countDown(); | ||
}); | ||
} | ||
long end = System.currentTimeMillis(); | ||
latch.await(); | ||
System.out.println("DEBUG: accept3 took " + (end - start) + " MilliSeconds"); | ||
} | ||
} |
This file contains 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 will not work with
LongAdder
as sum is eventual sum and may never hit 1024 value, it can be 1023 and then jump to 1025 before this check happens.