public String getNextServer() { if (serversRing.size() > 0) { try { return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } catch (IndexOutOfBoundsException outOfBoundsException) { // In the very rare case where nextServerIndex overflowed, this will end up with a negative number, // resulting in an IndexOutOfBoundsException. // We should then start back at the beginning of the server list. // Note that this might happen on several threads at once, in which the reset might happen a few times log.info("Resetting next server index"); nextServerIndex.set(0); return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } } else { throw new NoServerConfiguredException("No Server is assigned to client to connect"); } }
@Override public void failed(final Exception ex) { log.error("Exception occurred during async execution.", ex); if (ex instanceof HttpHostConnectException) { String host = ((HttpHostConnectException) ex).getHost().toURI(); resultHandler.failed(new CouldNotConnectException(host, ex)); return; } resultHandler.failed(ex); }
removeNodeAndUpdateServers(cnce.getHost()); return;
@Override public <T extends JestResult> T execute(JestClient client, Action<T> clientRequest) { do { LOG.debug("Executing request {}", clientRequest); try { T result = client.execute(clientRequest); return result; } catch (CouldNotConnectException connectException) { LOG.error("Could not connect to elastic endpoint: {}", connectException.getHost(), connectException); } catch (IOException ex) { LOG.error("Could not perform request {}: {}", clientRequest, ex.getMessage(), ex); } catch (com.google.gson.JsonSyntaxException gsonException) { LOG.error("A Json error occurred: {}", gsonException.getMessage(), gsonException); } // Retry-Logic LOG.debug("Request was not executed properly. Attempting Retry..."); if (cooldownInMs > 0) { LOG.debug("Sleep " + cooldownInMs + " before retrying"); try { Thread.sleep(cooldownInMs); } catch (InterruptedException e) { throw new RuntimeException("Thread interrupted.", e); } } LOG.debug("Retrying now"); } while (true); }
public <T extends JestResult> T execute(Action<T> clientRequest, RequestConfig requestConfig) throws IOException { HttpUriRequest request = prepareRequest(clientRequest, requestConfig); CloseableHttpResponse response = null; try { response = executeRequest(request); return deserializeResponse(response, request, clientRequest); } catch (HttpHostConnectException ex) { throw new CouldNotConnectException(ex.getHost().toURI(), ex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { log.error("Exception occurred while closing response stream.", ex); } } } }
public String getNextServer() { if (serversRing.size() > 0) { try { return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } catch (IndexOutOfBoundsException outOfBoundsException) { // In the very rare case where nextServerIndex overflowed, this will end up with a negative number, // resulting in an IndexOutOfBoundsException. // We should then start back at the beginning of the server list. // Note that this might happen on several threads at once, in which the reset might happen a few times log.info("Resetting next server index"); nextServerIndex.set(0); return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } } else { throw new NoServerConfiguredException("No Server is assigned to client to connect"); } }
@Override public <T extends JestResult> T execute(JestClient client, Action<T> clientRequest) { do { LOG.debug("Executing request {}", clientRequest); try { T result = client.execute(clientRequest); return result; } catch (CouldNotConnectException connectException) { LOG.error("Could not connect to elastic endpoint: {}", connectException.getHost(), connectException); } catch (IOException ex) { LOG.error("Could not perform request {}: {}", clientRequest, ex.getMessage(), ex); } catch (com.google.gson.JsonSyntaxException gsonException) { LOG.error("A Json error occurred: {}", gsonException.getMessage(), gsonException); } // Retry-Logic LOG.debug("Request was not executed properly. Attempting Retry..."); if (cooldownInMs > 0) { LOG.debug("Sleep " + cooldownInMs + " before retrying"); try { Thread.sleep(cooldownInMs); } catch (InterruptedException e) { throw new RuntimeException("Thread interrupted.", e); } } LOG.debug("Retrying now"); } while (true); }
public <T extends JestResult> T execute(Action<T> clientRequest, RequestConfig requestConfig) throws IOException { String elasticSearchRestUrl = getRequestURL(getNextServer(), clientRequest.getURI(ElasticsearchVersion.UNKNOWN)); HttpUriRequest request = constructHttpMethod(clientRequest.getRestMethodName(), elasticSearchRestUrl, clientRequest.getData(gson), requestConfig); // add headers added to action if (!clientRequest.getHeaders().isEmpty()) { for (Map.Entry<String, Object> header : clientRequest.getHeaders().entrySet()) { request.addHeader(header.getKey(), header.getValue().toString()); } } try { HttpResponse response = httpClient.execute(request); return deserializeResponse(response, clientRequest); } catch (HttpHostConnectException ex) { throw new CouldNotConnectException(ex.getHost().toURI(), ex); } }
public String getNextServer() { if (serversRing.size() > 0) { try { return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } catch (IndexOutOfBoundsException outOfBoundsException) { // In the very rare case where nextServerIndex overflowed, this will end up with a negative number, // resulting in an IndexOutOfBoundsException. // We should then start back at the beginning of the server list. // Note that this might happen on several threads at once, in which the reset might happen a few times log.info("Resetting next server index"); nextServerIndex.set(0); return serversRing.get(nextServerIndex.getAndIncrement() % serversRing.size()); } } else { throw new NoServerConfiguredException("No Server is assigned to client to connect"); } }
removeNodeAndUpdateServers(cnce.getHost()); return;
@Override public void failed(final Exception ex) { log.error("Exception occurred during async execution.", ex); if (ex instanceof HttpHostConnectException) { String host = ((HttpHostConnectException) ex).getHost().toURI(); resultHandler.failed(new CouldNotConnectException(host, ex)); return; } resultHandler.failed(ex); }
removeNodeAndUpdateServers(cnce.getHost()); return;
public <T extends JestResult> T execute(Action<T> clientRequest, RequestConfig requestConfig) throws IOException { HttpUriRequest request = prepareRequest(clientRequest, requestConfig); CloseableHttpResponse response = null; try { response = executeRequest(request); return deserializeResponse(response, request, clientRequest); } catch (HttpHostConnectException ex) { throw new CouldNotConnectException(ex.getHost().toURI(), ex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { log.error("Exception occurred while closing response stream.", ex); } } } }