-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
StatisticsImpl.java
1024 lines (833 loc) · 29.9 KB
/
StatisticsImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.stat.internal;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import org.hibernate.cache.spi.QueryResultsCache;
import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.Region;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metamodel.model.domain.NavigableRole;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.Service;
import org.hibernate.service.spi.Manageable;
import org.hibernate.stat.Statistics;
import org.hibernate.stat.spi.StatisticsImplementor;
import static org.hibernate.internal.CoreLogging.messageLogger;
/**
* Implementation of {@link org.hibernate.stat.Statistics} based on the {@link java.util.concurrent} package.
*
* @author Alex Snaps
* @author Sanne Grinovero
*/
@SuppressWarnings({ "unchecked" })
public class StatisticsImpl implements StatisticsImplementor, Service, Manageable {
private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class );
private final SessionFactoryImplementor sessionFactory;
private volatile boolean isStatisticsEnabled;
private volatile long startTime;
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 final LongAdder prepareStatementCount = new LongAdder();
private final LongAdder closeStatementCount = new LongAdder();
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 final LongAdder secondLevelCacheHitCount = new LongAdder();
private final LongAdder secondLevelCacheMissCount = new LongAdder();
private final LongAdder secondLevelCachePutCount = new LongAdder();
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 final LongAdder queryExecutionCount = new LongAdder();
private final AtomicLong queryExecutionMaxTime = new AtomicLong();
private volatile String queryExecutionMaxTimeQueryString;
private final LongAdder queryCacheHitCount = new LongAdder();
private final LongAdder queryCacheMissCount = new LongAdder();
private final LongAdder queryCachePutCount = new LongAdder();
private final LongAdder queryPlanCacheHitCount = new LongAdder();
private final LongAdder queryPlanCacheMissCount = new LongAdder();
private final LongAdder updateTimestampsCacheHitCount = new LongAdder();
private final LongAdder updateTimestampsCacheMissCount = new LongAdder();
private final LongAdder updateTimestampsCachePutCount = new LongAdder();
private final LongAdder committedTransactionCount = new LongAdder();
private final LongAdder transactionCount = new LongAdder();
private final LongAdder optimisticFailureCount = new LongAdder();
private final StatsConcurrentHashMap<EntityStatisticsImpl> entityStatsMap = new StatsConcurrentHashMap();
private final StatsConcurrentHashMap<NaturalIdStatisticsImpl> naturalIdQueryStatsMap = new StatsConcurrentHashMap();
private final StatsConcurrentHashMap<CollectionStatisticsImpl> collectionStatsMap = new StatsConcurrentHashMap();
/**
* Keyed by query string
*/
private final StatsConcurrentHashMap<QueryStatisticsImpl> queryStatsMap;
/**
* Keyed by region name
*/
private final StatsConcurrentHashMap<CacheRegionStatisticsImpl> l2CacheStatsMap = new StatsConcurrentHashMap<>();
private final StatsConcurrentHashMap<DeprecatedNaturalIdCacheStatisticsImpl> deprecatedNaturalIdStatsMap = new StatsConcurrentHashMap();
@SuppressWarnings({ "UnusedDeclaration" })
public StatisticsImpl() {
this( null );
}
public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
this.queryStatsMap = new StatsConcurrentHashMap(
sessionFactory != null ?
sessionFactory.getSessionFactoryOptions().getQueryStatisticsMaxSize() :
Statistics.DEFAULT_QUERY_STATISTICS_MAX_SIZE,
20
);
clear();
}
/**
* reset all statistics
*/
public void clear() {
secondLevelCacheHitCount.reset();
secondLevelCacheMissCount.reset();
secondLevelCachePutCount.reset();
naturalIdCacheHitCount.reset();
naturalIdCacheMissCount.reset();
naturalIdCachePutCount.reset();
naturalIdQueryExecutionCount.reset();
naturalIdQueryExecutionMaxTime.set( 0L );
naturalIdQueryExecutionMaxTimeRegion = null;
naturalIdQueryExecutionMaxTimeEntity = null;
sessionCloseCount.reset();
sessionOpenCount.reset();
flushCount.reset();
connectCount.reset();
prepareStatementCount.reset();
closeStatementCount.reset();
entityDeleteCount.reset();
entityInsertCount.reset();
entityUpdateCount.reset();
entityLoadCount.reset();
entityFetchCount.reset();
collectionRemoveCount.reset();
collectionUpdateCount.reset();
collectionRecreateCount.reset();
collectionLoadCount.reset();
collectionFetchCount.reset();
queryExecutionCount.reset();
queryCacheHitCount.reset();
queryExecutionMaxTime.set( 0L );
queryExecutionMaxTimeQueryString = null;
queryCacheMissCount.reset();
queryCachePutCount.reset();
updateTimestampsCacheMissCount.reset();
updateTimestampsCacheHitCount.reset();
updateTimestampsCachePutCount.reset();
transactionCount.reset();
committedTransactionCount.reset();
optimisticFailureCount.reset();
entityStatsMap.clear();
collectionStatsMap.clear();
naturalIdQueryStatsMap.clear();
l2CacheStatsMap.clear();
queryStatsMap.clear();
deprecatedNaturalIdStatsMap.clear();
queryPlanCacheHitCount.reset();
queryPlanCacheMissCount.reset();
startTime = System.currentTimeMillis();
}
@Override
public long getStartTime() {
return startTime;
}
@Override
public boolean isStatisticsEnabled() {
return isStatisticsEnabled;
}
@Override
public void setStatisticsEnabled(boolean b) {
isStatisticsEnabled = b;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Entity stats
@Override
public String[] getEntityNames() {
if ( sessionFactory == null ) {
return entityStatsMap.keysAsArray();
}
else {
return sessionFactory.getMetamodel().getAllEntityNames();
}
}
@Override
public EntityStatisticsImpl getEntityStatistics(String entityName) {
if ( sessionFactory == null ) {
return null;
}
return entityStatsMap.getOrCompute(
entityName,
s -> new EntityStatisticsImpl( sessionFactory.getMetamodel().entityPersister( s ) )
);
}
@Override
public long getEntityLoadCount() {
return entityLoadCount.sum();
}
@Override
public long getEntityFetchCount() {
return entityFetchCount.sum();
}
@Override
public long getEntityDeleteCount() {
return entityDeleteCount.sum();
}
@Override
public long getEntityInsertCount() {
return entityInsertCount.sum();
}
@Override
public long getEntityUpdateCount() {
return entityUpdateCount.sum();
}
@Override
public long getOptimisticFailureCount() {
return optimisticFailureCount.sum();
}
@Override
public void loadEntity(String entityName) {
entityLoadCount.increment();
getEntityStatistics( entityName ).incrementLoadCount();
}
@Override
public void fetchEntity(String entityName) {
entityFetchCount.increment();
getEntityStatistics( entityName ).incrementFetchCount();
}
@Override
public void updateEntity(String entityName) {
entityUpdateCount.increment();
getEntityStatistics( entityName ).incrementUpdateCount();
}
@Override
public void insertEntity(String entityName) {
entityInsertCount.increment();
getEntityStatistics( entityName ).incrementInsertCount();
}
@Override
public void deleteEntity(String entityName) {
entityDeleteCount.increment();
getEntityStatistics( entityName ).incrementDeleteCount();
}
@Override
public void optimisticFailure(String entityName) {
optimisticFailureCount.increment();
getEntityStatistics( entityName ).incrementOptimisticFailureCount();
}
@Override
public void entityCachePut(NavigableRole entityName, String regionName) {
secondLevelCachePutCount.increment();
getDomainDataRegionStatistics( regionName ).incrementPutCount();
getEntityStatistics( entityName.getFullPath() ).incrementCachePutCount();
}
@Override
public void entityCacheHit(NavigableRole entityName, String regionName) {
secondLevelCacheHitCount.increment();
getDomainDataRegionStatistics( regionName ).incrementHitCount();
getEntityStatistics( entityName.getFullPath() ).incrementCacheHitCount();
}
@Override
public void entityCacheMiss(NavigableRole entityName, String regionName) {
secondLevelCacheMissCount.increment();
getDomainDataRegionStatistics( regionName ).incrementMissCount();
getEntityStatistics( entityName.getFullPath() ).incrementCacheMissCount();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Collection stats
@Override
public String[] getCollectionRoleNames() {
if ( sessionFactory == null ) {
return collectionStatsMap.keysAsArray();
}
else {
return sessionFactory.getMetamodel().getAllCollectionRoles();
}
}
@Override
public CollectionStatisticsImpl getCollectionStatistics(String role) {
if ( sessionFactory == null ) {
return null;
}
return collectionStatsMap.getOrCompute(
role,
s -> new CollectionStatisticsImpl( sessionFactory.getMetamodel().collectionPersister( s ) )
);
}
@Override
public long getCollectionLoadCount() {
return collectionLoadCount.sum();
}
@Override
public long getCollectionFetchCount() {
return collectionFetchCount.sum();
}
@Override
public long getCollectionUpdateCount() {
return collectionUpdateCount.sum();
}
@Override
public long getCollectionRemoveCount() {
return collectionRemoveCount.sum();
}
@Override
public long getCollectionRecreateCount() {
return collectionRecreateCount.sum();
}
@Override
public void loadCollection(String role) {
collectionLoadCount.increment();
getCollectionStatistics( role ).incrementLoadCount();
}
@Override
public void fetchCollection(String role) {
collectionFetchCount.increment();
getCollectionStatistics( role ).incrementFetchCount();
}
@Override
public void updateCollection(String role) {
collectionUpdateCount.increment();
getCollectionStatistics( role ).incrementUpdateCount();
}
@Override
public void recreateCollection(String role) {
collectionRecreateCount.increment();
getCollectionStatistics( role ).incrementRecreateCount();
}
@Override
public void removeCollection(String role) {
collectionRemoveCount.increment();
getCollectionStatistics( role ).incrementRemoveCount();
}
@Override
public void collectionCachePut(NavigableRole collectionRole, String regionName) {
secondLevelCachePutCount.increment();
getDomainDataRegionStatistics( regionName ).incrementPutCount();
getCollectionStatistics( collectionRole.getFullPath() ).incrementCachePutCount();
}
@Override
public void collectionCacheHit(NavigableRole collectionRole, String regionName) {
secondLevelCacheHitCount.increment();
getDomainDataRegionStatistics( regionName ).incrementHitCount();
getCollectionStatistics( collectionRole.getFullPath() ).incrementCacheHitCount();
}
@Override
public void collectionCacheMiss(NavigableRole collectionRole, String regionName) {
secondLevelCacheMissCount.increment();
getDomainDataRegionStatistics( regionName ).incrementMissCount();
getCollectionStatistics( collectionRole.getFullPath() ).incrementCacheMissCount();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Natural-id stats
@Override
public NaturalIdStatisticsImpl getNaturalIdStatistics(String rootEntityName) {
if ( sessionFactory == null ) {
return null;
}
return naturalIdQueryStatsMap.getOrCompute(
rootEntityName,
s -> {
final EntityPersister entityDescriptor = sessionFactory.getMetamodel().entityPersister( s );
if ( !entityDescriptor.hasNaturalIdentifier() ) {
throw new IllegalArgumentException( "Given entity [" + s + "] does not define natural-id" );
}
return new NaturalIdStatisticsImpl( entityDescriptor );
}
);
}
@Override
public DeprecatedNaturalIdCacheStatisticsImpl getNaturalIdCacheStatistics(String regionName) {
final String key = sessionFactory.getCache().unqualifyRegionName( regionName );
return deprecatedNaturalIdStatsMap.getOrCompute(
key,
unqualifiedRegionName -> new DeprecatedNaturalIdCacheStatisticsImpl(
unqualifiedRegionName,
sessionFactory.getCache().getNaturalIdAccessesInRegion( unqualifiedRegionName )
)
);
}
@Override
public long getNaturalIdQueryExecutionCount() {
return naturalIdQueryExecutionCount.sum();
}
@Override
public long getNaturalIdQueryExecutionMaxTime() {
return naturalIdQueryExecutionMaxTime.get();
}
@Override
public String getNaturalIdQueryExecutionMaxTimeRegion() {
return naturalIdQueryExecutionMaxTimeRegion;
}
@Override
public String getNaturalIdQueryExecutionMaxTimeEntity() {
return naturalIdQueryExecutionMaxTimeEntity;
}
@Override
public long getNaturalIdCacheHitCount() {
return naturalIdCacheHitCount.sum();
}
@Override
public long getNaturalIdCacheMissCount() {
return naturalIdCacheMissCount.sum();
}
@Override
public long getNaturalIdCachePutCount() {
return naturalIdCachePutCount.sum();
}
@Override
public void naturalIdCachePut(
NavigableRole rootEntityName,
String regionName) {
naturalIdCachePutCount.increment();
getDomainDataRegionStatistics( regionName ).incrementPutCount();
getNaturalIdStatistics( rootEntityName.getFullPath() ).incrementCachePutCount();
getNaturalIdCacheStatistics( qualify( regionName ) ).incrementPutCount();
}
@Override
public void naturalIdCacheHit(
NavigableRole rootEntityName,
String regionName) {
naturalIdCacheHitCount.increment();
getDomainDataRegionStatistics( regionName ).incrementHitCount();
getNaturalIdStatistics( rootEntityName.getFullPath() ).incrementCacheHitCount();
getNaturalIdCacheStatistics( qualify( regionName ) ).incrementHitCount();
}
@Override
public void naturalIdCacheMiss(
NavigableRole rootEntityName,
String regionName) {
naturalIdCacheMissCount.increment();
getDomainDataRegionStatistics( regionName ).incrementMissCount();
getNaturalIdStatistics( rootEntityName.getFullPath() ).incrementCacheMissCount();
getNaturalIdCacheStatistics( qualify( regionName ) ).incrementMissCount();
}
protected String qualify(String regionName) {
return sessionFactory.getSessionFactoryOptions().getCacheRegionPrefix() == null
? regionName
: sessionFactory.getSessionFactoryOptions().getCacheRegionPrefix() + '.' + regionName;
}
@Override
public void naturalIdQueryExecuted(String rootEntityName, long time) {
naturalIdQueryExecutionCount.increment();
boolean isLongestQuery;
//noinspection StatementWithEmptyBody
for ( long old = naturalIdQueryExecutionMaxTime.get();
( isLongestQuery = time > old ) && ( !naturalIdQueryExecutionMaxTime.compareAndSet( old, time ) );
old = naturalIdQueryExecutionMaxTime.get() ) {
// nothing to do here given the odd loop structure...
}
if ( isLongestQuery ) {
naturalIdQueryExecutionMaxTimeEntity = rootEntityName;
}
final EntityPersister rootEntityPersister = sessionFactory.getMetamodel().entityPersister( rootEntityName );
getNaturalIdStatistics( rootEntityName ).queryExecuted( time );
if ( rootEntityPersister.hasNaturalIdCache() ) {
final String naturalIdRegionName = rootEntityPersister.getNaturalIdCacheAccessStrategy()
.getRegion()
.getName();
getNaturalIdCacheStatistics( qualify( naturalIdRegionName ) ).queryExecuted( time );
if ( isLongestQuery ) {
naturalIdQueryExecutionMaxTimeRegion = naturalIdRegionName;
}
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Second-level cache region stats
@Override
public String[] getSecondLevelCacheRegionNames() {
if ( sessionFactory == null ) {
throw new IllegalStateException( "Statistics no longer associated with SessionFactory - cannot get (legacy) region names" );
}
return sessionFactory.getCache().getSecondLevelCacheRegionNames();
}
@Override
public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
return l2CacheStatsMap.getOrCompute(
regionName,
s -> {
final Region region = sessionFactory.getCache().getRegion( s );
if ( region == null ) {
throw new IllegalArgumentException( "Unknown cache region : " + s );
}
if ( region instanceof QueryResultsRegion ) {
throw new IllegalArgumentException(
"Region name [" + s + "] referred to a query result region, not a domain data region"
);
}
return new CacheRegionStatisticsImpl( region );
}
);
}
@Override
public CacheRegionStatisticsImpl getQueryRegionStatistics(String regionName) {
final CacheRegionStatisticsImpl existing = l2CacheStatsMap.get( regionName );
if ( existing != null ) {
return existing;
}
if ( sessionFactory == null ) {
return null;
}
final QueryResultsCache regionAccess = sessionFactory.getCache()
.getQueryResultsCacheStrictly( regionName );
if ( regionAccess == null ) {
return null;
}
return l2CacheStatsMap.getOrCompute(
regionName,
s -> new CacheRegionStatisticsImpl( regionAccess.getRegion() )
);
}
@Override
public CacheRegionStatisticsImpl getCacheRegionStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
if ( ! sessionFactory.getSessionFactoryOptions().isSecondLevelCacheEnabled() ) {
return null;
}
return l2CacheStatsMap.getOrCompute(
regionName,
s -> {
Region region = sessionFactory.getCache().getRegion( s );
if ( region == null ) {
if ( ! sessionFactory.getSessionFactoryOptions().isQueryCacheEnabled() ) {
return null;
}
// 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
region = sessionFactory.getCache().getQueryResultsCache( s ).getRegion();
}
return new CacheRegionStatisticsImpl( region );
}
);
}
@Override
public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
return getCacheRegionStatistics( sessionFactory.getCache().unqualifyRegionName( regionName ) );
}
@Override
public long getSecondLevelCacheHitCount() {
return secondLevelCacheHitCount.sum();
}
@Override
public long getSecondLevelCacheMissCount() {
return secondLevelCacheMissCount.sum();
}
@Override
public long getSecondLevelCachePutCount() {
return secondLevelCachePutCount.sum();
}
@Override
public long getUpdateTimestampsCacheHitCount() {
return updateTimestampsCacheHitCount.sum();
}
@Override
public long getUpdateTimestampsCacheMissCount() {
return updateTimestampsCacheMissCount.sum();
}
@Override
public long getUpdateTimestampsCachePutCount() {
return updateTimestampsCachePutCount.sum();
}
@Override
public void updateTimestampsCacheHit() {
updateTimestampsCacheHitCount.increment();
}
@Override
public void updateTimestampsCacheMiss() {
updateTimestampsCacheMissCount.increment();
}
@Override
public void updateTimestampsCachePut() {
updateTimestampsCachePutCount.increment();
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Query statistics
@Override
public String[] getQueries() {
return queryStatsMap.keysAsArray();
}
@Override
public QueryStatisticsImpl getQueryStatistics(String queryString) {
return queryStatsMap.getOrCompute(
queryString,
s -> new QueryStatisticsImpl( s )
);
}
@Override
public long getQueryExecutionCount() {
return queryExecutionCount.sum();
}
@Override
public long getQueryCacheHitCount() {
return queryCacheHitCount.sum();
}
@Override
public long getQueryCacheMissCount() {
return queryCacheMissCount.sum();
}
@Override
public long getQueryCachePutCount() {
return queryCachePutCount.sum();
}
@Override
public String getQueryExecutionMaxTimeQueryString() {
return queryExecutionMaxTimeQueryString;
}
@Override
public long getQueryExecutionMaxTime() {
return queryExecutionMaxTime.get();
}
@Override
public void queryExecuted(String hql, int rows, long time) {
LOG.hql(hql, time, (long) rows );
queryExecutionCount.increment();
boolean isLongestQuery;
//noinspection StatementWithEmptyBody
for ( long old = queryExecutionMaxTime.get();
( isLongestQuery = time > old ) && ( !queryExecutionMaxTime.compareAndSet( old, time ) );
old = queryExecutionMaxTime.get() ) {
// nothing to do here given the odd loop structure...
}
if ( isLongestQuery ) {
queryExecutionMaxTimeQueryString = hql;
}
if ( hql != null ) {
getQueryStatistics( hql ).executed( rows, time );
}
}
@Override
public void queryCacheHit(String hql, String regionName) {
LOG.tracef( "Statistics#queryCacheHit( `%s`, `%s` )", hql, regionName );
queryCacheHitCount.increment();
getQueryRegionStats( regionName ).incrementHitCount();
if ( hql != null ) {
getQueryStatistics( hql ).incrementCacheHitCount();
}
}
@Override
public void queryCacheMiss(String hql, String regionName) {
LOG.tracef( "Statistics#queryCacheMiss( `%s`, `%s` )", hql, regionName );
queryCacheMissCount.increment();
getQueryRegionStats( regionName ).incrementMissCount();
if ( hql != null ) {
getQueryStatistics( hql ).incrementCacheMissCount();
}
}
@Override
public void queryCachePut(String hql, String regionName) {
LOG.tracef( "Statistics#queryCachePut( `%s`, `%s` )", hql, regionName );
queryCachePutCount.increment();
getQueryRegionStats( regionName ).incrementPutCount();
if ( hql != null ) {
getQueryStatistics( hql ).incrementCachePutCount();
}
}
@Override
public long getQueryPlanCacheHitCount() {
return queryPlanCacheHitCount.sum();
}
@Override
public long getQueryPlanCacheMissCount() {
return queryPlanCacheMissCount.sum();
}
@Override
public void queryCompiled(String hql, long microseconds) {
queryPlanCacheMissCount.increment();
if ( hql != null ) {
getQueryStatistics( hql ).compiled( microseconds );
}
}
@Override
public void queryPlanCacheHit(String hql) {
queryPlanCacheHitCount.increment();
if ( hql != null ) {
getQueryStatistics( hql ).incrementPlanCacheHitCount();
}
}
private CacheRegionStatisticsImpl getQueryRegionStats(String regionName) {
return l2CacheStatsMap.getOrCompute(
regionName,
s -> new CacheRegionStatisticsImpl( sessionFactory.getCache().getQueryResultsCache( regionName ).getRegion() )
);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Session/misc stats
@Override
public long getSessionOpenCount() {
return sessionOpenCount.sum();
}
@Override
public long getSessionCloseCount() {
return sessionCloseCount.sum();
}
@Override
public long getFlushCount() {
return flushCount.sum();
}
@Override
public long getConnectCount() {
return connectCount.sum();
}
@Override
public long getSuccessfulTransactionCount() {
return committedTransactionCount.sum();
}
@Override
public long getTransactionCount() {
return transactionCount.sum();
}
@Override
public long getCloseStatementCount() {
return closeStatementCount.sum();
}
@Override
public long getPrepareStatementCount() {
return prepareStatementCount.sum();
}
@Override
public void openSession() {
sessionOpenCount.increment();
}
@Override
public void closeSession() {
sessionCloseCount.increment();
}
@Override
public void flush() {
flushCount.increment();
}
@Override
public void connect() {
connectCount.increment();
}
@Override
public void prepareStatement() {
prepareStatementCount.increment();
}
@Override
public void closeStatement() {
closeStatementCount.increment();
}
@Override
public void endTransaction(boolean success) {
transactionCount.increment();
if ( success ) {
committedTransactionCount.increment();
}
}
@Override
public void logSummary() {
LOG.loggingStatistics();
LOG.startTime( startTime );
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.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() );
LOG.queryPlanCacheHits( queryPlanCacheHitCount.sum() );
LOG.queryPlanCacheMisses( queryPlanCacheMissCount.sum() );
}
@Override
public String toString() {
return new StringBuilder()
.append( "Statistics[" )
.append( "start time=" ).append( startTime )
.append( ",sessions opened=" ).append( sessionOpenCount )
.append( ",sessions closed=" ).append( sessionCloseCount )
.append( ",transactions=" ).append( transactionCount )
.append( ",successful transactions=" ).append( committedTransactionCount )
.append( ",optimistic lock failures=" ).append( optimisticFailureCount )
.append( ",flushes=" ).append( flushCount )
.append( ",connections obtained=" ).append( connectCount )
.append( ",statements prepared=" ).append( prepareStatementCount )
.append( ",statements closed=" ).append( closeStatementCount )
.append( ",second level cache puts=" ).append( secondLevelCachePutCount )
.append( ",second level cache hits=" ).append( secondLevelCacheHitCount )
.append( ",second level cache misses=" ).append( secondLevelCacheMissCount )
.append( ",entities loaded=" ).append( entityLoadCount )
.append( ",entities updated=" ).append( entityUpdateCount )
.append( ",entities inserted=" ).append( entityInsertCount )
.append( ",entities deleted=" ).append( entityDeleteCount )
.append( ",entities fetched=" ).append( entityFetchCount )