Skip to content

Commit

Permalink
Switch reindex tests to new style requests (#31941)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nik9000 committed Jul 11, 2018
1 parent 907a0fe commit d3d7309
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@

package org.elasticsearch.index.reindex;

import org.apache.http.entity.ContentType;
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.client.Request;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.junit.Before;

import java.io.IOException;
import java.util.Map;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.hasEntry;

/**
Expand All @@ -50,48 +44,69 @@ public void setupTestIndex() throws IOException {
bulk.append("{\"index\":{}}\n");
bulk.append("{\"test\":\"test\"}\n");
}
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
Request request = new Request("POST", "/test/test/_bulk");
request.addParameter("refresh", "true");
request.setJsonEntity(bulk.toString());
client().performRequest(request);
}

public void testReindex() throws IOException {
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
"{\"source\":{\"index\":\"test\"}, \"dest\":{\"index\":\"des\"}}",
ContentType.APPLICATION_JSON)));
Request request = new Request("POST", "/_reindex");
request.setJsonEntity(
"{\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("created", count));
}

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");
Map<?, ?> nodeInfo = (Map<?, ?>) nodesInfo.values().iterator().next();
Map<?, ?> http = (Map<?, ?>) nodeInfo.get("http");
String remote = "http://"+ http.get("publish_address");
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
"{\"source\":{\"index\":\"test\",\"remote\":{\"host\":\"" + remote + "\"}}, \"dest\":{\"index\":\"des\"}}",
ContentType.APPLICATION_JSON)));
Request request = new Request("POST", "/_reindex");
request.setJsonEntity(
"{\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("created", count));
}


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("updated", count));
}

public void testDeleteByQuery() throws IOException {
Map<String, Object> response = toMap(client().performRequest("POST", "/test/_delete_by_query", emptyMap(), new StringEntity(
"{\"query\":{\"match_all\":{}}}",
ContentType.APPLICATION_JSON)));
Request request = new Request("POST", "/test/_delete_by_query");
request.setJsonEntity(
"{\n" +
" \"query\":{\n" +
" \"match_all\": {}\n" +
" }\n" +
"}");
Map<String, Object> response = entityAsMap(client().performRequest(request));
assertThat(response, hasEntry("total", count));
assertThat(response, hasEntry("deleted", count));
}

static Map<String, Object> toMap(Response response) throws IOException {
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(), false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,44 @@

package org.elasticsearch.index.reindex.remote;

import org.apache.http.HttpEntity;
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.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.test.rest.ESRestTestCase;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.containsString;

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 {
boolean enabled = Booleans.parseBoolean(System.getProperty("tests.fromOld"));
assumeTrue("test is disabled, probably because this is windows", enabled);

int oldEsPort = Integer.parseInt(System.getProperty(portPropertyName));
try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) {
try {
HttpEntity entity = new StringEntity("{\"settings\":{\"number_of_shards\": 1}}", ContentType.APPLICATION_JSON);
oldEs.performRequest("PUT", "/test", singletonMap("refresh", "true"), entity);

entity = new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON);
oldEs.performRequest("PUT", "/test/doc/testdoc1", singletonMap("refresh", "true"), entity);
oldEs.performRequest("PUT", "/test/doc/testdoc2", singletonMap("refresh", "true"), entity);
oldEs.performRequest("PUT", "/test/doc/testdoc3", singletonMap("refresh", "true"), entity);
oldEs.performRequest("PUT", "/test/doc/testdoc4", singletonMap("refresh", "true"), entity);
oldEs.performRequest("PUT", "/test/doc/testdoc5", singletonMap("refresh", "true"), entity);
Request createIndex = new Request("PUT", "/test");
createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": 1}}");
oldEs.performRequest(createIndex);

for (int i = 0; i < DOCS; i++) {
Request doc = new Request("PUT", "/test/doc/testdoc" + i);
doc.addParameter("refresh", "true");
doc.setJsonEntity("{\"test\":\"test\"}");
oldEs.performRequest(doc);
}

entity = new StringEntity(
Request reindex = new Request("POST", "/_reindex");
reindex.setJsonEntity(
"{\n"
+ " \"source\":{\n"
+ " \"index\": \"test\",\n"
Expand All @@ -67,36 +68,23 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th
+ " \"dest\": {\n"
+ " \"index\": \"test\"\n"
+ " }\n"
+ "}",
ContentType.APPLICATION_JSON);
Map<String, String> params = new TreeMap<>();
params.put("refresh", "true");
params.put("pretty", "true");
+ "}");
reindex.addParameter("refresh", "true");
reindex.addParameter("pretty", "true");
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());
assertThat(result, containsString("\"_id\" : \"testdoc1\""));
} finally {
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");
}
for (int i = 0; i < DOCS; i++) {
assertThat(result, containsString("\"_id\" : \"testdoc" + i + "\""));
}
} finally {
oldEs.performRequest(new Request("DELETE", "/test"));
}
}
}
Expand Down

0 comments on commit d3d7309

Please sign in to comment.