Search in sources :

Example 26 with CacheMetrics

use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.

the class CacheMetricsEntitiesCountTest method checkCacheClusterMetrics.

/**
 * @param cacheIdx Cache index.
 */
private void checkCacheClusterMetrics(int cacheIdx, long cacheSize, long offHeapEntriesCnt, long offHeapPrimaryEntriesCnt, long offHeapBackupEntriesCnt, long heapEntriesCnt) {
    long cacheSizeSum = 0;
    long offHeapEntriesCntSum = 0;
    long offHeapPrimaryEntriesCntSum = 0;
    long offHeapBackupEntriesCntSum = 0;
    long heapEntriesCntSum = 0;
    boolean isEmptySum = true;
    for (int igniteIdx = 0; igniteIdx < GRID_CNT; igniteIdx++) {
        IgniteCache cache = grid(igniteIdx).cache(CACHE_PREFIX + cacheIdx);
        CacheMetrics metrics = cache.metrics();
        String cacheInfo = "igniteIdx=" + igniteIdx + ", cacheIdx=" + cacheIdx + " ";
        assertEquals(cacheInfo + " CacheSize", cacheSize, metrics.getCacheSize());
        assertEquals(cacheInfo + " offHeapEntriesCnt", offHeapEntriesCnt, metrics.getOffHeapEntriesCount());
        assertEquals(cacheInfo + " offHeapBackupEntriesCnt", offHeapBackupEntriesCnt, metrics.getOffHeapBackupEntriesCount());
        assertEquals(cacheInfo + " offHeapPrimaryEntriesCnt", offHeapPrimaryEntriesCnt, metrics.getOffHeapPrimaryEntriesCount());
        if (// Onheap cache is not supported in Mvcc mode.
        !MvccFeatureChecker.forcedMvcc())
            assertEquals(cacheInfo + " heapEntriesCnt", heapEntriesCnt, metrics.getHeapEntriesCount());
        assertEquals(cacheInfo + " size", cacheSize, metrics.getSize());
        assertEquals(cacheInfo + " keySize", cacheSize, metrics.getKeySize());
        assertEquals(cacheInfo + " isEmpty", cacheSize == 0, metrics.isEmpty());
        metrics = cache.localMetrics();
        cacheSizeSum += metrics.getCacheSize();
        offHeapEntriesCntSum += metrics.getOffHeapEntriesCount();
        offHeapPrimaryEntriesCntSum += metrics.getOffHeapPrimaryEntriesCount();
        offHeapBackupEntriesCntSum += metrics.getOffHeapBackupEntriesCount();
        heapEntriesCntSum += metrics.getHeapEntriesCount();
        isEmptySum = isEmptySum && metrics.isEmpty();
    }
    String cacheInfo = "cacheIdx=" + cacheIdx + " check sum";
    assertEquals(cacheInfo + " CacheSize", cacheSize, cacheSizeSum);
    assertEquals(cacheInfo + " offHeapEntriesCnt", offHeapEntriesCnt, offHeapEntriesCntSum);
    assertEquals(cacheInfo + " offHeapBackupEntriesCnt", offHeapBackupEntriesCnt, offHeapBackupEntriesCntSum);
    assertEquals(cacheInfo + " offHeapPrimaryEntriesCnt", offHeapPrimaryEntriesCnt, offHeapPrimaryEntriesCntSum);
    if (// Onheap cache is not supported in Mvcc mode.
    !MvccFeatureChecker.forcedMvcc())
        assertEquals(cacheInfo + " heapEntriesCnt", heapEntriesCnt, heapEntriesCntSum);
    assertEquals(cacheInfo + " isEmpty", cacheSize == 0, isEmptySum);
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) IgniteCache(org.apache.ignite.IgniteCache)

Example 27 with CacheMetrics

use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.

the class GridCacheTransactionalAbstractMetricsSelfTest method testCommits.

/**
 * @param concurrency Concurrency control.
 * @param isolation Isolation level.
 * @param put Put some data if {@code true}.
 * @throws Exception If failed.
 */
