2727import org .elasticsearch .action .admin .cluster .node .stats .NodesStatsResponse ;
2828import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
2929import org .elasticsearch .action .search .SearchPhaseExecutionException ;
30+ import org .elasticsearch .action .search .SearchRequestBuilder ;
3031import org .elasticsearch .client .Requests ;
3132import org .elasticsearch .common .settings .ImmutableSettings ;
3233import org .elasticsearch .common .settings .Settings ;
3738import org .elasticsearch .test .ElasticsearchIntegrationTest ;
3839import org .elasticsearch .test .engine .MockInternalEngine ;
3940import org .elasticsearch .test .engine .ThrowingAtomicReaderWrapper ;
40- import org .elasticsearch .test .junit .annotations .TestLogging ;
41- import org .elasticsearch .test .store .MockDirectoryHelper ;
4241import org .junit .Test ;
4342
4443import java .io .IOException ;
5554public class RandomExceptionCircuitBreakerTests extends ElasticsearchIntegrationTest {
5655
5756 @ Test
58- @ TestLogging ("org.elasticsearch.indices.fielddata.breaker:TRACE,org.elasticsearch.index.fielddata:TRACE,org.elasticsearch.common.breaker:TRACE" )
5957 public void testBreakerWithRandomExceptions () throws IOException , InterruptedException , ExecutionException {
6058 final int numShards = between (1 , 5 );
59+ final int numReplicas = randomIntBetween (0 , 1 );
6160 String mapping = XContentFactory .jsonBuilder ()
6261 .startObject ()
6362 .startObject ("type" )
@@ -80,33 +79,34 @@ public void testBreakerWithRandomExceptions() throws IOException, InterruptedExc
8079 .endObject () // type
8180 .endObject () // {}
8281 .string ();
83- final double exceptionRate ;
84- final double exceptionOnOpenRate ;
82+ final double topLevelRate ;
83+ final double lowLevelRate ;
8584 if (frequently ()) {
8685 if (randomBoolean ()) {
8786 if (randomBoolean ()) {
88- exceptionOnOpenRate = 1.0 /between (5 , 100 );
89- exceptionRate = 0.0d ;
87+ lowLevelRate = 1.0 /between (2 , 10 );
88+ topLevelRate = 0.0d ;
9089 } else {
91- exceptionRate = 1.0 /between (5 , 100 );
92- exceptionOnOpenRate = 0.0d ;
90+ topLevelRate = 1.0 /between (2 , 10 );
91+ lowLevelRate = 0.0d ;
9392 }
9493 } else {
95- exceptionOnOpenRate = 1.0 /between (5 , 100 );
96- exceptionRate = 1.0 /between (5 , 100 );
94+ lowLevelRate = 1.0 /between (2 , 10 );
95+ topLevelRate = 1.0 /between (2 , 10 );
9796 }
9897 } else {
9998 // rarely no exception
100- exceptionRate = 0d ;
101- exceptionOnOpenRate = 0d ;
99+ topLevelRate = 0d ;
100+ lowLevelRate = 0d ;
102101 }
103102
104103 ImmutableSettings .Builder settings = settingsBuilder ()
105104 .put ("index.number_of_shards" , numShards )
106- .put ("index.number_of_replicas" , randomIntBetween (0 , 1 ))
107- .put (MockDirectoryHelper .RANDOM_IO_EXCEPTION_RATE , exceptionRate )
108- .put (MockDirectoryHelper .RANDOM_IO_EXCEPTION_RATE_ON_OPEN , exceptionOnOpenRate )
109- .put (MockDirectoryHelper .CHECK_INDEX_ON_CLOSE , true );
105+ .put ("index.number_of_replicas" , numReplicas )
106+ .put (MockInternalEngine .READER_WRAPPER_TYPE , RandomExceptionDirectoryReaderWrapper .class .getName ())
107+ .put (EXCEPTION_TOP_LEVEL_RATIO_KEY , topLevelRate )
108+ .put (EXCEPTION_LOW_LEVEL_RATIO_KEY , lowLevelRate )
109+ .put (MockInternalEngine .WRAP_READER_RATIO , 1.0d );
110110 logger .info ("creating index: [test] using settings: [{}]" , settings .build ().getAsMap ());
111111 client ().admin ().indices ().prepareCreate ("test" )
112112 .setSettings (settings )
@@ -137,14 +137,32 @@ public void testBreakerWithRandomExceptions() throws IOException, InterruptedExc
137137 logger .info ("Refresh failed: [{}] numShardsFailed: [{}], shardFailuresLength: [{}], successfulShards: [{}], totalShards: [{}] " ,
138138 refreshFailed , refreshResponse .getFailedShards (), refreshResponse .getShardFailures ().length ,
139139 refreshResponse .getSuccessfulShards (), refreshResponse .getTotalShards ());
140- final int numSearches = atLeast (10 );
140+ final int numSearches = atLeast (50 );
141+ NodesStatsResponse resp = client ().admin ().cluster ().prepareNodesStats ()
142+ .clear ().setBreaker (true ).execute ().actionGet ();
143+ for (NodeStats stats : resp .getNodes ()) {
144+ assertThat ("Breaker is set to 0" , stats .getBreaker ().getEstimated (), equalTo (0L ));
145+ }
141146
142147 for (int i = 0 ; i < numSearches ; i ++) {
148+ SearchRequestBuilder searchRequestBuilder = client ().prepareSearch ().setQuery (QueryBuilders .matchAllQuery ());
149+ switch (randomIntBetween (0 , 5 )) {
150+ case 5 :
151+ case 4 :
152+ case 3 :
153+ searchRequestBuilder .addSort ("test-str" , SortOrder .ASC );
154+ // fall through - sometimes get both fields
155+ case 2 :
156+ case 1 :
157+ default :
158+ searchRequestBuilder .addSort ("test-num" , SortOrder .ASC );
159+
160+ }
161+ boolean success = false ;
143162 try {
144163 // Sort by the string and numeric fields, to load them into field data
145- client ().prepareSearch ().setQuery (QueryBuilders .matchAllQuery ())
146- .addSort ("test-str" , SortOrder .ASC )
147- .addSort ("test-num" , SortOrder .ASC ).get ();
164+ searchRequestBuilder .get ();
165+ success = true ;
148166 } catch (SearchPhaseExecutionException ex ) {
149167 logger .info ("expected SearchPhaseException: [{}]" , ex .getMessage ());
150168 }
@@ -155,9 +173,10 @@ public void testBreakerWithRandomExceptions() throws IOException, InterruptedExc
155173 // breaker adjustment code, it should show up here by the breaker
156174 // estimate being either positive or negative.
157175 client ().admin ().indices ().prepareClearCache ("test" ).setFieldDataCache (true ).execute ().actionGet ();
158- NodesStatsResponse resp = client ().admin ().cluster ().prepareNodesStats ().all ().execute ().actionGet ();
159- for (NodeStats stats : resp .getNodes ()) {
160- assertThat ("Breaker reset to 0" , stats .getBreaker ().getEstimated (), equalTo (0L ));
176+ NodesStatsResponse nodeStats = client ().admin ().cluster ().prepareNodesStats ()
177+ .clear ().setBreaker (true ).execute ().actionGet ();
178+ for (NodeStats stats : nodeStats .getNodes ()) {
179+ assertThat ("Breaker reset to 0 last search success: " + success + " mapping: " + mapping , stats .getBreaker ().getEstimated (), equalTo (0L ));
161180 }
162181 }
163182 }
@@ -217,10 +236,15 @@ public void maybeThrow(ThrowingAtomicReaderWrapper.Flags flag) throws IOExceptio
217236 }
218237 break ;
219238 }
239+ }
220240
241+ public boolean wrapTerms (String field ) {
242+ return field .startsWith ("test" );
221243 }
222244 }
223245
246+
247+
224248 public RandomExceptionDirectoryReaderWrapper (DirectoryReader in , Settings settings ) {
225249 super (in , new ThrowingSubReaderWrapper (settings ));
226250 this .settings = settings ;
0 commit comments