use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class NodeRestUsageIT method testAggregationUsage.
@SuppressWarnings("unchecked")
public void testAggregationUsage() throws IOException {
// First get the current usage figures
String path = randomFrom("_nodes/usage", "_nodes/usage/aggregations", "_nodes/usage/_all");
Response beforeResponse = client().performRequest(new Request("GET", path));
Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
assertThat(beforeResponseBodyMap, notNullValue());
int beforeSuccessful = assertSuccess(beforeResponseBodyMap);
Map<String, Object> beforeNodesMap = (Map<String, Object>) beforeResponseBodyMap.get("nodes");
assertThat(beforeNodesMap, notNullValue());
assertThat(beforeNodesMap.size(), equalTo(beforeSuccessful));
Map<String, Map<String, Long>> beforeCombinedAggsUsage = getTotalUsage(beforeNodesMap);
// Do some requests to get some rest usage stats
Request create = new Request("PUT", "/test");
create.setJsonEntity("{\"mappings\": {\"properties\": { \"str\": {\"type\": \"keyword\"}, " + "\"foo\": {\"type\": \"keyword\"}, \"num\": {\"type\": \"long\"}, \"start\": {\"type\": \"date\"} } }}");
client().performRequest(create);
Request searchRequest = new Request("GET", "/test/_search");
SearchSourceBuilder searchSource = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("str_terms").field("str.keyword")).aggregation(AggregationBuilders.terms("num_terms").field("num")).aggregation(AggregationBuilders.avg("num_avg").field("num"));
searchRequest.setJsonEntity(Strings.toString(searchSource));
searchRequest.setJsonEntity(Strings.toString(searchSource));
client().performRequest(searchRequest);
searchRequest = new Request("GET", "/test/_search");
searchSource = new SearchSourceBuilder().aggregation(AggregationBuilders.terms("start").field("start")).aggregation(AggregationBuilders.avg("num1").field("num")).aggregation(AggregationBuilders.avg("num2").field("num")).aggregation(AggregationBuilders.terms("foo").field("foo.keyword"));
String r = Strings.toString(searchSource);
searchRequest.setJsonEntity(Strings.toString(searchSource));
client().performRequest(searchRequest);
Response response = client().performRequest(new Request("GET", "_nodes/usage"));
Map<String, Object> responseBodyMap = entityAsMap(response);
assertThat(responseBodyMap, notNullValue());
int successful = assertSuccess(responseBodyMap);
Map<String, Object> nodesMap = (Map<String, Object>) responseBodyMap.get("nodes");
assertThat(nodesMap, notNullValue());
assertThat(nodesMap.size(), equalTo(successful));
Map<String, Map<String, Long>> afterCombinedAggsUsage = getTotalUsage(nodesMap);
assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "numeric", 1L);
assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "date", 1L);
assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "terms", "bytes", 2L);
assertDiff(beforeCombinedAggsUsage, afterCombinedAggsUsage, "avg", "numeric", 3L);
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class NodeRestUsageIT method testWithRestUsage.
@SuppressWarnings("unchecked")
public void testWithRestUsage() throws IOException {
// First get the current usage figures
String path = randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all");
Response beforeResponse = client().performRequest(new Request("GET", path));
Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
assertThat(beforeResponseBodyMap, notNullValue());
int beforeSuccessful = assertSuccess(beforeResponseBodyMap);
Map<String, Object> beforeNodesMap = (Map<String, Object>) beforeResponseBodyMap.get("nodes");
assertThat(beforeNodesMap, notNullValue());
assertThat(beforeNodesMap.size(), equalTo(beforeSuccessful));
Map<String, Long> beforeCombinedRestUsage = new HashMap<>();
beforeCombinedRestUsage.put("nodes_usage_action", 0L);
beforeCombinedRestUsage.put("create_index_action", 0L);
beforeCombinedRestUsage.put("document_index_action", 0L);
beforeCombinedRestUsage.put("search_action", 0L);
beforeCombinedRestUsage.put("refresh_action", 0L);
beforeCombinedRestUsage.put("cat_indices_action", 0L);
beforeCombinedRestUsage.put("nodes_info_action", 0L);
beforeCombinedRestUsage.put("nodes_stats_action", 0L);
beforeCombinedRestUsage.put("delete_index_action", 0L);
for (Map.Entry<String, Object> nodeEntry : beforeNodesMap.entrySet()) {
Map<String, Object> beforeRestActionUsage = (Map<String, Object>) ((Map<String, Object>) nodeEntry.getValue()).get("rest_actions");
assertThat(beforeRestActionUsage, notNullValue());
for (Map.Entry<String, Object> restActionEntry : beforeRestActionUsage.entrySet()) {
Long currentUsage = beforeCombinedRestUsage.get(restActionEntry.getKey());
if (currentUsage == null) {
beforeCombinedRestUsage.put(restActionEntry.getKey(), ((Number) restActionEntry.getValue()).longValue());
} else {
beforeCombinedRestUsage.put(restActionEntry.getKey(), currentUsage + ((Number) restActionEntry.getValue()).longValue());
}
}
}
// Do some requests to get some rest usage stats
client().performRequest(new Request("PUT", "/test"));
for (int i = 0; i < 3; i++) {
final Request index = new Request("POST", "/test/_doc/1");
index.setJsonEntity("{\"foo\": \"bar\"}");
client().performRequest(index);
}
client().performRequest(new Request("GET", "/test/_search"));
final Request index4 = new Request("POST", "/test/_doc/4");
index4.setJsonEntity("{\"foo\": \"bar\"}");
client().performRequest(index4);
client().performRequest(new Request("POST", "/test/_refresh"));
client().performRequest(new Request("GET", "/_cat/indices"));
client().performRequest(new Request("GET", "/_nodes"));
client().performRequest(new Request("GET", "/test/_search"));
client().performRequest(new Request("GET", "/_nodes/stats"));
client().performRequest(new Request("DELETE", "/test"));
Response response = client().performRequest(new Request("GET", "_nodes/usage"));
Map<String, Object> responseBodyMap = entityAsMap(response);
assertThat(responseBodyMap, notNullValue());
int successful = assertSuccess(responseBodyMap);
Map<String, Object> nodesMap = (Map<String, Object>) responseBodyMap.get("nodes");
assertThat(nodesMap, notNullValue());
assertThat(nodesMap.size(), equalTo(successful));
Map<String, Long> combinedRestUsage = new HashMap<>();
for (Map.Entry<String, Object> nodeEntry : nodesMap.entrySet()) {
Map<String, Object> restActionUsage = (Map<String, Object>) ((Map<String, Object>) nodeEntry.getValue()).get("rest_actions");
assertThat(restActionUsage, notNullValue());
for (Map.Entry<String, Object> restActionEntry : restActionUsage.entrySet()) {
Long currentUsage = combinedRestUsage.get(restActionEntry.getKey());
if (currentUsage == null) {
combinedRestUsage.put(restActionEntry.getKey(), ((Number) restActionEntry.getValue()).longValue());
} else {
combinedRestUsage.put(restActionEntry.getKey(), currentUsage + ((Number) restActionEntry.getValue()).longValue());
}
}
}
assertThat(combinedRestUsage.get("nodes_usage_action") - beforeCombinedRestUsage.get("nodes_usage_action"), equalTo(1L));
assertThat(combinedRestUsage.get("create_index_action") - beforeCombinedRestUsage.get("create_index_action"), equalTo(1L));
assertThat(combinedRestUsage.get("document_index_action") - beforeCombinedRestUsage.get("document_index_action"), equalTo(4L));
assertThat(combinedRestUsage.get("search_action") - beforeCombinedRestUsage.get("search_action"), equalTo(2L));
assertThat(combinedRestUsage.get("refresh_action") - beforeCombinedRestUsage.get("refresh_action"), equalTo(1L));
assertThat(combinedRestUsage.get("cat_indices_action") - beforeCombinedRestUsage.get("cat_indices_action"), equalTo(1L));
assertThat(combinedRestUsage.get("nodes_info_action") - beforeCombinedRestUsage.get("nodes_info_action"), equalTo(1L));
assertThat(combinedRestUsage.get("nodes_stats_action") - beforeCombinedRestUsage.get("nodes_stats_action"), equalTo(1L));
assertThat(combinedRestUsage.get("delete_index_action") - beforeCombinedRestUsage.get("delete_index_action"), equalTo(1L));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class OpenSearchNodesSnifferTests method testSniffNodes.
public void testSniffNodes() throws IOException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
try {
List<Node> sniffedNodes = sniffer.sniff();
if (sniffResponse.isFailure) {
fail("sniffNodes should have failed");
}
assertEquals(sniffResponse.result, sniffedNodes);
} catch (ResponseException e) {
Response response = e.getResponse();
if (sniffResponse.isFailure) {
final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
assertThat(e.getMessage(), startsWith(errorPrefix));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(response.getHost(), equalTo(httpHost));
assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
} else {
fail("sniffNodes should have succeeded: " + response.getStatusLine());
}
}
}
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class MultiVersionRepositoryAccessIT method createIndex.
private void createIndex(RestHighLevelClient client, String name, int shards) throws IOException {
final Request putIndexRequest = new Request("PUT", "/" + name);
putIndexRequest.setJsonEntity("{\n" + " \"settings\" : {\n" + " \"index\" : {\n" + " \"number_of_shards\" : " + shards + ", \n" + " \"number_of_replicas\" : 0 \n" + " }\n" + " }\n" + "}");
final Response response = client.getLowLevelClient().performRequest(putIndexRequest);
assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class MultiVersionRepositoryAccessIT method createSnapshot.
private static void createSnapshot(RestHighLevelClient client, String repoName, String name, String index) throws IOException {
final Request createSnapshotRequest = new Request("PUT", "/_snapshot/" + repoName + "/" + name);
createSnapshotRequest.addParameter("wait_for_completion", "true");
createSnapshotRequest.setJsonEntity("{ \"indices\" : \"" + index + "\"}");
final Response response = client.getLowLevelClient().performRequest(createSnapshotRequest);
assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK));
}
Aggregations