Search in sources :

Example 6 with IntMetric

use of org.apache.ignite.spi.metric.IntMetric in project ignite by apache.

the class CdcCacheVersionTest method testConflictVersionWritten.

/**
 * Test that conflict version is writtern to WAL.
 */
@Test
public void testConflictVersionWritten() throws Exception {
    walProvider = (ctx) -> new FileWriteAheadLogManager(ctx) {

        @Override
        public WALPointer log(WALRecord rec) throws IgniteCheckedException {
            if (rec.type() != DATA_RECORD_V2)
                return super.log(rec);
            DataRecord dataRec = (DataRecord) rec;
            for (int i = 0; i < dataRec.entryCount(); i++) {
                DataEntry dataEntry = dataRec.writeEntries().get(i);
                assertEquals(CU.cacheId(DEFAULT_CACHE_NAME), dataEntry.cacheId());
                assertEquals(DFLT_CLUSTER_ID, dataEntry.writeVersion().dataCenterId());
                assertNotNull(dataEntry.writeVersion().conflictVersion());
                assertEquals(OTHER_CLUSTER_ID, dataEntry.writeVersion().conflictVersion().dataCenterId());
                walRecCheckedCntr.incrementAndGet();
            }
            return super.log(rec);
        }
    };
    conflictResolutionMgrSupplier = () -> new CacheVersionConflictResolver() {

        @Override
        public <K1, V1> GridCacheVersionConflictContext<K1, V1> resolve(CacheObjectValueContext ctx, GridCacheVersionedEntryEx<K1, V1> oldEntry, GridCacheVersionedEntryEx<K1, V1> newEntry, boolean atomicVerComparator) {
            GridCacheVersionConflictContext<K1, V1> res = new GridCacheVersionConflictContext<>(ctx, oldEntry, newEntry);
            res.useNew();
            assertEquals(OTHER_CLUSTER_ID, newEntry.version().dataCenterId());
            if (!oldEntry.isStartVersion())
                assertEquals(OTHER_CLUSTER_ID, oldEntry.version().dataCenterId());
            conflictCheckedCntr.incrementAndGet();
            return res;
        }

        @Override
        public String toString() {
            return "TestCacheConflictResolutionManager";
        }
    };
    startGrids(gridCnt);
    IgniteEx cli = startClientGrid(gridCnt);
    for (int i = 0; i < gridCnt; i++) {
        grid(i).context().cache().context().versions().dataCenterId(DFLT_CLUSTER_ID);
        assertEquals(DFLT_CLUSTER_ID, grid(i).context().metric().registry(CACHE_METRICS).<IntMetric>findMetric(DATA_VER_CLUSTER_ID).value());
    }
    cli.cluster().state(ACTIVE);
    IgniteCache<Integer, User> cache = cli.getOrCreateCache(new CacheConfiguration<Integer, User>(DEFAULT_CACHE_NAME).setCacheMode(cacheMode).setAtomicityMode(atomicityMode).setBackups(Integer.MAX_VALUE));
    if (atomicityMode == ATOMIC)
        putRemoveCheck(cli, cache, null, null);
    else {
        // Check operations for transaction cache without explicit transaction.
        putRemoveCheck(cli, cache, null, null);
        // Check operations for transaction cache with explicit transaction in all modes.
        for (TransactionConcurrency concurrency : TransactionConcurrency.values()) for (TransactionIsolation isolation : TransactionIsolation.values()) putRemoveCheck(cli, cache, concurrency, isolation);
    }
    for (int i = 0; i < gridCnt; i++) {
        boolean dfltCacheFound = false;
        assertFalse(grid(i).context().clientNode());
        SystemView<CacheView> caches = grid(i).context().systemView().view(CACHES_VIEW);
        for (CacheView v : caches) {
            if (v.cacheName().equals(DEFAULT_CACHE_NAME)) {
                assertEquals(v.conflictResolver(), "TestCacheConflictResolutionManager");
                dfltCacheFound = true;
            } else
                assertNull(v.conflictResolver());
        }
        assertTrue(dfltCacheFound);
    }
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheView(org.apache.ignite.spi.systemview.view.CacheView) CacheObjectValueContext(org.apache.ignite.internal.processors.cache.CacheObjectValueContext) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) IntMetric(org.apache.ignite.spi.metric.IntMetric) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CacheVersionConflictResolver(org.apache.ignite.internal.processors.cache.version.CacheVersionConflictResolver) GridCacheVersionConflictContext(org.apache.ignite.internal.processors.cache.version.GridCacheVersionConflictContext) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) IgniteEx(org.apache.ignite.internal.IgniteEx) FileWriteAheadLogManager(org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager) Test(org.junit.Test)

