Skip to content

Commit 268c2e2

Browse files
committed
Fix RandomExceptionCircuitBreakerTests
RandomExceptionCircuitBreakerTests never actually installed the FilteredAtomicReader that should throw random exceptions when field data is loaded.
1 parent ea9e095 commit 268c2e2

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

src/test/java/org/elasticsearch/indices/fielddata/breaker/RandomExceptionCircuitBreakerTests.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse;
2828
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
2929
import org.elasticsearch.action.search.SearchPhaseExecutionException;
30+
import org.elasticsearch.action.search.SearchRequestBuilder;
3031
import org.elasticsearch.client.Requests;
3132
import org.elasticsearch.common.settings.ImmutableSettings;
3233
import org.elasticsearch.common.settings.Settings;
@@ -37,8 +38,6 @@
3738
import org.elasticsearch.test.ElasticsearchIntegrationTest;
3839
import org.elasticsearch.test.engine.MockInternalEngine;
3940
import org.elasticsearch.test.engine.ThrowingAtomicReaderWrapper;
40-
import org.elasticsearch.test.junit.annotations.TestLogging;
41-
import org.elasticsearch.test.store.MockDirectoryHelper;
4241
import org.junit.Test;
4342

4443
import java.io.IOException;
@@ -55,9 +54,9 @@
5554
public 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;

src/test/java/org/elasticsearch/search/basic/SearchWithRandomExceptionsTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,10 @@ public void maybeThrow(ThrowingAtomicReaderWrapper.Flags flag) throws IOExceptio
286286
}
287287
break;
288288
}
289+
}
289290

291+
public boolean wrapTerms(String field) {
292+
return true;
290293
}
291294
}
292295

src/test/java/org/elasticsearch/test/engine/ThrowingAtomicReaderWrapper.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public static interface Thrower {
5959
* Maybe throws an exception ;)
6060
*/
6161
public void maybeThrow(Flags flag) throws IOException;
62+
63+
/**
64+
* If this method returns true the {@link Terms} instance for the given field
65+
* is wrapped with Thrower support otherwise no exception will be thrown for
66+
* the current {@link Terms} instance or any other instance obtained from it.
67+
*/
68+
public boolean wrapTerms(String field);
6269
}
6370

6471
public ThrowingAtomicReaderWrapper(AtomicReader in, Thrower thrower) {
@@ -95,8 +102,11 @@ public ThrowingFields(Fields in, Thrower thrower) {
95102
@Override
96103
public Terms terms(String field) throws IOException {
97104
Terms terms = super.terms(field);
98-
thrower.maybeThrow(Flags.Terms);
99-
return terms == null ? null : new ThrowingTerms(terms, thrower);
105+
if (thrower.wrapTerms(field)) {
106+
thrower.maybeThrow(Flags.Terms);
107+
return terms == null ? null : new ThrowingTerms(terms, thrower);
108+
}
109+
return terms;
100110
}
101111
}
102112

0 commit comments

Comments
 (0)