Reduce overhead and accuracy of Dropwizard gauges.

pull/333/head
Brett Wooldridge 10 years ago
parent 6b3a689cea
commit 74a78beab7

@ -16,13 +16,10 @@
package com.zaxxer.hikari.metrics;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.CachedGauge;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.zaxxer.hikari.pool.HikariPool;
import com.zaxxer.hikari.pool.PoolBagEntry;
import com.zaxxer.hikari.util.ClockSource;
@ -30,51 +27,46 @@ public final class CodaHaleMetricsTracker extends MetricsTracker
{
private static final ClockSource clockSource = ClockSource.INSTANCE;
private final String poolName;
private final Timer connectionObtainTimer;
private final Histogram connectionUsage;
private final MetricRegistry registry;
public CodaHaleMetricsTracker(final HikariPool pool, final MetricRegistry registry) {
super(pool);
public CodaHaleMetricsTracker(final String poolName, final PoolStats poolStats, final MetricRegistry registry) {
this.poolName = poolName;
this.registry = registry;
final String poolName = pool.getConfiguration().getPoolName();
this.connectionObtainTimer = registry.timer(MetricRegistry.name(poolName, "pool", "Wait"));
this.connectionUsage = registry.histogram(MetricRegistry.name(poolName, "pool", "Usage"));
registry.register(MetricRegistry.name(poolName, "pool", "TotalConnections"),
new CachedGauge<Integer>(10, TimeUnit.SECONDS) {
new Gauge<Integer>() {
@Override
protected Integer loadValue()
{
return pool.getTotalConnections();
public Integer getValue() {
return poolStats.getTotalConnections();
}
});
registry.register(MetricRegistry.name(poolName, "pool", "IdleConnections"),
new CachedGauge<Integer>(10, TimeUnit.SECONDS) {
new Gauge<Integer>() {
@Override
protected Integer loadValue()
{
return pool.getIdleConnections();
public Integer getValue() {
return poolStats.getIdleConnections();
}
});
registry.register(MetricRegistry.name(poolName, "pool", "ActiveConnections"),
new CachedGauge<Integer>(10, TimeUnit.SECONDS) {
new Gauge<Integer>() {
@Override
protected Integer loadValue()
{
return pool.getActiveConnections();
public Integer getValue() {
return poolStats.getActiveConnections();
}
});
registry.register(MetricRegistry.name(poolName, "pool", "PendingConnections"),
new CachedGauge<Integer>(10, TimeUnit.SECONDS) {
new Gauge<Integer>() {
@Override
protected Integer loadValue()
{
return pool.getThreadsAwaitingConnection();
public Integer getValue() {
return poolStats.getPendingThreads();
}
});
}
@ -83,7 +75,6 @@ public final class CodaHaleMetricsTracker extends MetricsTracker
@Override
public void close()
{
final String poolName = pool.getConfiguration().getPoolName();
registry.remove(MetricRegistry.name(poolName, "pool", "Wait"));
registry.remove(MetricRegistry.name(poolName, "pool", "Usage"));
registry.remove(MetricRegistry.name(poolName, "pool", "TotalConnections"));

@ -16,7 +16,6 @@
package com.zaxxer.hikari.metrics;
import com.zaxxer.hikari.pool.HikariPool;
import com.zaxxer.hikari.pool.PoolBagEntry;
import com.zaxxer.hikari.util.ClockSource;
@ -29,11 +28,8 @@ public class MetricsTracker implements AutoCloseable
{
public static final MetricsContext NO_CONTEXT = new MetricsContext();
protected final HikariPool pool;
public MetricsTracker(final HikariPool pool)
public MetricsTracker()
{
this.pool = pool;
}
public MetricsContext recordConnectionRequest()

@ -0,0 +1,96 @@
/*
* Copyright (C) 2015 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari.metrics;
import java.util.concurrent.atomic.AtomicLong;
import com.zaxxer.hikari.util.ClockSource;
/**
*
* @author Brett Wooldridge
*/
public abstract class PoolStats
{
private final ClockSource clock;
private final AtomicLong reloadAt;
private final long timeoutMs;
protected volatile int totalConnections;
protected volatile int idleConnections;
protected volatile int activeConnections;
protected volatile int pendingThreads;
public PoolStats(final long timeoutMs)
{
this.timeoutMs = timeoutMs;
this.reloadAt = new AtomicLong(0);
this.clock = ClockSource.INSTANCE;
}
protected int getTotalConnections()
{
if (shouldLoad()) {
update();
}
return totalConnections;
}
protected int getIdleConnections()
{
if (shouldLoad()) {
update();
}
return idleConnections;
}
protected int getActiveConnections()
{
if (shouldLoad()) {
update();
}
return activeConnections;
}
protected int getPendingThreads()
{
if (shouldLoad()) {
update();
}
return pendingThreads;
}
protected abstract void update();
private boolean shouldLoad()
{
for (; ; ) {
final long time = clock.currentTime();
final long current = reloadAt.get();
if (current > time) {
return false;
}
if (reloadAt.compareAndSet(current, clock.plusMillis(time, timeoutMs))) {
return true;
}
}
}
}

@ -51,6 +51,7 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.metrics.CodaHaleMetricsTracker;
import com.zaxxer.hikari.metrics.CodahaleHealthChecker;
import com.zaxxer.hikari.metrics.MetricsTracker;
import com.zaxxer.hikari.metrics.PoolStats;
import com.zaxxer.hikari.metrics.MetricsTracker.MetricsContext;
import com.zaxxer.hikari.proxy.ConnectionProxy;
import com.zaxxer.hikari.proxy.IHikariConnectionProxy;
@ -324,10 +325,10 @@ public class HikariPool implements HikariPoolMBean, IBagStateListener
{
this.isRecordMetrics = metricRegistry != null;
if (isRecordMetrics) {
this.metricsTracker = new CodaHaleMetricsTracker(this, (MetricRegistry) metricRegistry);
this.metricsTracker = new CodaHaleMetricsTracker(configuration.getPoolName(), getPoolStats(), (MetricRegistry) metricRegistry);
}
else {
this.metricsTracker = new MetricsTracker(this);
this.metricsTracker = new MetricsTracker();
}
}
@ -643,6 +644,19 @@ public class HikariPool implements HikariPoolMBean, IBagStateListener
fillPool();
}
private PoolStats getPoolStats()
{
return new PoolStats(TimeUnit.SECONDS.toMillis(1)) {
@Override
protected void update() {
this.pendingThreads = HikariPool.this.getThreadsAwaitingConnection();
this.idleConnections = HikariPool.this.getIdleConnections();
this.totalConnections = HikariPool.this.getTotalConnections();
this.activeConnections = HikariPool.this.getActiveConnections();
}
};
}
// ***********************************************************************
// Non-anonymous Inner-classes
// ***********************************************************************

@ -39,10 +39,9 @@ public final class PoolBagEntry implements IConcurrentBagEntry
public Connection connection;
public long lastAccess;
public long lastOpenTime;
public volatile long lastOpenTime;
public volatile boolean evicted;
public volatile boolean aborted;
private volatile ScheduledFuture<?> endOfLife;

@ -51,6 +51,15 @@ public interface ClockSource
*/
long elapsedTimeMs(long startTime);
/**
* Return the specified opaque time-stamp plus the specified number of milliseconds.
*
* @param time an opaque time-stamp
* @param millis milliseconds to add
* @return a new opaque time-stamp
*/
long plusMillis(long time, long millis);
/**
* Factory class used to create a platform-specific ClockSource.
*/

@ -45,4 +45,11 @@ class MillisecondClockSource implements ClockSource
{
return time;
}
/** {@inheritDoc} */
@Override
public long plusMillis(long time, long millis)
{
return time + millis;
}
}

@ -46,4 +46,11 @@ class NanosecondClockSource implements ClockSource
{
return TimeUnit.NANOSECONDS.toMillis(time);
}
/** {@inheritDoc} */
@Override
public long plusMillis(final long time, final long millis)
{
return time + TimeUnit.MILLISECONDS.toNanos(millis);
}
}

Loading…
Cancel
Save