@@ -1747,8 +1747,11 @@ public void testCancellationWithoutCircuitBreaker() throws Exception {
1747
1747
int batchedReduceSize = randomIntBetween (2 , expectedNumResults - 1 );
1748
1748
SearchRequest request = getAggregationSearchRequestWithBatchedReduceSize (batchedReduceSize );
1749
1749
AssertingCircuitBreaker circuitBreaker = new AssertingCircuitBreaker (CircuitBreaker .REQUEST );
1750
- AtomicInteger checkCount = new AtomicInteger (0 );
1751
- int cancelAfter = expectedNumResults / 2 ;
1750
+ // To make it deterministic, we can count the number of times the partialReduce and reduce are called
1751
+ // The exception is only thrown during the call to reduce which will happen once all shard level
1752
+ // results have arrived
1753
+ int partialReduceMethodCallCount = expectedNumResults / batchedReduceSize ;
1754
+ AtomicInteger checkCount = new AtomicInteger (expectedNumResults + partialReduceMethodCallCount );
1752
1755
1753
1756
QueryPhaseResultConsumer consumer = searchPhaseController .newSearchPhaseResults (
1754
1757
fixedExecutor ,
@@ -1758,7 +1761,7 @@ public void testCancellationWithoutCircuitBreaker() throws Exception {
1758
1761
expectedNumResults ,
1759
1762
exc -> {},
1760
1763
() -> {
1761
- return checkCount .incrementAndGet () > cancelAfter ;
1764
+ return checkCount .decrementAndGet () <= 0 ;
1762
1765
}
1763
1766
);
1764
1767
@@ -1775,9 +1778,8 @@ public void testCancellationDoesNotMaskCircuitBreakerException() throws Exceptio
1775
1778
1776
1779
// making sure circuit breaker trips first
1777
1780
circuitBreaker .shouldBreak .set (true );
1778
- AtomicInteger checkCount = new AtomicInteger (0 );
1779
- int cancelAfter = expectedNumResults + 1 ;
1780
-
1781
+ int partialReduceMethodCallCount = expectedNumResults / batchedReduceSize ;
1782
+ AtomicInteger checkCount = new AtomicInteger (expectedNumResults + partialReduceMethodCallCount );
1781
1783
QueryPhaseResultConsumer consumer = searchPhaseController .newSearchPhaseResults (
1782
1784
fixedExecutor ,
1783
1785
circuitBreaker ,
@@ -1786,7 +1788,7 @@ public void testCancellationDoesNotMaskCircuitBreakerException() throws Exceptio
1786
1788
expectedNumResults ,
1787
1789
exc -> {},
1788
1790
() -> {
1789
- return checkCount .incrementAndGet () > cancelAfter ;
1791
+ return checkCount .decrementAndGet () <= 0 ;
1790
1792
}
1791
1793
);
1792
1794
@@ -1826,13 +1828,7 @@ private static void consumeShardLevelQueryPhaseResultsAsync(int expectedNumResul
1826
1828
result .setShardIndex (index );
1827
1829
result .size (1 );
1828
1830
1829
- try {
1830
- consumer .consumeResult (result , latch ::countDown );
1831
- } catch (Exception e ) {
1832
- // Ensure latch counts down even on cancellation
1833
- latch .countDown ();
1834
- // Don't rethrow - let the thread complete normally
1835
- }
1831
+ consumer .consumeResult (result , latch ::countDown );
1836
1832
});
1837
1833
threads [index ].start ();
1838
1834
}
0 commit comments