Skip to content

Commit 24311e0

Browse files
Stabilize IndicesRequestCacheIT test (#75315)
This test fails from time to time on 6.8 This patch tries to address failures in 2 ways: 1. Disables automatic refreshes. Request cache gets invalidated when refresh happens (as IndexReader's version may change), which may lead to test failures. 2. Makes predictable indexing. Before we were using random indexing requests that could lead to multiple segments with deleted documents. This patch ensures that we are indexing only necessary docs in a single bulk request which should create only a single segment for each index. Relates to #61565
1 parent 98dca65 commit 24311e0

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java

+34-37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.elasticsearch.action.admin.indices.alias.Alias;
2323
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
24+
import org.elasticsearch.action.bulk.BulkRequestBuilder;
25+
import org.elasticsearch.action.bulk.BulkResponse;
2426
import org.elasticsearch.action.search.SearchResponse;
2527
import org.elasticsearch.action.search.SearchType;
2628
import org.elasticsearch.client.Client;
@@ -41,6 +43,7 @@
4143
import java.util.Arrays;
4244
import java.util.List;
4345

46+
import static org.elasticsearch.index.IndexSettings.INDEX_REFRESH_INTERVAL_SETTING;
4447
import static org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram;
4548
import static org.elasticsearch.search.aggregations.AggregationBuilders.dateRange;
4649
import static org.elasticsearch.search.aggregations.AggregationBuilders.filter;
@@ -185,29 +188,27 @@ public void testQueryRewriteMissingValues() throws Exception {
185188
assertCacheState(client, "index", 2, 1);
186189
}
187190

188-
public void testQueryRewriteDates() throws Exception {
191+
public void testQueryRewriteDates() {
189192
Client client = client();
190193
assertAcked(client.admin().indices().prepareCreate("index").addMapping("type", "d", "type=date")
191194
.setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
192-
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get());
193-
indexRandom(true, client.prepareIndex("index", "type", "1").setSource("d", "2014-01-01T00:00:00"),
194-
client.prepareIndex("index", "type", "2").setSource("d", "2014-02-01T00:00:00"),
195-
client.prepareIndex("index", "type", "3").setSource("d", "2014-03-01T00:00:00"),
196-
client.prepareIndex("index", "type", "4").setSource("d", "2014-04-01T00:00:00"),
197-
client.prepareIndex("index", "type", "5").setSource("d", "2014-05-01T00:00:00"),
198-
client.prepareIndex("index", "type", "6").setSource("d", "2014-06-01T00:00:00"),
199-
client.prepareIndex("index", "type", "7").setSource("d", "2014-07-01T00:00:00"),
200-
client.prepareIndex("index", "type", "8").setSource("d", "2014-08-01T00:00:00"),
201-
client.prepareIndex("index", "type", "9").setSource("d", "2014-09-01T00:00:00"));
202-
ensureSearchable("index");
203-
assertCacheState(client, "index", 0, 0);
195+
.put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "-1") // disable automatic refreshes
196+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get());
204197

205-
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
206-
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge("index").setFlush(true).get();
207-
ElasticsearchAssertions.assertAllSuccessful(forceMergeResponse);
208-
refresh();
198+
BulkRequestBuilder bulkBuilder = client().prepareBulk();
199+
bulkBuilder.add(client.prepareIndex("index", "type", "1").setSource("d", "2014-01-01T00:00:00"));
200+
bulkBuilder.add(client.prepareIndex("index", "type", "2").setSource("d", "2014-02-01T00:00:00"));
201+
bulkBuilder.add(client.prepareIndex("index", "type", "3").setSource("d", "2014-03-01T00:00:00"));
202+
bulkBuilder.add(client.prepareIndex("index", "type", "4").setSource("d", "2014-04-01T00:00:00"));
203+
bulkBuilder.add(client.prepareIndex("index", "type", "5").setSource("d", "2014-05-01T00:00:00"));
204+
bulkBuilder.add(client.prepareIndex("index", "type", "6").setSource("d", "2014-06-01T00:00:00"));
205+
bulkBuilder.add(client.prepareIndex("index", "type", "7").setSource("d", "2014-07-01T00:00:00"));
206+
bulkBuilder.add(client.prepareIndex("index", "type", "8").setSource("d", "2014-08-01T00:00:00"));
207+
bulkBuilder.add(client.prepareIndex("index", "type", "9").setSource("d", "2014-09-01T00:00:00"));
208+
BulkResponse bulkResponse = bulkBuilder.execute().actionGet();
209+
assertThat(bulkResponse.hasFailures() ? bulkResponse.buildFailureMessage() : "", bulkResponse.hasFailures(), equalTo(false));
209210
ensureSearchable("index");
210-
211+
refresh("index");
211212
assertCacheState(client, "index", 0, 0);
212213

213214
final SearchResponse r1 = client.prepareSearch("index").setSearchType(SearchType.QUERY_THEN_FETCH).setSize(0)
@@ -232,9 +233,10 @@ public void testQueryRewriteDates() throws Exception {
232233
assertCacheState(client, "index", 2, 1);
233234
}
234235

235-
public void testQueryRewriteDatesWithNow() throws Exception {
236+
public void testQueryRewriteDatesWithNow() {
236237
Client client = client();
237238
Settings settings = Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true)
239+
.put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "-1") // disable automatic refreshes
238240
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build();
239241
assertAcked(client.admin().indices().prepareCreate("index-1").addMapping("type", "d", "type=date")
240242
.setSettings(settings).get());
@@ -243,26 +245,21 @@ public void testQueryRewriteDatesWithNow() throws Exception {
243245
assertAcked(client.admin().indices().prepareCreate("index-3").addMapping("type", "d", "type=date")
244246
.setSettings(settings).get());
245247
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
246-
indexRandom(true, client.prepareIndex("index-1", "type", "1").setSource("d", now),
247-
client.prepareIndex("index-1", "type", "2").setSource("d", now.minusDays(1)),
248-
client.prepareIndex("index-1", "type", "3").setSource("d", now.minusDays(2)),
249-
client.prepareIndex("index-2", "type", "4").setSource("d", now.minusDays(3)),
250-
client.prepareIndex("index-2", "type", "5").setSource("d", now.minusDays(4)),
251-
client.prepareIndex("index-2", "type", "6").setSource("d", now.minusDays(5)),
252-
client.prepareIndex("index-3", "type", "7").setSource("d", now.minusDays(6)),
253-
client.prepareIndex("index-3", "type", "8").setSource("d", now.minusDays(7)),
254-
client.prepareIndex("index-3", "type", "9").setSource("d", now.minusDays(8)));
255-
ensureSearchable("index-1", "index-2", "index-3");
256-
assertCacheState(client, "index-1", 0, 0);
257-
assertCacheState(client, "index-2", 0, 0);
258-
assertCacheState(client, "index-3", 0, 0);
259248

260-
// Force merge the index to ensure there can be no background merges during the subsequent searches that would invalidate the cache
261-
ForceMergeResponse forceMergeResponse = client.admin().indices().prepareForceMerge("index-1", "index-2", "index-3").setFlush(true)
262-
.get();
263-
ElasticsearchAssertions.assertAllSuccessful(forceMergeResponse);
264-
refresh();
249+
BulkRequestBuilder bulkBuilder = client().prepareBulk();
250+
bulkBuilder.add(client.prepareIndex("index-1", "type", "1").setSource("d", now));
251+
bulkBuilder.add(client.prepareIndex("index-1", "type", "2").setSource("d", now.minusDays(1)));
252+
bulkBuilder.add(client.prepareIndex("index-1", "type", "3").setSource("d", now.minusDays(2)));
253+
bulkBuilder.add(client.prepareIndex("index-2", "type", "4").setSource("d", now.minusDays(3)));
254+
bulkBuilder.add(client.prepareIndex("index-2", "type", "5").setSource("d", now.minusDays(4)));
255+
bulkBuilder.add(client.prepareIndex("index-2", "type", "6").setSource("d", now.minusDays(5)));
256+
bulkBuilder.add(client.prepareIndex("index-3", "type", "7").setSource("d", now.minusDays(6)));
257+
bulkBuilder.add(client.prepareIndex("index-3", "type", "8").setSource("d", now.minusDays(7)));
258+
bulkBuilder.add(client.prepareIndex("index-3", "type", "9").setSource("d", now.minusDays(8)));
259+
BulkResponse bulkResponse = bulkBuilder.execute().actionGet();
260+
assertThat(bulkResponse.hasFailures() ? bulkResponse.buildFailureMessage() : "", bulkResponse.hasFailures(), equalTo(false));
265261
ensureSearchable("index-1", "index-2", "index-3");
262+
refresh("index-1", "index-2", "index-3");
266263

267264
assertCacheState(client, "index-1", 0, 0);
268265
assertCacheState(client, "index-2", 0, 0);

0 commit comments

Comments
 (0)