Skip to content

Commit 6ab512d

Browse files
Fix field caps integration tests
1 parent c20d9e8 commit 6ab512d

File tree

2 files changed

+38
-72
lines changed

2 files changed

+38
-72
lines changed

server/src/internalClusterTest/java/org/elasticsearch/search/fieldcaps/CCSFieldCapabilitiesIT.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
1515
import org.elasticsearch.client.internal.Client;
1616
import org.elasticsearch.common.settings.Settings;
17-
import org.elasticsearch.plugins.Plugin;
18-
import org.elasticsearch.search.fieldcaps.FieldCapabilitiesIT.ExceptionOnRewriteQueryBuilder;
19-
import org.elasticsearch.search.fieldcaps.FieldCapabilitiesIT.ExceptionOnRewriteQueryPlugin;
17+
import org.elasticsearch.index.shard.IllegalIndexShardStateException;
2018
import org.elasticsearch.test.AbstractMultiClustersTestCase;
2119
import org.elasticsearch.transport.RemoteTransportException;
2220

23-
import java.util.ArrayList;
21+
import java.io.IOException;
2422
import java.util.Arrays;
25-
import java.util.Collection;
2623
import java.util.List;
2724

2825
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -46,14 +43,7 @@ protected boolean reuseClusters() {
4643
return false;
4744
}
4845

