@Test public void testTimelineMetricCacheProviderGets() throws Exception { Configuration configuration = createNiceMock(Configuration.class); expect(configuration.getMetricCacheTTLSeconds()).andReturn(3600); expect(configuration.getMetricCacheIdleSeconds()).andReturn(100); expect(configuration.getMetricsCacheManagerHeapPercent()).andReturn("10%").anyTimes(); replay(configuration); final long now = System.currentTimeMillis(); TimelineMetrics metrics = new TimelineMetrics(); TimelineMetric timelineMetric = new TimelineMetric(); timelineMetric.setMetricName("cpu_user"); timelineMetric.setAppId("app1"); TreeMap<Long, Double> metricValues = new TreeMap<Long, Double>(); metricValues.put(now + 100, 1.0); metricValues.put(now + 200, 2.0); metricValues.put(now + 300, 3.0); timelineMetric.setMetricValues(metricValues); metrics.getMetrics().add(timelineMetric); TimelineMetricCacheEntryFactory cacheEntryFactory = createMock(TimelineMetricCacheEntryFactory.class); TimelineAppMetricCacheKey queryKey = new TimelineAppMetricCacheKey( Collections.singleton("cpu_user"), "app1", new TemporalInfoImpl(now, now + 1000, 1) ); TimelineMetricsCacheValue value = new TimelineMetricsCacheValue(now, now + 1000, metrics, null); TimelineAppMetricCacheKey testKey = new TimelineAppMetricCacheKey( Collections.singleton("cpu_user"), "app1", new TemporalInfoImpl(now, now + 2000, 1) ); expect(cacheEntryFactory.createEntry(anyObject())).andReturn(value); cacheEntryFactory.updateEntryValue(testKey, value); expectLastCall().once(); replay(cacheEntryFactory); TimelineMetricCacheProvider cacheProvider = createMockBuilder(TimelineMetricCacheProvider.class) .addMockedMethod("createCacheConfiguration") .withConstructor(configuration, cacheEntryFactory) .createNiceMock(); expect(cacheProvider.createCacheConfiguration()).andReturn(createTestCacheConfiguration(configuration)).anyTimes(); replay(cacheProvider); TimelineMetricCache cache = cacheProvider.getTimelineMetricsCache(); metrics = cache.getAppTimelineMetricsFromCache(queryKey); List<TimelineMetric> metricsList = metrics.getMetrics(); Assert.assertEquals(1, metricsList.size()); TimelineMetric metric = metricsList.iterator().next(); Assert.assertEquals("cpu_user", metric.getMetricName()); Assert.assertEquals("app1", metric.getAppId()); Assert.assertSame(metricValues, metric.getMetricValues()); metrics = cache.getAppTimelineMetricsFromCache(testKey); metricsList = metrics.getMetrics(); Assert.assertEquals(1, metricsList.size()); Assert.assertEquals("cpu_user", metric.getMetricName()); Assert.assertEquals("app1", metric.getAppId()); Assert.assertSame(metricValues, metric.getMetricValues()); verify(configuration, cacheEntryFactory); }
public TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key) throws IllegalArgumentException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("Fetching metrics with key: " + key); } validateKey(key); Element element = null; try { element = get(key); } catch (LockTimeoutException le) { Throwable t = le.getCause(); if (t instanceof CacheException) { t = t.getCause(); if (t instanceof SocketTimeoutException) { throw new SocketTimeoutException(t.getMessage()); } if (t instanceof ConnectException) { throw new ConnectException(t.getMessage()); } } } TimelineMetrics timelineMetrics = new TimelineMetrics(); if (element != null && element.getObjectValue() != null) { TimelineMetricsCacheValue value = (TimelineMetricsCacheValue) element.getObjectValue(); if (LOG.isDebugEnabled()) { LOG.debug("Returning value from cache: " + value); } timelineMetrics = value.getTimelineMetrics(); } if (LOG.isDebugEnabled()) { if (printCacheStatsCounter.getAndIncrement() == 0) { StatisticsGateway statistics = this.getStatistics(); LOG.debug("Metrics cache stats => \n" + ", Evictions = " + statistics.cacheEvictedCount() + ", Expired = " + statistics.cacheExpiredCount() + ", Hits = " + statistics.cacheHitCount() + ", Misses = " + statistics.cacheMissCount() + ", Hit ratio = " + statistics.cacheHitRatio() + ", Puts = " + statistics.cachePutCount() + ", Size in MB = " + (statistics.getLocalHeapSizeInBytes() / 1048576)); } else { printCacheStatsCounter.compareAndSet(100, 0); } } return timelineMetrics; }
TimelineMetricCache extends UpdatingSelfPopulatingCache { public TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key) throws IllegalArgumentException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("Fetching metrics with key: " + key); } validateKey(key); Element element = null; try { element = get(key); } catch (LockTimeoutException le) { Throwable t = le.getCause(); if (t instanceof CacheException) { t = t.getCause(); if (t instanceof SocketTimeoutException) { throw new SocketTimeoutException(t.getMessage()); } if (t instanceof ConnectException) { throw new ConnectException(t.getMessage()); } } } TimelineMetrics timelineMetrics = new TimelineMetrics(); if (element != null && element.getObjectValue() != null) { TimelineMetricsCacheValue value = (TimelineMetricsCacheValue) element.getObjectValue(); if (LOG.isDebugEnabled()) { LOG.debug("Returning value from cache: " + value); } timelineMetrics = value.getTimelineMetrics(); } if (LOG.isDebugEnabled()) { if (printCacheStatsCounter.getAndIncrement() == 0) { StatisticsGateway statistics = this.getStatistics(); LOG.debug("Metrics cache stats => \n" + ", Evictions = " + statistics.cacheEvictedCount() + ", Expired = " + statistics.cacheExpiredCount() + ", Hits = " + statistics.cacheHitCount() + ", Misses = " + statistics.cacheMissCount() + ", Hit ratio = " + statistics.cacheHitRatio() + ", Puts = " + statistics.cachePutCount() + ", Size in MB = " + (statistics.getLocalHeapSizeInBytes() / 1048576)); } else { printCacheStatsCounter.compareAndSet(100, 0); } } return timelineMetrics; } }
TimelineMetricCache extends UpdatingSelfPopulatingCache { public TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key) throws IllegalArgumentException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("Fetching metrics with key: " + key); } validateKey(key); Element element = null; try { element = get(key); } catch (LockTimeoutException le) { Throwable t = le.getCause(); if (t instanceof CacheException) { t = t.getCause(); if (t instanceof SocketTimeoutException) { throw new SocketTimeoutException(t.getMessage()); } if (t instanceof ConnectException) { throw new ConnectException(t.getMessage()); } } } TimelineMetrics timelineMetrics = new TimelineMetrics(); if (element != null && element.getObjectValue() != null) { TimelineMetricsCacheValue value = (TimelineMetricsCacheValue) element.getObjectValue(); if (LOG.isDebugEnabled()) { LOG.debug("Returning value from cache: " + value); } timelineMetrics = value.getTimelineMetrics(); } if (LOG.isDebugEnabled()) { if (printCacheStatsCounter.getAndIncrement() == 0) { StatisticsGateway statistics = this.getStatistics(); LOG.debug("Metrics cache stats => \n" + ", Evictions = " + statistics.cacheEvictedCount() + ", Expired = " + statistics.cacheExpiredCount() + ", Hits = " + statistics.cacheHitCount() + ", Misses = " + statistics.cacheMissCount() + ", Hit ratio = " + statistics.cacheHitRatio() + ", Puts = " + statistics.cachePutCount() + ", Size in MB = " + (statistics.getLocalHeapSizeInBytes() / 1048576)); } else { printCacheStatsCounter.compareAndSet(100, 0); } } return timelineMetrics; } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); }
TimelineMetricCache extends UpdatingSelfPopulatingCache { public TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key) throws IllegalArgumentException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("Fetching metrics with key: " + key); } validateKey(key); Element element = null; try { element = get(key); } catch (LockTimeoutException le) { Throwable t = le.getCause(); if (t instanceof CacheException) { t = t.getCause(); if (t instanceof SocketTimeoutException) { throw new SocketTimeoutException(t.getMessage()); } if (t instanceof ConnectException) { throw new ConnectException(t.getMessage()); } } } TimelineMetrics timelineMetrics = new TimelineMetrics(); if (element != null && element.getObjectValue() != null) { TimelineMetricsCacheValue value = (TimelineMetricsCacheValue) element.getObjectValue(); if (LOG.isDebugEnabled()) { LOG.debug("Returning value from cache: " + value); } timelineMetrics = value.getTimelineMetrics(); } if (LOG.isDebugEnabled()) { if (printCacheStatsCounter.getAndIncrement() == 0) { StatisticsGateway statistics = this.getStatistics(); LOG.debug("Metrics cache stats => \n" + ", Evictions = " + statistics.cacheEvictedCount() + ", Expired = " + statistics.cacheExpiredCount() + ", Hits = " + statistics.cacheHitCount() + ", Misses = " + statistics.cacheMissCount() + ", Hit ratio = " + statistics.cacheHitRatio() + ", Puts = " + statistics.cachePutCount() + ", Size in MB = " + (statistics.getLocalHeapSizeInBytes() / 1048576)); } else { printCacheStatsCounter.compareAndSet(100, 0); } } return timelineMetrics; } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key); @Override Element get(Object key); }
TimelineMetricCache extends UpdatingSelfPopulatingCache { public TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key) throws IllegalArgumentException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("Fetching metrics with key: " + key); } validateKey(key); Element element = null; try { element = get(key); } catch (LockTimeoutException le) { Throwable t = le.getCause(); if (t instanceof CacheException) { t = t.getCause(); if (t instanceof SocketTimeoutException) { throw new SocketTimeoutException(t.getMessage()); } if (t instanceof ConnectException) { throw new ConnectException(t.getMessage()); } } } TimelineMetrics timelineMetrics = new TimelineMetrics(); if (element != null && element.getObjectValue() != null) { TimelineMetricsCacheValue value = (TimelineMetricsCacheValue) element.getObjectValue(); if (LOG.isDebugEnabled()) { LOG.debug("Returning value from cache: " + value); } timelineMetrics = value.getTimelineMetrics(); } if (LOG.isDebugEnabled()) { if (printCacheStatsCounter.getAndIncrement() == 0) { StatisticsGateway statistics = this.getStatistics(); LOG.debug("Metrics cache stats => \n" + ", Evictions = " + statistics.cacheEvictedCount() + ", Expired = " + statistics.cacheExpiredCount() + ", Hits = " + statistics.cacheHitCount() + ", Misses = " + statistics.cacheMissCount() + ", Hit ratio = " + statistics.cacheHitRatio() + ", Puts = " + statistics.cachePutCount() + ", Size in MB = " + (statistics.getLocalHeapSizeInBytes() / 1048576)); } else { printCacheStatsCounter.compareAndSet(100, 0); } } return timelineMetrics; } TimelineMetricCache(Ehcache cache, UpdatingCacheEntryFactory factory); TimelineMetrics getAppTimelineMetricsFromCache(TimelineAppMetricCacheKey key); @Override Element get(Object key);  }