Search in sources :

Example 21 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project XobotOS by xamarin.

the class DefaultResponseParser method parseHead.

@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    // clear the buffer
    this.lineBuf.clear();
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;
    do {
        int i = sessionBuffer.readLine(this.lineBuf);
        if (i == -1 && count == 0) {
            // The server just dropped connection on us
            throw new NoHttpResponseException("The target server failed to respond");
        }
        cursor = new ParserCursor(0, this.lineBuf.length());
        if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
            // Got one
            break;
        } else if (i == -1 || count >= this.maxGarbageLines) {
            // Giving up
            throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
        }
        count++;
    } while (true);
    //create the status line from the status string
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ProtocolException(org.apache.http.ProtocolException)

Example 22 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project robovm by robovm.

the class DefaultResponseParser method parseHead.

@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    // clear the buffer
    this.lineBuf.clear();
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;
    do {
        int i = sessionBuffer.readLine(this.lineBuf);
        if (i == -1 && count == 0) {
            // The server just dropped connection on us
            throw new NoHttpResponseException("The target server failed to respond");
        }
        cursor = new ParserCursor(0, this.lineBuf.length());
        if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
            // Got one
            break;
        } else if (i == -1 || count >= this.maxGarbageLines) {
            // Giving up
            throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
        }
        count++;
    } while (true);
    //create the status line from the status string
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ProtocolException(org.apache.http.ProtocolException)

Example 23 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project lucene-solr by apache.

the class CloudSolrClient method requestWithRetryOnStaleState.

/**
   * As this class doesn't watch external collections on the client side,
   * there's a chance that the request will fail due to cached stale state,
   * which means the state must be refreshed from ZK and retried.
   */