private void testCommits(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean put) throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < TX_CNT; i++) {
        Transaction tx = grid(0).transactions().txStart(concurrency, isolation);
        if (put)
            for (int j = 0; j < keyCount(); j++) cache.put(j, j);
        // Waiting 30 ms for metrics. U.currentTimeMillis() method has 10 ms discretization.
        U.sleep(30);
        tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        TransactionMetrics metrics = grid(i).transactions().metrics();
        CacheMetrics cacheMetrics = grid(i).cache(DEFAULT_CACHE_NAME).localMetrics();
        if (i == 0) {
            assertEquals(TX_CNT, metrics.txCommits());
            if (put) {
                assertEquals(TX_CNT, cacheMetrics.getCacheTxCommits());
                // Expected metric value should be in microseconds.
                assert cacheMetrics.getAverageTxCommitTime() > 1000 : cacheMetrics.getAverageTxCommitTime();
            }
        } else {
            assertEquals(0, metrics.txCommits());
            assertEquals(0, cacheMetrics.getCacheTxCommits());
        }
        assertEquals(0, metrics.txRollbacks());
        assertEquals(0, cacheMetrics.getCacheTxRollbacks());
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) Transaction(org.apache.ignite.transactions.Transaction) TransactionMetrics(org.apache.ignite.transactions.TransactionMetrics)

Example 28 with CacheMetrics

use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.

the class GridCachePartitionedHitsAndMissesSelfTest method testHitsAndMisses.

/**
 * This test is just a wrapper for org.apache.ignite.examples.datagrid.CachePopularNumbersExample
 *
 * @throws Exception If failed.
 */
@Test
public void testHitsAndMisses() throws Exception {
    MvccFeatureChecker.skipIfNotSupported(MvccFeatureChecker.Feature.METRICS);
    startGrids(GRID_CNT);
    awaitPartitionMapExchange();
    try {
        final Ignite g = grid(0);
        realTimePopulate(g);
        // Check metrics for the whole cache.
        long hits = 0;
        long misses = 0;
        for (int i = 0; i < GRID_CNT; i++) {
            CacheMetrics m = grid(i).cache(DEFAULT_CACHE_NAME).localMetrics();
            hits += m.getCacheHits();
            misses += m.getCacheMisses();
        }
        // Check that invoke and loader updated metrics
        assertEquals(CNT / 2, hits);
        assertEquals(CNT / 2, misses);
    } finally {
        stopAllGrids();
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 29 with CacheMetrics

use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.

the class TxWithKeyContentionSelfTest method runKeyCollisionsMetric.

/**
 * Tests metric correct results while tx collisions occured.
 *
 * @param concurrency Concurrency level.
 * @param isolation Isolation level.
 * @throws Exception If failed.
 */
private void runKeyCollisionsMetric(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
    if (MvccFeatureChecker.forcedMvcc())
        // Not supported.
        return;
    Ignite ig = startGridsMultiThreaded(3);
    int contCnt = (int) U.staticField(IgniteTxManager.class, "COLLISIONS_QUEUE_THRESHOLD") * 5;
    CountDownLatch txLatch = new CountDownLatch(contCnt);
    ig.cluster().active(true);
    client = true;
    Ignite cl = startGrid();
    IgniteTransactions txMgr = cl.transactions();
    IgniteCache<Integer, Integer> cache = ig.cache(DEFAULT_CACHE_NAME);
    IgniteCache<Integer, Integer> cache0 = cl.cache(DEFAULT_CACHE_NAME);
    final Integer keyId = primaryKey(cache);
    CountDownLatch blockOnce = new CountDownLatch(1);
    for (Ignite ig0 : G.allGrids()) {
        if (ig0.configuration().isClientMode())
            continue;
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        commSpi0.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridNearTxFinishResponse && blockOnce.getCount() > 0) {
                    blockOnce.countDown();
                    return true;
                }
                return false;
            }
        });
    }
    IgniteInternalFuture f = GridTestUtils.runAsync(() -> {
        try (Transaction tx = txMgr.txStart(concurrency, isolation)) {
            cache0.put(keyId, 0);
            tx.commit();
        }
    });
    blockOnce.await();
    GridCompoundFuture<?, ?> finishFut = new GridCompoundFuture<>();
    for (int i = 0; i < contCnt; ++i) {
        IgniteInternalFuture f0 = GridTestUtils.runAsync(() -> {
            try (Transaction tx = txMgr.txStart(concurrency, isolation)) {
                cache0.put(keyId, 0);
                tx.commit();
                txLatch.countDown();
            }
        });
        finishFut.add(f0);
    }
    finishFut.markInitialized();
    for (Ignite ig0 : G.allGrids()) {
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        if (ig0.configuration().isClientMode())
            continue;
        commSpi0.stopBlock();
    }
    IgniteTxManager txManager = ((IgniteEx) ig).context().cache().context().tm();
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                U.invoke(IgniteTxManager.class, txManager, "collectTxCollisionsInfo");
            } catch (IgniteCheckedException e) {
                fail(e.toString());
            }
            CacheMetrics metrics = ig.cache(DEFAULT_CACHE_NAME).localMetrics();
            String coll1 = metrics.getTxKeyCollisions();
            if (!coll1.isEmpty()) {
                String coll2 = metrics.getTxKeyCollisions();
                // check idempotent
                assertEquals(coll1, coll2);
                assertTrue(coll1.contains("queueSize"));
                return true;
            } else
                return false;
        }
    }, 10_000));
    f.get();
    finishFut.get();
    txLatch.await();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheMetrics(org.apache.ignite.cache.CacheMetrics) Message(org.apache.ignite.plugin.extensions.communication.Message) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) GridNearTxFinishResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishResponse) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 30 with CacheMetrics

