Switch reindex tests to new style requests (#31941)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `modules/reindex` project to use the new versions.
This commit is contained in:
parent
aedbfc63cd
commit
939983d783
|
@ -19,19 +19,13 @@
|
||||||
|
|
||||||
package org.elasticsearch.index.reindex;
|
package org.elasticsearch.index.reindex;
|
||||||
|
|
||||||
import org.apache.http.entity.ContentType;
|
import org.elasticsearch.client.Request;
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.elasticsearch.client.Response;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
||||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
||||||
import org.elasticsearch.test.rest.ESRestTestCase;
|
import org.elasticsearch.test.rest.ESRestTestCase;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.util.Collections.emptyMap;
|
|
||||||
import static java.util.Collections.singletonMap;
|
|
||||||
import static org.hamcrest.Matchers.hasEntry;
|
import static org.hamcrest.Matchers.hasEntry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,48 +44,69 @@ public class ManyDocumentsIT extends ESRestTestCase {
|
||||||
bulk.append("{\"index\":{}}\n");
|
bulk.append("{\"index\":{}}\n");
|
||||||
bulk.append("{\"test\":\"test\"}\n");
|
bulk.append("{\"test\":\"test\"}\n");
|
||||||
}
|
}
|
||||||
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
|
Request request = new Request("POST", "/test/test/_bulk");
|
||||||
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
|
request.addParameter("refresh", "true");
|
||||||
|
request.setJsonEntity(bulk.toString());
|
||||||
|
client().performRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReindex() throws IOException {
|
public void testReindex() throws IOException {
|
||||||
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
|
Request request = new Request("POST", "/_reindex");
|
||||||
"{\"source\":{\"index\":\"test\"}, \"dest\":{\"index\":\"des\"}}",
|
request.setJsonEntity(
|
||||||
ContentType.APPLICATION_JSON)));
|
"{\n" +
|
||||||
|
" \"source\":{\n" +
|
||||||
|
" \"index\":\"test\"\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"dest\":{\n" +
|
||||||
|
" \"index\":\"des\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}");
|
||||||
|
Map<String, Object> response = entityAsMap(client().performRequest(request));
|
||||||
assertThat(response, hasEntry("total", count));
|
assertThat(response, hasEntry("total", count));
|
||||||
assertThat(response, hasEntry("created", count));
|
assertThat(response, hasEntry("created", count));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReindexFromRemote() throws IOException {
|
public void testReindexFromRemote() throws IOException {
|
||||||
Map<?, ?> nodesInfo = toMap(client().performRequest("GET", "/_nodes/http"));
|
Map<?, ?> nodesInfo = entityAsMap(client().performRequest(new Request("GET", "/_nodes/http")));
|
||||||
nodesInfo = (Map<?, ?>) nodesInfo.get("nodes");
|
nodesInfo = (Map<?, ?>) nodesInfo.get("nodes");
|
||||||
Map<?, ?> nodeInfo = (Map<?, ?>) nodesInfo.values().iterator().next();
|
Map<?, ?> nodeInfo = (Map<?, ?>) nodesInfo.values().iterator().next();
|
||||||
Map<?, ?> http = (Map<?, ?>) nodeInfo.get("http");
|
Map<?, ?> http = (Map<?, ?>) nodeInfo.get("http");
|
||||||
String remote = "http://"+ http.get("publish_address");
|
String remote = "http://"+ http.get("publish_address");
|
||||||
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
|
Request request = new Request("POST", "/_reindex");
|
||||||
"{\"source\":{\"index\":\"test\",\"remote\":{\"host\":\"" + remote + "\"}}, \"dest\":{\"index\":\"des\"}}",
|
request.setJsonEntity(
|
||||||
ContentType.APPLICATION_JSON)));
|
"{\n" +
|
||||||
|
" \"source\":{\n" +
|
||||||
|
" \"index\":\"test\",\n" +
|
||||||
|
" \"remote\":{\n" +
|
||||||
|
" \"host\":\"" + remote + "\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n," +
|
||||||
|
" \"dest\":{\n" +
|
||||||
|
" \"index\":\"des\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}");
|
||||||
|
Map<String, Object> response = entityAsMap(client().performRequest(request));
|
||||||
assertThat(response, hasEntry("total", count));
|
assertThat(response, hasEntry("total", count));
|
||||||
assertThat(response, hasEntry("created", count));
|
assertThat(response, hasEntry("created", count));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testUpdateByQuery() throws IOException {
|
public void testUpdateByQuery() throws IOException {
|
||||||
Map<String, Object> response = toMap(client().performRequest("POST", "/test/_update_by_query"));
|
Map<String, Object> response = entityAsMap(client().performRequest(new Request("POST", "/test/_update_by_query")));
|
||||||
assertThat(response, hasEntry("total", count));
|
assertThat(response, hasEntry("total", count));
|
||||||
assertThat(response, hasEntry("updated", count));
|
assertThat(response, hasEntry("updated", count));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDeleteByQuery() throws IOException {
|
public void testDeleteByQuery() throws IOException {
|
||||||
Map<String, Object> response = toMap(client().performRequest("POST", "/test/_delete_by_query", emptyMap(), new StringEntity(
|
Request request = new Request("POST", "/test/_delete_by_query");
|
||||||
"{\"query\":{\"match_all\":{}}}",
|
request.setJsonEntity(
|
||||||
ContentType.APPLICATION_JSON)));
|
"{\n" +
|
||||||
|
" \"query\":{\n" +
|
||||||
|
" \"match_all\": {}\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}");
|
||||||
|
Map<String, Object> response = entityAsMap(client().performRequest(request));
|
||||||
assertThat(response, hasEntry("total", count));
|
assertThat(response, hasEntry("total", count));
|
||||||
assertThat(response, hasEntry("deleted", count));
|
assertThat(response, hasEntry("deleted", count));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map<String, Object> toMap(Response response) throws IOException {
|
|
||||||
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,25 +19,24 @@
|
||||||
|
|
||||||
package org.elasticsearch.index.reindex.remote;
|
package org.elasticsearch.index.reindex.remote;
|
||||||
|
|
||||||
import org.apache.http.HttpEntity;
|
|
||||||
import org.apache.http.HttpHost;
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.entity.ContentType;
|
|
||||||
import org.apache.http.entity.StringEntity;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.elasticsearch.client.Request;
|
||||||
import org.elasticsearch.client.Response;
|
import org.elasticsearch.client.Response;
|
||||||
import org.elasticsearch.client.ResponseException;
|
|
||||||
import org.elasticsearch.client.RestClient;
|
import org.elasticsearch.client.RestClient;
|
||||||
import org.elasticsearch.common.Booleans;
|
import org.elasticsearch.common.Booleans;
|
||||||
import org.elasticsearch.test.rest.ESRestTestCase;
|
import org.elasticsearch.test.rest.ESRestTestCase;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
import static java.util.Collections.singletonMap;
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
|
||||||
public class ReindexFromOldRemoteIT extends ESRestTestCase {
|
public class ReindexFromOldRemoteIT extends ESRestTestCase {
|
||||||
|
/**
|
||||||
|
* Number of documents to test when reindexing from an old version.
|
||||||
|
*/
|
||||||
|
private static final int DOCS = 5;
|
||||||
|
|
||||||
private void oldEsTestCase(String portPropertyName, String requestsPerSecond) throws IOException {
|
private void oldEsTestCase(String portPropertyName, String requestsPerSecond) throws IOException {
|
||||||
boolean enabled = Booleans.parseBoolean(System.getProperty("tests.fromOld"));
|
boolean enabled = Booleans.parseBoolean(System.getProperty("tests.fromOld"));
|
||||||
assumeTrue("test is disabled, probably because this is windows", enabled);
|
assumeTrue("test is disabled, probably because this is windows", enabled);
|
||||||
|
@ -45,17 +44,19 @@ public class ReindexFromOldRemoteIT extends ESRestTestCase {
|
||||||
int oldEsPort = Integer.parseInt(System.getProperty(portPropertyName));
|
int oldEsPort = Integer.parseInt(System.getProperty(portPropertyName));
|
||||||
try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) {
|
try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) {
|
||||||
try {
|
try {
|
||||||
HttpEntity entity = new StringEntity("{\"settings\":{\"number_of_shards\": 1}}", ContentType.APPLICATION_JSON);
|
Request createIndex = new Request("PUT", "/test");
|
||||||
oldEs.performRequest("PUT", "/test", singletonMap("refresh", "true"), entity);
|
createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": 1}}");
|
||||||
|
oldEs.performRequest(createIndex);
|
||||||
|
|
||||||
entity = new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON);
|
for (int i = 0; i < DOCS; i++) {
|
||||||
oldEs.performRequest("PUT", "/test/doc/testdoc1", singletonMap("refresh", "true"), entity);
|
Request doc = new Request("PUT", "/test/doc/testdoc" + i);
|
||||||
oldEs.performRequest("PUT", "/test/doc/testdoc2", singletonMap("refresh", "true"), entity);
|
doc.addParameter("refresh", "true");
|
||||||
oldEs.performRequest("PUT", "/test/doc/testdoc3", singletonMap("refresh", "true"), entity);
|
doc.setJsonEntity("{\"test\":\"test\"}");
|
||||||
oldEs.performRequest("PUT", "/test/doc/testdoc4", singletonMap("refresh", "true"), entity);
|
oldEs.performRequest(doc);
|
||||||
oldEs.performRequest("PUT", "/test/doc/testdoc5", singletonMap("refresh", "true"), entity);
|
}
|
||||||
|
|
||||||
entity = new StringEntity(
|
Request reindex = new Request("POST", "/_reindex");
|
||||||
|
reindex.setJsonEntity(
|
||||||
"{\n"
|
"{\n"
|
||||||
+ " \"source\":{\n"
|
+ " \"source\":{\n"
|
||||||
+ " \"index\": \"test\",\n"
|
+ " \"index\": \"test\",\n"
|
||||||
|
@ -67,36 +68,23 @@ public class ReindexFromOldRemoteIT extends ESRestTestCase {
|
||||||
+ " \"dest\": {\n"
|
+ " \"dest\": {\n"
|
||||||
+ " \"index\": \"test\"\n"
|
+ " \"index\": \"test\"\n"
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}",
|
+ "}");
|
||||||
ContentType.APPLICATION_JSON);
|
reindex.addParameter("refresh", "true");
|
||||||
Map<String, String> params = new TreeMap<>();
|
reindex.addParameter("pretty", "true");
|
||||||
params.put("refresh", "true");
|
|
||||||
params.put("pretty", "true");
|
|
||||||
if (requestsPerSecond != null) {
|
if (requestsPerSecond != null) {
|
||||||
params.put("requests_per_second", requestsPerSecond);
|
reindex.addParameter("requests_per_second", requestsPerSecond);
|
||||||
}
|
}
|
||||||
client().performRequest("POST", "/_reindex", params, entity);
|
client().performRequest(reindex);
|
||||||
|
|
||||||
Response response = client().performRequest("POST", "test/_search", singletonMap("pretty", "true"));
|
Request search = new Request("POST", "/test/_search");
|
||||||
|
search.addParameter("pretty", "true");
|
||||||
|
Response response = client().performRequest(search);
|
||||||
String result = EntityUtils.toString(response.getEntity());
|
String result = EntityUtils.toString(response.getEntity());
|
||||||
assertThat(result, containsString("\"_id\" : \"testdoc1\""));
|
for (int i = 0; i < DOCS; i++) {
|
||||||
} finally {
|
assertThat(result, containsString("\"_id\" : \"testdoc" + i + "\""));
|
||||||
try {
|
|
||||||
oldEs.performRequest("DELETE", "/test");
|
|
||||||
} catch (ResponseException e) {
|
|
||||||
/* Try not to throw ResponseException for as it'll eat the
|
|
||||||
* real exception. This is because the rest client throws
|
|
||||||
* exceptions in a "funny" way that isn't compatible with
|
|
||||||
* `suppressed`. In the case of 404s we'll just log something
|
|
||||||
* and move on because that just means that a previous
|
|
||||||
* failure caused the index not to be created. */
|
|
||||||
if (e.getResponse().getStatusLine().getStatusCode() == 404) {
|
|
||||||
logger.warn("old index not deleted because it doesn't exist");
|
|
||||||
} else {
|
|
||||||
logger.error("failed to remove old index", e);
|
|
||||||
fail("failed to remove old index, see log");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
oldEs.performRequest(new Request("DELETE", "/test"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue