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);
}
}
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();
}
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);
}
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();
}
}
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();
}
}
Aggregations