|
42 | 42 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
43 | 43 | import org.elasticsearch.index.Index;
|
44 | 44 | import org.elasticsearch.index.IndexModule;
|
| 45 | +import org.elasticsearch.index.IndexNotFoundException; |
45 | 46 | import org.elasticsearch.index.IndexService;
|
46 | 47 | import org.elasticsearch.index.IndexSettings;
|
47 | 48 | import org.elasticsearch.index.query.AbstractQueryBuilder;
|
@@ -113,7 +114,8 @@ protected boolean resetNodeAfterTest() {
|
113 | 114 |
|
114 | 115 | @Override
|
115 | 116 | protected Collection<Class<? extends Plugin>> getPlugins() {
|
116 |
| - return pluginList(FailOnRewriteQueryPlugin.class, CustomScriptPlugin.class, InternalOrPrivateSettingsPlugin.class); |
| 117 | + return pluginList(FailOnRewriteQueryPlugin.class, CustomScriptPlugin.class, InternalOrPrivateSettingsPlugin.class, |
| 118 | + MockSearchService.TestPlugin.class); |
117 | 119 | }
|
118 | 120 |
|
119 | 121 | public static class CustomScriptPlugin extends MockScriptPlugin {
|
@@ -288,6 +290,7 @@ public void onFailure(Exception e) {
|
288 | 290 | service.executeFetchPhase(req, new SearchTask(123L, "", "", "", null, Collections.emptyMap()), listener);
|
289 | 291 | listener.get();
|
290 | 292 | if (useScroll) {
|
| 293 | + // have to free context since this test does not remove the index from IndicesService. |
291 | 294 | service.freeContext(searchPhaseResult.getRequestId());
|
292 | 295 | }
|
293 | 296 | } catch (ExecutionException ex) {
|
@@ -316,6 +319,59 @@ public void onFailure(Exception e) {
|
316 | 319 | assertEquals(0, totalStats.getFetchCurrent());
|
317 | 320 | }
|
318 | 321 |
|
| 322 | + public void testSearchWhileIndexDeletedDoesNotLeakSearchContext() throws ExecutionException, InterruptedException { |
| 323 | + createIndex("index"); |
| 324 | + client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get(); |
| 325 | + |
| 326 | + IndicesService indicesService = getInstanceFromNode(IndicesService.class); |
| 327 | + IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index")); |
| 328 | + IndexShard indexShard = indexService.getShard(0); |
| 329 | + |
| 330 | + MockSearchService service = (MockSearchService) getInstanceFromNode(SearchService.class); |
| 331 | + service.setOnPutContext( |
| 332 | + context -> { |
| 333 | + if (context.indexShard() == indexShard) { |
| 334 | + assertAcked(client().admin().indices().prepareDelete("index")); |
| 335 | + } |
| 336 | + } |
| 337 | + ); |
| 338 | + |
| 339 | + SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(true); |
| 340 | + SearchRequest scrollSearchRequest = new SearchRequest().allowPartialSearchResults(true) |
| 341 | + .scroll(new Scroll(TimeValue.timeValueMinutes(1))); |
| 342 | + |
| 343 | + // the scrolls are not explicitly freed, but should all be gone when the test finished. |
| 344 | + // for completeness, we also randomly test the regular search path. |
| 345 | + final boolean useScroll = randomBoolean(); |
| 346 | + PlainActionFuture<SearchPhaseResult> result = new PlainActionFuture<>(); |
| 347 | + ShardSearchLocalRequest shardRequest; |
| 348 | + if (useScroll) { |
| 349 | + shardRequest = new ShardScrollRequestTest(indexShard.shardId()); |
| 350 | + } else { |
| 351 | + shardRequest = new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, |
| 352 | + new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f, |
| 353 | + true, null, null); |
| 354 | + } |
| 355 | + service.executeQueryPhase( |
| 356 | + shardRequest, |
| 357 | + new SearchTask(123L, "", "", "", null, Collections.emptyMap()), result); |
| 358 | + |
| 359 | + try { |
| 360 | + result.get(); |
| 361 | + } catch (Exception e) { |
| 362 | + // ok |
| 363 | + } |
| 364 | + |
| 365 | + expectThrows(IndexNotFoundException.class, () -> client().admin().indices().prepareGetIndex().setIndices("index").get()); |
| 366 | + |
| 367 | + assertEquals(0, service.getActiveContexts()); |
| 368 | + |
| 369 | + SearchStats.Stats totalStats = indexShard.searchStats().getTotal(); |
| 370 | + assertEquals(0, totalStats.getQueryCurrent()); |
| 371 | + assertEquals(0, totalStats.getScrollCurrent()); |
| 372 | + assertEquals(0, totalStats.getFetchCurrent()); |
| 373 | + } |
| 374 | + |
319 | 375 | public void testTimeout() throws IOException {
|
320 | 376 | createIndex("index");
|
321 | 377 | final SearchService service = getInstanceFromNode(SearchService.class);
|
@@ -480,6 +536,9 @@ public void testMaxOpenScrollContexts() throws RuntimeException, IOException {
|
480 | 536 | "This limit can be set by changing the [search.max_open_scroll_context] setting."
|
481 | 537 | )
|
482 | 538 | );
|
| 539 | + clearScrollRequest = new ClearScrollRequest(); |
| 540 | + clearScrollRequest.addScrollId("_all"); |
| 541 | + client().clearScroll(clearScrollRequest); |
483 | 542 | } finally {
|
484 | 543 | client().admin().cluster().prepareUpdateSettings()
|
485 | 544 | .setPersistentSettings(
|
|
0 commit comments