49-
@Override
50-
protected Collection<Class<? extends Plugin>> nodePlugins(String clusterAlias) {
51-
final List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins(clusterAlias));
52-
plugins.add(ExceptionOnRewriteQueryPlugin.class);
53-
return plugins;
54-
}
55-
56-
public void testFailuresFromRemote() {
46+
public void testFailuresFromRemote() throws IOException {
5747
Settings indexSettings = Settings.builder().put("index.number_of_replicas", 0).build();
5848
final Client localClient = client(LOCAL_CLUSTER);
5949
final Client remoteClient = client("remote_cluster");
@@ -71,10 +61,11 @@ public void testFailuresFromRemote() {
7161
FieldCapabilitiesResponse response = client().prepareFieldCaps("*", "remote_cluster:*").setFields("*").get();
7262
assertThat(Arrays.asList(response.getIndices()), containsInAnyOrder(localIndex, "remote_cluster:" + remoteErrorIndex));
7363

74-
// adding an index filter so remote call should fail
64+
// Closed shards will result to index error because shards must be in readable state
65+
FieldCapabilitiesIT.closeShards(cluster("remote_cluster"), remoteErrorIndex);
66+
7567
response = client().prepareFieldCaps("*", "remote_cluster:*")
7668
.setFields("*")
77-
.setIndexFilter(new ExceptionOnRewriteQueryBuilder())
7869
.get();
7970
assertThat(response.getIndices()[0], equalTo(localIndex));
8071
assertThat(response.getFailedIndicesCount(), equalTo(1));
@@ -86,15 +77,15 @@ public void testFailuresFromRemote() {
8677
Exception ex = failure.getException();
8778
assertEquals(RemoteTransportException.class, ex.getClass());
8879
Throwable cause = ExceptionsHelper.unwrapCause(ex);
89-
assertEquals(IllegalArgumentException.class, cause.getClass());
90-
assertEquals("I throw because I choose to.", cause.getMessage());
80+
assertEquals(IllegalIndexShardStateException.class, cause.getClass());
81+
assertEquals("CurrentState[CLOSED] operations only allowed when shard state is one of [POST_RECOVERY, STARTED]", cause.getMessage());
9182

9283
// if we only query the remote we should get back an exception only
9384
ex = expectThrows(
94-
IllegalArgumentException.class,
95-
client().prepareFieldCaps("remote_cluster:*").setFields("*").setIndexFilter(new ExceptionOnRewriteQueryBuilder())
85+
IllegalIndexShardStateException.class,
86+
client().prepareFieldCaps("remote_cluster:*").setFields("*")
9687
);
97-
assertEquals("I throw because I choose to.", ex.getMessage());
88+
assertEquals("CurrentState[CLOSED] operations only allowed when shard state is one of [POST_RECOVERY, STARTED]", ex.getMessage());
9889

9990
// add an index that doesn't fail to the remote
10091
assertAcked(remoteClient.admin().indices().prepareCreate("okay_remote_index"));
@@ -103,7 +94,6 @@ public void testFailuresFromRemote() {
10394

10495
response = client().prepareFieldCaps("*", "remote_cluster:*")
10596
.setFields("*")
106-
.setIndexFilter(new ExceptionOnRewriteQueryBuilder())
10797
.get();
10898
assertThat(Arrays.asList(response.getIndices()), containsInAnyOrder(localIndex, "remote_cluster:okay_remote_index"));
10999
assertThat(response.getFailedIndicesCount(), equalTo(1));
@@ -113,8 +103,8 @@ public void testFailuresFromRemote() {
113103
.findFirst()
114104
.get();
115105
ex = failure.getException();
116-
assertEquals(IllegalArgumentException.class, ex.getClass());
117-
assertEquals("I throw because I choose to.", ex.getMessage());
106+
assertEquals(IllegalIndexShardStateException.class, ex.getClass());
107+
assertEquals("CurrentState[CLOSED] operations only allowed when shard state is one of [POST_RECOVERY, STARTED]", ex.getMessage());
118108
}
119109

120110
public void testFailedToConnectToRemoteCluster() throws Exception {

server/src/internalClusterTest/java/org/elasticsearch/search/fieldcaps/FieldCapabilitiesIT.java

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import org.elasticsearch.index.query.QueryBuilder;
4848
import org.elasticsearch.index.query.QueryBuilders;
4949
import org.elasticsearch.index.query.QueryRewriteContext;
50-
import org.elasticsearch.index.query.SearchExecutionContext;
50+
import org.elasticsearch.index.shard.IllegalIndexShardStateException;
5151
import org.elasticsearch.index.shard.IndexShard;
5252
import org.elasticsearch.index.shard.ShardId;
5353
import org.elasticsearch.indices.IndexClosedException;
@@ -59,6 +59,7 @@
5959
import org.elasticsearch.search.DummyQueryBuilder;
6060
import org.elasticsearch.tasks.TaskInfo;
6161
import org.elasticsearch.test.ESIntegTestCase;
62+
import org.elasticsearch.test.InternalTestCluster;
6263
import org.elasticsearch.test.MockLog;
6364
import org.elasticsearch.test.junit.annotations.TestLogging;
6465
import org.elasticsearch.test.transport.MockTransportService;
@@ -78,6 +79,7 @@
7879
import java.util.HashSet;
7980
import java.util.List;
8081
import java.util.Map;
82+
import java.util.Set;
8183
import java.util.concurrent.CancellationException;
8284
import java.util.concurrent.CountDownLatch;
8385
import java.util.concurrent.TimeUnit;
@@ -87,7 +89,6 @@
8789
import java.util.function.Predicate;
8890
import java.util.stream.IntStream;
8991

90-
import static java.util.Collections.singletonList;
9192
import static org.elasticsearch.action.support.ActionTestUtils.wrapAsRestResponseListener;
9293
import static org.elasticsearch.index.shard.IndexShardTestCase.closeShardNoCheck;
9394
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -193,7 +194,7 @@ public void setUp() throws Exception {
193194

194195
@Override
195196
protected Collection<Class<? extends Plugin>> nodePlugins() {
196-
return List.of(TestMapperPlugin.class, ExceptionOnRewriteQueryPlugin.class, BlockingOnRewriteQueryPlugin.class);
197+
return List.of(TestMapperPlugin.class, BlockingOnRewriteQueryPlugin.class);
197198
}
198199

199200
@Override
@@ -445,30 +446,33 @@ public void testFieldMetricsAndDimensions() {
445446
assertThat(response.get().get("some_dimension").get("keyword").nonDimensionIndices(), array(equalTo("new_index")));
446447
}
447448

448-
public void testFailures() throws InterruptedException {
449+
public void testFailures() throws IOException {
449450
// in addition to the existing "old_index" and "new_index", create two where the test query throws an error on rewrite
450451
assertAcked(prepareCreate("index1-error"), prepareCreate("index2-error"));
451452
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")
453458
.setFields("*")
454-
.setIndexFilter(new ExceptionOnRewriteQueryBuilder())
455459
.get();
456460
assertEquals(1, response.getFailures().size());
457461
assertEquals(2, response.getFailedIndicesCount());
458462
assertThat(response.getFailures().get(0).getIndices(), arrayContainingInAnyOrder("index1-error", "index2-error"));
459463
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());
462466

463467
// the "indices" section should not include failed ones
464468
assertThat(Arrays.asList(response.getIndices()), containsInAnyOrder("old_index", "new_index"));
465469

466470
// 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("*")
470474
);
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());
472476
}
473477

474478
private void populateTimeRangeIndices() throws Exception {
@@ -913,45 +917,17 @@ private void assertIndices(FieldCapabilitiesResponse response, String... indices
913917
assertArrayEquals(indices, response.getIndices());
914918
}
915919

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+
}
947929
}
948930
}
949-
return this;
950-
}
951-
952-
@Override
953-
public String getWriteableName() {
954-
return NAME;
955931
}
956932
}
957933

0 commit comments

Comments
 (0)