From 90b058507e57fbbd39cecc3e60e49e554b2c9301 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Wed, 25 Apr 2018 21:52:14 +0100 Subject: [PATCH] HHH-12521 Take advantage of Java 8 improvements to optimise Statistics --- .../AbstractCacheableDataStatistics.java | 27 +- .../internal/CacheRegionStatisticsImpl.java | 20 +- .../internal/CollectionStatisticsImpl.java | 34 +- ...eprecatedNaturalIdCacheStatisticsImpl.java | 9 +- .../stat/internal/EntityStatisticsImpl.java | 40 +- .../internal/NaturalIdStatisticsImpl.java | 14 +- .../stat/internal/QueryStatisticsImpl.java | 52 ++- .../stat/internal/StatisticsImpl.java | 349 +++++++++--------- .../stat/internal/StatisticsInitiator.java | 20 +- 9 files changed, 278 insertions(+), 287 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/AbstractCacheableDataStatistics.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/AbstractCacheableDataStatistics.java index f6aa03a79f..91956fab64 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/AbstractCacheableDataStatistics.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/AbstractCacheableDataStatistics.java @@ -6,7 +6,7 @@ */ package org.hibernate.stat.internal; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import java.util.function.Supplier; import org.hibernate.cache.spi.Region; @@ -21,10 +21,9 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt private static final Logger log = Logger.getLogger( AbstractCacheableDataStatistics.class ); private final String cacheRegionName; - - private final AtomicLong cacheHitCount; - private final AtomicLong cacheMissCount; - private final AtomicLong cachePutCount; + private final LongAdder cacheHitCount; + private final LongAdder cacheMissCount; + private final LongAdder cachePutCount; public AbstractCacheableDataStatistics(Supplier regionSupplier) { final Region region = regionSupplier.get(); @@ -36,9 +35,9 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt } else { this.cacheRegionName = region.getName(); - this.cacheHitCount = new AtomicLong(); - this.cacheMissCount = new AtomicLong(); - this.cachePutCount = new AtomicLong(); + this.cacheHitCount = new LongAdder(); + this.cacheMissCount = new LongAdder(); + this.cachePutCount = new LongAdder(); } } @@ -52,7 +51,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt return NOT_CACHED_COUNT; } - return cacheHitCount.get(); + return cacheHitCount.sum(); } public long getCachePutCount() { @@ -60,7 +59,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt return NOT_CACHED_COUNT; } - return cachePutCount.get(); + return cachePutCount.sum(); } public long getCacheMissCount() { @@ -68,7 +67,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt return NOT_CACHED_COUNT; } - return cacheMissCount.get(); + return cacheMissCount.sum(); } public void incrementCacheHitCount() { @@ -76,7 +75,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt throw new IllegalStateException( "Illegal attempt to increment cache hit count for non-cached data" ); } - cacheHitCount.getAndIncrement(); + cacheHitCount.increment(); } public void incrementCacheMissCount() { @@ -84,7 +83,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt throw new IllegalStateException( "Illegal attempt to increment cache miss count for non-cached data" ); } - cacheMissCount.getAndIncrement(); + cacheMissCount.increment(); } public void incrementCachePutCount() { @@ -92,7 +91,7 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt throw new IllegalStateException( "Illegal attempt to increment cache put count for non-cached data" ); } - cachePutCount.getAndIncrement(); + cachePutCount.increment(); } protected void appendCacheStats(StringBuilder buf) { diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java index 6fede670f6..d393f2a36e 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java @@ -7,7 +7,7 @@ package org.hibernate.stat.internal; import java.io.Serializable; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import org.hibernate.cache.spi.ExtendedStatisticsSupport; import org.hibernate.cache.spi.Region; @@ -22,9 +22,9 @@ import org.hibernate.stat.SecondLevelCacheStatistics; public class CacheRegionStatisticsImpl implements CacheRegionStatistics, SecondLevelCacheStatistics, Serializable { private final transient Region region; - private AtomicLong hitCount = new AtomicLong(); - private AtomicLong missCount = new AtomicLong(); - private AtomicLong putCount = new AtomicLong(); + private final LongAdder hitCount = new LongAdder(); + private final LongAdder missCount = new LongAdder(); + private final LongAdder putCount = new LongAdder(); CacheRegionStatisticsImpl(Region region) { this.region = region; @@ -37,17 +37,17 @@ public class CacheRegionStatisticsImpl implements CacheRegionStatistics, SecondL @Override public long getHitCount() { - return hitCount.get(); + return hitCount.sum(); } @Override public long getMissCount() { - return missCount.get(); + return missCount.sum(); } @Override public long getPutCount() { - return putCount.get(); + return putCount.sum(); } @Override @@ -75,15 +75,15 @@ public class CacheRegionStatisticsImpl implements CacheRegionStatistics, SecondL } void incrementHitCount() { - hitCount.getAndIncrement(); + hitCount.increment(); } void incrementMissCount() { - missCount.getAndIncrement(); + missCount.increment(); } void incrementPutCount() { - putCount.getAndIncrement(); + putCount.increment(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/CollectionStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/CollectionStatisticsImpl.java index b3b24702e2..49f8e78228 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/CollectionStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/CollectionStatisticsImpl.java @@ -7,7 +7,7 @@ package org.hibernate.stat.internal; import java.io.Serializable; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.stat.CollectionStatistics; @@ -18,13 +18,13 @@ import org.hibernate.stat.CollectionStatistics; * @author Alex Snaps */ public class CollectionStatisticsImpl extends AbstractCacheableDataStatistics implements CollectionStatistics, Serializable { - private final String collectionRole; - private AtomicLong loadCount = new AtomicLong(); - private AtomicLong fetchCount = new AtomicLong(); - private AtomicLong updateCount = new AtomicLong(); - private AtomicLong removeCount = new AtomicLong(); - private AtomicLong recreateCount = new AtomicLong(); + private final String collectionRole; + private final LongAdder loadCount = new LongAdder(); + private final LongAdder fetchCount = new LongAdder(); + private final LongAdder updateCount = new LongAdder(); + private final LongAdder removeCount = new LongAdder(); + private final LongAdder recreateCount = new LongAdder(); CollectionStatisticsImpl(CollectionPersister persister) { super( @@ -37,43 +37,43 @@ public class CollectionStatisticsImpl extends AbstractCacheableDataStatistics im } public long getLoadCount() { - return loadCount.get(); + return loadCount.sum(); } public long getFetchCount() { - return fetchCount.get(); + return fetchCount.sum(); } public long getRecreateCount() { - return recreateCount.get(); + return recreateCount.sum(); } public long getRemoveCount() { - return removeCount.get(); + return removeCount.sum(); } public long getUpdateCount() { - return updateCount.get(); + return updateCount.sum(); } void incrementLoadCount() { - loadCount.getAndIncrement(); + loadCount.increment(); } void incrementFetchCount() { - fetchCount.getAndIncrement(); + fetchCount.increment(); } void incrementUpdateCount() { - updateCount.getAndIncrement(); + updateCount.increment(); } void incrementRecreateCount() { - recreateCount.getAndIncrement(); + recreateCount.increment(); } void incrementRemoveCount() { - removeCount.getAndIncrement(); + removeCount.increment(); } public String toString() { diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/DeprecatedNaturalIdCacheStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/DeprecatedNaturalIdCacheStatisticsImpl.java index b3942445d3..74eb0bb43f 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/DeprecatedNaturalIdCacheStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/DeprecatedNaturalIdCacheStatisticsImpl.java @@ -46,15 +46,12 @@ public class DeprecatedNaturalIdCacheStatisticsImpl implements NaturalIdCacheSta private final Lock readLock; private final Lock writeLock; - { - final ReadWriteLock lock = new ReentrantReadWriteLock(); - this.readLock = lock.readLock(); - this.writeLock = lock.writeLock(); - } - DeprecatedNaturalIdCacheStatisticsImpl(String regionName, Set accessStrategies) { this.regionName = regionName; this.accessStrategies = accessStrategies; + final ReadWriteLock lock = new ReentrantReadWriteLock(); + this.readLock = lock.readLock(); + this.writeLock = lock.writeLock(); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/EntityStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/EntityStatisticsImpl.java index 7904ae04bb..4d8247db08 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/EntityStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/EntityStatisticsImpl.java @@ -7,7 +7,7 @@ package org.hibernate.stat.internal; import java.io.Serializable; -import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.EntityStatistics; @@ -18,14 +18,14 @@ import org.hibernate.stat.EntityStatistics; * @author Alex Snaps */ public class EntityStatisticsImpl extends AbstractCacheableDataStatistics implements EntityStatistics, Serializable { - private final String rootEntityName; - private AtomicLong loadCount = new AtomicLong(); - private AtomicLong updateCount = new AtomicLong(); - private AtomicLong insertCount = new AtomicLong(); - private AtomicLong deleteCount = new AtomicLong(); - private AtomicLong fetchCount = new AtomicLong(); - private AtomicLong optimisticFailureCount = new AtomicLong(); + private final String rootEntityName; + private final LongAdder loadCount = new LongAdder(); + private final LongAdder updateCount = new LongAdder(); + private final LongAdder insertCount = new LongAdder(); + private final LongAdder deleteCount = new LongAdder(); + private final LongAdder fetchCount = new LongAdder(); + private final LongAdder optimisticFailureCount = new LongAdder(); EntityStatisticsImpl(EntityPersister rootEntityDescriptor) { super( @@ -37,51 +37,51 @@ public class EntityStatisticsImpl extends AbstractCacheableDataStatistics implem } public long getDeleteCount() { - return deleteCount.get(); + return deleteCount.sum(); } public long getInsertCount() { - return insertCount.get(); + return insertCount.sum(); } public long getLoadCount() { - return loadCount.get(); + return loadCount.sum(); } public long getUpdateCount() { - return updateCount.get(); + return updateCount.sum(); } public long getFetchCount() { - return fetchCount.get(); + return fetchCount.sum(); } public long getOptimisticFailureCount() { - return optimisticFailureCount.get(); + return optimisticFailureCount.sum(); } void incrementLoadCount() { - loadCount.getAndIncrement(); + loadCount.increment(); } void incrementFetchCount() { - fetchCount.getAndIncrement(); + fetchCount.increment(); } void incrementUpdateCount() { - updateCount.getAndIncrement(); + updateCount.increment(); } void incrementInsertCount() { - insertCount.getAndIncrement(); + insertCount.increment(); } void incrementDeleteCount() { - deleteCount.getAndIncrement(); + deleteCount.increment(); } void incrementOptimisticFailureCount() { - optimisticFailureCount.getAndIncrement(); + optimisticFailureCount.increment(); } public String toString() { diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/NaturalIdStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/NaturalIdStatisticsImpl.java index 2426fa77f2..17428ffc84 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/NaturalIdStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/NaturalIdStatisticsImpl.java @@ -8,6 +8,7 @@ package org.hibernate.stat.internal; import java.io.Serializable; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -21,10 +22,8 @@ import org.hibernate.stat.NaturalIdStatistics; * @author Eric Dalquist */ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics implements NaturalIdStatistics, Serializable { - private static final long serialVersionUID = 1L; private final String rootEntityName; - private final AtomicLong executionCount = new AtomicLong(); private final AtomicLong executionMaxTime = new AtomicLong(); private final AtomicLong executionMinTime = new AtomicLong( Long.MAX_VALUE ); @@ -33,12 +32,6 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp private final Lock readLock; private final Lock writeLock; - { - final ReadWriteLock lock = new ReentrantReadWriteLock(); - this.readLock = lock.readLock(); - this.writeLock = lock.writeLock(); - } - NaturalIdStatisticsImpl(EntityPersister rootEntityDescriptor) { super( () -> rootEntityDescriptor.getNaturalIdCacheAccessStrategy() != null @@ -46,6 +39,9 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp : null ); this.rootEntityName = rootEntityDescriptor.getRootEntityName(); + final ReadWriteLock lock = new ReentrantReadWriteLock(); + this.readLock = lock.readLock(); + this.writeLock = lock.writeLock(); } /** @@ -86,7 +82,7 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp } /** - * min time in ms taken by the excution of this query onto the DB + * min time in ms taken by the execution of this query onto the DB */ @Override public long getExecutionMinTime() { diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/QueryStatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/QueryStatisticsImpl.java index 851394937a..00ff28ba3a 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/QueryStatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/QueryStatisticsImpl.java @@ -7,6 +7,7 @@ package org.hibernate.stat.internal; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -27,11 +28,11 @@ public class QueryStatisticsImpl implements QueryStatistics { private final String query; - private final AtomicLong cacheHitCount = new AtomicLong(); - private final AtomicLong cacheMissCount = new AtomicLong(); - private final AtomicLong cachePutCount = new AtomicLong(); - private final AtomicLong executionCount = new AtomicLong(); - private final AtomicLong executionRowCount = new AtomicLong(); + private final LongAdder cacheHitCount = new LongAdder(); + private final LongAdder cacheMissCount = new LongAdder(); + private final LongAdder cachePutCount = new LongAdder(); + private final LongAdder executionCount = new LongAdder(); + private final LongAdder executionRowCount = new LongAdder(); private final AtomicLong executionMaxTime = new AtomicLong(); private final AtomicLong executionMinTime = new AtomicLong(Long.MAX_VALUE); private final AtomicLong totalExecutionTime = new AtomicLong(); @@ -39,36 +40,33 @@ public class QueryStatisticsImpl implements QueryStatistics { private final Lock readLock; private final Lock writeLock; - { - ReadWriteLock lock = new ReentrantReadWriteLock(); - readLock = lock.readLock(); - writeLock = lock.writeLock(); - } - QueryStatisticsImpl(String query) { this.query = query; + ReadWriteLock lock = new ReentrantReadWriteLock(); + this.readLock = lock.readLock(); + this.writeLock = lock.writeLock(); } /** * queries executed to the DB */ public long getExecutionCount() { - return executionCount.get(); + return executionCount.sum(); } /** * Queries retrieved successfully from the cache */ public long getCacheHitCount() { - return cacheHitCount.get(); + return cacheHitCount.sum(); } public long getCachePutCount() { - return cachePutCount.get(); + return cachePutCount.sum(); } public long getCacheMissCount() { - return cacheMissCount.get(); + return cacheMissCount.sum(); } /** @@ -81,7 +79,7 @@ public class QueryStatisticsImpl implements QueryStatistics { * is not known at execution time. */ public long getExecutionRowCount() { - return executionRowCount.get(); + return executionRowCount.sum(); } /** @@ -101,9 +99,9 @@ public class QueryStatisticsImpl implements QueryStatistics { writeLock.lock(); try { double avgExecutionTime = 0; - if ( executionCount.get() > 0 ) { - avgExecutionTime = totalExecutionTime.get() / (double) executionCount - .get(); + final long ec = executionCount.sum(); + if ( ec > 0 ) { + avgExecutionTime = totalExecutionTime.get() / (double) ec; } return avgExecutionTime; } @@ -147,11 +145,11 @@ public class QueryStatisticsImpl implements QueryStatistics { readLock.lock(); try { // Less chances for a context switch - for (long old = executionMinTime.get(); (time < old) && !executionMinTime.compareAndSet(old, time); old = executionMinTime.get()) {} - for (long old = executionMaxTime.get(); (time > old) && !executionMaxTime.compareAndSet(old, time); old = executionMaxTime.get()) {} - executionCount.getAndIncrement(); - executionRowCount.addAndGet(rows); - totalExecutionTime.addAndGet(time); + for ( long old = executionMinTime.get(); (time < old) && !executionMinTime.compareAndSet(old, time); old = executionMinTime.get() ) {} + for ( long old = executionMaxTime.get(); (time > old) && !executionMaxTime.compareAndSet(old, time); old = executionMaxTime.get() ) {} + executionCount.increment(); + executionRowCount.add( rows ); + totalExecutionTime.addAndGet( time ); } finally { readLock.unlock(); @@ -161,19 +159,19 @@ public class QueryStatisticsImpl implements QueryStatistics { void incrementCacheHitCount() { log.tracef( "QueryStatistics - cache hit : %s", query ); - cacheHitCount.getAndIncrement(); + cacheHitCount.increment(); } void incrementCacheMissCount() { log.tracef( "QueryStatistics - cache miss : %s", query ); - cacheMissCount.getAndIncrement(); + cacheMissCount.increment(); } void incrementCachePutCount() { log.tracef( "QueryStatistics - cache put : %s", query ); - cachePutCount.getAndIncrement(); + cachePutCount.increment(); } public String toString() { diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java index 264a1728f4..fccf57ecb2 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java @@ -9,6 +9,7 @@ package org.hibernate.stat.internal; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.LongAdder; import org.hibernate.cache.spi.QueryResultsCache; import org.hibernate.cache.spi.QueryResultsRegion; @@ -32,57 +33,57 @@ import static org.hibernate.internal.CoreLogging.messageLogger; public class StatisticsImpl implements StatisticsImplementor, Service { private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class ); - private SessionFactoryImplementor sessionFactory; + private final SessionFactoryImplementor sessionFactory; private volatile boolean isStatisticsEnabled; private volatile long startTime; - private AtomicLong sessionOpenCount = new AtomicLong(); - private AtomicLong sessionCloseCount = new AtomicLong(); - private AtomicLong flushCount = new AtomicLong(); - private AtomicLong connectCount = new AtomicLong(); + private final LongAdder sessionOpenCount = new LongAdder(); + private final LongAdder sessionCloseCount = new LongAdder(); + private final LongAdder flushCount = new LongAdder(); + private final LongAdder connectCount = new LongAdder(); - private AtomicLong prepareStatementCount = new AtomicLong(); - private AtomicLong closeStatementCount = new AtomicLong(); + private final LongAdder prepareStatementCount = new LongAdder(); + private final LongAdder closeStatementCount = new LongAdder(); - private AtomicLong entityLoadCount = new AtomicLong(); - private AtomicLong entityUpdateCount = new AtomicLong(); - private AtomicLong entityInsertCount = new AtomicLong(); - private AtomicLong entityDeleteCount = new AtomicLong(); - private AtomicLong entityFetchCount = new AtomicLong(); - private AtomicLong collectionLoadCount = new AtomicLong(); - private AtomicLong collectionUpdateCount = new AtomicLong(); - private AtomicLong collectionRemoveCount = new AtomicLong(); - private AtomicLong collectionRecreateCount = new AtomicLong(); - private AtomicLong collectionFetchCount = new AtomicLong(); + private final LongAdder entityLoadCount = new LongAdder(); + private final LongAdder entityUpdateCount = new LongAdder(); + private final LongAdder entityInsertCount = new LongAdder(); + private final LongAdder entityDeleteCount = new LongAdder(); + private final LongAdder entityFetchCount = new LongAdder(); + private final LongAdder collectionLoadCount = new LongAdder(); + private final LongAdder collectionUpdateCount = new LongAdder(); + private final LongAdder collectionRemoveCount = new LongAdder(); + private final LongAdder collectionRecreateCount = new LongAdder(); + private final LongAdder collectionFetchCount = new LongAdder(); - private AtomicLong secondLevelCacheHitCount = new AtomicLong(); - private AtomicLong secondLevelCacheMissCount = new AtomicLong(); - private AtomicLong secondLevelCachePutCount = new AtomicLong(); + private final LongAdder secondLevelCacheHitCount = new LongAdder(); + private final LongAdder secondLevelCacheMissCount = new LongAdder(); + private final LongAdder secondLevelCachePutCount = new LongAdder(); - private AtomicLong naturalIdCacheHitCount = new AtomicLong(); - private AtomicLong naturalIdCacheMissCount = new AtomicLong(); - private AtomicLong naturalIdCachePutCount = new AtomicLong(); - private AtomicLong naturalIdQueryExecutionCount = new AtomicLong(); - private AtomicLong naturalIdQueryExecutionMaxTime = new AtomicLong(); + private final LongAdder naturalIdCacheHitCount = new LongAdder(); + private final LongAdder naturalIdCacheMissCount = new LongAdder(); + private final LongAdder naturalIdCachePutCount = new LongAdder(); + private final LongAdder naturalIdQueryExecutionCount = new LongAdder(); + private final AtomicLong naturalIdQueryExecutionMaxTime = new AtomicLong(); private volatile String naturalIdQueryExecutionMaxTimeRegion; private volatile String naturalIdQueryExecutionMaxTimeEntity; - private AtomicLong queryExecutionCount = new AtomicLong(); - private AtomicLong queryExecutionMaxTime = new AtomicLong(); + private final LongAdder queryExecutionCount = new LongAdder(); + private final AtomicLong queryExecutionMaxTime = new AtomicLong(); private volatile String queryExecutionMaxTimeQueryString; - private AtomicLong queryCacheHitCount = new AtomicLong(); - private AtomicLong queryCacheMissCount = new AtomicLong(); - private AtomicLong queryCachePutCount = new AtomicLong(); + private final LongAdder queryCacheHitCount = new LongAdder(); + private final LongAdder queryCacheMissCount = new LongAdder(); + private final LongAdder queryCachePutCount = new LongAdder(); - private AtomicLong updateTimestampsCacheHitCount = new AtomicLong(); - private AtomicLong updateTimestampsCacheMissCount = new AtomicLong(); - private AtomicLong updateTimestampsCachePutCount = new AtomicLong(); + private final LongAdder updateTimestampsCacheHitCount = new LongAdder(); + private final LongAdder updateTimestampsCacheMissCount = new LongAdder(); + private final LongAdder updateTimestampsCachePutCount = new LongAdder(); - private AtomicLong committedTransactionCount = new AtomicLong(); - private AtomicLong transactionCount = new AtomicLong(); + private final LongAdder committedTransactionCount = new LongAdder(); + private final LongAdder transactionCount = new LongAdder(); - private AtomicLong optimisticFailureCount = new AtomicLong(); + private final LongAdder optimisticFailureCount = new LongAdder(); private final ConcurrentMap entityStatsMap = new ConcurrentHashMap(); private final ConcurrentMap naturalIdQueryStatsMap = new ConcurrentHashMap(); @@ -103,7 +104,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @SuppressWarnings({ "UnusedDeclaration" }) public StatisticsImpl() { - clear(); + this( null ); } public StatisticsImpl(SessionFactoryImplementor sessionFactory) { @@ -115,53 +116,53 @@ public class StatisticsImpl implements StatisticsImplementor, Service { * reset all statistics */ public void clear() { - secondLevelCacheHitCount.set( 0 ); - secondLevelCacheMissCount.set( 0 ); - secondLevelCachePutCount.set( 0 ); + secondLevelCacheHitCount.reset(); + secondLevelCacheMissCount.reset(); + secondLevelCachePutCount.reset(); - naturalIdCacheHitCount.set( 0 ); - naturalIdCacheMissCount.set( 0 ); - naturalIdCachePutCount.set( 0 ); - naturalIdQueryExecutionCount.set( 0 ); - naturalIdQueryExecutionMaxTime.set( 0 ); + naturalIdCacheHitCount.reset(); + naturalIdCacheMissCount.reset(); + naturalIdCachePutCount.reset(); + naturalIdQueryExecutionCount.reset(); + naturalIdQueryExecutionMaxTime.set( 0L ); naturalIdQueryExecutionMaxTimeRegion = null; naturalIdQueryExecutionMaxTimeEntity = null; - sessionCloseCount.set( 0 ); - sessionOpenCount.set( 0 ); - flushCount.set( 0 ); - connectCount.set( 0 ); + sessionCloseCount.reset(); + sessionOpenCount.reset(); + flushCount.reset(); + connectCount.reset(); - prepareStatementCount.set( 0 ); - closeStatementCount.set( 0 ); + prepareStatementCount.reset(); + closeStatementCount.reset(); - entityDeleteCount.set( 0 ); - entityInsertCount.set( 0 ); - entityUpdateCount.set( 0 ); - entityLoadCount.set( 0 ); - entityFetchCount.set( 0 ); + entityDeleteCount.reset(); + entityInsertCount.reset(); + entityUpdateCount.reset(); + entityLoadCount.reset(); + entityFetchCount.reset(); - collectionRemoveCount.set( 0 ); - collectionUpdateCount.set( 0 ); - collectionRecreateCount.set( 0 ); - collectionLoadCount.set( 0 ); - collectionFetchCount.set( 0 ); + collectionRemoveCount.reset(); + collectionUpdateCount.reset(); + collectionRecreateCount.reset(); + collectionLoadCount.reset(); + collectionFetchCount.reset(); - queryExecutionCount.set( 0 ); - queryCacheHitCount.set( 0 ); - queryExecutionMaxTime.set( 0 ); + queryExecutionCount.reset(); + queryCacheHitCount.reset(); + queryExecutionMaxTime.set( 0L ); queryExecutionMaxTimeQueryString = null; - queryCacheMissCount.set( 0 ); - queryCachePutCount.set( 0 ); + queryCacheMissCount.reset(); + queryCachePutCount.reset(); - updateTimestampsCacheMissCount.set( 0 ); - updateTimestampsCacheHitCount.set( 0 ); - updateTimestampsCachePutCount.set( 0 ); + updateTimestampsCacheMissCount.reset(); + updateTimestampsCacheHitCount.reset(); + updateTimestampsCachePutCount.reset(); - transactionCount.set( 0 ); - committedTransactionCount.set( 0 ); + transactionCount.reset(); + committedTransactionCount.reset(); - optimisticFailureCount.set( 0 ); + optimisticFailureCount.reset(); entityStatsMap.clear(); collectionStatsMap.clear(); @@ -217,87 +218,87 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getEntityLoadCount() { - return entityLoadCount.get(); + return entityLoadCount.sum(); } @Override public long getEntityFetchCount() { - return entityFetchCount.get(); + return entityFetchCount.sum(); } @Override public long getEntityDeleteCount() { - return entityDeleteCount.get(); + return entityDeleteCount.sum(); } @Override public long getEntityInsertCount() { - return entityInsertCount.get(); + return entityInsertCount.sum(); } @Override public long getEntityUpdateCount() { - return entityUpdateCount.get(); + return entityUpdateCount.sum(); } @Override public long getOptimisticFailureCount() { - return optimisticFailureCount.get(); + return optimisticFailureCount.sum(); } @Override public void loadEntity(String entityName) { - entityLoadCount.getAndIncrement(); + entityLoadCount.increment(); getEntityStatistics( entityName ).incrementLoadCount(); } @Override public void fetchEntity(String entityName) { - entityFetchCount.getAndIncrement(); + entityFetchCount.increment(); getEntityStatistics( entityName ).incrementFetchCount(); } @Override public void updateEntity(String entityName) { - entityUpdateCount.getAndIncrement(); + entityUpdateCount.increment(); getEntityStatistics( entityName ).incrementUpdateCount(); } @Override public void insertEntity(String entityName) { - entityInsertCount.getAndIncrement(); + entityInsertCount.increment(); getEntityStatistics( entityName ).incrementInsertCount(); } @Override public void deleteEntity(String entityName) { - entityDeleteCount.getAndIncrement(); + entityDeleteCount.increment(); getEntityStatistics( entityName ).incrementDeleteCount(); } @Override public void optimisticFailure(String entityName) { - optimisticFailureCount.getAndIncrement(); + optimisticFailureCount.increment(); getEntityStatistics( entityName ).incrementOptimisticFailureCount(); } @Override public void entityCachePut(NavigableRole entityName, String regionName) { - secondLevelCachePutCount.getAndIncrement(); + secondLevelCachePutCount.increment(); getDomainDataRegionStatistics( regionName ).incrementPutCount(); getEntityStatistics( entityName.getFullPath() ).incrementCachePutCount(); } @Override public void entityCacheHit(NavigableRole entityName, String regionName) { - secondLevelCacheHitCount.getAndIncrement(); + secondLevelCacheHitCount.increment(); getDomainDataRegionStatistics( regionName ).incrementHitCount(); getEntityStatistics( entityName.getFullPath() ).incrementCacheHitCount(); } @Override public void entityCacheMiss(NavigableRole entityName, String regionName) { - secondLevelCacheMissCount.getAndIncrement(); + secondLevelCacheMissCount.increment(); getDomainDataRegionStatistics( regionName ).incrementMissCount(); getEntityStatistics( entityName.getFullPath() ).incrementCacheMissCount(); } @@ -330,76 +331,76 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getCollectionLoadCount() { - return collectionLoadCount.get(); + return collectionLoadCount.sum(); } @Override public long getCollectionFetchCount() { - return collectionFetchCount.get(); + return collectionFetchCount.sum(); } @Override public long getCollectionUpdateCount() { - return collectionUpdateCount.get(); + return collectionUpdateCount.sum(); } @Override public long getCollectionRemoveCount() { - return collectionRemoveCount.get(); + return collectionRemoveCount.sum(); } @Override public long getCollectionRecreateCount() { - return collectionRecreateCount.get(); + return collectionRecreateCount.sum(); } @Override public void loadCollection(String role) { - collectionLoadCount.getAndIncrement(); + collectionLoadCount.increment(); getCollectionStatistics( role ).incrementLoadCount(); } @Override public void fetchCollection(String role) { - collectionFetchCount.getAndIncrement(); + collectionFetchCount.increment(); getCollectionStatistics( role ).incrementFetchCount(); } @Override public void updateCollection(String role) { - collectionUpdateCount.getAndIncrement(); + collectionUpdateCount.increment(); getCollectionStatistics( role ).incrementUpdateCount(); } @Override public void recreateCollection(String role) { - collectionRecreateCount.getAndIncrement(); + collectionRecreateCount.increment(); getCollectionStatistics( role ).incrementRecreateCount(); } @Override public void removeCollection(String role) { - collectionRemoveCount.getAndIncrement(); + collectionRemoveCount.increment(); getCollectionStatistics( role ).incrementRemoveCount(); } @Override public void collectionCachePut(NavigableRole collectionRole, String regionName) { - secondLevelCachePutCount.getAndIncrement(); + secondLevelCachePutCount.increment(); getDomainDataRegionStatistics( regionName ).incrementPutCount(); getCollectionStatistics( collectionRole.getFullPath() ).incrementCachePutCount(); } @Override public void collectionCacheHit(NavigableRole collectionRole, String regionName) { - secondLevelCacheHitCount.getAndIncrement(); + secondLevelCacheHitCount.increment(); getDomainDataRegionStatistics( regionName ).incrementHitCount(); getCollectionStatistics( collectionRole.getFullPath() ).incrementCacheHitCount(); } @Override public void collectionCacheMiss(NavigableRole collectionRole, String regionName) { - secondLevelCacheMissCount.getAndIncrement(); + secondLevelCacheMissCount.increment(); getDomainDataRegionStatistics( regionName ).incrementMissCount(); getCollectionStatistics( collectionRole.getFullPath() ).incrementCacheMissCount(); } @@ -439,7 +440,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getNaturalIdQueryExecutionCount() { - return naturalIdQueryExecutionCount.get(); + return naturalIdQueryExecutionCount.sum(); } @Override @@ -459,24 +460,24 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getNaturalIdCacheHitCount() { - return naturalIdCacheHitCount.get(); + return naturalIdCacheHitCount.sum(); } @Override public long getNaturalIdCacheMissCount() { - return naturalIdCacheMissCount.get(); + return naturalIdCacheMissCount.sum(); } @Override public long getNaturalIdCachePutCount() { - return naturalIdCachePutCount.get(); + return naturalIdCachePutCount.sum(); } @Override public void naturalIdCachePut( NavigableRole rootEntityName, String regionName) { - naturalIdCachePutCount.getAndIncrement(); + naturalIdCachePutCount.increment(); getDomainDataRegionStatistics( regionName ).incrementPutCount(); @@ -489,7 +490,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void naturalIdCacheHit( NavigableRole rootEntityName, String regionName) { - naturalIdCacheHitCount.getAndIncrement(); + naturalIdCacheHitCount.increment(); getDomainDataRegionStatistics( regionName ).incrementHitCount(); @@ -502,7 +503,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void naturalIdCacheMiss( NavigableRole rootEntityName, String regionName) { - naturalIdCacheMissCount.getAndIncrement(); + naturalIdCacheMissCount.increment(); getDomainDataRegionStatistics( regionName ).incrementMissCount(); @@ -519,7 +520,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public void naturalIdQueryExecuted(String rootEntityName, long time) { - naturalIdQueryExecutionCount.getAndIncrement(); + naturalIdQueryExecutionCount.increment(); boolean isLongestQuery; //noinspection StatementWithEmptyBody @@ -642,47 +643,47 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getSecondLevelCacheHitCount() { - return secondLevelCacheHitCount.get(); + return secondLevelCacheHitCount.sum(); } @Override public long getSecondLevelCacheMissCount() { - return secondLevelCacheMissCount.get(); + return secondLevelCacheMissCount.sum(); } @Override public long getSecondLevelCachePutCount() { - return secondLevelCachePutCount.get(); + return secondLevelCachePutCount.sum(); } @Override public long getUpdateTimestampsCacheHitCount() { - return updateTimestampsCacheHitCount.get(); + return updateTimestampsCacheHitCount.sum(); } @Override public long getUpdateTimestampsCacheMissCount() { - return updateTimestampsCacheMissCount.get(); + return updateTimestampsCacheMissCount.sum(); } @Override public long getUpdateTimestampsCachePutCount() { - return updateTimestampsCachePutCount.get(); + return updateTimestampsCachePutCount.sum(); } @Override public void updateTimestampsCacheHit() { - updateTimestampsCacheHitCount.getAndIncrement(); + updateTimestampsCacheHitCount.increment(); } @Override public void updateTimestampsCacheMiss() { - updateTimestampsCacheMissCount.getAndIncrement(); + updateTimestampsCacheMissCount.increment(); } @Override public void updateTimestampsCachePut() { - updateTimestampsCachePutCount.getAndIncrement(); + updateTimestampsCachePutCount.increment(); } @@ -704,22 +705,22 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getQueryExecutionCount() { - return queryExecutionCount.get(); + return queryExecutionCount.sum(); } @Override public long getQueryCacheHitCount() { - return queryCacheHitCount.get(); + return queryCacheHitCount.sum(); } @Override public long getQueryCacheMissCount() { - return queryCacheMissCount.get(); + return queryCacheMissCount.sum(); } @Override public long getQueryCachePutCount() { - return queryCachePutCount.get(); + return queryCachePutCount.sum(); } @Override @@ -735,7 +736,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public void queryExecuted(String hql, int rows, long time) { LOG.hql(hql, time, (long) rows ); - queryExecutionCount.getAndIncrement(); + queryExecutionCount.increment(); boolean isLongestQuery; //noinspection StatementWithEmptyBody @@ -758,7 +759,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void queryCacheHit(String hql, String regionName) { LOG.tracef( "Statistics#queryCacheHit( `%s`, `%s` )", hql, regionName ); - queryCacheHitCount.getAndIncrement(); + queryCacheHitCount.increment(); getQueryRegionStats( regionName ).incrementHitCount(); @@ -779,7 +780,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void queryCacheMiss(String hql, String regionName) { LOG.tracef( "Statistics#queryCacheMiss( `%s`, `%s` )", hql, regionName ); - queryCacheMissCount.getAndIncrement(); + queryCacheMissCount.increment(); getQueryRegionStats( regionName ).incrementMissCount(); @@ -792,7 +793,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void queryCachePut(String hql, String regionName) { LOG.tracef( "Statistics#queryCachePut( `%s`, `%s` )", hql, regionName ); - queryCachePutCount.getAndIncrement(); + queryCachePutCount.increment(); getQueryRegionStats( regionName ).incrementPutCount(); @@ -808,79 +809,79 @@ public class StatisticsImpl implements StatisticsImplementor, Service { @Override public long getSessionOpenCount() { - return sessionOpenCount.get(); + return sessionOpenCount.sum(); } @Override public long getSessionCloseCount() { - return sessionCloseCount.get(); + return sessionCloseCount.sum(); } @Override public long getFlushCount() { - return flushCount.get(); + return flushCount.sum(); } @Override public long getConnectCount() { - return connectCount.get(); + return connectCount.sum(); } @Override public long getSuccessfulTransactionCount() { - return committedTransactionCount.get(); + return committedTransactionCount.sum(); } @Override public long getTransactionCount() { - return transactionCount.get(); + return transactionCount.sum(); } @Override public long getCloseStatementCount() { - return closeStatementCount.get(); + return closeStatementCount.sum(); } @Override public long getPrepareStatementCount() { - return prepareStatementCount.get(); + return prepareStatementCount.sum(); } @Override public void openSession() { - sessionOpenCount.getAndIncrement(); + sessionOpenCount.increment(); } @Override public void closeSession() { - sessionCloseCount.getAndIncrement(); + sessionCloseCount.increment(); } @Override public void flush() { - flushCount.getAndIncrement(); + flushCount.increment(); } @Override public void connect() { - connectCount.getAndIncrement(); + connectCount.increment(); } @Override public void prepareStatement() { - prepareStatementCount.getAndIncrement(); + prepareStatementCount.increment(); } @Override public void closeStatement() { - closeStatementCount.getAndIncrement(); + closeStatementCount.increment(); } @Override public void endTransaction(boolean success) { - transactionCount.getAndIncrement(); + transactionCount.increment(); if ( success ) { - committedTransactionCount.getAndIncrement(); + committedTransactionCount.increment(); } } @@ -890,40 +891,40 @@ public class StatisticsImpl implements StatisticsImplementor, Service { public void logSummary() { LOG.loggingStatistics(); LOG.startTime( startTime ); - LOG.sessionsOpened( sessionOpenCount.get() ); - LOG.sessionsClosed( sessionCloseCount.get() ); - LOG.transactions( transactionCount.get() ); - LOG.successfulTransactions( committedTransactionCount.get() ); - LOG.optimisticLockFailures( optimisticFailureCount.get() ); - LOG.flushes( flushCount.get() ); - LOG.connectionsObtained( connectCount.get() ); - LOG.statementsPrepared( prepareStatementCount.get() ); - LOG.statementsClosed( closeStatementCount.get() ); - LOG.secondLevelCachePuts( secondLevelCachePutCount.get() ); - LOG.secondLevelCacheHits( secondLevelCacheHitCount.get() ); - LOG.secondLevelCacheMisses( secondLevelCacheMissCount.get() ); - LOG.entitiesLoaded( entityLoadCount.get() ); - LOG.entitiesUpdated( entityUpdateCount.get() ); - LOG.entitiesInserted( entityInsertCount.get() ); - LOG.entitiesDeleted( entityDeleteCount.get() ); - LOG.entitiesFetched( entityFetchCount.get() ); - LOG.collectionsLoaded( collectionLoadCount.get() ); - LOG.collectionsUpdated( collectionUpdateCount.get() ); - LOG.collectionsRemoved( collectionRemoveCount.get() ); - LOG.collectionsRecreated( collectionRecreateCount.get() ); - LOG.collectionsFetched( collectionFetchCount.get() ); - LOG.naturalIdCachePuts( naturalIdCachePutCount.get() ); - LOG.naturalIdCacheHits( naturalIdCacheHitCount.get() ); - LOG.naturalIdCacheMisses( naturalIdCacheMissCount.get() ); + LOG.sessionsOpened( sessionOpenCount.sum() ); + LOG.sessionsClosed( sessionCloseCount.sum() ); + LOG.transactions( transactionCount.sum() ); + LOG.successfulTransactions( committedTransactionCount.sum() ); + LOG.optimisticLockFailures( optimisticFailureCount.sum() ); + LOG.flushes( flushCount.sum() ); + LOG.connectionsObtained( connectCount.sum() ); + LOG.statementsPrepared( prepareStatementCount.sum() ); + LOG.statementsClosed( closeStatementCount.sum() ); + LOG.secondLevelCachePuts( secondLevelCachePutCount.sum() ); + LOG.secondLevelCacheHits( secondLevelCacheHitCount.sum() ); + LOG.secondLevelCacheMisses( secondLevelCacheMissCount.sum() ); + LOG.entitiesLoaded( entityLoadCount.sum() ); + LOG.entitiesUpdated( entityUpdateCount.sum() ); + LOG.entitiesInserted( entityInsertCount.sum() ); + LOG.entitiesDeleted( entityDeleteCount.sum() ); + LOG.entitiesFetched( entityFetchCount.sum() ); + LOG.collectionsLoaded( collectionLoadCount.sum() ); + LOG.collectionsUpdated( collectionUpdateCount.sum() ); + LOG.collectionsRemoved( collectionRemoveCount.sum() ); + LOG.collectionsRecreated( collectionRecreateCount.sum() ); + LOG.collectionsFetched( collectionFetchCount.sum() ); + LOG.naturalIdCachePuts( naturalIdCachePutCount.sum() ); + LOG.naturalIdCacheHits( naturalIdCacheHitCount.sum() ); + LOG.naturalIdCacheMisses( naturalIdCacheMissCount.sum() ); LOG.naturalIdMaxQueryTime( naturalIdQueryExecutionMaxTime.get() ); - LOG.naturalIdQueriesExecuted( naturalIdQueryExecutionCount.get() ); - LOG.queriesExecuted( queryExecutionCount.get() ); - LOG.queryCachePuts( queryCachePutCount.get() ); - LOG.timestampCachePuts( updateTimestampsCachePutCount.get() ); - LOG.timestampCacheHits( updateTimestampsCacheHitCount.get() ); - LOG.timestampCacheMisses( updateTimestampsCacheMissCount.get() ); - LOG.queryCacheHits( queryCacheHitCount.get() ); - LOG.queryCacheMisses( queryCacheMissCount.get() ); + LOG.naturalIdQueriesExecuted( naturalIdQueryExecutionCount.sum() ); + LOG.queriesExecuted( queryExecutionCount.sum() ); + LOG.queryCachePuts( queryCachePutCount.sum() ); + LOG.timestampCachePuts( updateTimestampsCachePutCount.sum() ); + LOG.timestampCacheHits( updateTimestampsCacheHitCount.sum() ); + LOG.timestampCacheMisses( updateTimestampsCacheMissCount.sum() ); + LOG.queryCacheHits( queryCacheHitCount.sum() ); + LOG.queryCacheMisses( queryCacheMissCount.sum() ); LOG.maxQueryTime( queryExecutionMaxTime.get() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java index eb5291c554..856eb2b9d0 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java @@ -65,9 +65,9 @@ public class StatisticsInitiator implements SessionFactoryServiceInitiator