23
27
import javax.management.NotCompliantMBeanException;
24
28
import javax.management.Notification;
26
import net.sf.ehcache.Cache;
27
import net.sf.ehcache.CacheException;
28
import net.sf.ehcache.CacheManager;
29
import net.sf.ehcache.Ehcache;
30
import net.sf.ehcache.Status;
31
import net.sf.ehcache.config.CacheWriterConfiguration;
32
import net.sf.ehcache.event.CacheManagerEventListener;
33
import net.sf.ehcache.hibernate.management.impl.BaseEmitterBean;
34
import net.sf.ehcache.statistics.LiveCacheStatistics;
35
import net.sf.ehcache.statistics.sampled.SampledCacheStatistics;
36
import net.sf.ehcache.writer.writebehind.WriteBehindManager;
39
31
* An implementation of {@link SampledCacheManagerMBean}
43
33
* @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
34
* @author <a href="mailto:byoukste@terracottatech.com">byoukste</a>
46
36
public class SampledCacheManager extends BaseEmitterBean implements SampledCacheManagerMBean {
47
37
private static final MBeanNotificationInfo[] NOTIFICATION_INFO;
49
private final CacheManager cacheManager;
39
private final CacheManagerSampler sampledCacheManagerDelegate;
50
40
private String mbeanRegisteredName;
51
41
private volatile boolean mbeanRegisteredNameSet;
52
private final EventListener cacheManagerEventListener;
55
final String[] notifTypes = new String[] {CACHES_ENABLED, CACHES_CLEARED, STATISTICS_ENABLED, STATISTICS_RESET, };
44
final String[] notifTypes = new String[] {CACHES_ENABLED, CACHES_CLEARED, STATISTICS_ENABLED, STATISTICS_RESET};
56
45
final String name = Notification.class.getName();
57
46
final String description = "Ehcache SampledCacheManager Event";
58
NOTIFICATION_INFO = new MBeanNotificationInfo[] {new MBeanNotificationInfo(notifTypes, name, description), };
47
NOTIFICATION_INFO = new MBeanNotificationInfo[] {new MBeanNotificationInfo(notifTypes, name, description)};
62
51
* Constructor taking the backing {@link CacheManager}
53
* @param cacheManager the cacheManager to wrap
54
* @throws javax.management.NotCompliantMBeanException
55
* if invalid object is registered
66
57
public SampledCacheManager(CacheManager cacheManager) throws NotCompliantMBeanException {
67
58
super(SampledCacheManagerMBean.class);
68
this.cacheManager = cacheManager;
69
cacheManagerEventListener = new EventListener();
70
cacheManager.setCacheManagerEventListener(cacheManagerEventListener);
59
sampledCacheManagerDelegate = new CacheManagerSamplerImpl(cacheManager);
150
114
* @return map of cache metrics (hits, misses)
152
116
public Map<String, long[]> getCacheMetrics() {
153
Map<String, long[]> result = new HashMap<String, long[]>();
154
for (String cacheName : getCacheNames()) {
155
Ehcache cache = cacheManager.getEhcache(cacheName);
157
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
158
result.put(cacheName, new long[] {stats.getCacheHitMostRecentSample(),
159
stats.getCacheMissNotFoundMostRecentSample()
160
+ stats.getCacheMissExpiredMostRecentSample(),
161
stats.getCacheElementPutMostRecentSample(), });
117
return sampledCacheManagerDelegate.getCacheMetrics();
168
121
* @return aggregate hit rate
170
123
public long getCacheHitRate() {
172
for (String cacheName : getCacheNames()) {
173
Ehcache cache = cacheManager.getEhcache(cacheName);
175
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
176
result += stats.getCacheHitMostRecentSample();
124
return sampledCacheManagerDelegate.getCacheHitRate();
183
128
* @return aggregate in-memory hit rate
185
130
public long getCacheInMemoryHitRate() {
187
for (String cacheName : getCacheNames()) {
188
Ehcache cache = cacheManager.getEhcache(cacheName);
190
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
191
result += stats.getCacheHitInMemoryMostRecentSample();
131
return sampledCacheManagerDelegate.getCacheInMemoryHitRate();
198
135
* @return aggregate off-heap hit rate
200
137
public long getCacheOffHeapHitRate() {
202
for (String cacheName : getCacheNames()) {
203
Ehcache cache = cacheManager.getEhcache(cacheName);
205
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
206
result += stats.getCacheHitOffHeapMostRecentSample();
138
return sampledCacheManagerDelegate.getCacheOffHeapHitRate();
213
142
* @return aggregate on-disk hit rate
215
144
public long getCacheOnDiskHitRate() {
217
for (String cacheName : getCacheNames()) {
218
Ehcache cache = cacheManager.getEhcache(cacheName);
220
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
221
result += stats.getCacheHitOnDiskMostRecentSample();
145
return sampledCacheManagerDelegate.getCacheOnDiskHitRate();
228
149
* @return aggregate miss rate
230
151
public long getCacheMissRate() {
232
for (String cacheName : getCacheNames()) {
233
Ehcache cache = cacheManager.getEhcache(cacheName);
235
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
236
result += (stats.getCacheMissNotFoundMostRecentSample()
237
+ stats.getCacheMissExpiredMostRecentSample());
152
return sampledCacheManagerDelegate.getCacheMissRate();
244
156
* @return aggregate in-memory miss rate
246
158
public long getCacheInMemoryMissRate() {
248
for (String cacheName : getCacheNames()) {
249
Ehcache cache = cacheManager.getEhcache(cacheName);
251
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
252
result += stats.getCacheMissInMemoryMostRecentSample();
159
return sampledCacheManagerDelegate.getCacheInMemoryMissRate();
259
163
* @return aggregate off-heap miss rate
261
165
public long getCacheOffHeapMissRate() {
263
for (String cacheName : getCacheNames()) {
264
Ehcache cache = cacheManager.getEhcache(cacheName);
266
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
267
result += stats.getCacheMissOffHeapMostRecentSample();
166
return sampledCacheManagerDelegate.getCacheOffHeapMissRate();
274
170
* @return aggregate on-disk miss rate
276
172
public long getCacheOnDiskMissRate() {
278
for (String cacheName : getCacheNames()) {
279
Ehcache cache = cacheManager.getEhcache(cacheName);
281
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
282
result += stats.getCacheMissOnDiskMostRecentSample();
173
return sampledCacheManagerDelegate.getCacheOnDiskMissRate();
289
177
* @return aggregate put rate
291
179
public long getCachePutRate() {
293
for (String cacheName : getCacheNames()) {
294
Ehcache cache = cacheManager.getEhcache(cacheName);
296
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
297
result += stats.getCacheElementPutMostRecentSample();
180
return sampledCacheManagerDelegate.getCachePutRate();
304
184
* @return aggregate update rate
306
186
public long getCacheUpdateRate() {
308
for (String cacheName : getCacheNames()) {
309
Ehcache cache = cacheManager.getEhcache(cacheName);
311
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
312
result += stats.getCacheElementUpdatedMostRecentSample();
187
return sampledCacheManagerDelegate.getCacheUpdateRate();
319
191
* @return aggregate remove rate
321
193
public long getCacheRemoveRate() {
323
for (String cacheName : getCacheNames()) {
324
Ehcache cache = cacheManager.getEhcache(cacheName);
326
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
327
result += stats.getCacheElementRemovedMostRecentSample();
194
return sampledCacheManagerDelegate.getCacheRemoveRate();
334
198
* @return aggregate eviction rate
336
200
public long getCacheEvictionRate() {
338
for (String cacheName : getCacheNames()) {
339
Ehcache cache = cacheManager.getEhcache(cacheName);
341
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
342
result += stats.getCacheElementEvictedMostRecentSample();
201
return sampledCacheManagerDelegate.getCacheEvictionRate();
349
205
* @return aggregate expiration rate
351
207
public long getCacheExpirationRate() {
353
for (String cacheName : getCacheNames()) {
354
Ehcache cache = cacheManager.getEhcache(cacheName);
356
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
357
result += stats.getCacheElementExpiredMostRecentSample();
208
return sampledCacheManagerDelegate.getCacheExpirationRate();
364
212
* @return aggregate average get time (ms.)
366
214
public float getCacheAverageGetTime() {
369
for (String cacheName : getCacheNames()) {
370
Ehcache cache = cacheManager.getEhcache(cacheName);
372
result += cache.getAverageGetTime();
376
return instances > 0 ? result / instances : 0;
215
return sampledCacheManagerDelegate.getCacheAverageGetTime();
380
219
* @return aggregate search rate
382
221
public long getCacheSearchRate() {
384
for (String cacheName : getCacheNames()) {
385
Ehcache cache = cacheManager.getEhcache(cacheName);
387
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
388
result += stats.getSearchesPerSecond();
222
return sampledCacheManagerDelegate.getCacheSearchRate();
395
226
* @return aggregate search time
397
228
public long getCacheAverageSearchTime() {
399
for (String cacheName : getCacheNames()) {
400
Ehcache cache = cacheManager.getEhcache(cacheName);
402
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
403
result += stats.getAverageSearchTime();
229
return sampledCacheManagerDelegate.getCacheAverageSearchTime();
412
235
public boolean getHasWriteBehindWriter() {
413
for (String cacheName : getCacheNames()) {
414
Ehcache cache = cacheManager.getEhcache(cacheName);
416
if (cache.getWriterManager() instanceof WriteBehindManager &&
417
cache.getRegisteredCacheWriter() != null) {
236
return sampledCacheManagerDelegate.getHasWriteBehindWriter();
426
240
* @return aggregate writer queue length
428
242
public long getWriterQueueLength() {
430
for (String cacheName : getCacheNames()) {
431
Ehcache cache = cacheManager.getEhcache(cacheName);
433
LiveCacheStatistics stats = cache.getLiveCacheStatistics();
434
result += Math.max(stats.getWriterQueueLength(), 0);
243
return sampledCacheManagerDelegate.getWriterQueueLength();
443
249
public int getWriterMaxQueueSize() {
445
for (String cacheName : getCacheNames()) {
446
Ehcache cache = cacheManager.getEhcache(cacheName);
448
CacheWriterConfiguration writerConfig = cache.getCacheConfiguration().getCacheWriterConfiguration();
449
result += (writerConfig.getWriteBehindMaxQueueSize() * writerConfig.getWriteBehindConcurrency());
250
return sampledCacheManagerDelegate.getWriterMaxQueueSize();
458
256
public long getMaxBytesLocalDisk() {
459
return cacheManager.getConfiguration().getMaxBytesLocalDisk();
257
return sampledCacheManagerDelegate.getMaxBytesLocalDisk();
465
263
public String getMaxBytesLocalDiskAsString() {
466
return cacheManager.getConfiguration().getMaxBytesLocalDiskAsString();
264
return sampledCacheManagerDelegate.getMaxBytesLocalDiskAsString();
472
270
public void setMaxBytesLocalDisk(long maxBytes) {
474
cacheManager.getConfiguration().setMaxBytesLocalDisk(maxBytes);
475
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
476
} catch (RuntimeException e) {
477
throw SampledCache.newPlainException(e);
271
sampledCacheManagerDelegate.setMaxBytesLocalDisk(maxBytes);
272
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
484
278
public void setMaxBytesLocalDiskAsString(String maxBytes) {
486
cacheManager.getConfiguration().setMaxBytesLocalDisk(maxBytes);
487
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
488
} catch (RuntimeException e) {
489
throw SampledCache.newPlainException(e);
279
sampledCacheManagerDelegate.setMaxBytesLocalDiskAsString(maxBytes);
280
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
496
286
public long getMaxBytesLocalHeap() {
497
return cacheManager.getConfiguration().getMaxBytesLocalHeap();
287
return sampledCacheManagerDelegate.getMaxBytesLocalHeap();
503
293
public String getMaxBytesLocalHeapAsString() {
504
return cacheManager.getConfiguration().getMaxBytesLocalHeapAsString();
294
return sampledCacheManagerDelegate.getMaxBytesLocalHeapAsString();
510
300
public void setMaxBytesLocalHeap(long maxBytes) {
512
cacheManager.getConfiguration().setMaxBytesLocalHeap(maxBytes);
513
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
514
} catch (RuntimeException e) {
515
throw SampledCache.newPlainException(e);
301
sampledCacheManagerDelegate.setMaxBytesLocalHeap(maxBytes);
302
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
522
308
public void setMaxBytesLocalHeapAsString(String maxBytes) {
524
cacheManager.getConfiguration().setMaxBytesLocalHeap(maxBytes);
525
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
526
} catch (RuntimeException e) {
527
throw SampledCache.newPlainException(e);
309
sampledCacheManagerDelegate.setMaxBytesLocalHeapAsString(maxBytes);
310
sendNotification(CACHE_MANAGER_CHANGED, getCacheManagerAttributes(), getName());
534
316
public long getMaxBytesLocalOffHeap() {
535
return cacheManager.getConfiguration().getMaxBytesLocalOffHeap();
317
return sampledCacheManagerDelegate.getMaxBytesLocalOffHeap();
541
323
public String getMaxBytesLocalOffHeapAsString() {
542
return cacheManager.getConfiguration().getMaxBytesLocalOffHeapAsString();
324
return sampledCacheManagerDelegate.getMaxBytesLocalOffHeapAsString();
548
* @see net.sf.ehcache.management.sampled.SampledCacheManagerMBean#getName()
330
* @see net.sf.ehcache.management.sampled.SampledCacheManager#getName()
550
332
public String getName() {
551
return cacheManager.getName();
333
return sampledCacheManagerDelegate.getName();
337
* @see net.sf.ehcache.management.sampled.SampledCacheManager#getClusterUUID()
339
public String getClusterUUID() {
340
return sampledCacheManagerDelegate.getClusterUUID();
657
406
* @return Cache configuration as String
659
408
public String generateActiveConfigDeclaration(String cacheName) {
660
return this.cacheManager.getActiveConfigurationText(cacheName);
409
return sampledCacheManagerDelegate.generateActiveConfigDeclaration(cacheName);
666
415
public boolean getTransactional() {
667
for (String cacheName : getCacheNames()) {
668
Ehcache cache = cacheManager.getEhcache(cacheName);
669
if (cache != null && cache.getCacheConfiguration().getTransactionalMode().isTransactional()) {
416
return sampledCacheManagerDelegate.getTransactional();
679
422
public boolean getSearchable() {
680
for (String cacheName : getCacheNames()) {
681
Ehcache cache = cacheManager.getEhcache(cacheName);
682
if (cache != null && cache.getCacheConfiguration().getSearchable() != null) {
423
return sampledCacheManagerDelegate.getSearchable();
692
429
public long getTransactionCommittedCount() {
693
return this.cacheManager.getTransactionController().getTransactionCommittedCount();
430
return sampledCacheManagerDelegate.getTransactionCommittedCount();
699
436
public long getTransactionCommitRate() {
701
for (String cacheName : getCacheNames()) {
702
Ehcache cache = cacheManager.getEhcache(cacheName);
704
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
705
result += stats.getCacheXaCommitsMostRecentSample();
437
return sampledCacheManagerDelegate.getTransactionCommitRate();
714
443
public long getTransactionRolledBackCount() {
715
return this.cacheManager.getTransactionController().getTransactionRolledBackCount();
444
return sampledCacheManagerDelegate.getTransactionRolledBackCount();
721
450
public long getTransactionRollbackRate() {
723
for (String cacheName : getCacheNames()) {
724
Ehcache cache = cacheManager.getEhcache(cacheName);
726
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
727
result += stats.getCacheXaRollbacksMostRecentSample();
451
return sampledCacheManagerDelegate.getTransactionRollbackRate();
736
457
public long getTransactionTimedOutCount() {
737
return this.cacheManager.getTransactionController().getTransactionTimedOutCount();
458
return sampledCacheManagerDelegate.getTransactionTimedOutCount();
741
462
* Returns if each contained cache is enabled.
743
464
public boolean isEnabled() throws CacheException {
744
for (String cacheName : getCacheNames()) {
745
Ehcache cache = cacheManager.getEhcache(cacheName);
746
if (cache != null && cache.isDisabled()) {
465
return sampledCacheManagerDelegate.isEnabled();
754
469
* Enables/disables each of the contained caches.
756
471
public void setEnabled(boolean enabled) {
757
for (String cacheName : getCacheNames()) {
758
Ehcache cache = cacheManager.getEhcache(cacheName);
760
cache.setDisabled(!enabled);
763
sendNotification(CACHES_ENABLED, Boolean.valueOf(enabled));
767
* @return is each cache enabled
769
private boolean determineEnabled() {
770
for (String cacheName : getCacheNames()) {
771
Cache cache = cacheManager.getCache(cacheName);
773
if (cache.isDisabled()) {
782
* getCacheManagerAttributes
784
* @return map of attribute name -> value
786
public Map<String, Object> getCacheManagerAttributes() {
472
sampledCacheManagerDelegate.setEnabled(enabled);
473
sendNotification(CACHES_ENABLED, enabled);
477
* @see BaseEmitterBean#getNotificationInfo()
480
public MBeanNotificationInfo[] getNotificationInfo() {
481
return NOTIFICATION_INFO;
484
private Map<String, Object> getCacheManagerAttributes() {
787
485
Map<String, Object> result = new HashMap<String, Object>();
788
486
result.put("MaxBytesLocalHeapAsString", getMaxBytesLocalHeapAsString());
789
487
result.put("MaxBytesLocalOffHeapAsString", getMaxBytesLocalOffHeapAsString());