org.apache.http.pool.PoolStats Java Examples

The following examples show how to use org.apache.http.pool.PoolStats. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientUtils.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #2
Source File: AbstractRestClient.java    From hugegraph-common with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #3
Source File: HttpClientUtils.java    From paas with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #4
Source File: PoolingHttpClientConnectionManagerMetricsBinderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: PoolingHttpClientConnectionManagerMetricsBinderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #6
Source File: PoolingHttpClientConnectionManagerMetricsBinderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: PoolingHttpClientConnectionManagerMetricsBinderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: AmazonHttpClient.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
    }

}
 
Example #9
Source File: HttpConnectionPoolMetrics.java    From riptide with MIT License 5 votes vote down vote up
@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);
}
 
Example #10
Source File: HttpConnectionPoolMetrics.java    From riptide with MIT License 5 votes vote down vote up
private Gauge.Builder<Supplier<Number>> gauge(
        final String name,
        final ToIntFunction<PoolStats> function) {

    return Gauge.builder(metricName + "." + name, () -> function.applyAsInt(stats.get()))
            .tags(defaultTags);
}
 
Example #11
Source File: HttpClientUtils.java    From blog-sample with Apache License 2.0 4 votes vote down vote up
public static PoolStats getConnManagerTotalStats() {
    if (connManager != null) {
        return connManager.getTotalStats();
    }
    return null;
}
 
Example #12
Source File: RestClientTest.java    From hugegraph-common with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #13
Source File: HttpClientUtils.java    From paas with Apache License 2.0 4 votes vote down vote up
public static PoolStats getConnManagerTotalStats() {
    if (connManager != null) {
        return connManager.getTotalStats();
    }
    return null;
}
 
Example #14
Source File: HttpSolrClientConPoolTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
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);
  }
}
 
Example #15
Source File: HttpConnectionPool.java    From Poseidon with Apache License 2.0 4 votes vote down vote up
/**
 * Get the statistics
 */
public String getStats() {
    PoolingClientConnectionManager cm = (PoolingClientConnectionManager) this.client.getConnectionManager();
    PoolStats stats = cm.getTotalStats();
    return "Connections: " + stats.toString() + " AvailableRequests: " + processQueue.availablePermits();
}
 
Example #16
Source File: APIService.java    From data-prep with Apache License 2.0 4 votes vote down vote up
/**
 * @return the connection pool stats.
 */
protected PoolStats getConnectionStats() {
    return connectionManager.getTotalStats();
}