Search in sources :

Example 46 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class RestHighLevelClientTests method testWrapResponseListenerOnResponseExceptionWithIgnores.

public void testWrapResponseListenerOnResponseExceptionWithIgnores() throws IOException {
    TrackingActionListener trackingActionListener = new TrackingActionListener();
    ResponseListener responseListener = restHighLevelClient.wrapResponseListener(response -> response.getStatusLine().getStatusCode(), trackingActionListener, Collections.singleton(404));
    HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(RestStatus.NOT_FOUND));
    Response response = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
    ResponseException responseException = new ResponseException(response);
    responseListener.onFailure(responseException);
    //although we got an exception, we turn it into a successful response because the status code was provided among ignores
    assertNull(trackingActionListener.exception.get());
    assertEquals(404, trackingActionListener.statusCode.get());
}
Also used : MainResponse(org.elasticsearch.action.main.MainResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpHost(org.apache.http.HttpHost) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse)

Example 47 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class RestClient method nextHost.

/**
     * Returns an {@link Iterable} of hosts to be used for a request call.
     * Ideally, the first host is retrieved from the iterable and used successfully for the request.
     * Otherwise, after each failure the next host has to be retrieved from the iterator so that the request can be retried until
     * there are no more hosts available to retry against. The maximum total of attempts is equal to the number of hosts in the iterable.
     * The iterator returned will never be empty. In case there are no healthy hosts available, or dead ones to be be retried,
     * one dead host gets returned so that it can be retried.
     */
private HostTuple<Iterator<HttpHost>> nextHost() {
    final HostTuple<Set<HttpHost>> hostTuple = this.hostTuple;
    Collection<HttpHost> nextHosts = Collections.emptySet();
    do {
        Set<HttpHost> filteredHosts = new HashSet<>(hostTuple.hosts);
        for (Map.Entry<HttpHost, DeadHostState> entry : blacklist.entrySet()) {
            if (System.nanoTime() - entry.getValue().getDeadUntilNanos() < 0) {
                filteredHosts.remove(entry.getKey());
            }
        }
        if (filteredHosts.isEmpty()) {
            //last resort: if there are no good host to use, return a single dead one, the one that's closest to being retried
            List<Map.Entry<HttpHost, DeadHostState>> sortedHosts = new ArrayList<>(blacklist.entrySet());
            if (sortedHosts.size() > 0) {
                Collections.sort(sortedHosts, new Comparator<Map.Entry<HttpHost, DeadHostState>>() {

                    @Override
                    public int compare(Map.Entry<HttpHost, DeadHostState> o1, Map.Entry<HttpHost, DeadHostState> o2) {
                        return Long.compare(o1.getValue().getDeadUntilNanos(), o2.getValue().getDeadUntilNanos());
                    }
                });
                HttpHost deadHost = sortedHosts.get(0).getKey();
                logger.trace("resurrecting host [" + deadHost + "]");
                nextHosts = Collections.singleton(deadHost);
            }
        } else {
            List<HttpHost> rotatedHosts = new ArrayList<>(filteredHosts);
            Collections.rotate(rotatedHosts, rotatedHosts.size() - lastHostIndex.getAndIncrement());
            nextHosts = rotatedHosts;
        }
    } while (nextHosts.isEmpty());
    return new HostTuple<>(nextHosts.iterator(), hostTuple.authCache);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) HttpHost(org.apache.http.HttpHost) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet)

Example 48 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class SyncResponseListenerTests method mockResponse.

private static Response mockResponse() {
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
    StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "OK");
    HttpResponse httpResponse = new BasicHttpResponse(statusLine);
    return new Response(requestLine, new HttpHost("localhost", 9200), httpResponse);
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicRequestLine(org.apache.http.message.BasicRequestLine) RequestLine(org.apache.http.RequestLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicRequestLine(org.apache.http.message.BasicRequestLine) HttpHost(org.apache.http.HttpHost) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 49 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ElasticsearchHostsSniffer method readHost.

private static HttpHost readHost(String nodeId, JsonParser parser, Scheme scheme) throws IOException {
    HttpHost httpHost = null;
    String fieldName = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        if (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            fieldName = parser.getCurrentName();
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            if ("http".equals(fieldName)) {
                while (parser.nextToken() != JsonToken.END_OBJECT) {
                    if (parser.getCurrentToken() == JsonToken.VALUE_STRING && "publish_address".equals(parser.getCurrentName())) {
                        URI boundAddressAsURI = URI.create(scheme + "://" + parser.getValueAsString());
                        httpHost = new HttpHost(boundAddressAsURI.getHost(), boundAddressAsURI.getPort(), boundAddressAsURI.getScheme());
                    } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                        parser.skipChildren();
                    }
                }
            } else {
                parser.skipChildren();
            }
        }
    }
    //http section is not present if http is not enabled on the node, ignore such nodes
    if (httpHost == null) {
        logger.debug("skipping node [" + nodeId + "] with http disabled");
        return null;
    }
    return httpHost;
}
Also used : HttpHost(org.apache.http.HttpHost) URI(java.net.URI)

Example 50 with HttpHost

use of org.apache.http.HttpHost in project elasticsearch by elastic.

the class ElasticsearchHostsSniffer method readHosts.

private List<HttpHost> readHosts(HttpEntity entity) throws IOException {
    try (InputStream inputStream = entity.getContent()) {
        JsonParser parser = jsonFactory.createParser(inputStream);
        if (parser.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("expected data to start with an object");
        }
        List<HttpHost> hosts = new ArrayList<>();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                if ("nodes".equals(parser.getCurrentName())) {
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        JsonToken token = parser.nextToken();
                        assert token == JsonToken.START_OBJECT;
                        String nodeId = parser.getCurrentName();
                        HttpHost sniffedHost = readHost(nodeId, parser, this.scheme);
                        if (sniffedHost != null) {
                            logger.trace("adding node [" + nodeId + "]");
                            hosts.add(sniffedHost);
                        }
                    }
                } else {
                    parser.skipChildren();
                }
            }
        }
        return hosts;
    }
}
Also used : InputStream(java.io.InputStream) HttpHost(org.apache.http.HttpHost) ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

HttpHost (org.apache.http.HttpHost)258 HttpResponse (org.apache.http.HttpResponse)50 IOException (java.io.IOException)44 URI (java.net.URI)35 Header (org.apache.http.Header)35 HttpEntity (org.apache.http.HttpEntity)32 AuthScope (org.apache.http.auth.AuthScope)31 HttpGet (org.apache.http.client.methods.HttpGet)29 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)28 CredentialsProvider (org.apache.http.client.CredentialsProvider)28 HttpRequest (org.apache.http.HttpRequest)27 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)25 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)23 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)23 HttpParams (org.apache.http.params.HttpParams)23 Test (org.junit.Test)23 URISyntaxException (java.net.URISyntaxException)21 ProtocolVersion (org.apache.http.ProtocolVersion)21 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)19 Before (org.junit.Before)18