Example 7 with IntMetric

use of org.apache.ignite.spi.metric.IntMetric in project ignite by apache.

the class TransactionMetricsTest method testTxMetric.

/**
 */
@Test
public void testTxMetric() throws Exception {
    // given:
    int keysNumber = 10;
    IgniteEx ignite = startGrid(0);
    startGrid(1);
    IgniteEx client = startClientGrid(getConfiguration(getTestIgniteInstanceName(2)));
    awaitPartitionMapExchange();
    TransactionMetricsMxBean txMXBean = getMxBean(getTestIgniteInstanceName(0), "TransactionMetrics", TransactionMetricsMxBeanImpl.class, TransactionMetricsMxBean.class);
    MetricRegistry mreg = grid(0).context().metric().registry(TX_METRICS);
    final IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
    // when: one transaction commit
    ignite.transactions().txStart().commit();
    // then:
    assertEquals(1, txMXBean.getTransactionsCommittedNumber());
    assertEquals(1, mreg.<IntMetric>findMetric("txCommits").value());
    // when: transaction is opening
    final Transaction tx1 = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
    int localKeysNum = 0;
    for (int i = 0; i < keysNumber; i++) {
        cache.put(i, "");
        if (affinity(cache).isPrimary(ignite.localNode(), i))
            localKeysNum++;
    }
    // then:
    assertEquals(localKeysNum, mreg.<LongMetric>findMetric("LockedKeysNumber").value());
    assertEquals(1, mreg.<LongMetric>findMetric("TransactionsHoldingLockNumber").value());
    assertEquals(1, mreg.<LongMetric>findMetric("OwnerTransactionsNumber").value());
    // when: transaction rollback
    tx1.rollback();
    // then:
    assertEquals(1, txMXBean.getTransactionsRolledBackNumber());
    assertEquals(1, mreg.<IntMetric>findMetric("txRollbacks").value());
    assertEquals(0, mreg.<LongMetric>findMetric("LockedKeysNumber").value());
    assertEquals(0, mreg.<LongMetric>findMetric("TransactionsHoldingLockNumber").value());
    assertEquals(0, mreg.<LongMetric>findMetric("OwnerTransactionsNumber").value());
    // when: keysNumber transactions from owner node + keysNumber transactions from client.
    CountDownLatch commitAllower = new CountDownLatch(1);
    CountDownLatch transactionStarter = new CountDownLatch(keysNumber + keysNumber);
    int txNumFromOwner = 0;
    for (int i = 0; i < keysNumber; i++) {
        new Thread(new TxThread(commitAllower, transactionStarter, ignite, i, i)).start();
        if (affinity(cache).isPrimary(ignite.localNode(), i))
            txNumFromOwner++;
    }
    int txNumFromClient = 0;
    for (int i = keysNumber; i < keysNumber * 2; i++) {
        new Thread(new TxThread(commitAllower, transactionStarter, client, i, i)).start();
        if (affinity(cache).isPrimary(ignite.localNode(), i))
            txNumFromClient++;
    }
    transactionStarter.await();
    // then:
    assertEquals(txNumFromOwner + txNumFromClient, mreg.<LongMetric>findMetric("LockedKeysNumber").value());
    assertEquals(keysNumber + txNumFromClient, mreg.<LongMetric>findMetric("TransactionsHoldingLockNumber").value());
    assertEquals(keysNumber, mreg.<LongMetric>findMetric("OwnerTransactionsNumber").value());
    commitAllower.countDown();
}
Also used : MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LongMetric(org.apache.ignite.spi.metric.LongMetric) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionMetricsMxBean(org.apache.ignite.mxbean.TransactionMetricsMxBean) Transaction(org.apache.ignite.transactions.Transaction) IntMetric(org.apache.ignite.spi.metric.IntMetric) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with IntMetric

