|
| 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 | +package org.elasticsearch.backwards; |
| 20 | + |
| 21 | +import org.apache.http.HttpHost; |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.backwards.IndexingIT.Node; |
| 24 | +import org.elasticsearch.backwards.IndexingIT.Nodes; |
| 25 | +import org.elasticsearch.client.Request; |
| 26 | +import org.elasticsearch.client.Response; |
| 27 | +import org.elasticsearch.client.ResponseException; |
| 28 | +import org.elasticsearch.client.RestClient; |
| 29 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 30 | +import org.elasticsearch.common.CheckedConsumer; |
| 31 | +import org.elasticsearch.common.settings.Settings; |
| 32 | +import org.elasticsearch.rest.RestStatus; |
| 33 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 34 | +import org.elasticsearch.test.rest.yaml.ObjectPath; |
| 35 | +import org.junit.Before; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +import static org.hamcrest.Matchers.containsString; |
| 43 | +import static org.hamcrest.Matchers.equalTo; |
| 44 | + |
| 45 | +public class SearchWithMinCompatibleSearchNodeIT extends ESRestTestCase { |
| 46 | + |
| 47 | + private static String index = "test_min_version"; |
| 48 | + private static int numShards; |
| 49 | + private static int numReplicas = 1; |
| 50 | + private static int numDocs; |
| 51 | + private static Nodes nodes; |
| 52 | + private static List<Node> bwcNodes; |
| 53 | + private static List<Node> newNodes; |
| 54 | + private static Version bwcVersion; |
| 55 | + private static Version newVersion; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void prepareTestData() throws IOException { |
| 59 | + nodes = IndexingIT.buildNodeAndVersions(client()); |
| 60 | + numShards = nodes.size(); |
| 61 | + numDocs = randomIntBetween(numShards, 16); |
| 62 | + bwcNodes = new ArrayList<>(); |
| 63 | + newNodes = new ArrayList<>(); |
| 64 | + bwcNodes.addAll(nodes.getBWCNodes()); |
| 65 | + newNodes.addAll(nodes.getNewNodes()); |
| 66 | + bwcVersion = bwcNodes.get(0).getVersion(); |
| 67 | + newVersion = newNodes.get(0).getVersion(); |
| 68 | + |
| 69 | + if (client().performRequest(new Request("HEAD", "/" + index)).getStatusLine().getStatusCode() == 404) { |
| 70 | + createIndex(index, Settings.builder() |
| 71 | + .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), numShards) |
| 72 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas).build()); |
| 73 | + for (int i = 0; i < numDocs; i++) { |
| 74 | + Request request = new Request("PUT", index + "/_doc/" + i); |
| 75 | + request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}"); |
| 76 | + assertOK(client().performRequest(request)); |
| 77 | + } |
| 78 | + ensureGreen(index); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void testMinVersionAsNewVersion() throws Exception { |
| 83 | + Request newVersionRequest = new Request("POST", |
| 84 | + index + "/_search?min_compatible_shard_node=" + newVersion + "&ccs_minimize_roundtrips=false"); |
| 85 | + assertWithBwcVersionCheck((client) -> { |
| 86 | + ResponseException responseException = expectThrows(ResponseException.class, () -> client.performRequest(newVersionRequest)); |
| 87 | + assertThat(responseException.getResponse().getStatusLine().getStatusCode(), |
| 88 | + equalTo(RestStatus.INTERNAL_SERVER_ERROR.getStatus())); |
| 89 | + assertThat(responseException.getMessage(), |
| 90 | + containsString("{\"error\":{\"root_cause\":[],\"type\":\"search_phase_execution_exception\"")); |
| 91 | + assertThat(responseException.getMessage(), containsString("caused_by\":{\"type\":\"version_mismatch_exception\"," |
| 92 | + + "\"reason\":\"One of the shards is incompatible with the required minimum version [" + newVersion + "]\"")); |
| 93 | + }, newVersionRequest); |
| 94 | + } |
| 95 | + |
| 96 | + public void testMinVersionAsOldVersion() throws Exception { |
| 97 | + Request oldVersionRequest = new Request("POST", index + "/_search?min_compatible_shard_node=" + bwcVersion + |
| 98 | + "&ccs_minimize_roundtrips=false"); |
| 99 | + oldVersionRequest.setJsonEntity("{\"query\":{\"match_all\":{}},\"_source\":false}"); |
| 100 | + assertWithBwcVersionCheck((client) -> { |
| 101 | + Response response = client.performRequest(oldVersionRequest); |
| 102 | + ObjectPath responseObject = ObjectPath.createFromResponse(response); |
| 103 | + Map<String, Object> shardsResult = responseObject.evaluate("_shards"); |
| 104 | + assertThat(shardsResult.get("total"), equalTo(numShards)); |
| 105 | + assertThat(shardsResult.get("successful"), equalTo(numShards)); |
| 106 | + assertThat(shardsResult.get("failed"), equalTo(0)); |
| 107 | + Map<String, Object> hitsResult = responseObject.evaluate("hits.total"); |
| 108 | + assertThat(hitsResult.get("value"), equalTo(numDocs)); |
| 109 | + assertThat(hitsResult.get("relation"), equalTo("eq")); |
| 110 | + }, oldVersionRequest); |
| 111 | + } |
| 112 | + |
| 113 | + public void testCcsMinimizeRoundtripsIsFalse() throws Exception { |
| 114 | + Version version = randomBoolean() ? newVersion : bwcVersion; |
| 115 | + |
| 116 | + Request request = new Request("POST", index + "/_search?min_compatible_shard_node=" + version + "&ccs_minimize_roundtrips=true"); |
| 117 | + assertWithBwcVersionCheck((client) -> { |
| 118 | + ResponseException responseException = expectThrows(ResponseException.class, () -> client.performRequest(request)); |
| 119 | + assertThat(responseException.getResponse().getStatusLine().getStatusCode(), |
| 120 | + equalTo(RestStatus.BAD_REQUEST.getStatus())); |
| 121 | + assertThat(responseException.getMessage(), |
| 122 | + containsString("{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\"")); |
| 123 | + assertThat(responseException.getMessage(), containsString("\"reason\":\"Validation Failed: 1: " |
| 124 | + + "[ccs_minimize_roundtrips] cannot be [true] when setting a minimum compatible shard version;\"")); |
| 125 | + }, request); |
| 126 | + } |
| 127 | + |
| 128 | + private void assertWithBwcVersionCheck(CheckedConsumer<RestClient, Exception> code, Request request) throws Exception { |
| 129 | + try (RestClient client = buildClient(restClientSettings(), |
| 130 | + newNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { |
| 131 | + assertBusy(() -> { |
| 132 | + code.accept(client); |
| 133 | + }); |
| 134 | + } |
| 135 | + try (RestClient client = buildClient(restClientSettings(), |
| 136 | + bwcNodes.stream().map(Node::getPublishAddress).toArray(HttpHost[]::new))) { |
| 137 | + if (bwcVersion.before(Version.V_7_12_0)) { |
| 138 | + ResponseException exception = expectThrows(ResponseException.class, () -> client.performRequest(request)); |
| 139 | + assertThat(exception.getResponse().getStatusLine().getStatusCode(), |
| 140 | + equalTo(RestStatus.BAD_REQUEST.getStatus())); |
| 141 | + if (bwcVersion.onOrAfter(Version.V_7_0_0)) { |
| 142 | + // min_compatible_shard_node support doesn't exist in older versions and there will be an "unrecognized parameter" |
| 143 | + // exception |
| 144 | + assertThat(exception.getMessage(), containsString("contains unrecognized parameter: [min_compatible_shard_node]")); |
| 145 | + } else { |
| 146 | + // ccs_minimize_roundtrips support doesn't exist in 6.x versions and there will be an "unrecognized parameter" exception |
| 147 | + assertThat(exception.getMessage(), containsString("contains unrecognized parameters: [ccs_minimize_roundtrips]")); |
| 148 | + } |
| 149 | + } else { |
| 150 | + assertBusy(() -> { |
| 151 | + code.accept(client); |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments