HHH-13546 Fetch configuration details at initialization of StatisticsImpl

This commit is contained in:
Sanne Grinovero 2019-08-07 11:30:11 +01:00
parent 8460cd8796
commit 87907e1edd
1 changed files with 38 additions and 25 deletions

View File

@ -10,12 +10,15 @@ import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.atomic.LongAdder;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.spi.CacheImplementor;
import org.hibernate.cache.spi.QueryResultsCache; import org.hibernate.cache.spi.QueryResultsCache;
import org.hibernate.cache.spi.QueryResultsRegion; import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.Region; import org.hibernate.cache.spi.Region;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.Service; import org.hibernate.service.Service;
import org.hibernate.service.spi.Manageable; import org.hibernate.service.spi.Manageable;
@ -35,7 +38,11 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class ); private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class );
private final SessionFactoryImplementor sessionFactory; private final MetamodelImplementor metamodel;
private final CacheImplementor cache;
private final String cacheRegionPrefix;
private final boolean secondLevelCacheEnabled;
private final boolean queryCacheEnabled;
private volatile boolean isStatisticsEnabled; private volatile boolean isStatisticsEnabled;
private volatile long startTime; private volatile long startTime;
@ -107,14 +114,20 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
private final StatsNamedContainer<DeprecatedNaturalIdCacheStatisticsImpl> deprecatedNaturalIdStatsMap = new StatsNamedContainer(); private final StatsNamedContainer<DeprecatedNaturalIdCacheStatisticsImpl> deprecatedNaturalIdStatsMap = new StatsNamedContainer();
public StatisticsImpl(SessionFactoryImplementor sessionFactory) { public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = Objects.requireNonNull( sessionFactory ); Objects.requireNonNull( sessionFactory );
SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
this.queryStatsMap = new StatsNamedContainer( this.queryStatsMap = new StatsNamedContainer(
sessionFactory != null ? sessionFactory != null ?
sessionFactory.getSessionFactoryOptions().getQueryStatisticsMaxSize() : sessionFactoryOptions.getQueryStatisticsMaxSize() :
Statistics.DEFAULT_QUERY_STATISTICS_MAX_SIZE, Statistics.DEFAULT_QUERY_STATISTICS_MAX_SIZE,
20 20
); );
clear(); clear();
metamodel = sessionFactory.getMetamodel();
cache = sessionFactory.getCache();
cacheRegionPrefix = sessionFactoryOptions.getCacheRegionPrefix();
secondLevelCacheEnabled = sessionFactoryOptions.isSecondLevelCacheEnabled();
queryCacheEnabled = sessionFactoryOptions.isQueryCacheEnabled();
} }
/** /**
@ -204,14 +217,14 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getEntityNames() { public String[] getEntityNames() {
return sessionFactory.getMetamodel().getAllEntityNames(); return metamodel.getAllEntityNames();
} }
@Override @Override
public EntityStatisticsImpl getEntityStatistics(String entityName) { public EntityStatisticsImpl getEntityStatistics(String entityName) {
return entityStatsMap.getOrCompute( return entityStatsMap.getOrCompute(
entityName, entityName,
s -> new EntityStatisticsImpl( sessionFactory.getMetamodel().entityPersister( s ) ) s -> new EntityStatisticsImpl( metamodel.entityPersister( s ) )
); );
} }
@ -308,14 +321,14 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getCollectionRoleNames() { public String[] getCollectionRoleNames() {
return sessionFactory.getMetamodel().getAllCollectionRoles(); return metamodel.getAllCollectionRoles();
} }
@Override @Override
public CollectionStatisticsImpl getCollectionStatistics(String role) { public CollectionStatisticsImpl getCollectionStatistics(String role) {
return collectionStatsMap.getOrCompute( return collectionStatsMap.getOrCompute(
role, role,
s -> new CollectionStatisticsImpl( sessionFactory.getMetamodel().collectionPersister( s ) ) s -> new CollectionStatisticsImpl( metamodel.collectionPersister( s ) )
); );
} }
@ -404,7 +417,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
return naturalIdQueryStatsMap.getOrCompute( return naturalIdQueryStatsMap.getOrCompute(
rootEntityName, rootEntityName,
s -> { s -> {
final EntityPersister entityDescriptor = sessionFactory.getMetamodel().entityPersister( s ); final EntityPersister entityDescriptor = metamodel.entityPersister( s );
if ( !entityDescriptor.hasNaturalIdentifier() ) { if ( !entityDescriptor.hasNaturalIdentifier() ) {
throw new IllegalArgumentException( "Given entity [" + s + "] does not define natural-id" ); throw new IllegalArgumentException( "Given entity [" + s + "] does not define natural-id" );
} }
@ -415,12 +428,12 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public DeprecatedNaturalIdCacheStatisticsImpl getNaturalIdCacheStatistics(String regionName) { public DeprecatedNaturalIdCacheStatisticsImpl getNaturalIdCacheStatistics(String regionName) {
final String key = sessionFactory.getCache().unqualifyRegionName( regionName ); final String key = cache.unqualifyRegionName( regionName );
return deprecatedNaturalIdStatsMap.getOrCompute( return deprecatedNaturalIdStatsMap.getOrCompute(
key, key,
unqualifiedRegionName -> new DeprecatedNaturalIdCacheStatisticsImpl( unqualifiedRegionName -> new DeprecatedNaturalIdCacheStatisticsImpl(
unqualifiedRegionName, unqualifiedRegionName,
sessionFactory.getCache().getNaturalIdAccessesInRegion( unqualifiedRegionName ) cache.getNaturalIdAccessesInRegion( unqualifiedRegionName )
) )
); );
} }
@ -499,10 +512,10 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
getNaturalIdCacheStatistics( qualify( regionName ) ).incrementMissCount(); getNaturalIdCacheStatistics( qualify( regionName ) ).incrementMissCount();
} }
protected String qualify(String regionName) { private String qualify(final String regionName) {
return sessionFactory.getSessionFactoryOptions().getCacheRegionPrefix() == null return cacheRegionPrefix == null
? regionName ? regionName
: sessionFactory.getSessionFactoryOptions().getCacheRegionPrefix() + '.' + regionName; : cacheRegionPrefix + '.' + regionName;
} }
@Override @Override
@ -521,7 +534,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
naturalIdQueryExecutionMaxTimeEntity = rootEntityName; naturalIdQueryExecutionMaxTimeEntity = rootEntityName;
} }
final EntityPersister rootEntityPersister = sessionFactory.getMetamodel().entityPersister( rootEntityName ); final EntityPersister rootEntityPersister = metamodel.entityPersister( rootEntityName );
getNaturalIdStatistics( rootEntityName ).queryExecuted( time ); getNaturalIdStatistics( rootEntityName ).queryExecuted( time );
@ -543,7 +556,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getSecondLevelCacheRegionNames() { public String[] getSecondLevelCacheRegionNames() {
return sessionFactory.getCache().getSecondLevelCacheRegionNames(); return cache.getSecondLevelCacheRegionNames();
} }
@Override @Override
@ -551,7 +564,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
return l2CacheStatsMap.getOrCompute( return l2CacheStatsMap.getOrCompute(
regionName, regionName,
s -> { s -> {
final Region region = sessionFactory.getCache().getRegion( s ); final Region region = cache.getRegion( s );
if ( region == null ) { if ( region == null ) {
throw new IllegalArgumentException( "Unknown cache region : " + s ); throw new IllegalArgumentException( "Unknown cache region : " + s );
@ -575,7 +588,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
return existing; return existing;
} }
final QueryResultsCache regionAccess = sessionFactory.getCache() final QueryResultsCache regionAccess = cache
.getQueryResultsCacheStrictly( regionName ); .getQueryResultsCacheStrictly( regionName );
if ( regionAccess == null ) { if ( regionAccess == null ) {
return null; return null;
@ -589,24 +602,24 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public CacheRegionStatisticsImpl getCacheRegionStatistics(String regionName) { public CacheRegionStatisticsImpl getCacheRegionStatistics(String regionName) {
if ( ! sessionFactory.getSessionFactoryOptions().isSecondLevelCacheEnabled() ) { if ( ! secondLevelCacheEnabled ) {
return null; return null;
} }
return l2CacheStatsMap.getOrCompute( return l2CacheStatsMap.getOrCompute(
regionName, regionName,
s -> { s -> {
Region region = sessionFactory.getCache().getRegion( s ); Region region = cache.getRegion( s );
if ( region == null ) { if ( region == null ) {
if ( ! sessionFactory.getSessionFactoryOptions().isQueryCacheEnabled() ) { if ( ! queryCacheEnabled ) {
return null; return null;
} }
// this is the pre-5.3 behavior. and since this is a pre-5.3 method it should behave consistently // this is the pre-5.3 behavior. and since this is a pre-5.3 method it should behave consistently
// NOTE that this method is deprecated // NOTE that this method is deprecated
region = sessionFactory.getCache().getQueryResultsCache( s ).getRegion(); region = cache.getQueryResultsCache( s ).getRegion();
} }
return new CacheRegionStatisticsImpl( region ); return new CacheRegionStatisticsImpl( region );
@ -616,7 +629,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) { public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
return getCacheRegionStatistics( sessionFactory.getCache().unqualifyRegionName( regionName ) ); return getCacheRegionStatistics( cache.unqualifyRegionName( regionName ) );
} }
@Override @Override
@ -713,13 +726,13 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public void queryExecuted(String hql, int rows, long time) { public void queryExecuted(String hql, int rows, long time) {
LOG.hql(hql, time, (long) rows ); LOG.hql( hql, time, (long) rows );
queryExecutionCount.increment(); queryExecutionCount.increment();
boolean isLongestQuery; boolean isLongestQuery;
//noinspection StatementWithEmptyBody //noinspection StatementWithEmptyBody
for ( long old = queryExecutionMaxTime.get(); for ( long old = queryExecutionMaxTime.get();
( isLongestQuery = time > old ) && ( !queryExecutionMaxTime.compareAndSet( old, time ) ); ( isLongestQuery = time > old ) && ( ! queryExecutionMaxTime.compareAndSet( old, time ) );
old = queryExecutionMaxTime.get() ) { old = queryExecutionMaxTime.get() ) {
// nothing to do here given the odd loop structure... // nothing to do here given the odd loop structure...
} }
@ -803,7 +816,7 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
private CacheRegionStatisticsImpl getQueryRegionStats(String regionName) { private CacheRegionStatisticsImpl getQueryRegionStats(String regionName) {
return l2CacheStatsMap.getOrCompute( return l2CacheStatsMap.getOrCompute(
regionName, regionName,
s -> new CacheRegionStatisticsImpl( sessionFactory.getCache().getQueryResultsCache( regionName ).getRegion() ) s -> new CacheRegionStatisticsImpl( cache.getQueryResultsCache( regionName ).getRegion() )
); );
} }