use of org.apache.ignite.spi.metric.IntMetric in project ignite by apache.

the class ClientListenerMetricsTest method waitForMetricValue.

/**
 * Wait for specific metric to change
 * @param mreg Metric registry.
 * @param metric Metric to check.
 * @param value Metric value to wait for.
 * @param timeout Timeout.
 */
private void waitForMetricValue(MetricRegistry mreg, String metric, long value, long timeout) throws IgniteInterruptedCheckedException {
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return mreg.<IntMetric>findMetric(metric).value() == value;
        }
    }, timeout);
    assertEquals(mreg.<IntMetric>findMetric(metric).value(), value);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IntMetric(org.apache.ignite.spi.metric.IntMetric)

Example 9 with IntMetric

use of org.apache.ignite.spi.metric.IntMetric in project ignite by apache.

the class OpenCensusMetricExporterSpi method export.

/**
 * {@inheritDoc}
 */
@Override
public void export() {
    StatsRecorder recorder = Stats.getStatsRecorder();
    try (Scope globalScope = tagScope()) {
        MeasureMap mmap = recorder.newMeasureMap();
        mreg.forEach(mreg -> {
            if (filter != null && !filter.test(mreg))
                return;
            mreg.forEach(metric -> {
                if (metric instanceof LongMetric || metric instanceof IntMetric || metric instanceof BooleanMetric || (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == Date.class) || (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == OffsetDateTime.class)) {
                    long val;
                    if (metric instanceof LongMetric)
                        val = ((LongMetric) metric).value();
                    else if (metric instanceof IntMetric)
                        val = ((IntMetric) metric).value();
                    else if (metric instanceof BooleanMetric)
                        val = ((BooleanMetric) metric).value() ? 1 : 0;
                    else if (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == Date.class)
                        val = ((ObjectMetric<Date>) metric).value().getTime();
                    else
                        val = ((ObjectMetric<OffsetDateTime>) metric).value().toInstant().toEpochMilli();
                    if (val < 0) {
                        if (log.isDebugEnabled())
                            log.debug("OpenCensus doesn't support negative values. Skip record of " + metric.name());
                        return;
                    }
                    MeasureLong msr = (MeasureLong) measures.computeIfAbsent(metric.name(), k -> createMeasure(metric, CREATE_LONG));
                    mmap.put(msr, val);
                } else if (metric instanceof DoubleMetric) {
                    double val = ((DoubleMetric) metric).value();
                    if (val < 0) {
                        if (log.isDebugEnabled())
                            log.debug("OpenCensus doesn't support negative values. Skip record of " + metric.name());
                        return;
                    }
                    MeasureDouble msr = (MeasureDouble) measures.computeIfAbsent(metric.name(), k -> createMeasure(metric, CREATE_DOUBLE));
                    mmap.put(msr, val);
                } else if (metric instanceof HistogramMetric) {
                    String[] names = histogramBucketNames((HistogramMetric) metric);
                    long[] vals = ((HistogramMetric) metric).value();
                    assert names.length == vals.length;
                    for (int i = 0; i < vals.length; i++) {
                        String name = names[i];
                        MeasureLong msr = (MeasureLong) measures.computeIfAbsent(name, k -> createMeasureLong(name, metric.description()));
                        mmap.put(msr, vals[i]);
                    }
                } else if (log.isDebugEnabled()) {
                    log.debug(metric.name() + "[" + metric.getClass() + "] not supported by Opencensus exporter");
                }
            });
        });
        mmap.record();
    }
}
Also used : StatsRecorder(io.opencensus.stats.StatsRecorder) TagContextBuilder(io.opencensus.tags.TagContextBuilder) StatsRecorder(io.opencensus.stats.StatsRecorder) IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) Metric(org.apache.ignite.spi.metric.Metric) Stats(io.opencensus.stats.Stats) Date(java.util.Date) HashMap(java.util.HashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) Function(java.util.function.Function) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) ArrayList(java.util.ArrayList) PushMetricsExporterAdapter(org.apache.ignite.internal.processors.metric.PushMetricsExporterAdapter) Measure(io.opencensus.stats.Measure) MeasureDouble(io.opencensus.stats.Measure.MeasureDouble) MeasureMap(io.opencensus.stats.MeasureMap) Map(java.util.Map) TagMetadata(io.opencensus.tags.TagMetadata) TagValue(io.opencensus.tags.TagValue) View(io.opencensus.stats.View) ReadOnlyMetricManager(org.apache.ignite.spi.metric.ReadOnlyMetricManager) Tags(io.opencensus.tags.Tags) BooleanMetric(org.apache.ignite.spi.metric.BooleanMetric) HistogramMetric(org.apache.ignite.spi.metric.HistogramMetric) IntMetric(org.apache.ignite.spi.metric.IntMetric) Scope(io.opencensus.common.Scope) Name(io.opencensus.stats.View.Name) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LastValue(io.opencensus.stats.Aggregation.LastValue) MeasureLong(io.opencensus.stats.Measure.MeasureLong) T2(org.apache.ignite.internal.util.typedef.T2) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) TagKey(io.opencensus.tags.TagKey) MetricUtils(org.apache.ignite.internal.processors.metric.impl.MetricUtils) UNLIMITED_PROPAGATION(io.opencensus.tags.TagMetadata.TagTtl.UNLIMITED_PROPAGATION) GridMetricManager(org.apache.ignite.internal.processors.metric.GridMetricManager) LongMetric(org.apache.ignite.spi.metric.LongMetric) DoubleMetric(org.apache.ignite.spi.metric.DoubleMetric) BooleanMetric(org.apache.ignite.spi.metric.BooleanMetric) HistogramMetric(org.apache.ignite.spi.metric.HistogramMetric) LongMetric(org.apache.ignite.spi.metric.LongMetric) MeasureDouble(io.opencensus.stats.Measure.MeasureDouble) MeasureMap(io.opencensus.stats.MeasureMap) Scope(io.opencensus.common.Scope) OffsetDateTime(java.time.OffsetDateTime) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) MeasureLong(io.opencensus.stats.Measure.MeasureLong) DoubleMetric(org.apache.ignite.spi.metric.DoubleMetric) IntMetric(org.apache.ignite.spi.metric.IntMetric)

