Search in sources :

Example 1 with CacheMetrics

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();
            }
        }));
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) IgniteClosure(org.apache.ignite.lang.IgniteClosure)

Example 2 with CacheMetrics

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);
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheMetricsMXBean(org.apache.ignite.mxbean.CacheMetricsMXBean) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 3 with CacheMetrics

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);
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheMetricsMXBean(org.apache.ignite.mxbean.CacheMetricsMXBean) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with CacheMetrics

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());
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) Marshaller(org.apache.ignite.marshaller.Marshaller) HashMap(java.util.HashMap) IgniteCache(org.apache.ignite.IgniteCache) TcpDiscoveryMetricsUpdateMessage(org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryMetricsUpdateMessage) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with CacheMetrics

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);
        }
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics)

Aggregations

CacheMetrics (org.apache.ignite.cache.CacheMetrics)30 Test (org.junit.Test)15 Ignite (org.apache.ignite.Ignite)9 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)8 IgniteCache (org.apache.ignite.IgniteCache)5 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 Transaction (org.apache.ignite.transactions.Transaction)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteClusterNode (org.apache.ignite.internal.managers.discovery.IgniteClusterNode)3 CacheMetricsMXBean (org.apache.ignite.mxbean.CacheMetricsMXBean)3 TransactionMetrics (org.apache.ignite.transactions.TransactionMetrics)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IgniteSystemProperties.getInteger (org.apache.ignite.IgniteSystemProperties.getInteger)2 IgniteTransactions (org.apache.ignite.IgniteTransactions)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)2 GridCacheTransactionalAbstractMetricsSelfTest (org.apache.ignite.internal.processors.cache.GridCacheTransactionalAbstractMetricsSelfTest)2