Search in sources :

Example 6 with CacheMetricsMXBean

use of org.apache.ignite.mxbean.CacheMetricsMXBean in project ignite by apache.

the class CacheMetricsManageTest method testTxContentionMetric.

/**
 * Test correct metric for tx key contention.
 */
@Test
@WithSystemProperty(key = IGNITE_DUMP_TX_COLLISIONS_INTERVAL, value = "30000")
public void testTxContentionMetric() throws Exception {
    Assume.assumeFalse("https://issues.apache.org/jira/browse/IGNITE-9224", MvccFeatureChecker.forcedMvcc());
    backups = 1;
    useTestCommSpi = true;
    Ignite ig = startGridsMultiThreaded(2);
    int contCnt = (int) U.staticField(IgniteTxManager.class, "COLLISIONS_QUEUE_THRESHOLD") * 20;
    CountDownLatch txLatch = new CountDownLatch(contCnt * 2);
    CountDownLatch txLatch0 = new CountDownLatch(contCnt * 2);
    ig.cluster().active(true);
    client = true;
    Ignite cl = startGrid();
    CacheConfiguration<?, ?> dfltCacheCfg = getCacheConfiguration();
    dfltCacheCfg.setStatisticsEnabled(true);
    String cacheName = dfltCacheCfg.getName();
    IgniteCache<Integer, Integer> cache = ig.cache(cacheName);
    IgniteCache<Integer, Integer> cache0 = cl.cache(cacheName);
    CacheMetricsMXBean mxBeanCache = mxBean(0, cacheName, CacheLocalMetricsMXBeanImpl.class);
    final List<Integer> priKeys = primaryKeys(cache, 3, 1);
    final Integer backKey = backupKey(cache);
    IgniteTransactions txMgr = cl.transactions();
    CountDownLatch blockOnce = new CountDownLatch(1);
    for (Ignite ig0 : G.allGrids()) {
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        commSpi0.blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {

            @Override
            public boolean apply(ClusterNode node, Message msg) {
                if (msg instanceof GridNearTxPrepareResponse && blockOnce.getCount() > 0) {
                    blockOnce.countDown();
                    return true;
                }
                return false;
            }
        });
    }
    IgniteInternalFuture f = GridTestUtils.runAsync(() -> {
        try (Transaction tx = txMgr.txStart(PESSIMISTIC, READ_COMMITTED)) {
            cache0.put(priKeys.get(0), 0);
            cache0.put(priKeys.get(2), 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(PESSIMISTIC, READ_COMMITTED)) {
                cache0.put(priKeys.get(0), 0);
                cache0.put(priKeys.get(1), 0);
                txLatch0.countDown();
                tx.commit();
                txLatch.countDown();
            }
            try (Transaction tx = txMgr.txStart(PESSIMISTIC, READ_COMMITTED)) {
                cache0.put(priKeys.get(2), 0);
                cache0.put(backKey, 0);
                txLatch0.countDown();
                tx.commit();
                txLatch.countDown();
            }
        });
        finishFut.add(f0);
    }
    finishFut.markInitialized();
    txLatch0.await();
    for (Ignite ig0 : G.allGrids()) {
        TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ig0.configuration().getCommunicationSpi();
        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());
            }
            String coll = mxBeanCache.getTxKeyCollisions();
            if (coll.contains("val=" + priKeys.get(2)) || coll.contains("val=" + priKeys.get(0)))
                ;
            return true;
        }
    }, 10_000));
    f.get();
    finishFut.get();
    txLatch.await();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Message(org.apache.ignite.plugin.extensions.communication.Message) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CacheMetricsMXBean(org.apache.ignite.mxbean.CacheMetricsMXBean) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Transaction(org.apache.ignite.transactions.Transaction) GridNearTxPrepareResponse(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareResponse) Ignite(org.apache.ignite.Ignite) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty)

Aggregations

CacheMetricsMXBean (org.apache.ignite.mxbean.CacheMetricsMXBean)6 Ignite (org.apache.ignite.Ignite)4 Test (org.junit.Test)4 CacheMetrics (org.apache.ignite.cache.CacheMetrics)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteTransactions (org.apache.ignite.IgniteTransactions)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)2 IgniteTxManager (org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager)2 GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)2 Message (org.apache.ignite.plugin.extensions.communication.Message)2 Transaction (org.apache.ignite.transactions.Transaction)2 Path (java.nio.file.Path)1