Example 10 with IntMetric

use of org.apache.ignite.spi.metric.IntMetric in project ignite by apache.

the class TcpDiscoverySpiMBeanTest method testMBean.

/**
 * Tests TcpDiscoverySpiMBean#getCurrentTopologyVersion() and TcpDiscoverySpiMBean#dumpRingStructure().
 *
 * @throws Exception if fails.
 */
@Test
public void testMBean() throws Exception {
    int cnt = 3;
    int cliIdx = cnt - 1;
    startGrids(cnt - 1);
    startClientGrid(cliIdx);
    ClusterNode crd = U.oldest(grid(0).context().discovery().aliveServerNodes(), null);
    assertNotNull(crd);
    try {
        for (int i = 0; i < cnt; i++) {
            IgniteEx grid = grid(i);
            MetricRegistry discoReg = grid.context().metric().registry(DISCO_METRICS);
            TcpDiscoverySpiMBean bean = getMxBean(grid.context().igniteInstanceName(), "SPIs", TcpDiscoverySpi.class, TcpDiscoverySpiMBean.class);
            assertNotNull(bean);
            assertEquals(grid.cluster().topologyVersion(), bean.getCurrentTopologyVersion());
            assertEquals(grid.cluster().topologyVersion(), discoReg.<LongMetric>findMetric("CurrentTopologyVersion").value());
            if (i != cliIdx) {
                assertEquals(crd.id(), bean.getCoordinator());
                assertEquals(crd.id(), discoReg.<ObjectMetric<UUID>>findMetric("Coordinator").value());
            } else {
                assertNull(bean.getCoordinator());
                assertNull(discoReg.findMetric("Coordinator"));
            }
            if (grid.localNode().id().equals(bean.getCoordinator())) {
                assertTrue(bean.getCoordinatorSinceTimestamp() > 0);
                assertTrue(discoReg.<LongMetric>findMetric("CoordinatorSince").value() > 0);
            } else {
                assertEquals(0, bean.getCoordinatorSinceTimestamp());
                if (i == cliIdx)
                    assertNull(discoReg.findMetric("CoordinatorSince"));
                else
                    assertEquals(0L, discoReg.<LongMetric>findMetric("CoordinatorSince").value());
            }
            // `getNodesJoined` returns count of joined nodes since local node startup.
            assertEquals((cnt - 1) - i, bean.getNodesJoined());
            assertEquals((cnt - 1) - i, discoReg.<IntMetric>findMetric("JoinedNodes").value());
            assertEquals(0L, bean.getNodesFailed());
            assertEquals(0, discoReg.<IntMetric>findMetric("FailedNodes").value());
            assertEquals(0L, bean.getNodesLeft());
            assertEquals(0, discoReg.<IntMetric>findMetric("LeftNodes").value());
            assertTrue(bean.getTotalReceivedMessages() > 0);
            assertTrue(discoReg.<IntMetric>findMetric("TotalReceivedMessages").value() > 0);
            assertTrue(bean.getTotalProcessedMessages() > 0);
            assertTrue(discoReg.<IntMetric>findMetric("TotalProcessedMessages").value() > 0);
            if (i != cliIdx) {
                assertTrue(bean.getPendingMessagesRegistered() > 0);
                assertTrue(discoReg.<IntMetric>findMetric("PendingMessagesRegistered").value() > 0);
            } else {
                assertEquals(0, bean.getPendingMessagesRegistered());
                assertEquals(0, discoReg.<IntMetric>findMetric("PendingMessagesRegistered").value());
            }
            assertEquals(0, bean.getPendingMessagesDiscarded());
            bean.dumpRingStructure();
            assertTrue(strLog.toString().contains("TcpDiscoveryNodesRing"));
            assertFalse(bean.getProcessedMessages().isEmpty());
            assertFalse(bean.getReceivedMessages().isEmpty());
            assertTrue(bean.getMaxMessageProcessingTime() >= 0);
            assertEquals(i == cliIdx, bean.isClientMode());
        }
        stopGrid(0);
        crd = U.oldest(grid(1).context().discovery().aliveServerNodes(), null);
        for (int i = 1; i < cnt; i++) {
            IgniteEx grid = grid(i);
            MetricRegistry discoReg = grid.context().metric().registry(DISCO_METRICS);
            TcpDiscoverySpiMBean bean = getMxBean(grid.context().igniteInstanceName(), "SPIs", TcpDiscoverySpi.class, TcpDiscoverySpiMBean.class);
            assertNotNull(bean);
            assertEquals(grid.cluster().topologyVersion(), bean.getCurrentTopologyVersion());
            assertEquals(grid.cluster().topologyVersion(), discoReg.<LongMetric>findMetric("CurrentTopologyVersion").value());
            if (i != cliIdx) {
                assertEquals(crd.id(), bean.getCoordinator());
                assertEquals(crd.id(), discoReg.<ObjectMetric<UUID>>findMetric("Coordinator").value());
            }
            if (grid.localNode().id().equals(crd.id())) {
                assertTrue(bean.getCoordinatorSinceTimestamp() > 0);
                assertTrue(discoReg.<LongMetric>findMetric("CoordinatorSince").value() > 0);
            }
            assertTrue(waitForCondition(() -> bean.getNodesLeft() == 1 && discoReg.<IntMetric>findMetric("LeftNodes").value() == 1, getTestTimeout()));
        }
    } finally {
        stopAllGrids();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) IgniteEx(org.apache.ignite.internal.IgniteEx) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LongMetric(org.apache.ignite.spi.metric.LongMetric) IntMetric(org.apache.ignite.spi.metric.IntMetric) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

IntMetric (org.apache.ignite.spi.metric.IntMetric)12 Test (org.junit.Test)10 MetricRegistry (org.apache.ignite.internal.processors.metric.MetricRegistry)9 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)9 IgniteEx (org.apache.ignite.internal.IgniteEx)6 LongMetric (org.apache.ignite.spi.metric.LongMetric)5 List (java.util.List)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterNode (org.apache.ignite.cluster.ClusterNode)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 BooleanMetric (org.apache.ignite.spi.metric.BooleanMetric)3 ObjectMetric (org.apache.ignite.spi.metric.ObjectMetric)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 GridDhtPartitionSupplyMessage (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionSupplyMessage)2 Message (org.apache.ignite.plugin.extensions.communication.Message)2 Scope (io.opencensus.common.Scope)1