use of org.apache.ignite.cache.CacheMetrics in project ignite by apache.

the class GridCacheAbstractMetricsSelfTest method testGetMetricsDisable.

/**
 * @throws Exception If failed.
 */
@Test
public void testGetMetricsDisable() throws Exception {
    // Disable statistics.
    for (int i = 0; i < gridCount(); i++) {
        Ignite g = grid(i);
        IgniteCache cache = g.cache(DEFAULT_CACHE_NAME);
        cache.enableStatistics(false);
    }
    IgniteCache<Object, Object> jcache = grid(0).cache(DEFAULT_CACHE_NAME);
    // Write to cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.put(i, i);
    // Invoke update on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            Object key = entry.getKey();
            entry.setValue(key);
            return null;
        }
    });
    // Read-only invoke on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            entry.getKey();
            return null;
        }
    });
    // Remove invoke on cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.invoke(i, new CacheEntryProcessor<Object, Object, Object>() {

        @Override
        public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
            entry.remove();
            return null;
        }
    });
    // Get from cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.get(i);
    // Remove from cache.
    for (int i = 0; i < KEY_CNT; i++) jcache.remove(i);
    // Assert that statistics is clear.
    for (int i = 0; i < gridCount(); i++) {
        CacheMetrics m = grid(i).cache(DEFAULT_CACHE_NAME).localMetrics();
        assertEquals(m.getCacheGets(), 0);
        assertEquals(m.getCachePuts(), 0);
        assertEquals(m.getCacheRemovals(), 0);
        assertEquals(m.getCacheHits(), 0);
        assertEquals(m.getCacheMisses(), 0);
        assertEquals(m.getAverageGetTime(), 0f);
        assertEquals(m.getAverageRemoveTime(), 0f);
        assertEquals(m.getAveragePutTime(), 0f);
        assertEquals(m.getAverageTxCommitTime(), 0f);
        assertEquals(m.getAverageTxRollbackTime(), 0f);
        assertEquals(m.getEntryProcessorPuts(), 0);
        assertEquals(m.getEntryProcessorRemovals(), 0);
        assertEquals(m.getEntryProcessorReadOnlyInvocations(), 0);
        assertEquals(m.getEntryProcessorMinInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorMaxInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorAverageInvocationTime(), 0f);
        assertEquals(m.getEntryProcessorInvocations(), 0);
    }
}
Also used : CacheMetrics(org.apache.ignite.cache.CacheMetrics) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) Test(org.junit.Test)

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