2
* Copyright Terracotta, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package net.sf.ehcache.management.sampled;
18
import net.sf.ehcache.Cache;
19
import net.sf.ehcache.CacheException;
20
import net.sf.ehcache.CacheManager;
21
import net.sf.ehcache.Ehcache;
22
import net.sf.ehcache.config.CacheWriterConfiguration;
23
import net.sf.ehcache.statistics.LiveCacheStatistics;
24
import net.sf.ehcache.statistics.sampled.SampledCacheStatistics;
25
import net.sf.ehcache.writer.writebehind.WriteBehindManager;
27
import java.util.HashMap;
31
* An implementation of {@link CacheManagerSampler}
33
* @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
34
* @author <a href="mailto:byoukste@terracottatech.com">byoukste</a>
36
public class CacheManagerSamplerImpl implements CacheManagerSampler {
38
private final CacheManager cacheManager;
41
* Constructor taking the backing {@link CacheManager}
43
* @param cacheManager to wrap
45
public CacheManagerSamplerImpl(CacheManager cacheManager) {
46
this.cacheManager = cacheManager;
52
public void clearAll() {
53
cacheManager.clearAll();
59
public String[] getCacheNames() throws IllegalStateException {
60
return cacheManager.getCacheNames();
66
public String getStatus() {
67
return cacheManager.getStatus().toString();
73
public void shutdown() {
80
public Map<String, long[]> getCacheMetrics() {
81
Map<String, long[]> result = new HashMap<String, long[]>();
82
for (String cacheName : getCacheNames()) {
83
Ehcache cache = cacheManager.getEhcache(cacheName);
85
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
86
result.put(cacheName, new long[] {stats.getCacheHitMostRecentSample(),
87
stats.getCacheMissNotFoundMostRecentSample()
88
+ stats.getCacheMissExpiredMostRecentSample(),
89
stats.getCacheElementPutMostRecentSample()});
98
public long getCacheHitRate() {
100
for (String cacheName : getCacheNames()) {
101
Ehcache cache = cacheManager.getEhcache(cacheName);
103
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
104
result += stats.getCacheHitMostRecentSample();
113
public long getCacheInMemoryHitRate() {
115
for (String cacheName : getCacheNames()) {
116
Ehcache cache = cacheManager.getEhcache(cacheName);
118
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
119
result += stats.getCacheHitInMemoryMostRecentSample();
128
public long getCacheOffHeapHitRate() {
130
for (String cacheName : getCacheNames()) {
131
Ehcache cache = cacheManager.getEhcache(cacheName);
133
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
134
result += stats.getCacheHitOffHeapMostRecentSample();
143
public long getCacheOnDiskHitRate() {
145
for (String cacheName : getCacheNames()) {
146
Ehcache cache = cacheManager.getEhcache(cacheName);
148
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
149
result += stats.getCacheHitOnDiskMostRecentSample();
158
public long getCacheMissRate() {
160
for (String cacheName : getCacheNames()) {
161
Ehcache cache = cacheManager.getEhcache(cacheName);
163
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
164
result += (stats.getCacheMissNotFoundMostRecentSample()
165
+ stats.getCacheMissExpiredMostRecentSample());
174
public long getCacheInMemoryMissRate() {
176
for (String cacheName : getCacheNames()) {
177
Ehcache cache = cacheManager.getEhcache(cacheName);
179
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
180
result += stats.getCacheMissInMemoryMostRecentSample();
189
public long getCacheOffHeapMissRate() {
191
for (String cacheName : getCacheNames()) {
192
Ehcache cache = cacheManager.getEhcache(cacheName);
194
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
195
result += stats.getCacheMissOffHeapMostRecentSample();
204
public long getCacheOnDiskMissRate() {
206
for (String cacheName : getCacheNames()) {
207
Ehcache cache = cacheManager.getEhcache(cacheName);
209
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
210
result += stats.getCacheMissOnDiskMostRecentSample();
219
public long getCachePutRate() {
221
for (String cacheName : getCacheNames()) {
222
Ehcache cache = cacheManager.getEhcache(cacheName);
224
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
225
result += stats.getCacheElementPutMostRecentSample();
234
public long getCacheUpdateRate() {
236
for (String cacheName : getCacheNames()) {
237
Ehcache cache = cacheManager.getEhcache(cacheName);
239
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
240
result += stats.getCacheElementUpdatedMostRecentSample();
249
public long getCacheRemoveRate() {
251
for (String cacheName : getCacheNames()) {
252
Ehcache cache = cacheManager.getEhcache(cacheName);
254
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
255
result += stats.getCacheElementRemovedMostRecentSample();
264
public long getCacheEvictionRate() {
266
for (String cacheName : getCacheNames()) {
267
Ehcache cache = cacheManager.getEhcache(cacheName);
269
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
270
result += stats.getCacheElementEvictedMostRecentSample();
279
public long getCacheExpirationRate() {
281
for (String cacheName : getCacheNames()) {
282
Ehcache cache = cacheManager.getEhcache(cacheName);
284
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
285
result += stats.getCacheElementExpiredMostRecentSample();
294
public float getCacheAverageGetTime() {
297
for (String cacheName : getCacheNames()) {
298
Ehcache cache = cacheManager.getEhcache(cacheName);
300
result += cache.getAverageGetTime();
304
return instances > 0 ? result / instances : 0;
310
public long getCacheSearchRate() {
312
for (String cacheName : getCacheNames()) {
313
Ehcache cache = cacheManager.getEhcache(cacheName);
315
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
316
result += stats.getSearchesPerSecond();
325
public long getCacheAverageSearchTime() {
327
for (String cacheName : getCacheNames()) {
328
Ehcache cache = cacheManager.getEhcache(cacheName);
330
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
331
result += stats.getAverageSearchTime();
340
public boolean getHasWriteBehindWriter() {
341
for (String cacheName : getCacheNames()) {
342
Ehcache cache = cacheManager.getEhcache(cacheName);
344
if (cache.getWriterManager() instanceof WriteBehindManager &&
345
cache.getRegisteredCacheWriter() != null) {
356
public long getWriterQueueLength() {
358
for (String cacheName : getCacheNames()) {
359
Ehcache cache = cacheManager.getEhcache(cacheName);
361
LiveCacheStatistics stats = cache.getLiveCacheStatistics();
362
result += Math.max(stats.getWriterQueueLength(), 0);
371
public int getWriterMaxQueueSize() {
373
for (String cacheName : getCacheNames()) {
374
Ehcache cache = cacheManager.getEhcache(cacheName);
376
CacheWriterConfiguration writerConfig = cache.getCacheConfiguration().getCacheWriterConfiguration();
377
result += (writerConfig.getWriteBehindMaxQueueSize() * writerConfig.getWriteBehindConcurrency());
386
public long getMaxBytesLocalDisk() {
387
return cacheManager.getConfiguration().getMaxBytesLocalDisk();
393
public String getMaxBytesLocalDiskAsString() {
394
return cacheManager.getConfiguration().getMaxBytesLocalDiskAsString();
400
public void setMaxBytesLocalDisk(long maxBytes) {
402
cacheManager.getConfiguration().setMaxBytesLocalDisk(maxBytes);
403
} catch (RuntimeException e) {
404
throw Utils.newPlainException(e);
411
public void setMaxBytesLocalDiskAsString(String maxBytes) {
413
cacheManager.getConfiguration().setMaxBytesLocalDisk(maxBytes);
414
} catch (RuntimeException e) {
415
throw Utils.newPlainException(e);
422
public long getMaxBytesLocalHeap() {
423
return cacheManager.getConfiguration().getMaxBytesLocalHeap();
429
public String getMaxBytesLocalHeapAsString() {
430
return cacheManager.getConfiguration().getMaxBytesLocalHeapAsString();
436
public void setMaxBytesLocalHeap(long maxBytes) {
438
cacheManager.getConfiguration().setMaxBytesLocalHeap(maxBytes);
439
} catch (RuntimeException e) {
440
throw Utils.newPlainException(e);
447
public void setMaxBytesLocalHeapAsString(String maxBytes) {
449
cacheManager.getConfiguration().setMaxBytesLocalHeap(maxBytes);
450
} catch (RuntimeException e) {
451
throw Utils.newPlainException(e);
458
public long getMaxBytesLocalOffHeap() {
459
return cacheManager.getConfiguration().getMaxBytesLocalOffHeap();
465
public String getMaxBytesLocalOffHeapAsString() {
466
return cacheManager.getConfiguration().getMaxBytesLocalOffHeapAsString();
472
public String getName() {
473
return cacheManager.getName();
479
public String getClusterUUID() {
480
return cacheManager.getClusterUUID();
486
public void clearStatistics() {
487
for (String cacheName : getCacheNames()) {
488
Cache cache = cacheManager.getCache(cacheName);
490
cache.clearStatistics();
498
public void enableStatistics() {
499
for (String cacheName : getCacheNames()) {
500
Cache cache = cacheManager.getCache(cacheName);
502
// enables regular statistics also
503
cache.setSampledStatisticsEnabled(true);
511
public void disableStatistics() {
512
for (String cacheName : getCacheNames()) {
513
Cache cache = cacheManager.getCache(cacheName);
515
// disables regular statistics also
516
cache.setStatisticsEnabled(false);
524
public void setStatisticsEnabled(boolean enabled) {
535
public boolean isStatisticsEnabled() {
536
for (String cacheName : getCacheNames()) {
537
Cache cache = cacheManager.getCache(cacheName);
539
if (!cache.isSampledStatisticsEnabled()) {
550
public String generateActiveConfigDeclaration() {
551
return this.cacheManager.getActiveConfigurationText();
557
public String generateActiveConfigDeclaration(String cacheName) {
558
return this.cacheManager.getActiveConfigurationText(cacheName);
564
public boolean getTransactional() {
565
for (String cacheName : getCacheNames()) {
566
Ehcache cache = cacheManager.getEhcache(cacheName);
567
if (cache != null && cache.getCacheConfiguration().getTransactionalMode().isTransactional()) {
577
public boolean getSearchable() {
578
for (String cacheName : getCacheNames()) {
579
Ehcache cache = cacheManager.getEhcache(cacheName);
580
if (cache != null && cache.getCacheConfiguration().getSearchable() != null) {
590
public long getTransactionCommittedCount() {
591
return this.cacheManager.getTransactionController().getTransactionCommittedCount();
597
public long getTransactionCommitRate() {
599
for (String cacheName : getCacheNames()) {
600
Ehcache cache = cacheManager.getEhcache(cacheName);
602
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
603
result += stats.getCacheXaCommitsMostRecentSample();
612
public long getTransactionRolledBackCount() {
613
return this.cacheManager.getTransactionController().getTransactionRolledBackCount();
619
public long getTransactionRollbackRate() {
621
for (String cacheName : getCacheNames()) {
622
Ehcache cache = cacheManager.getEhcache(cacheName);
624
SampledCacheStatistics stats = cache.getSampledCacheStatistics();
625
result += stats.getCacheXaRollbacksMostRecentSample();
634
public long getTransactionTimedOutCount() {
635
return this.cacheManager.getTransactionController().getTransactionTimedOutCount();
641
public boolean isEnabled() throws CacheException {
642
for (String cacheName : getCacheNames()) {
643
Ehcache cache = cacheManager.getEhcache(cacheName);
644
if (cache != null && cache.isDisabled()) {
654
public void setEnabled(boolean enabled) {
655
for (String cacheName : getCacheNames()) {
656
Ehcache cache = cacheManager.getEhcache(cacheName);
658
cache.setDisabled(!enabled);
b'\\ No newline at end of file'