use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.
the class CacheMetricsForClusterGroupSelfTest method assertMetrics.
/**
* @param cache Cache.
*/
private void assertMetrics(IgniteCache<Integer, Integer> cache) {
CacheMetrics[] ms = new CacheMetrics[GRID_CNT];
for (int i = 0; i < GRID_CNT; i++) {
CacheMetrics metrics = cache.metrics(grid(i).cluster().forCacheNodes(cache.getName()));
for (int j = 0; j < GRID_CNT; j++) ms[j] = grid(j).cache(cache.getName()).localMetrics();
// Static metrics
for (int j = 0; j < GRID_CNT; j++) assertEquals(metrics.name(), ms[j].name());
// Dynamic metrics
assertEquals(metrics.getCacheGets(), sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCacheGets();
}
}));
assertEquals(metrics.getCachePuts(), sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCachePuts();
}
}));
assertEquals(metrics.getCacheHits(), sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCacheHits();
}
}));
}
}
use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.
the class CacheMetricsEnableRuntimeTest method testJmxStatisticsEnable.
/**
* @param persistence Persistence.
*/
private void testJmxStatisticsEnable(boolean persistence) throws Exception {
this.persistence = persistence;
Ignite ig1 = startGrid(1);
Ignite ig2 = startGrid(2);
ig1.active(true);
CacheConfiguration ccfg = ig1.cache(CACHE1).getConfiguration(CacheConfiguration.class);
CacheConfiguration cacheCfg2 = new CacheConfiguration(ccfg);
cacheCfg2.setName(CACHE2);
cacheCfg2.setStatisticsEnabled(true);
ig2.getOrCreateCache(cacheCfg2);
CacheMetricsMXBean mxBeanCache1 = mxBean(2, CACHE1, CacheClusterMetricsMXBeanImpl.class);
CacheMetricsMXBean mxBeanCache2 = mxBean(2, CACHE2, CacheClusterMetricsMXBeanImpl.class);
CacheMetricsMXBean mxBeanCache1loc = mxBean(2, CACHE1, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache1.enableStatistics();
mxBeanCache2.disableStatistics();
assertCachesStatisticsMode(true, false);
stopGrid(1);
startGrid(3);
assertCachesStatisticsMode(true, false);
mxBeanCache1loc.disableStatistics();
assertCachesStatisticsMode(false, false);
mxBeanCache1.enableStatistics();
mxBeanCache2.enableStatistics();
// Start node 1 again.
startGrid(1);
assertCachesStatisticsMode(true, true);
int gridIdx = 0;
for (Ignite ignite : G.allGrids()) {
gridIdx++;
ignite.cache(CACHE1).put(gridIdx, gridIdx);
ignite.cache(CACHE2).put(gridIdx, gridIdx);
ignite.cache(CACHE1).get(gridIdx);
ignite.cache(CACHE2).get(gridIdx);
}
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
int cnt = G.allGrids().size();
for (Ignite ignite : G.allGrids()) {
CacheMetrics metrics1 = ignite.cache(CACHE1).metrics();
CacheMetrics metrics2 = ignite.cache(CACHE2).metrics();
if (metrics1.getCacheGets() < cnt || metrics2.getCacheGets() < cnt || metrics1.getCachePuts() < cnt || metrics2.getCachePuts() < cnt)
return false;
}
return true;
}
}, 10_000L));
stopAllGrids();
ig1 = startGrid(1);
ig1.active(true);
ig1.getOrCreateCache(cacheCfg2.setStatisticsEnabled(false));
if (persistence)
// cache1 - from local configuration, cache2 - restored from pds
assertCachesStatisticsMode(false, true);
else
assertCachesStatisticsMode(false, false);
mxBeanCache1 = mxBean(1, CACHE1, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache2 = mxBean(1, CACHE2, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache1.enableStatistics();
mxBeanCache2.disableStatistics();
startGrid(2);
assertCachesStatisticsMode(true, false);
}
use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.
the class CacheMetricsManageTest method testJmxStatisticsEnable.
/**
* @param persistence Persistence.
*/
private void testJmxStatisticsEnable(boolean persistence) throws Exception {
this.persistence = persistence;
Ignite ig1 = startGrid(1);
Ignite ig2 = startGrid(2);
ig1.cluster().active(true);
CacheConfiguration<Object, Object> ccfg = ig1.cache(CACHE1).getConfiguration(CacheConfiguration.class);
CacheConfiguration<Object, Object> cacheCfg2 = new CacheConfiguration<>(ccfg);
cacheCfg2.setName(CACHE2);
cacheCfg2.setStatisticsEnabled(true);
ig2.getOrCreateCache(cacheCfg2);
CacheMetricsMXBean mxBeanCache1 = mxBean(2, CACHE1, CacheClusterMetricsMXBeanImpl.class);
CacheMetricsMXBean mxBeanCache2 = mxBean(2, CACHE2, CacheClusterMetricsMXBeanImpl.class);
CacheMetricsMXBean mxBeanCache1loc = mxBean(2, CACHE1, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache1.enableStatistics();
mxBeanCache2.disableStatistics();
assertCachesStatisticsMode(true, false);
stopGrid(1);
startGrid(3);
assertCachesStatisticsMode(true, false);
mxBeanCache1loc.disableStatistics();
assertCachesStatisticsMode(false, false);
mxBeanCache1.enableStatistics();
mxBeanCache2.enableStatistics();
// Start node 1 again.
startGrid(1);
if (persistence)
ig2.resetLostPartitions(Arrays.asList(CACHE1, CACHE2));
assertCachesStatisticsMode(true, true);
int gridIdx = 0;
for (Ignite ignite : G.allGrids()) {
gridIdx++;
ignite.cache(CACHE1).put(gridIdx, gridIdx);
ignite.cache(CACHE2).put(gridIdx, gridIdx);
ignite.cache(CACHE1).get(gridIdx);
ignite.cache(CACHE2).get(gridIdx);
}
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
int cnt = G.allGrids().size();
for (Ignite ignite : G.allGrids()) {
CacheMetrics metrics1 = ignite.cache(CACHE1).metrics();
CacheMetrics metrics2 = ignite.cache(CACHE2).metrics();
if (metrics1.getCacheGets() < cnt || metrics2.getCacheGets() < cnt || metrics1.getCachePuts() < cnt || metrics2.getCachePuts() < cnt)
return false;
}
return true;
}
}, 10_000L));
stopAllGrids();
ig1 = startGrid(1);
ig1.cluster().active(true);
ig1.getOrCreateCache(cacheCfg2.setStatisticsEnabled(false));
if (persistence)
// Both caches restored from pds.
assertCachesStatisticsMode(true, true);
else
assertCachesStatisticsMode(false, false);
mxBeanCache1 = mxBean(1, CACHE1, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache2 = mxBean(1, CACHE2, CacheLocalMetricsMXBeanImpl.class);
mxBeanCache1.enableStatistics();
mxBeanCache2.disableStatistics();
startGrid(2);
assertCachesStatisticsMode(true, false);
}
use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.
the class CacheMetricsCacheSizeTest method testCacheSize.
/**
*/
@Test
public void testCacheSize() throws Exception {
startClientGrid(GRID_CNT);
IgniteCache cacheNode0 = grid(0).cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < ENTITIES_CNT; i++) cacheNode0.put("key-" + i, i);
GridCacheContext cacheContext = ((GatewayProtectedCacheProxy) cacheNode0).context();
CacheMetrics cacheMetric = new CacheMetricsSnapshotV2(new CacheMetricsImpl(cacheContext));
long size = cacheMetric.getCacheSize();
HashMap<Integer, CacheMetrics> cacheMetrics = new HashMap<>();
cacheMetrics.put(1, cacheMetric);
TcpDiscoveryMetricsUpdateMessage msg = new TcpDiscoveryMetricsUpdateMessage(UUID.randomUUID());
msg.setCacheMetrics(UUID.randomUUID(), cacheMetrics);
Marshaller marshaller = grid(0).context().config().getMarshaller();
byte[] buffer = marshaller.marshal(msg);
Object readObject = marshaller.unmarshal(buffer, getClass().getClassLoader());
assertTrue(readObject instanceof TcpDiscoveryMetricsUpdateMessage);
TcpDiscoveryMetricsUpdateMessage msg2 = (TcpDiscoveryMetricsUpdateMessage) readObject;
Map<Integer, CacheMetrics> cacheMetrics2 = msg2.cacheMetrics().values().iterator().next();
CacheMetrics cacheMetric2 = cacheMetrics2.values().iterator().next();
assertEquals("TcpDiscoveryMetricsUpdateMessage serialization error, cacheSize is different", size, cacheMetric2.getCacheSize());
IgniteCache cacheNode1 = grid(1).cache(DEFAULT_CACHE_NAME);
IgniteCache cacheNode2 = grid(2).cache(DEFAULT_CACHE_NAME);
IgniteCache cacheNode3 = grid(3).cache(DEFAULT_CACHE_NAME);
awaitMetricsUpdate(1);
assertEquals(ENTITIES_CNT, cacheNode0.metrics().getCacheSize());
long sizeNode0 = cacheNode0.localMetrics().getCacheSize();
assertEquals(ENTITIES_CNT, cacheNode1.metrics().getCacheSize());
long sizeNode1 = cacheNode1.localMetrics().getCacheSize();
assertEquals(ENTITIES_CNT, cacheNode2.metrics().getCacheSize());
long sizeNode2 = cacheNode2.localMetrics().getCacheSize();
assertEquals(ENTITIES_CNT, sizeNode0 + sizeNode1 + sizeNode2);
// Client metrics
assertEquals(ENTITIES_CNT, cacheNode3.metrics().getCacheSize());
}
use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.
the class CacheMetricsForClusterGroupSelfTest method assertMetrics.
/**
* @param cache Cache.
*/
private void assertMetrics(IgniteCache<Integer, Integer> cache, boolean expectNonZero) {
CacheMetrics[] ms = new CacheMetrics[GRID_CNT];
for (int i = 0; i < GRID_CNT; i++) {
CacheMetrics metrics = cache.metrics(grid(i).cluster().forCacheNodes(cache.getName()));
for (int j = 0; j < GRID_CNT; j++) ms[j] = grid(j).cache(cache.getName()).localMetrics();
// Static metrics
for (int j = 0; j < GRID_CNT; j++) assertEquals(metrics.name(), ms[j].name());
// Dynamic metrics
long sumGets = sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCacheGets();
}
}, expectNonZero);
assertEquals(metrics.getCacheGets(), sumGets);
assertEquals(cache.mxBean().getCacheGets(), sumGets);
long sumPuts = sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCachePuts();
}
}, expectNonZero);
assertEquals(metrics.getCachePuts(), sumPuts);
assertEquals(cache.mxBean().getCachePuts(), sumPuts);
long sumHits = sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getCacheHits();
}
}, expectNonZero);
assertEquals(metrics.getCacheHits(), sumHits);
assertEquals(cache.mxBean().getCacheHits(), sumHits);
if (expectNonZero) {
long sumHeapEntries = sum(ms, new IgniteClosure<CacheMetrics, Long>() {
@Override
public Long apply(CacheMetrics input) {
return input.getHeapEntriesCount();
}
}, true);
assertEquals(metrics.getHeapEntriesCount(), sumHeapEntries);
assertEquals(cache.mxBean().getHeapEntriesCount(), sumHeapEntries);
}
}
}
Aggregations