1 /**
2  *  Copyright Terracotta, Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package net.sf.ehcache.statistics.sampled;
18
19 import net.sf.ehcache.statistics.CacheUsageListener;
20 import net.sf.ehcache.util.FailSafeTimer;
21 import net.sf.ehcache.util.counter.sampled.SampledCounter;
22 import net.sf.ehcache.util.counter.sampled.SampledCounterConfig;
23 import net.sf.ehcache.util.counter.sampled.SampledRateCounter;
24 import net.sf.ehcache.util.counter.sampled.SampledRateCounterConfig;
25
26 /**
27  * An implementation of {@link SampledCacheStatistics} and also implements {@link CacheUsageListener} and depends on the notification
28  * received from
29  * these to update the stats. Uses separate delegates depending on whether
30  * sampled statistics is enabled or not.
31  * <p />
32  * To collect statistics data, instances of this class should be registered as a {@link CacheUsageListener} to a Cache
33  *
34  * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
35  * @since 1.7
36  */

37 public class SampledCacheStatisticsWrapper implements CacheUsageListener, CacheStatisticsSampler {
38
39     private static final NullSampledCacheStatistics NULL_SAMPLED_CACHE_STATISTICS = new NullSampledCacheStatistics();
40
41     private volatile SampledCacheStatistics delegate;
42
43     private volatile CacheStatisticsSampler samplerDelegate;
44
45     /**
46      * Default constructor.
47      */

48     public SampledCacheStatisticsWrapper() {
49         delegate = new NullSampledCacheStatistics();
50     }
51
52     /**
53      * Enabled sampled statistics with submitted {@link FailSafeTimer} and {@link SampledCounter} default configurations
54      *
55      * @param timer the {@code FailSafeTimer} for sampling
56      */

57     public void enableSampledStatistics(FailSafeTimer timer) {
58         enableSampledStatistics(new SampledCacheStatisticsImpl(timer));
59     }
60
61     /**
62      * Enabled sampled statistics with submitted {@link FailSafeTimer} and {@link SampledCounter} the specified configurations
63      *
64      * @param timer the {@code FailSafeTimer} for sampling
65      * @param config the {@code SampledCounterConfig} for sampling
66      * @param rateGetConfig the {@code SampledRateCounterConfig} for sampling average time of cache gets
67      * @param rateSearchConfig the {@code SampledCounterConfig} for sampling average time of cache searches
68      */

69     public void enableSampledStatistics(FailSafeTimer timer,
70                                         SampledCounterConfig config,
71                                         SampledRateCounterConfig rateGetConfig,
72                                         SampledRateCounterConfig rateSearchConfig) {
73         enableSampledStatistics(new SampledCacheStatisticsImpl(timer, config, rateGetConfig, rateSearchConfig));
74     }
75
76     /**
77      * Enable sampled statistics collection
78      *
79      * @param timer
80      */

81     private void enableSampledStatistics(SampledCacheStatisticsImpl sampledCacheStats) {
82         delegate.dispose();
83         samplerDelegate = sampledCacheStats;
84         delegate = sampledCacheStats;
85     }
86
87     /**
88      * Disable sampled statistics collection
89      */

90     public void disableSampledStatistics() {
91         delegate.dispose();
92         delegate = NULL_SAMPLED_CACHE_STATISTICS;
93         samplerDelegate = null;
94     }
95
96     /**
97      * {@inheritDoc}
98      */

99     public boolean isSampledStatisticsEnabled() {
100         return delegate instanceof SampledCacheStatisticsImpl;
101     }
102
103     /**
104      * {@inheritDoc}
105      *
106      */

107     public void dispose() {
108         delegate.dispose();
109     }
110
111     /**
112      * {@inheritDoc}
113      *
114      */

115     public long getAverageGetTimeMostRecentSample() {
116         return delegate.getAverageGetTimeMostRecentSample();
117     }
118
119
120     @Override
121     public long getAverageGetTimeNanosMostRecentSample() {
122         return delegate.getAverageGetTimeNanosMostRecentSample();
123     }
124
125     /**
126      * {@inheritDoc}
127      *
128      */

129     public long getCacheElementEvictedMostRecentSample() {
130         return delegate.getCacheElementEvictedMostRecentSample();
131     }
132
133     /**
134      * {@inheritDoc}
135      *
136      */

137     public long getCacheElementExpiredMostRecentSample() {
138         return delegate.getCacheElementExpiredMostRecentSample();
139     }
140
141     /**
142      * {@inheritDoc}
143      *
144      */

145     public long getCacheElementPutMostRecentSample() {
146         return delegate.getCacheElementPutMostRecentSample();
147     }
148
149     /**
150      * {@inheritDoc}
151      *
152      */

153     public long getCacheElementRemovedMostRecentSample() {
154         return delegate.getCacheElementRemovedMostRecentSample();
155     }
156
157     /**
158      * {@inheritDoc}
159      *
160      */

161     public long getCacheElementUpdatedMostRecentSample() {
162         return delegate.getCacheElementUpdatedMostRecentSample();
163     }
164
165     /**
166      * {@inheritDoc}
167      *
168      */

169     public long getCacheHitInMemoryMostRecentSample() {
170         return delegate.getCacheHitInMemoryMostRecentSample();
171     }
172
173     /**
174      * {@inheritDoc}
175      *
176      */

177     public long getCacheHitOffHeapMostRecentSample() {
178         return delegate.getCacheHitOffHeapMostRecentSample();
179     }
180
181     /**
182      * {@inheritDoc}
183      *
184      */

185     public long getCacheHitMostRecentSample() {
186         return delegate.getCacheHitMostRecentSample();
187     }
188
189     /**
190      * {@inheritDoc}
191      *
192      */

193     public long getCacheHitOnDiskMostRecentSample() {
194         return delegate.getCacheHitOnDiskMostRecentSample();
195     }
196
197     /**
198      * {@inheritDoc}
199      *
200      */

201     public long getCacheMissExpiredMostRecentSample() {
202         return delegate.getCacheMissExpiredMostRecentSample();
203     }
204
205     /**
206      * {@inheritDoc}
207      *
208      */

209     public long getCacheMissMostRecentSample() {
210         return delegate.getCacheMissMostRecentSample();
211     }
212
213     /**
214      * {@inheritDoc}
215      *
216      */

217     public long getCacheMissInMemoryMostRecentSample() {
218         return delegate.getCacheMissInMemoryMostRecentSample();
219     }
220
221     /**
222      * {@inheritDoc}
223      *
224      */

225     public long getCacheMissOffHeapMostRecentSample() {
226         return delegate.getCacheMissOffHeapMostRecentSample();
227     }
228
229     /**
230      * {@inheritDoc}
231      *
232      */

233     public long getCacheMissOnDiskMostRecentSample() {
234         return delegate.getCacheMissOnDiskMostRecentSample();
235     }
236
237     /**
238      * {@inheritDoc}
239      *
240      */

241     public long getCacheMissNotFoundMostRecentSample() {
242         return delegate.getCacheMissNotFoundMostRecentSample();
243     }
244
245     /**
246      * {@inheritDoc}
247      *
248      */

249     public int getStatisticsAccuracy() {
250         return delegate.getStatisticsAccuracy();
251     }
252
253     /**
254      * {@inheritDoc}
255      *
256      * @see net.sf.ehcache.statistics.sampled.SampledCacheStatistics#clearStatistics()
257      */

258     public void clearStatistics() {
259         delegate.clearStatistics();
260     }
261
262     /**
263      * {@inheritDoc}
264      *
265      */

266     public String getStatisticsAccuracyDescription() {
267         return delegate.getStatisticsAccuracyDescription();
268     }
269
270     private CacheUsageListener getDelegateAsListener() {
271         return (CacheUsageListener) delegate;
272     }
273
274     /**
275      * {@inheritDoc}
276      *
277      */

278     public void notifyCacheElementEvicted() {
279         getDelegateAsListener().notifyCacheElementEvicted();
280     }
281
282     /**
283      * {@inheritDoc}
284      *
285      */

286     public void notifyCacheElementExpired() {
287         getDelegateAsListener().notifyCacheElementExpired();
288     }
289
290     /**
291      * {@inheritDoc}
292      *
293      */

294     public void notifyCacheElementPut() {
295         getDelegateAsListener().notifyCacheElementPut();
296     }
297
298     /**
299      * {@inheritDoc}
300      *
301      */

302     public void notifyCacheElementRemoved() {
303         getDelegateAsListener().notifyCacheElementRemoved();
304     }
305
306     /**
307      * {@inheritDoc}
308      *
309      */

310     public void notifyCacheElementUpdated() {
311         getDelegateAsListener().notifyCacheElementUpdated();
312     }
313
314     /**
315      * {@inheritDoc}
316      *
317      */

318     public void notifyCacheHitInMemory() {
319         getDelegateAsListener().notifyCacheHitInMemory();
320     }
321
322     /**
323      * {@inheritDoc}
324      */

325     public void notifyCacheHitOffHeap() {
326         getDelegateAsListener().notifyCacheHitOffHeap();
327     }
328
329     /**
330      * {@inheritDoc}
331      *
332      */

333     public void notifyCacheHitOnDisk() {
334         getDelegateAsListener().notifyCacheHitOnDisk();
335     }
336
337     /**
338      * {@inheritDoc}
339      *
340      */

341     public void notifyCacheMissedWithExpired() {
342         getDelegateAsListener().notifyCacheMissedWithExpired();
343     }
344
345     /**
346      * {@inheritDoc}
347      *
348      */

349     public void notifyCacheMissedWithNotFound() {
350         getDelegateAsListener().notifyCacheMissedWithNotFound();
351     }
352
353     /**
354      * {@inheritDoc}
355      *
356      */

357     public void notifyCacheMissInMemory() {
358         getDelegateAsListener().notifyCacheMissInMemory();
359     }
360
361     /**
362      * {@inheritDoc}
363      *
364      */

365     public void notifyCacheMissOffHeap() {
366         getDelegateAsListener().notifyCacheMissOffHeap();
367     }
368
369     /**
370      * {@inheritDoc}
371      *
372      */

373     public void notifyCacheMissOnDisk() {
374         getDelegateAsListener().notifyCacheMissOnDisk();
375     }
376
377     /**
378      * {@inheritDoc}
379      *
380      */

381     public void notifyRemoveAll() {
382         getDelegateAsListener().notifyRemoveAll();
383     }
384
385     /**
386      * {@inheritDoc}
387      *
388      */

389     public void notifyStatisticsAccuracyChanged(int statisticsAccuracy) {
390         getDelegateAsListener().notifyStatisticsAccuracyChanged(statisticsAccuracy);
391     }
392
393     /**
394      * {@inheritDoc}
395      *
396      */

397     public void notifyStatisticsCleared() {
398         getDelegateAsListener().notifyStatisticsCleared();
399     }
400
401     /**
402      * {@inheritDoc}
403      *
404      */

405     public void notifyStatisticsEnabledChanged(boolean enableStatistics) {
406         getDelegateAsListener().notifyStatisticsEnabledChanged(enableStatistics);
407     }
408
409     /**
410      * {@inheritDoc}
411      *
412      */

413     public void notifyTimeTakenForGet(long millis) {
414         /**/
415     }
416
417     /**
418      * {@inheritDoc}
419      *
420      */

421     public void notifyGetTimeNanos(long nanos) {
422         getDelegateAsListener().notifyGetTimeNanos(nanos);
423     }
424
425     /**
426      * {@inheritDoc}
427      */

428     public long getAverageSearchTime() {
429         return delegate.getAverageSearchTime();
430     }
431
432     /**
433      * {@inheritDoc}
434      */

435     public long getSearchesPerSecond() {
436         return delegate.getSearchesPerSecond();
437     }
438
439     /**
440      * {@inheritDoc}
441      */

442     public void notifyCacheSearch(long executeTime) {
443         getDelegateAsListener().notifyCacheSearch(executeTime);
444     }
445
446     /**
447      * {@inheritDoc}
448      */

449     public void notifyXaCommit() {
450         getDelegateAsListener().notifyXaCommit();
451     }
452
453     /**
454      * {@inheritDoc}
455      */

456     public void notifyXaRollback() {
457         getDelegateAsListener().notifyXaRollback();
458     }
459
460     /**
461      * {@inheritDoc}
462      */

463     public long getCacheXaCommitsMostRecentSample() {
464         return delegate.getCacheXaCommitsMostRecentSample();
465     }
466
467     /**
468      * {@inheritDoc}
469      */

470     public long getCacheXaRollbacksMostRecentSample() {
471         return delegate.getCacheXaRollbacksMostRecentSample();
472     }
473
474     @Override
475     public SampledCounter getCacheHitSample() {
476         return samplerDelegate == null ? null : samplerDelegate.getCacheHitSample();
477     }
478
479     @Override
480     public SampledCounter getCacheHitInMemorySample() {
481         return samplerDelegate == null ? null : samplerDelegate.getCacheHitInMemorySample();
482     }
483
484     @Override
485     public SampledCounter getCacheHitOffHeapSample() {
486         return samplerDelegate == null ? null : samplerDelegate.getCacheHitOffHeapSample();
487     }
488
489     @Override
490     public SampledCounter getCacheHitOnDiskSample() {
491         return samplerDelegate == null ? null : samplerDelegate.getCacheHitOnDiskSample();
492     }
493
494     @Override
495     public SampledCounter getCacheMissSample() {
496         return samplerDelegate == null ? null : samplerDelegate.getCacheMissSample();
497     }
498
499     @Override
500     public SampledCounter getCacheMissInMemorySample() {
501         return samplerDelegate == null ? null : samplerDelegate.getCacheHitInMemorySample();
502     }
503
504     @Override
505     public SampledCounter getCacheMissOffHeapSample() {
506         return samplerDelegate == null ? null : samplerDelegate.getCacheMissOffHeapSample();
507     }
508
509     @Override
510     public SampledCounter getCacheMissOnDiskSample() {
511         return samplerDelegate == null ? null : samplerDelegate.getCacheMissOnDiskSample();
512     }
513
514     @Override
515     public SampledCounter getCacheMissExpiredSample() {
516         return samplerDelegate == null ? null : samplerDelegate.getCacheMissExpiredSample();
517     }
518
519     @Override
520     public SampledCounter getCacheMissNotFoundSample() {
521         return samplerDelegate == null ? null : samplerDelegate.getCacheMissNotFoundSample();
522     }
523
524     @Override
525     public SampledCounter getCacheElementEvictedSample() {
526         return samplerDelegate == null ? null : samplerDelegate.getCacheElementEvictedSample();
527     }
528
529     @Override
530     public SampledCounter getCacheElementRemovedSample() {
531         return samplerDelegate == null ? null : samplerDelegate.getCacheElementRemovedSample();
532     }
533
534     @Override
535     public SampledCounter getCacheElementExpiredSample() {
536         return samplerDelegate == null ? null : samplerDelegate.getCacheElementExpiredSample();
537     }
538
539     @Override
540     public SampledCounter getCacheElementPutSample() {
541         return samplerDelegate == null ? null : samplerDelegate.getCacheElementPutSample();
542     }
543
544     @Override
545     public SampledCounter getCacheElementUpdatedSample() {
546         return samplerDelegate == null ? null : samplerDelegate.getCacheElementUpdatedSample();
547     }
548
549     @Override
550     public SampledRateCounter getAverageGetTimeSample() {
551         return samplerDelegate == null ? null : samplerDelegate.getAverageGetTimeSample();
552     }
553
554     @Override
555     public SampledRateCounter getAverageGetTimeNanosSample() {
556         return samplerDelegate == null ? null : samplerDelegate.getAverageGetTimeNanosSample();
557     }
558
559     @Override
560     public SampledRateCounter getAverageSearchTimeSample() {
561         return samplerDelegate == null ? null : samplerDelegate.getAverageSearchTimeSample();
562     }
563
564     @Override
565     public SampledCounter getSearchesPerSecondSample() {
566         return samplerDelegate == null ? null : samplerDelegate.getSearchesPerSecondSample();
567     }
568
569     @Override
570     public SampledCounter getCacheXaCommitsSample() {
571         return samplerDelegate == null ? null : samplerDelegate.getCacheXaCommitsSample();
572     }
573
574     @Override
575     public SampledCounter getCacheXaRollbacksSample() {
576         return samplerDelegate == null ? null : samplerDelegate.getCacheXaRollbacksSample();
577     }
578
579     @Override
580     public int getCacheHitRatioMostRecentSample() {
581         return delegate.getCacheHitRatioMostRecentSample();
582     }
583
584     @Override
585     public SampledCounter getCacheHitRatioSample() {
586         return samplerDelegate == null ? null : samplerDelegate.getCacheHitRatioSample();
587     }
588 }
589