Skip to content

Commit

Permalink
Switch many QA projects to use new style requests (#30574)
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 the old requests. This
changes many calls in the `qa` projects to use the new version.
  • Loading branch information
nik9000 committed May 15, 2018
1 parent abc06d5 commit 6695d11
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
Expand Down Expand Up @@ -134,7 +135,7 @@ public void testSearchSkipUnavailable() throws IOException {
for (int i = 0; i < 10; i++) {
restHighLevelClient.index(new IndexRequest("index", "doc", String.valueOf(i)).source("field", "value"));
}
Response refreshResponse = client().performRequest("POST", "/index/_refresh");
Response refreshResponse = client().performRequest(new Request("POST", "/index/_refresh"));
assertEquals(200, refreshResponse.getStatusLine().getStatusCode());

{
Expand Down Expand Up @@ -223,10 +224,11 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException {

{
//check that skip_unavailable alone cannot be set
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(
Collections.singletonMap("skip_unavailable", randomBoolean()));
Request request = new Request("PUT", "/_cluster/settings");
request.setEntity(buildUpdateSettingsRequestBody(
Collections.singletonMap("skip_unavailable", randomBoolean())));
ResponseException responseException = expectThrows(ResponseException.class,
() -> client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity));
() -> client().performRequest(request));
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
assertThat(responseException.getMessage(),
containsString("Missing required setting [search.remote.remote1.seeds] " +
Expand All @@ -240,9 +242,10 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException {

{
//check that seeds cannot be reset alone if skip_unavailable is set
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(Collections.singletonMap("seeds", null));
Request request = new Request("PUT", "/_cluster/settings");
request.setEntity(buildUpdateSettingsRequestBody(Collections.singletonMap("seeds", null)));
ResponseException responseException = expectThrows(ResponseException.class,
() -> client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity));
() -> client().performRequest(request));
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
assertThat(responseException.getMessage(), containsString("Missing required setting [search.remote.remote1.seeds] " +
"for setting [search.remote.remote1.skip_unavailable]"));
Expand Down Expand Up @@ -284,8 +287,9 @@ private static void assertSearchConnectFailure() {


private static void updateRemoteClusterSettings(Map<String, Object> settings) throws IOException {
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(settings);
Response response = client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity);
Request request = new Request("PUT", "/_cluster/settings");
request.setEntity(buildUpdateSettingsRequestBody(settings));
Response response = client().performRequest(request);
assertEquals(200, response.getStatusLine().getStatusCode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.http.ConnectionClosedException;
import org.apache.lucene.util.Constants;
import org.elasticsearch.client.Request;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.hamcrest.Matcher;
Expand Down Expand Up @@ -51,7 +52,8 @@ public void testDieWithDignity() throws Exception {
assertThat(pidFileLines, hasSize(1));
final int pid = Integer.parseInt(pidFileLines.get(0));
Files.delete(pidFile);
IOException e = expectThrows(IOException.class, () -> client().performRequest("GET", "/_die_with_dignity"));
IOException e = expectThrows(IOException.class,
() -> client().performRequest(new Request("GET", "/_die_with_dignity")));
Matcher<IOException> failureMatcher = instanceOf(ConnectionClosedException.class);
if (Constants.WINDOWS) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
package org.elasticsearch.backwards;

import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
Expand All @@ -34,25 +33,21 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

public class IndexingIT extends ESRestTestCase {

private int indexDocs(String index, final int idStart, final int numDocs) throws IOException {
for (int i = 0; i < numDocs; i++) {
final int id = idStart + i;
assertOK(client().performRequest("PUT", index + "/test/" + id, emptyMap(),
new StringEntity("{\"test\": \"test_" + randomAsciiOfLength(2) + "\"}", ContentType.APPLICATION_JSON)));
Request request = new Request("PUT", index + "/test/" + id);
request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}");
assertOK(client().performRequest(request));
}
return numDocs;
}
Expand Down Expand Up @@ -105,7 +100,7 @@ public void testIndexVersionPropagation() throws Exception {
logger.info("allowing shards on all nodes");
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
ensureGreen(index);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
List<Shard> shards = buildShards(index, nodes, newNodeClient);
Shard primary = buildShards(index, nodes, newNodeClient).stream().filter(Shard::isPrimary).findFirst().get();
logger.info("primary resolved to: " + primary.getNode().getNodeName());
Expand All @@ -117,7 +112,7 @@ public void testIndexVersionPropagation() throws Exception {
nUpdates = randomIntBetween(minUpdates, maxUpdates);
logger.info("indexing docs with [{}] concurrent updates after allowing shards on all nodes", nUpdates);
final int finalVersionForDoc2 = indexDocWithConcurrentUpdates(index, 2, nUpdates);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
shards = buildShards(index, nodes, newNodeClient);
primary = shards.stream().filter(Shard::isPrimary).findFirst().get();
logger.info("primary resolved to: " + primary.getNode().getNodeName());
Expand All @@ -133,7 +128,7 @@ public void testIndexVersionPropagation() throws Exception {
nUpdates = randomIntBetween(minUpdates, maxUpdates);
logger.info("indexing docs with [{}] concurrent updates after moving primary", nUpdates);
final int finalVersionForDoc3 = indexDocWithConcurrentUpdates(index, 3, nUpdates);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
shards = buildShards(index, nodes, newNodeClient);
for (Shard shard : shards) {
assertVersion(index, 3, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc3);
Expand All @@ -146,7 +141,7 @@ public void testIndexVersionPropagation() throws Exception {
nUpdates = randomIntBetween(minUpdates, maxUpdates);
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 0", nUpdates);
final int finalVersionForDoc4 = indexDocWithConcurrentUpdates(index, 4, nUpdates);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
shards = buildShards(index, nodes, newNodeClient);
for (Shard shard : shards) {
assertVersion(index, 4, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc4);
Expand All @@ -159,7 +154,7 @@ public void testIndexVersionPropagation() throws Exception {
nUpdates = randomIntBetween(minUpdates, maxUpdates);
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 1", nUpdates);
final int finalVersionForDoc5 = indexDocWithConcurrentUpdates(index, 5, nUpdates);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
shards = buildShards(index, nodes, newNodeClient);
for (Shard shard : shards) {
assertVersion(index, 5, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc5);
Expand Down Expand Up @@ -191,7 +186,7 @@ public void testSeqNoCheckpoints() throws Exception {
logger.info("allowing shards on all nodes");
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
ensureGreen(index);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
for (final String bwcName : bwcNamesList) {
assertCount(index, "_only_nodes:" + bwcName, numDocs);
}
Expand Down Expand Up @@ -222,7 +217,7 @@ public void testSeqNoCheckpoints() throws Exception {
logger.info("setting number of replicas to 1");
updateIndexSettings(index, Settings.builder().put("index.number_of_replicas", 1));
ensureGreen(index);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));

for (Shard shard : buildShards(index, nodes, newNodeClient)) {
assertCount(index, "_only_nodes:" + shard.node.nodeName, numDocs);
Expand All @@ -237,20 +232,18 @@ public void testUpdateSnapshotStatus() throws Exception {
logger.info("cluster discovered: {}", nodes.toString());

// Create the repository before taking the snapshot.
String repoConfig = Strings
Request request = new Request("PUT", "/_snapshot/repo");
request.setJsonEntity(Strings
.toString(JsonXContent.contentBuilder()
.startObject()
.field("type", "fs")
.startObject("settings")
.field("compress", randomBoolean())
.field("location", System.getProperty("tests.path.repo"))
.endObject()
.endObject());

assertOK(
client().performRequest("PUT", "/_snapshot/repo", emptyMap(),
new StringEntity(repoConfig, ContentType.APPLICATION_JSON))
);
.field("type", "fs")
.startObject("settings")
.field("compress", randomBoolean())
.field("location", System.getProperty("tests.path.repo"))
.endObject()
.endObject()));

assertOK(client().performRequest(request));

String bwcNames = nodes.getBWCNodes().stream().map(Node::getNodeName).collect(Collectors.joining(","));

Expand All @@ -264,34 +257,36 @@ public void testUpdateSnapshotStatus() throws Exception {
createIndex(index, settings.build());
indexDocs(index, 0, between(50, 100));
ensureGreen(index);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));

assertOK(
client().performRequest("PUT", "/_snapshot/repo/bwc-snapshot", singletonMap("wait_for_completion", "true"),
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
);
request = new Request("PUT", "/_snapshot/repo/bwc-snapshot");
request.addParameter("wait_for_completion", "true");
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
assertOK(client().performRequest(request));

// Allocating shards on all nodes, taking snapshots should happen on all nodes.
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
ensureGreen(index);
assertOK(client().performRequest("POST", index + "/_refresh"));
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));

assertOK(
client().performRequest("PUT", "/_snapshot/repo/mixed-snapshot", singletonMap("wait_for_completion", "true"),
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
);
request = new Request("PUT", "/_snapshot/repo/mixed-snapshot");
request.addParameter("wait_for_completion", "true");
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
}

private void assertCount(final String index, final String preference, final int expectedCount) throws IOException {
final Response response = client().performRequest("GET", index + "/_count", Collections.singletonMap("preference", preference));
Request request = new Request("GET", index + "/_count");
request.addParameter("preference", preference);
final Response response = client().performRequest(request);
assertOK(response);
final int actualCount = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("count").toString());
assertThat(actualCount, equalTo(expectedCount));
}

private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
final Response response = client().performRequest("GET", index + "/test/" + docId,
Collections.singletonMap("preference", preference));
Request request = new Request("GET", index + "/test/" + docId);
request.addParameter("preference", preference);
final Response response = client().performRequest(request);
assertOK(response);
final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion));
Expand Down Expand Up @@ -323,7 +318,9 @@ private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestCli
}

private List<Shard> buildShards(String index, Nodes nodes, RestClient client) throws IOException {
Response response = client.performRequest("GET", index + "/_stats", singletonMap("level", "shards"));
Request request = new Request("GET", index + "/_stats");
request.addParameter("level", "shards");
Response response = client.performRequest(request);
List<Object> shardStats = ObjectPath.createFromResponse(response).evaluate("indices." + index + ".shards.0");
ArrayList<Shard> shards = new ArrayList<>();
for (Object shard : shardStats) {
Expand All @@ -341,7 +338,7 @@ private List<Shard> buildShards(String index, Nodes nodes, RestClient client) th
}

private Nodes buildNodeAndVersions() throws IOException {
Response response = client().performRequest("GET", "_nodes");
Response response = client().performRequest(new Request("GET", "_nodes"));
ObjectPath objectPath = ObjectPath.createFromResponse(response);
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
Nodes nodes = new Nodes();
Expand All @@ -352,7 +349,7 @@ private Nodes buildNodeAndVersions() throws IOException {
Version.fromString(objectPath.evaluate("nodes." + id + ".version")),
HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address"))));
}
response = client().performRequest("GET", "_cluster/state");
response = client().performRequest(new Request("GET", "_cluster/state"));
nodes.setMasterNodeId(ObjectPath.createFromResponse(response).evaluate("master_node"));
return nodes;
}
Expand Down
Loading

0 comments on commit 6695d11

Please sign in to comment.