protected Response execute(Request request, boolean checkStatus) { Response response = network.execute(request); if (checkStatus) { checkResponse(request, response); } return response; }
public RestClient(Settings settings) { this(settings, new NetworkClient(settings)); }
@Override public void close() { closeTransport(); }
public InputStream scroll(String scrollId) { // NB: dynamically get the stats since the transport can change long start = network.transportStats().netTotalTime; try { BytesArray body; if (internalVersion.onOrAfter(EsMajorVersion.V_2_X)) { body = new BytesArray("{\"scroll_id\":\"" + scrollId + "\"}"); } else { body = new BytesArray(scrollId); } // use post instead of get to avoid some weird encoding issues (caused by the long URL) InputStream is = execute(POST, "_search/scroll?scroll=" + scrollKeepAlive.toString(), body).body(); stats.scrollTotal++; return is; } finally { stats.scrollTotalTime += network.transportStats().netTotalTime - start; } }
public NetworkClient(Settings settings, TransportFactory transportFactory) { this.settings = settings.copy(); this.secureSettings = new SecureSettings(settings); this.nodes = SettingsUtils.discoveredOrDeclaredNodes(settings); this.transportFactory = transportFactory; // shuffle the list of nodes so in case of failures, the fallback is spread Collections.shuffle(nodes); if (SettingsUtils.hasPinnedNode(settings)) { // move pinned node in front to be selected (only once) String pinnedNode = SettingsUtils.getPinnedNode(settings); if (log.isDebugEnabled()) { log.debug("Opening (pinned) network client to " + pinnedNode); } nodes.remove(pinnedNode); nodes.add(0, pinnedNode); } selectNextNode(); Assert.notNull(currentTransport, "no node information provided"); }
@Override public Stats stats() { Stats copy = new Stats(stats); if (network != null) { copy.aggregate(network.stats()); } return copy; }
public String getCurrentNode() { return network.currentNode(); } }
private boolean selectNextNode() { if (nextClient >= nodes.size()) { return false; } if (currentTransport != null) { stats.nodeRetries++; } closeTransport(); currentNode = nodes.get(nextClient++); SettingsUtils.pinNode(settings, currentNode); currentTransport = transportFactory.create(settings, secureSettings, currentNode); return true; }
/** * Executes a single bulk operation against the provided resource, using the passed data as the request body. * This method will retry bulk requests if the entire bulk request fails, but will not retry singular * document failures. * * @param resource target of the bulk request. * @param data bulk request body. This body will be cleared of entries on any successful bulk request. * @return a BulkActionResponse object that will detail if there were failing documents that should be retried. */ public BulkActionResponse bulk(Resource resource, TrackingBytesArray data) { // NB: dynamically get the stats since the transport can change long start = network.transportStats().netTotalTime; Response response = execute(PUT, resource.bulk(), data); long spent = network.transportStats().netTotalTime - start; stats.bulkTotal++; stats.docsSent += data.entries(); stats.bulkTotalTime += spent; // bytes will be counted by the transport layer return new BulkActionResponse(parseBulkActionResponse(response), response.status(), spent); }
newNode = selectNextNode();
@Override public Stats stats() { Stats copy = new Stats(stats); if (network != null) { copy.aggregate(network.stats()); } return copy; }
public String getCurrentNode() { return network.currentNode(); } }
Mockito.when(mock.execute(Mockito.eq(request))).thenReturn(new SimpleResponse(201, new FastByteArrayInputStream(new BytesArray(response)), "localhost:9200"));
@Override public void close() { closeTransport(); }
public InputStream scroll(String scrollId) { // NB: dynamically get the stats since the transport can change long start = network.transportStats().netTotalTime; try { // use post instead of get to avoid some weird encoding issues (caused by the long URL) InputStream is = execute(POST, "_search/scroll?scroll=" + scrollKeepAlive.toString(), new BytesArray(scrollId)).body(); stats.scrollTotal++; return is; } finally { stats.scrollTotalTime += network.transportStats().netTotalTime - start; } }
public NetworkClient(Settings settings) { this.settings = settings.copy(); this.nodes = SettingsUtils.discoveredOrDeclaredNodes(settings); // shuffle the list of nodes so in case of failures, the fallback is spread Collections.shuffle(nodes); if (SettingsUtils.hasPinnedNode(settings)) { // move pinned node in front to be selected (only once) String pinnedNode = SettingsUtils.getPinnedNode(settings); if (log.isDebugEnabled()) { log.debug("Opening (pinned) network client to " + pinnedNode); } nodes.remove(pinnedNode); nodes.add(0, pinnedNode); } selectNextNode(); Assert.notNull(currentTransport, "no node information provided"); }
@Override public Stats stats() { Stats copy = new Stats(stats); if (network != null) { copy.aggregate(network.stats()); } return copy; }
public String getCurrentNode() { return network.currentNode(); } }