|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.oldrepos; |
| 9 | + |
| 10 | +import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 11 | +import com.carrotsearch.randomizedtesting.annotations.Name; |
| 12 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 13 | + |
| 14 | +import org.apache.http.HttpHost; |
| 15 | +import org.elasticsearch.Version; |
| 16 | +import org.elasticsearch.client.Request; |
| 17 | +import org.elasticsearch.client.RestClient; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.settings.SecureString; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 22 | +import org.elasticsearch.core.Booleans; |
| 23 | +import org.elasticsearch.core.PathUtils; |
| 24 | +import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; |
| 25 | +import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; |
| 26 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.xcontent.XContentFactory; |
| 28 | +import org.junit.Before; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests doc-value-based searches against indices imported from clusters older than N-1. |
| 34 | + * We reuse the YAML tests in search/390_doc_values_search.yml but have to do the setup |
| 35 | + * manually here as the setup is done on the old cluster for which we have to use the |
| 36 | + * low-level REST client instead of the YAML set up that only knows how to talk to |
| 37 | + * newer ES versions. |
| 38 | + * |
| 39 | + * We mimic the setup in search/390_doc_values_search.yml here, but adapt it to work |
| 40 | + * against older version clusters. |
| 41 | + */ |
| 42 | +public class DocValueOnlyFieldsIT extends ESClientYamlSuiteTestCase { |
| 43 | + |
| 44 | + final Version oldVersion = Version.fromString(System.getProperty("tests.es.version")); |
| 45 | + static boolean setupDone; |
| 46 | + |
| 47 | + public DocValueOnlyFieldsIT(@Name("yaml") ClientYamlTestCandidate testCandidate) { |
| 48 | + super(testCandidate); |
| 49 | + } |
| 50 | + |
| 51 | + @ParametersFactory |
| 52 | + public static Iterable<Object[]> parameters() throws Exception { |
| 53 | + return ESClientYamlSuiteTestCase.createParameters(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected boolean preserveClusterUponCompletion() { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected Settings restClientSettings() { |
| 63 | + String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray())); |
| 64 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected boolean skipSetupSections() { |
| 69 | + // setup in the YAML file is replaced by the method below |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + @Before |
| 74 | + public void setupIndex() throws IOException { |
| 75 | + final boolean afterRestart = Booleans.parseBoolean(System.getProperty("tests.after_restart")); |
| 76 | + if (afterRestart) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // The following is bit of a hack. While we wish we could make this an @BeforeClass, it does not work because the client() is only |
| 81 | + // initialized later, so we do it when running the first test |
| 82 | + if (setupDone) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + setupDone = true; |
| 87 | + |
| 88 | + String repoLocation = PathUtils.get(System.getProperty("tests.repo.location")) |
| 89 | + .resolve(RandomizedTest.getContext().getTargetClass().getName()) |
| 90 | + .toString(); |
| 91 | + |
| 92 | + String indexName = "test"; |
| 93 | + String repoName = "doc_values_repo"; |
| 94 | + String snapshotName = "snap"; |
| 95 | + String[] basicTypes = new String[] { |
| 96 | + "byte", |
| 97 | + "double", |
| 98 | + "float", |
| 99 | + "half_float", |
| 100 | + "integer", |
| 101 | + "long", |
| 102 | + "short", |
| 103 | + "boolean", |
| 104 | + "keyword", |
| 105 | + "ip", |
| 106 | + "geo_point" }; // date is manually added as it need further configuration |
| 107 | + |
| 108 | + int oldEsPort = Integer.parseInt(System.getProperty("tests.es.port")); |
| 109 | + try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) { |
| 110 | + Request createIndex = new Request("PUT", "/" + indexName); |
| 111 | + int numberOfShards = randomIntBetween(1, 3); |
| 112 | + |
| 113 | + boolean multiTypes = oldVersion.before(Version.V_7_0_0); |
| 114 | + |
| 115 | + XContentBuilder settingsBuilder = XContentFactory.jsonBuilder() |
| 116 | + .startObject() |
| 117 | + .startObject("settings") |
| 118 | + .field("index.number_of_shards", numberOfShards) |
| 119 | + .endObject() |
| 120 | + .startObject("mappings"); |
| 121 | + if (multiTypes) { |
| 122 | + settingsBuilder.startObject("doc"); |
| 123 | + } |
| 124 | + settingsBuilder.field("dynamic", false).startObject("properties"); |
| 125 | + for (String type : basicTypes) { |
| 126 | + settingsBuilder.startObject(type).field("type", type).endObject(); |
| 127 | + } |
| 128 | + settingsBuilder.startObject("date").field("type", "date").field("format", "yyyy/MM/dd").endObject(); |
| 129 | + if (multiTypes) { |
| 130 | + settingsBuilder.endObject(); |
| 131 | + } |
| 132 | + settingsBuilder.endObject().endObject().endObject(); |
| 133 | + |
| 134 | + createIndex.setJsonEntity(Strings.toString(settingsBuilder)); |
| 135 | + assertOK(oldEs.performRequest(createIndex)); |
| 136 | + |
| 137 | + Request doc1 = new Request("PUT", "/" + indexName + "/" + "doc" + "/" + "1"); |
| 138 | + doc1.addParameter("refresh", "true"); |
| 139 | + XContentBuilder bodyDoc1 = XContentFactory.jsonBuilder() |
| 140 | + .startObject() |
| 141 | + .field("byte", 1) |
| 142 | + .field("double", 1.0) |
| 143 | + .field("float", 1.0) |
| 144 | + .field("half_float", 1.0) |
| 145 | + .field("integer", 1) |
| 146 | + .field("long", 1) |
| 147 | + .field("short", 1) |
| 148 | + .field("date", "2017/01/01") |
| 149 | + .field("keyword", "key1") |
| 150 | + .field("boolean", false) |
| 151 | + .field("ip", "192.168.0.1") |
| 152 | + .array("geo_point", 13.5, 34.89) |
| 153 | + .endObject(); |
| 154 | + doc1.setJsonEntity(Strings.toString(bodyDoc1)); |
| 155 | + assertOK(oldEs.performRequest(doc1)); |
| 156 | + |
| 157 | + Request doc2 = new Request("PUT", "/" + indexName + "/" + "doc" + "/" + "2"); |
| 158 | + doc2.addParameter("refresh", "true"); |
| 159 | + XContentBuilder bodyDoc2 = XContentFactory.jsonBuilder() |
| 160 | + .startObject() |
| 161 | + .field("byte", 2) |
| 162 | + .field("double", 2.0) |
| 163 | + .field("float", 2.0) |
| 164 | + .field("half_float", 2.0) |
| 165 | + .field("integer", 2) |
| 166 | + .field("long", 2) |
| 167 | + .field("short", 2) |
| 168 | + .field("date", "2017/01/02") |
| 169 | + .field("keyword", "key2") |
| 170 | + .field("boolean", true) |
| 171 | + .field("ip", "192.168.0.2") |
| 172 | + .array("geo_point", -63.24, 31.0) |
| 173 | + .endObject(); |
| 174 | + doc2.setJsonEntity(Strings.toString(bodyDoc2)); |
| 175 | + assertOK(oldEs.performRequest(doc2)); |
| 176 | + |
| 177 | + // register repo on old ES and take snapshot |
| 178 | + Request createRepoRequest = new Request("PUT", "/_snapshot/" + repoName); |
| 179 | + createRepoRequest.setJsonEntity(Strings.format(""" |
| 180 | + {"type":"fs","settings":{"location":"%s"}} |
| 181 | + """, repoLocation)); |
| 182 | + assertOK(oldEs.performRequest(createRepoRequest)); |
| 183 | + |
| 184 | + Request createSnapshotRequest = new Request("PUT", "/_snapshot/" + repoName + "/" + snapshotName); |
| 185 | + createSnapshotRequest.addParameter("wait_for_completion", "true"); |
| 186 | + createSnapshotRequest.setJsonEntity("{\"indices\":\"" + indexName + "\"}"); |
| 187 | + assertOK(oldEs.performRequest(createSnapshotRequest)); |
| 188 | + } |
| 189 | + |
| 190 | + // register repo on new ES and restore snapshot |
| 191 | + Request createRepoRequest2 = new Request("PUT", "/_snapshot/" + repoName); |
| 192 | + createRepoRequest2.setJsonEntity(Strings.format(""" |
| 193 | + {"type":"fs","settings":{"location":"%s"}} |
| 194 | + """, repoLocation)); |
| 195 | + assertOK(client().performRequest(createRepoRequest2)); |
| 196 | + |
| 197 | + final Request createRestoreRequest = new Request("POST", "/_snapshot/" + repoName + "/" + snapshotName + "/_restore"); |
| 198 | + createRestoreRequest.addParameter("wait_for_completion", "true"); |
| 199 | + createRestoreRequest.setJsonEntity("{\"indices\":\"" + indexName + "\"}"); |
| 200 | + assertOK(client().performRequest(createRestoreRequest)); |
| 201 | + } |
| 202 | +} |
0 commit comments