|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.basic; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.action.ActionFuture; |
| 24 | +import org.elasticsearch.action.NoShardAvailableActionException; |
| 25 | +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 26 | +import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; |
| 27 | +import org.elasticsearch.action.get.GetRequest; |
| 28 | +import org.elasticsearch.action.search.SearchResponse; |
| 29 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.common.unit.TimeValue; |
| 32 | +import org.elasticsearch.index.IndexNotFoundException; |
| 33 | +import org.elasticsearch.plugins.Plugin; |
| 34 | +import org.elasticsearch.test.ESIntegTestCase; |
| 35 | +import org.elasticsearch.test.disruption.NetworkDisruption; |
| 36 | +import org.elasticsearch.test.transport.MockTransportService; |
| 37 | + |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 43 | +import static org.hamcrest.Matchers.anyOf; |
| 44 | +import static org.hamcrest.Matchers.equalTo; |
| 45 | +import static org.hamcrest.Matchers.instanceOf; |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * This integration test contains two tests to provoke a search failure while initializing a new empty index: |
| 50 | + * <ul> |
| 51 | + * <li>testSearchDuringCreate: just tries to create an index and search, has low likelihood for provoking issue (but it does happen) |
| 52 | + * </li> |
| 53 | + * <li>testDelayIsolatedPrimary: delays network messages from all nodes except search coordinator to primary, ensuring that every test |
| 54 | + * run hits the case where a primary is initializing a newly created shard</i> |
| 55 | + * </ul> |
| 56 | + */ |
| 57 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST) |
| 58 | +public class SearchWhileInitializingEmptyIT extends ESIntegTestCase { |
| 59 | + |
| 60 | + @Override |
| 61 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 62 | + return Collections.singletonList(MockTransportService.TestPlugin.class); |
| 63 | + } |
| 64 | + |
| 65 | + public void testSearchDuringCreate() { |
| 66 | + ActionFuture<CreateIndexResponse> createFuture = prepareCreate("test").execute(); |
| 67 | + |
| 68 | + for (int i = 0; i < 100; ++i) { |
| 69 | + SearchResponse searchResponse = client().prepareSearch("test*").setAllowPartialSearchResults(randomBoolean()).get(); |
| 70 | + assertThat(searchResponse.getFailedShards(), equalTo(0)); |
| 71 | + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); |
| 72 | + } |
| 73 | + |
| 74 | + logger.info("done searching"); |
| 75 | + assertAcked(createFuture.actionGet()); |
| 76 | + } |
| 77 | + |
| 78 | + public void testDelayIsolatedPrimary() throws Exception { |
| 79 | + String[] originalNodes = internalCluster().getNodeNames(); |
| 80 | + String dataNode = internalCluster().startDataOnlyNode(); |
| 81 | + String coordinatorNode = internalCluster().startCoordinatingOnlyNode(Settings.EMPTY); |
| 82 | + |
| 83 | + NetworkDisruption.Bridge bridge = new NetworkDisruption.Bridge(coordinatorNode, Set.of(dataNode), Set.of(originalNodes)); |
| 84 | + NetworkDisruption scheme = new NetworkDisruption(bridge, new NetworkDisruption.NetworkDelay(NetworkDisruption.NetworkDelay.DEFAULT_DELAY_MIN)); |
| 85 | + setDisruptionScheme(scheme); |
| 86 | + scheme.startDisrupting(); |
| 87 | + |
| 88 | + ActionFuture<CreateIndexResponse> createFuture; |
| 89 | + try { |
| 90 | + Settings.Builder builder = Settings.builder().put(indexSettings()) |
| 91 | + .put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + "._name", dataNode); |
| 92 | + |
| 93 | + createFuture = |
| 94 | + internalCluster().masterClient().admin().indices().create(new CreateIndexRequest("test", builder.build()).timeout(TimeValue.ZERO)); |
| 95 | + |
| 96 | + // wait for available on coordinator |
| 97 | + assertBusy(() -> { |
| 98 | + try { |
| 99 | + client(coordinatorNode).get(new GetRequest("test", "0")).actionGet(); |
| 100 | + throw new IllegalStateException("non-assertion exception to escape assertBusy, get request must fail"); |
| 101 | + } catch (IndexNotFoundException e) { |
| 102 | + throw new AssertionError(e); |
| 103 | + } catch (NoShardAvailableActionException e) { |
| 104 | + // now coordinator knows about the index. |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // not available on data node. |
| 109 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> |
| 110 | + client(dataNode).get(new GetRequest("test", "0")).actionGet()); |
| 111 | + assertThat(exception, anyOf(instanceOf(NoShardAvailableActionException.class), instanceOf(IndexNotFoundException.class))); |
| 112 | + |
| 113 | + for (String indices : new String[] {"test*", "tes*", "test"}){ |
| 114 | + logger.info("Searching for [{}]", indices); |
| 115 | + SearchResponse searchResponse = |
| 116 | + client(coordinatorNode).prepareSearch(indices).setAllowPartialSearchResults(randomBoolean()).get(); |
| 117 | + assertThat(searchResponse.getFailedShards(), equalTo(0)); |
| 118 | + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); |
| 119 | + } |
| 120 | + } finally { |
| 121 | + internalCluster().clearDisruptionScheme(true); |
| 122 | + } |
| 123 | + createFuture.actionGet(); |
| 124 | + } |
| 125 | +} |
0 commit comments