|
47 | 47 | import org.elasticsearch.index.query.QueryBuilder;
|
48 | 48 | import org.elasticsearch.index.query.QueryBuilders;
|
49 | 49 | import org.elasticsearch.index.query.QueryRewriteContext;
|
50 |
| -import org.elasticsearch.index.query.SearchExecutionContext; |
| 50 | +import org.elasticsearch.index.shard.IllegalIndexShardStateException; |
51 | 51 | import org.elasticsearch.index.shard.IndexShard;
|
52 | 52 | import org.elasticsearch.index.shard.ShardId;
|
53 | 53 | import org.elasticsearch.indices.IndexClosedException;
|
|
59 | 59 | import org.elasticsearch.search.DummyQueryBuilder;
|
60 | 60 | import org.elasticsearch.tasks.TaskInfo;
|
61 | 61 | import org.elasticsearch.test.ESIntegTestCase;
|
| 62 | +import org.elasticsearch.test.InternalTestCluster; |
62 | 63 | import org.elasticsearch.test.MockLog;
|
63 | 64 | import org.elasticsearch.test.junit.annotations.TestLogging;
|
64 | 65 | import org.elasticsearch.test.transport.MockTransportService;
|
|
78 | 79 | import java.util.HashSet;
|
79 | 80 | import java.util.List;
|
80 | 81 | import java.util.Map;
|
| 82 | +import java.util.Set; |
81 | 83 | import java.util.concurrent.CancellationException;
|
82 | 84 | import java.util.concurrent.CountDownLatch;
|
83 | 85 | import java.util.concurrent.TimeUnit;
|
|
87 | 89 | import java.util.function.Predicate;
|
88 | 90 | import java.util.stream.IntStream;
|
89 | 91 |
|
90 |
| -import static java.util.Collections.singletonList; |
91 | 92 | import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
|
92 | 93 | import static org.elasticsearch.index.shard.IndexShardTestCase.closeShardNoCheck;
|
93 | 94 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
@@ -193,7 +194,7 @@ public void setUp() throws Exception {
|
193 | 194 |
|
194 | 195 | @Override
|
195 | 196 | protected Collection<Class<? extends Plugin>> nodePlugins() {
|
196 |
| - return List.of(TestMapperPlugin.class, ExceptionOnRewriteQueryPlugin.class, BlockingOnRewriteQueryPlugin.class); |
| 197 | + return List.of(TestMapperPlugin.class, BlockingOnRewriteQueryPlugin.class); |
197 | 198 | }
|
198 | 199 |
|
199 | 200 | @Override
|
@@ -445,30 +446,33 @@ public void testFieldMetricsAndDimensions() {
|
445 | 446 | assertThat(response.get().get("some_dimension").get("keyword").nonDimensionIndices(), array(equalTo("new_index")));
|
446 | 447 | }
|
447 | 448 |
|
448 |
| - public void testFailures() throws InterruptedException { |
| 449 | + public void testFailures() throws IOException { |
449 | 450 | // in addition to the existing "old_index" and "new_index", create two where the test query throws an error on rewrite
|
450 | 451 | assertAcked(prepareCreate("index1-error"), prepareCreate("index2-error"));
|
451 | 452 | ensureGreen("index1-error", "index2-error");
|
452 |
| - FieldCapabilitiesResponse response = client().prepareFieldCaps() |
| 453 | + |
| 454 | + // Closed shards will result to index error because shards must be in readable state |
| 455 | + closeShards(internalCluster(), "index1-error", "index2-error"); |
| 456 | + |
| 457 | + FieldCapabilitiesResponse response = client().prepareFieldCaps("old_index", "new_index", "index1-error", "index2-error") |
453 | 458 | .setFields("*")
|
454 |
| - .setIndexFilter(new ExceptionOnRewriteQueryBuilder()) |
455 | 459 | .get();
|
456 | 460 | assertEquals(1, response.getFailures().size());
|
457 | 461 | assertEquals(2, response.getFailedIndicesCount());
|
458 | 462 | assertThat(response.getFailures().get(0).getIndices(), arrayContainingInAnyOrder("index1-error", "index2-error"));
|
459 | 463 | Exception failure = response.getFailures().get(0).getException();
|
460 |
| - assertEquals(IllegalArgumentException.class, failure.getClass()); |
461 |
| - assertEquals("I throw because I choose to.", failure.getMessage()); |
| 464 | + assertEquals(IllegalIndexShardStateException.class, failure.getClass()); |
| 465 | + assertEquals("CurrentState[CLOSED] operations only allowed when shard state is one of [POST_RECOVERY, STARTED]", failure.getMessage()); |
462 | 466 |
|
463 | 467 | // the "indices" section should not include failed ones
|
464 | 468 | assertThat(Arrays.asList(response.getIndices()), containsInAnyOrder("old_index", "new_index"));
|
465 | 469 |
|
466 | 470 | // if all requested indices failed, we fail the request by throwing the exception
|
467 |
| - IllegalArgumentException ex = expectThrows( |
468 |
| - IllegalArgumentException.class, |
469 |
| - client().prepareFieldCaps("index1-error", "index2-error").setFields("*").setIndexFilter(new ExceptionOnRewriteQueryBuilder()) |
| 471 | + IllegalIndexShardStateException ex = expectThrows( |
| 472 | + IllegalIndexShardStateException.class, |
| 473 | + client().prepareFieldCaps("index1-error", "index2-error").setFields("*") |
470 | 474 | );
|
471 |
| - assertEquals("I throw because I choose to.", ex.getMessage()); |
| 475 | + assertEquals("CurrentState[CLOSED] operations only allowed when shard state is one of [POST_RECOVERY, STARTED]", ex.getMessage()); |
472 | 476 | }
|
473 | 477 |
|
474 | 478 | private void populateTimeRangeIndices() throws Exception {
|
@@ -913,45 +917,17 @@ private void assertIndices(FieldCapabilitiesResponse response, String... indices
|
913 | 917 | assertArrayEquals(indices, response.getIndices());
|
914 | 918 | }
|
915 | 919 |
|
916 |
| - /** |
917 |
| - * Adds an "exception" query that throws on rewrite if the index name contains the string "error" |
918 |
| - */ |
919 |
| - public static class ExceptionOnRewriteQueryPlugin extends Plugin implements SearchPlugin { |
920 |
| - |
921 |
| - public ExceptionOnRewriteQueryPlugin() {} |
922 |
| - |
923 |
| - @Override |
924 |
| - public List<QuerySpec<?>> getQueries() { |
925 |
| - return singletonList( |
926 |
| - new QuerySpec<>("exception", ExceptionOnRewriteQueryBuilder::new, p -> new ExceptionOnRewriteQueryBuilder()) |
927 |
| - ); |
928 |
| - } |
929 |
| - } |
930 |
| - |
931 |
| - static class ExceptionOnRewriteQueryBuilder extends DummyQueryBuilder { |
932 |
| - |
933 |
| - public static final String NAME = "exception"; |
934 |
| - |
935 |
| - ExceptionOnRewriteQueryBuilder() {} |
936 |
| - |
937 |
| - ExceptionOnRewriteQueryBuilder(StreamInput in) throws IOException { |
938 |
| - super(in); |
939 |
| - } |
940 |
| - |
941 |
| - @Override |
942 |
| - protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { |
943 |
| - SearchExecutionContext searchExecutionContext = queryRewriteContext.convertToSearchExecutionContext(); |
944 |
| - if (searchExecutionContext != null) { |
945 |
| - if (searchExecutionContext.indexMatches("*error*")) { |
946 |
| - throw new IllegalArgumentException("I throw because I choose to."); |
| 920 | + static void closeShards(InternalTestCluster cluster, String... indices) throws IOException { |
| 921 | + final Set<String> indicesToClose = Set.of(indices); |
| 922 | + for (String node :cluster.getNodeNames()) { |
| 923 | + final IndicesService indicesService = cluster.getInstance(IndicesService.class, node); |
| 924 | + for (IndexService indexService : indicesService) { |
| 925 | + if (indicesToClose.contains(indexService.getMetadata().getIndex().getName())) { |
| 926 | + for (IndexShard indexShard : indexService) { |
| 927 | + closeShardNoCheck(indexShard); |
| 928 | + } |
947 | 929 | }
|
948 | 930 | }
|
949 |
| - return this; |
950 |
| - } |
951 |
| - |
952 |
| - @Override |
953 |
| - public String getWriteableName() { |
954 |
| - return NAME; |
955 | 931 | }
|
956 | 932 | }
|
957 | 933 |
|
|
0 commit comments