Search in sources :

Example 91 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method delete.

@Override
public Status delete(final String table, final String key) {
    try {
        final Response searchResponse = search(table, key);
        final int statusCode = searchResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_NOT_FOUND) {
            return Status.NOT_FOUND;
        } else if (statusCode != HttpStatus.SC_OK) {
            return Status.ERROR;
        }
        final Map<String, Object> map = map(searchResponse);
        @SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
        final int total = (int) hits.get("total");
        if (total == 0) {
            return Status.NOT_FOUND;
        }
        @SuppressWarnings("unchecked") final Map<String, Object> hit = (Map<String, Object>) ((List<Object>) hits.get("hits")).get(0);
        final Response deleteResponse = restClient.performRequest("DELETE", "/" + indexKey + "/" + table + "/" + hit.get("_id"));
        if (deleteResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            return Status.ERROR;
        }
        if (!isRefreshNeeded) {
            synchronized (this) {
                isRefreshNeeded = true;
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : Response(org.elasticsearch.client.Response) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Example 92 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicatorTests method elasticsearchWithYellowStatusIsUp.

@Test
void elasticsearchWithYellowStatusIsUp() throws IOException {
    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "yellow").getBytes()));
    Response response = mock(Response.class);
    StatusLine statusLine = mock(StatusLine.class);
    given(statusLine.getStatusCode()).willReturn(200);
    given(response.getStatusLine()).willReturn(statusLine);
    given(response.getEntity()).willReturn(httpEntity);
    given(this.restClient.performRequest(any(Request.class))).willReturn(response);
    Health health = this.elasticsearchRestHealthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertHealthDetailsWithStatus(health.getDetails(), "yellow");
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.jupiter.api.Test)

Example 93 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicatorTests method elasticsearchIsUp.

@Test
void elasticsearchIsUp() throws IOException {
    BasicHttpEntity httpEntity = new BasicHttpEntity();
    httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "green").getBytes()));
    Response response = mock(Response.class);
    StatusLine statusLine = mock(StatusLine.class);
    given(statusLine.getStatusCode()).willReturn(200);
    given(response.getStatusLine()).willReturn(statusLine);
    given(response.getEntity()).willReturn(httpEntity);
    given(this.restClient.performRequest(any(Request.class))).willReturn(response);
    Health health = this.elasticsearchRestHealthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.UP);
    assertHealthDetailsWithStatus(health.getDetails(), "green");
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.jupiter.api.Test)

Example 94 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    Response response = this.client.performRequest(new Request("GET", "/_cluster/health/"));
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
        builder.down();
        builder.withDetail("statusCode", statusLine.getStatusCode());
        builder.withDetail("reasonPhrase", statusLine.getReasonPhrase());
        return;
    }
    try (InputStream inputStream = response.getEntity().getContent()) {
        doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
    }
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) InputStream(java.io.InputStream) Request(org.elasticsearch.client.Request)

Example 95 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClient method shards.

/**
 * Returns list of shards for given indexes
 *
 * Only STARTED shards are returned.
 *
 * @param indices indexes to return shards for (wildcard format accepted)
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
public List<Shard> shards(String... indices) {
    Map<String, String> idToAddress = nodes().stream().collect(toMap(Node::getId, Node::getHttpAddress));
    try {
        Request r = new Request("GET", "/_cat/shards/" + String.join(",", indices));
        r.addParameter("format", "json");
        r.addParameter("h", "id,index,shard,prirep,docs,state,ip,node");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            List<Shard> shards = new ArrayList<>(array.size());
            for (JsonValue value : array) {
                Optional<Shard> shard = convertToShard(value, idToAddress);
                shard.ifPresent(shards::add);
            }
            LOG.log(FINE, "Shards " + shards);
            return shards;
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES shards", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException) Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

Response (org.elasticsearch.client.Response)111 Request (org.elasticsearch.client.Request)38 IOException (java.io.IOException)33 HttpEntity (org.apache.http.HttpEntity)24 NStringEntity (org.apache.http.nio.entity.NStringEntity)21 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Map (java.util.Map)14 BasicHeader (org.apache.http.message.BasicHeader)14 ResponseException (org.elasticsearch.client.ResponseException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 RestClient (org.elasticsearch.client.RestClient)11 ArrayList (java.util.ArrayList)10 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)10 StringEntity (org.apache.http.entity.StringEntity)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 InputStream (java.io.InputStream)8 JSONObject (org.json.simple.JSONObject)8 MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7