private static TransportClient open( final ElasticsearchReporter reporter, final Settings settings, final Iterable<TransportAddress> addresses) { assert settings != null; reporter.logNotice(format("opening shared client for %s", identify(settings))); try { TransportClient client = new TransportClient(settings); client.addTransportAddresses(toArray(addresses, TransportAddress.class)); reporter.logNotice( format( "shared client for %s opened -> %s", identify(settings), toDefaultString(client))); return client; } catch (Exception failure) { reporter.logError( format("unable to open shared client for %s", identify(settings)), failure); throw failure; } }
@Test // issue #6 public void testThatEmptyMetricsDoNotResultInBrokenBulkRequest() throws Exception { long connectionsBeforeReporting = getTotalHttpConnections(); elasticsearchReporter.report(); long connectionsAfterReporting = getTotalHttpConnections(); org.assertj.core.api.Assertions.assertThat(connectionsAfterReporting) .isEqualTo(connectionsBeforeReporting); }
private SharedSecrets connect(final ElasticsearchReporter reporter, final Settings settings) { assert settings != null; int count = this.counters.add(settings, 1) + 1; assert count > 0; reporter.logNotice(format("connect to %s -> %d connections", identify(settings), count)); return this; }
private ElasticsearchReporter.Builder createElasticsearchReporterBuilder() { Map<String, Object> additionalFields = new HashMap<>(); additionalFields.put("host", "localhost"); return ElasticsearchReporter.forRegistry(registry) .hosts("localhost:" + getPortOfRunningNode()) .prefixedWith(prefix) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .filter(MetricFilter.ALL) .index(index) .additionalFields(additionalFields); }
private SharedSecrets disconnect( final ElasticsearchReporter reporter, final Settings settings, final TimeValue wait) { assert settings != null; int count = this.counters.remove(settings, 1) - 1; assert count >= 0; reporter.logNotice(format("disconnect from %s -> %d connections", identify(settings), count)); if (count == 0) { TransportClient client = this.clients.remove(settings); if (client != null) { close(reporter, settings, client, wait); } } return this; }
private static void close( final ElasticsearchReporter reporter, final Settings settings, final TransportClient client, final TimeValue wait) { assert client != null; checkNotNull(wait); reporter.logNotice( format( "closing shared client for %s -> %s", identify(settings), toDefaultString(client))); ExecutorService service = newSingleThreadExecutor(); service.execute( new Runnable() { public void run() { sleepUninterruptibly(wait.duration(), wait.unit()); try { client.close(); reporter.logNotice(format("shared client for %s closed", identify(settings))); } catch (Exception failure) { reporter.logError( format( "unable to close shared client for %s -> %s", identify(settings), toDefaultString(client)), failure); } } }); shutdownAndAwaitTermination(service, wait.duration(), wait.unit()); }
private void reportAndRefresh() { elasticsearchReporter.report(); client().admin().indices().prepareRefresh(indexWithDate).execute().actionGet(); }