下面列出了怎么用org.apache.http.pool.PoolStats的API类实例代码及写法,或者点击链接到github查看源代码。
public static Map<HttpRoute, PoolStats> getConnManagerStats() {
if (connManager != null) {
Set<HttpRoute> routeSet = connManager.getRoutes();
if (routeSet != null && !routeSet.isEmpty()) {
Map<HttpRoute, PoolStats> routeStatsMap = new HashMap<HttpRoute, PoolStats>();
for (HttpRoute route : routeSet) {
PoolStats stats = connManager.getStats(route);
routeStatsMap.put(route, stats);
}
return routeStatsMap;
}
}
return null;
}
public AbstractRestClient(String url, ClientConfig config) {
Client client = null;
Object protocol = config.getProperty("protocol");
if (protocol != null && protocol.equals("https")) {
client = wrapTrustConfig(url, config);
} else {
client = ClientBuilder.newClient(config);
}
this.client = client;
this.client.register(GZipEncoder.class);
this.target = this.client.target(url);
this.pool = (PoolingHttpClientConnectionManager) config.getProperty(
ApacheClientProperties.CONNECTION_MANAGER);
if (this.pool != null) {
this.cleanExecutor = ExecutorUtil.newScheduledThreadPool(
"conn-clean-worker-%d");
Number idleTimeProp = (Number) config.getProperty("idleTime");
final long idleTime = idleTimeProp == null ?
IDLE_TIME : idleTimeProp.longValue();
final long checkPeriod = idleTime / 2L;
this.cleanExecutor.scheduleWithFixedDelay(() -> {
PoolStats stats = this.pool.getTotalStats();
int using = stats.getLeased() + stats.getPending();
if (using > 0) {
// Do clean only when all connections are idle
return;
}
// Release connections when all clients are inactive
this.pool.closeIdleConnections(idleTime, TimeUnit.MILLISECONDS);
this.pool.closeExpiredConnections();
}, checkPeriod, checkPeriod, TimeUnit.MILLISECONDS);
}
}
public static Map<HttpRoute, PoolStats> getConnManagerStats() {
if (connManager != null) {
Set<HttpRoute> routeSet = connManager.getRoutes();
if (routeSet != null && !routeSet.isEmpty()) {
Map<HttpRoute, PoolStats> routeStatsMap = new HashMap<HttpRoute, PoolStats>();
for (HttpRoute route : routeSet) {
PoolStats stats = connManager.getStats(route);
routeStatsMap.put(route, stats);
}
return routeStatsMap;
}
}
return null;
}
@Test
void totalMax() {
PoolStats poolStats = mock(PoolStats.class);
when(poolStats.getMax()).thenReturn(13);
when(connPoolControl.getTotalStats()).thenReturn(poolStats);
assertThat(registry.get("httpcomponents.httpclient.pool.total.max")
.tags("httpclient", "test")
.gauge().value()).isEqualTo(13.0);
}
@Test
void totalAvailable() {
PoolStats poolStats = mock(PoolStats.class);
when(poolStats.getAvailable()).thenReturn(17);
when(connPoolControl.getTotalStats()).thenReturn(poolStats);
assertThat(registry.get("httpcomponents.httpclient.pool.total.connections")
.tags("httpclient", "test", "state", "available")
.gauge().value()).isEqualTo(17.0);
}
@Test
void totalLeased() {
PoolStats poolStats = mock(PoolStats.class);
when(poolStats.getLeased()).thenReturn(23);
when(connPoolControl.getTotalStats()).thenReturn(poolStats);
assertThat(registry.get("httpcomponents.httpclient.pool.total.connections")
.tags("httpclient", "test", "state", "leased")
.gauge().value()).isEqualTo(23.0);
}
@Test
void totalPending() {
PoolStats poolStats = mock(PoolStats.class);
when(poolStats.getPending()).thenReturn(37);
when(connPoolControl.getTotalStats()).thenReturn(poolStats);
assertThat(registry.get("httpcomponents.httpclient.pool.total.pending")
.tags("httpclient", "test")
.gauge().value()).isEqualTo(37.0);
}
/**
* Captures the connection pool metrics.
*/
private void captureConnectionPoolMetrics() {
if (awsRequestMetrics.isEnabled() &&
httpClient.getHttpClientConnectionManager() instanceof
ConnPoolControl<?>) {
final PoolStats stats = ((ConnPoolControl<?>) httpClient
.getHttpClientConnectionManager()).getTotalStats();
awsRequestMetrics
.withCounter(HttpClientPoolAvailableCount, stats.getAvailable())
.withCounter(HttpClientPoolLeasedCount, stats.getLeased())
.withCounter(HttpClientPoolPendingCount, stats.getPending());
}
}
@Override
public void bindTo(final MeterRegistry registry) {
gauge("available", PoolStats::getAvailable)
.description("The number idle connections")
.baseUnit(CONNECTIONS)
.register(registry);
gauge("leased", PoolStats::getLeased)
.description("The number of connections that are actively executing requests")
.baseUnit(CONNECTIONS)
.register(registry);
gauge("total", stats -> stats.getAvailable() + stats.getLeased())
.description("The number of connections that are currently in the pool")
.baseUnit(CONNECTIONS)
.register(registry);
gauge("min", stats -> 0)
.description("The minimum number of connections in the pool")
.baseUnit(CONNECTIONS)
.register(registry);
gauge("max", PoolStats::getMax)
.description("The maximum number of connections in the pool")
.baseUnit(CONNECTIONS)
.register(registry);
gauge("queued", PoolStats::getPending)
.description("The number of queued connection lease requests")
.baseUnit(REQUESTS)
.register(registry);
}
private Gauge.Builder<Supplier<Number>> gauge(
final String name,
final ToIntFunction<PoolStats> function) {
return Gauge.builder(metricName + "." + name, () -> function.applyAsInt(stats.get()))
.tags(defaultTags);
}
public static PoolStats getConnManagerTotalStats() {
if (connManager != null) {
return connManager.getTotalStats();
}
return null;
}
@Test
public void testCleanExecutor() throws Exception {
// Modify IDLE_TIME 100ms to speed test
int newIdleTime = 100;
int newCheckPeriod = newIdleTime + 20;
RestClient client = new RestClientImpl("/test", 1000, newIdleTime,
10, 5, 200);
PoolingHttpClientConnectionManager pool;
pool = Whitebox.getInternalState(client, "pool");
pool = Mockito.spy(pool);
Whitebox.setInternalState(client, "pool", pool);
HttpRoute route = new HttpRoute(HttpHost.create(
"http://127.0.0.1:8080"));
// Create a connection manually, it will be put into leased list
HttpClientConnection conn = pool.requestConnection(route, null)
.get(1L, TimeUnit.SECONDS);
PoolStats stats = pool.getTotalStats();
int usingConns = stats.getLeased() + stats.getPending();
Assert.assertGte(1, usingConns);
// Sleep more than two check periods for busy connection
Thread.sleep(newCheckPeriod);
Mockito.verify(pool, Mockito.never()).closeExpiredConnections();
stats = pool.getTotalStats();
usingConns = stats.getLeased() + stats.getPending();
Assert.assertGte(1, usingConns);
// The connection will be put into available list
pool.releaseConnection(conn, null, 0, TimeUnit.SECONDS);
stats = pool.getTotalStats();
usingConns = stats.getLeased() + stats.getPending();
Assert.assertEquals(0, usingConns);
/*
* Sleep more than two check periods for free connection,
* ensure connection has been closed
*/
Thread.sleep(newCheckPeriod);
Mockito.verify(pool, Mockito.atLeastOnce())
.closeExpiredConnections();
Mockito.verify(pool, Mockito.atLeastOnce())
.closeIdleConnections(newIdleTime, TimeUnit.MILLISECONDS);
}
public static PoolStats getConnManagerTotalStats() {
if (connManager != null) {
return connManager.getTotalStats();
}
return null;
}
public void testLBClient() throws IOException, SolrServerException {
PoolingHttpClientConnectionManager pool = HttpClientUtil.createPoolingConnectionManager();
final HttpSolrClient client1 ;
int threadCount = atLeast(2);
final ExecutorService threads = ExecutorUtil.newMDCAwareFixedThreadPool(threadCount,
new SolrNamedThreadFactory(getClass().getSimpleName()+"TestScheduler"));
CloseableHttpClient httpClient = HttpClientUtil.createClient(new ModifiableSolrParams(), pool);
try{
final LBHttpSolrClient roundRobin = new LBHttpSolrClient.Builder().
withBaseSolrUrl(fooUrl).
withBaseSolrUrl(barUrl).
withHttpClient(httpClient)
.build();
List<ConcurrentUpdateSolrClient> concurrentClients = Arrays.asList(
new ConcurrentUpdateSolrClient.Builder(fooUrl)
.withHttpClient(httpClient).withThreadCount(threadCount)
.withQueueSize(10)
.withExecutorService(threads).build(),
new ConcurrentUpdateSolrClient.Builder(barUrl)
.withHttpClient(httpClient).withThreadCount(threadCount)
.withQueueSize(10)
.withExecutorService(threads).build());
for (int i=0; i<2; i++) {
roundRobin.deleteByQuery("*:*");
}
for (int i=0; i<57; i++) {
final SolrInputDocument doc = new SolrInputDocument("id", ""+i);
if (random().nextBoolean()) {
final ConcurrentUpdateSolrClient concurrentClient = concurrentClients.get(random().nextInt(concurrentClients.size()));
concurrentClient.add(doc); // here we are testing that CUSC and plain clients reuse pool
concurrentClient.blockUntilFinished();
} else {
if (random().nextBoolean()) {
roundRobin.add(doc);
} else {
final UpdateRequest updateRequest = new UpdateRequest();
updateRequest.add(doc); // here we mimic CloudSolrClient impl
final List<String> urls = Arrays.asList(fooUrl, barUrl);
Collections.shuffle(urls, random());
LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(updateRequest,
urls);
roundRobin.request(req);
}
}
}
for (int i=0; i<2; i++) {
roundRobin.commit();
}
int total=0;
for (int i=0; i<2; i++) {
total += roundRobin.query(new SolrQuery("*:*")).getResults().getNumFound();
}
assertEquals(57, total);
PoolStats stats = pool.getTotalStats();
//System.out.println("\n"+stats);
assertEquals("expected number of connections shouldn't exceed number of endpoints" + stats,
2, stats.getAvailable());
}finally {
threads.shutdown();
HttpClientUtil.close(httpClient);
}
}
/**
* Get the statistics
*/
public String getStats() {
PoolingClientConnectionManager cm = (PoolingClientConnectionManager) this.client.getConnectionManager();
PoolStats stats = cm.getTotalStats();
return "Connections: " + stats.toString() + " AvailableRequests: " + processQueue.availablePermits();
}
/**
* @return the connection pool stats.
*/
protected PoolStats getConnectionStats() {
return connectionManager.getTotalStats();
}