public ManagedCacheInstruments(final ManagedCache managedCache) { this.managedCache = notNull("managedCache", managedCache); this.name = managedCache.getName(); }
private Iterable<Map.Entry<CacheStatisticsKey,Supplier<Long>>> getStatistics() { // Include the heap size iff debug logging is enabled if (LOG.isDebugEnabled()) { return managedCache.getStatistics().entrySet(); } return filter(managedCache.getStatistics().entrySet(), IGNORE_HEAP_SIZE); }
@Override public void flushCaches() { for (final com.atlassian.util.concurrent.Supplier<ManagedCache> cacheRef : caches.values()) { final ManagedCache managedCache = cacheRef.get(); if (managedCache != null && managedCache.isFlushable()) { managedCache.clear(); } } }
@Test public void testClear() throws Exception { CachedReference<Long> ref = makeReference(); assertThat(ref.get(), equalTo(100L)); ((ManagedCache) ref).clear(); // This should have been recalculated assertThat(ref.get(), equalTo(101L)); }
@Override public boolean updateExpireAfterWrite(long expireAfter, @Nonnull TimeUnit timeUnit) { return getLocalCache().updateExpireAfterAccess(expireAfter, timeUnit); }
@Override public Long currentExpireAfterAccessMillis() { return getLocalCache().currentExpireAfterAccessMillis(); }
@Override public Long currentExpireAfterWriteMillis() { return getLocalCache().currentExpireAfterWriteMillis(); }
@Override public boolean updateMaxEntries(int newValue) { return getLocalCache().updateMaxEntries(newValue); }
@Override public Integer currentMaxEntries() { return getLocalCache().currentMaxEntries(); }
@Test public void testClear() throws Exception { Cache<String, Long> cache = makeSimpleCache(); // Add some entries using the builder cache.put("1", 11L); cache.put("2", 12L); cache.put("3", 13L); cache.put("4", 14L); assertThat(cache.get("1"), equalTo(11L)); assertThat(cache.get("2"), equalTo(12L)); assertThat(cache.get("3"), equalTo(13L)); assertThat(cache.get("4"), equalTo(14L)); assertSize(cache, 4); // A cache that doesn't expire any entries should return all the keys ((ManagedCache)cache).clear(); assertEmpty(cache); // Removed values should be recomputed assertThat(cache.get("1"), nullValue()); assertThat(cache.get("2"), nullValue()); assertThat(cache.get("3"), nullValue()); assertThat(cache.get("4"), nullValue()); assertEmpty(cache); }
@Override public boolean updateExpireAfterAccess(long expireAfter, @Nonnull TimeUnit timeUnit) { return getLocalCache().updateExpireAfterAccess(expireAfter, timeUnit); }
private Map<CacheStatisticsKey, Long> calculateStatistics(final ManagedCache cache) { final Map<CacheStatisticsKey, Long> stats = Maps.newHashMap(); for (final Map.Entry<CacheStatisticsKey, Supplier<Long>> entry : cache.getStatistics().entrySet()) { if (isCacheStatisticAllowed(entry.getKey())) { stats.put(entry.getKey(), entry.getValue().get()); } } return stats; }
private List<CacheStatisticsAnalyticEvent> getCacheStatistics() { return cacheManager.getManagedCaches().stream() .filter(ManagedCache::isStatisticsEnabled) .map(cache -> new CacheStatisticsAnalyticEvent(cache.getName(), calculateStatistics(cache))) .collect(toList()); }
@Test public void testClear() throws Exception { Cache<String, Long> cache = makeUnexpiringCache(); // Add some entries using the builder cache.put("1", 11L); cache.put("2", 12L); cache.put("3", 13L); cache.put("4", 14L); assertThat(cache.get("1"), equalTo(11L)); assertThat(cache.get("2"), equalTo(12L)); assertThat(cache.get("3"), equalTo(13L)); assertThat(cache.get("4"), equalTo(14L)); assertSize(cache, (4)); ((ManagedCache) cache).clear(); assertMissingKeys(cache, "1", "2", "3", "4"); assertEmpty(cache); // Removed values should be recomputed assertThat(cache.get("1"), equalTo(1L)); assertThat(cache.get("2"), equalTo(2L)); assertThat(cache.get("3"), equalTo(3L)); assertThat(cache.get("4"), equalTo(4L)); assertSize(cache, (4)); }
private long getProperty(CacheStatisticsKey propertyName, long defaultValue) { if(cacheManager.getManagedCache(cacheName) == null || cacheManager.getManagedCache(cacheName).getStatistics().get(propertyName) == null) { return defaultValue; } return cacheManager.getManagedCache(cacheName).getStatistics().get(propertyName).get(); } }
/** * Enables collecting JMX metrics. * <p> * Note: it must be called to allow the method <code>registerMBean</code> registering MBeans. *</p> * * @param mbeanServer {@link MBeanServer} instance used for registering and unregistering MBeans * @param cacheManager {@link CacheManager} instance used for retrieving caches metrics */ public void enableCollectingJMXMetrics(@Nonnull MBeanServer mbeanServer, @Nonnull CacheManager cacheManager) { this.mbeanServer = mbeanServer; this.cacheManager = cacheManager; Collection<ManagedCache> caches = cacheManager.getManagedCaches(); for(ManagedCache cache: caches) { registerMBean(cache.getName()); } }
@Test public void testStatisticsDisabledViaSettings() { Cache<String, Long> cache = makeStatisticsDisabledCache(); final SortedMap<CacheStatisticsKey, com.atlassian.util.concurrent.Supplier<Long>> stats = ((ManagedCache)cache).getStatistics(); cache.put("1",11L); assertThat(stats.entrySet(), hasSize(0)); }
@Test public void testGetName() throws Exception { CachedReference<Long> ref = makeReference(); assertThat(((ManagedCache) ref).getName(), equalTo("mycache")); }
@Test public void testStatisticsEnabledViaSettings() { Cache<String, Long> cache = makeStatisticsEnabledCache(); cache.put("1",11L); final SortedMap<CacheStatisticsKey, com.atlassian.util.concurrent.Supplier<Long>> stats = ((ManagedCache)cache).getStatistics(); assertThat(stats.entrySet(), hasSize(greaterThan(0))); assertThat(stats.get(SIZE), stat(1L)); assertThat(stats.get(PUT_COUNT), stat(1L)); assertThat(stats.get(HIT_COUNT), stat(0L)); assertThat(stats.get(REMOVE_COUNT), stat(0L)); assertThat(stats.get(EVICTION_COUNT), stat(0L)); assertThat(stats.get(MISS_COUNT), stat(0L)); }
@Test public void testFactoryGeneratedName() throws Exception { CachedReference<Long> ref = factory.getCachedReference( Object.class, "mycache", nullSupplier(), settingsBuilder().build()); assertThat(((ManagedCache)ref).getName(), equalTo("java.lang.Object.mycache")); }