protected NamedList<Object> requestWithRetryOnStaleState(SolrRequest request, int retryCount, String collection) throws SolrServerException, IOException {
    // important to call this before you start working with the ZkStateReader
    connect();
    // build up a _stateVer_ param to pass to the server containing all of the
    // external collection state versions involved in this request, which allows
    // the server to notify us that our cached state for one or more of the external
    // collections is stale and needs to be refreshed ... this code has no impact on internal collections
    String stateVerParam = null;
    List<DocCollection> requestedCollections = null;
    boolean isCollectionRequestOfV2 = false;
    if (request instanceof V2Request) {
        isCollectionRequestOfV2 = ((V2Request) request).isPerCollectionRequest();
    }
    boolean isAdmin = ADMIN_PATHS.contains(request.getPath());
    if (collection != null && !isAdmin && !isCollectionRequestOfV2) {
        // don't do _stateVer_ checking for admin, v2 api requests
        Set<String> requestedCollectionNames = getCollectionNames(collection);
        StringBuilder stateVerParamBuilder = null;
        for (String requestedCollection : requestedCollectionNames) {
            // track the version of state we're using on the client side using the _stateVer_ param
            DocCollection coll = getDocCollection(requestedCollection, null);
            if (coll == null) {
                throw new SolrException(ErrorCode.BAD_REQUEST, "Collection not found: " + requestedCollection);
            }
            int collVer = coll.getZNodeVersion();
            if (coll.getStateFormat() > 1) {
                if (requestedCollections == null)
                    requestedCollections = new ArrayList<>(requestedCollectionNames.size());
                requestedCollections.add(coll);
                if (stateVerParamBuilder == null) {
                    stateVerParamBuilder = new StringBuilder();
                } else {
                    // hopefully pipe is not an allowed char in a collection name
                    stateVerParamBuilder.append("|");
                }
                stateVerParamBuilder.append(coll.getName()).append(":").append(collVer);
            }
        }
        if (stateVerParamBuilder != null) {
            stateVerParam = stateVerParamBuilder.toString();
        }
    }
    if (request.getParams() instanceof ModifiableSolrParams) {
        ModifiableSolrParams params = (ModifiableSolrParams) request.getParams();
        if (stateVerParam != null) {
            params.set(STATE_VERSION, stateVerParam);
        } else {
            params.remove(STATE_VERSION);
        }
    }
    // else: ??? how to set this ???
    NamedList<Object> resp = null;
    try {
        resp = sendRequest(request, collection);
        //to avoid an O(n) operation we always add STATE_VERSION to the last and try to read it from there
        Object o = resp == null || resp.size() == 0 ? null : resp.get(STATE_VERSION, resp.size() - 1);
        if (o != null && o instanceof Map) {
            //remove this because no one else needs this and tests would fail if they are comparing responses
            resp.remove(resp.size() - 1);
            Map invalidStates = (Map) o;
            for (Object invalidEntries : invalidStates.entrySet()) {
                Map.Entry e = (Map.Entry) invalidEntries;
                getDocCollection((String) e.getKey(), (Integer) e.getValue());
            }
        }
    } catch (Exception exc) {
        Throwable rootCause = SolrException.getRootCause(exc);
        // or request is v2 api and its method is not GET
        if (collection == null || isAdmin || (request instanceof V2Request && request.getMethod() != SolrRequest.METHOD.GET)) {
            if (exc instanceof SolrServerException) {
                throw (SolrServerException) exc;
            } else if (exc instanceof IOException) {
                throw (IOException) exc;
            } else if (exc instanceof RuntimeException) {
                throw (RuntimeException) exc;
            } else {
                throw new SolrServerException(rootCause);
            }
        }
        int errorCode = (rootCause instanceof SolrException) ? ((SolrException) rootCause).code() : SolrException.ErrorCode.UNKNOWN.code;
        log.error("Request to collection {} failed due to (" + errorCode + ") {}, retry? " + retryCount, collection, rootCause.toString());
        boolean wasCommError = (rootCause instanceof ConnectException || rootCause instanceof ConnectTimeoutException || rootCause instanceof NoHttpResponseException || rootCause instanceof SocketException);
        if (wasCommError) {
            // in retryExpiryTime time
            for (DocCollection ext : requestedCollections) {
                ExpiringCachedDocCollection cacheEntry = collectionStateCache.get(ext.getName());
                if (cacheEntry == null)
                    continue;
                cacheEntry.maybeStale = true;
            }
            if (retryCount < MAX_STALE_RETRIES) {
                // the state would not have been updated
                return requestWithRetryOnStaleState(request, retryCount + 1, collection);
            }
        }
        boolean stateWasStale = false;
        if (retryCount < MAX_STALE_RETRIES && requestedCollections != null && !requestedCollections.isEmpty() && SolrException.ErrorCode.getErrorCode(errorCode) == SolrException.ErrorCode.INVALID_STATE) {
            // cached state for one or more external collections was stale
            // re-issue request using updated state
            stateWasStale = true;
            // just re-read state for all of them, which is a little heavy handed but hopefully a rare occurrence
            for (DocCollection ext : requestedCollections) {
                collectionStateCache.remove(ext.getName());
            }
        }
        // with ZK just to make sure the node we're trying to hit is still part of the collection
        if (retryCount < MAX_STALE_RETRIES && !stateWasStale && requestedCollections != null && !requestedCollections.isEmpty() && wasCommError) {
            for (DocCollection ext : requestedCollections) {
                DocCollection latestStateFromZk = getDocCollection(ext.getName(), null);
                if (latestStateFromZk.getZNodeVersion() != ext.getZNodeVersion()) {
                    // looks like we couldn't reach the server because the state was stale == retry
                    stateWasStale = true;
                    // we just pulled state from ZK, so update the cache so that the retry uses it
                    collectionStateCache.put(ext.getName(), new ExpiringCachedDocCollection(latestStateFromZk));
                }
            }
        }
        if (requestedCollections != null) {
            // done with this
            requestedCollections.clear();
        }
        // if the state was stale, then we retry the request once with new state pulled from Zk
        if (stateWasStale) {
            log.warn("Re-trying request to  collection(s) " + collection + " after stale state error from server.");
            resp = requestWithRetryOnStaleState(request, retryCount + 1, collection);
        } else {
            if (exc instanceof SolrException) {
                throw exc;
            }
            if (exc instanceof SolrServerException) {
                throw (SolrServerException) exc;
            } else if (exc instanceof IOException) {
                throw (IOException) exc;
            } else {
                throw new SolrServerException(rootCause);
            }
        }
    }
    return resp;
}
Also used : SocketException(java.net.SocketException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrException(org.apache.solr.common.SolrException) ConnectException(java.net.ConnectException) NoHttpResponseException(org.apache.http.NoHttpResponseException) IOException(java.io.IOException) V2Request(org.apache.solr.client.solrj.request.V2Request) TimeoutException(java.util.concurrent.TimeoutException) NoHttpResponseException(org.apache.http.NoHttpResponseException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) SocketException(java.net.SocketException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 24 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project lucene-solr by apache.

the class CloudSolrClientCacheTest method testCaching.

public void testCaching() throws Exception {
    String collName = "gettingstarted";
    Set<String> livenodes = new HashSet<>();
    Map<String, ClusterState.CollectionRef> refs = new HashMap<>();
    Map<String, DocCollection> colls = new HashMap<>();
    class Ref extends ClusterState.CollectionRef {

        private String c;

        public Ref(String c) {
            super(null);
            this.c = c;
        }

        @Override
        public boolean isLazilyLoaded() {
            return true;
        }

        @Override
        public DocCollection get() {
            gets.incrementAndGet();
            return colls.get(c);
        }
    }
    Map<String, Function> responses = new HashMap<>();
    NamedList okResponse = new NamedList();
    okResponse.add("responseHeader", new NamedList<>(Collections.singletonMap("status", 0)));
    LBHttpSolrClient mockLbclient = getMockLbHttpSolrClient(responses);
    AtomicInteger lbhttpRequestCount = new AtomicInteger();
    try (CloudSolrClient cloudClient = new CloudSolrClient.Builder().withLBHttpSolrClient(mockLbclient).withClusterStateProvider(getStateProvider(livenodes, refs)).build()) {
        livenodes.addAll(ImmutableSet.of("192.168.1.108:7574_solr", "192.168.1.108:8983_solr"));
        ClusterState cs = ClusterState.load(1, coll1State.getBytes(UTF_8), Collections.emptySet(), "/collections/gettingstarted/state.json");
        refs.put(collName, new Ref(collName));
        colls.put(collName, cs.getCollectionOrNull(collName));
        responses.put("request", o -> {
            int i = lbhttpRequestCount.incrementAndGet();
            if (i == 1)
                return new ConnectException("TEST");
            if (i == 2)
                return new SocketException("TEST");
            if (i == 3)
                return new NoHttpResponseException("TEST");
            return okResponse;
        });
        UpdateRequest update = new UpdateRequest().add("id", "123", "desc", "Something 0");
        cloudClient.request(update, collName);
        assertEquals(2, refs.get(collName).getCount());
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ClusterState(org.apache.solr.common.cloud.ClusterState) SocketException(java.net.SocketException) HashMap(java.util.HashMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) NamedList(org.apache.solr.common.util.NamedList) Function(java.util.function.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocCollection(org.apache.solr.common.cloud.DocCollection) HashSet(java.util.HashSet) ConnectException(java.net.ConnectException)

Example 25 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project lucene-solr by apache.

the class LeaderInitiatedRecoveryOnCommitTest method sendCommitWithRetry.

protected void sendCommitWithRetry(Replica replica) throws Exception {
    String replicaCoreUrl = replica.getCoreUrl();
    log.info("Sending commit request to: " + replicaCoreUrl);
    final RTimer timer = new RTimer();
    try (HttpSolrClient client = getHttpSolrClient(replicaCoreUrl)) {
        try {
            client.commit();
            log.info("Sent commit request to {} OK, took {}ms", replicaCoreUrl, timer.getTime());
        } catch (Exception exc) {
            Throwable rootCause = SolrException.getRootCause(exc);
            if (rootCause instanceof NoHttpResponseException) {
                log.warn("No HTTP response from sending commit request to " + replicaCoreUrl + "; will re-try after waiting 3 seconds");
                Thread.sleep(3000);
                client.commit();
                log.info("Second attempt at sending commit to " + replicaCoreUrl + " succeeded.");
            } else {
                throw exc;
            }
        }
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) NoHttpResponseException(org.apache.http.NoHttpResponseException) RTimer(org.apache.solr.util.RTimer) SolrException(org.apache.solr.common.SolrException) NoHttpResponseException(org.apache.http.NoHttpResponseException)

Aggregations

NoHttpResponseException (org.apache.http.NoHttpResponseException)38 IOException (java.io.IOException)13 SocketException (java.net.SocketException)12 StatusLine (org.apache.http.StatusLine)12 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)11 SocketTimeoutException (java.net.SocketTimeoutException)10 ParserCursor (org.apache.http.message.ParserCursor)9 UnknownHostException (java.net.UnknownHostException)8 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)8 HttpRequest (org.apache.http.HttpRequest)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 ConnectException (java.net.ConnectException)6 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)6 InterruptedIOException (java.io.InterruptedIOException)5 SSLException (javax.net.ssl.SSLException)5 HashMap (java.util.HashMap)4 HttpEntity (org.apache.http.HttpEntity)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4 SolrException (org.apache.solr.common.SolrException)4