use of org.apache.ignite.internal.processors.cache.GridCacheAdapter in project ignite by apache.
the class AbstractReadRepairTest method setDifferentValuesForSameKey.
/**
*/
private InconsistentMapping setDifferentValuesForSameKey(int key, boolean misses, boolean nulls, ReadRepairStrategy strategy) throws Exception {
List<Ignite> nodes = new ArrayList<>();
Map<Ignite, T2<Integer, GridCacheVersion>> mapping = new HashMap<>();
Ignite primary = primaryNode(key, DEFAULT_CACHE_NAME);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
if (rnd.nextBoolean()) {
// Reversed order.
nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
nodes.add(primary);
} else {
nodes.add(primary);
nodes.addAll(backupNodes(key, DEFAULT_CACHE_NAME));
}
if (// Random order.
rnd.nextBoolean())
Collections.shuffle(nodes);
IgniteInternalCache<Integer, Integer> internalCache = (grid(1)).cachex(DEFAULT_CACHE_NAME);
GridCacheVersionManager mgr = ((GridCacheAdapter) internalCache.cache()).context().shared().versions();
int incVal = 0;
Integer primVal = null;
Collection<T2<Integer, GridCacheVersion>> vals = new ArrayList<>();
if (misses) {
List<Ignite> keeped = nodes.subList(0, rnd.nextInt(1, nodes.size()));
nodes.stream().filter(node -> !keeped.contains(node)).forEach(node -> {
T2<Integer, GridCacheVersion> nullT2 = new T2<>(null, null);
vals.add(nullT2);
mapping.put(node, nullT2);
});
// Recording nulls (missed values).
nodes = keeped;
}
boolean rmvd = false;
boolean incVer = rnd.nextBoolean();
GridCacheVersion ver = null;
for (Ignite node : nodes) {
IgniteInternalCache<Integer, Integer> cache = ((IgniteEx) node).cachex(DEFAULT_CACHE_NAME);
GridCacheAdapter<Integer, Integer> adapter = (GridCacheAdapter) cache.cache();
GridCacheEntryEx entry = adapter.entryEx(key);
if (ver == null || incVer)
// Incremental version.
ver = mgr.next(entry.context().kernalContext().discovery().topologyVersion());
boolean rmv = nulls && (!rmvd || rnd.nextBoolean());
Integer val = rmv ? null : rnd.nextBoolean() ? /*increment or same as previously*/
++incVal : incVal;
T2<Integer, GridCacheVersion> valVer = new T2<>(val, val != null ? ver : null);
vals.add(valVer);
mapping.put(node, valVer);
GridKernalContext kctx = ((IgniteEx) node).context();
// Incremental value.
byte[] bytes = kctx.cacheObjects().marshal(entry.context().cacheObjectContext(), rmv ? -1 : val);
try {
kctx.cache().context().database().checkpointReadLock();
boolean init = entry.initialValue(new CacheObjectImpl(null, bytes), ver, 0, 0, false, AffinityTopologyVersion.NONE, GridDrType.DR_NONE, false, false);
if (rmv) {
if (cache.configuration().getAtomicityMode() == ATOMIC)
entry.innerUpdate(ver, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), GridCacheOperation.DELETE, null, null, false, false, false, false, null, false, false, false, false, AffinityTopologyVersion.NONE, null, GridDrType.DR_NONE, 0, 0, null, false, false, null, null, null, null, false);
else
entry.innerRemove(null, ((IgniteEx) node).localNode().id(), ((IgniteEx) node).localNode().id(), false, false, false, false, false, null, AffinityTopologyVersion.NONE, CU.empty0(), GridDrType.DR_NONE, null, null, null, 1L);
rmvd = true;
assertFalse(entry.hasValue());
} else
assertTrue(entry.hasValue());
assertTrue("iterableKey " + key + " already inited", init);
if (node.equals(primary))
primVal = val;
} finally {
((IgniteEx) node).context().cache().context().database().checkpointReadUnlock();
}
}
assertEquals(vals.size(), mapping.size());
assertEquals(vals.size(), internalCache.configuration().getCacheMode() == REPLICATED ? serverNodesCount() : backupsCount() + 1);
if (!misses && !nulls)
// Primary value set.
assertTrue(primVal != null);
Integer fixed;
boolean consistent;
boolean repairable;
if (vals.stream().distinct().count() == 1) {
// Consistent value.
consistent = true;
repairable = true;
fixed = vals.iterator().next().getKey();
} else {
consistent = false;
// Currently, Atomic caches can not be repaired.
repairable = atomicityMode() != ATOMIC;
switch(strategy) {
case LWW:
if (misses || rmvd || !incVer) {
repairable = false;
// Should never be returned.
fixed = Integer.MIN_VALUE;
} else
fixed = incVal;
break;
case PRIMARY:
fixed = primVal;
break;
case RELATIVE_MAJORITY:
// Should never be returned.
fixed = Integer.MIN_VALUE;
Map<T2<Integer, GridCacheVersion>, Integer> counts = new HashMap<>();
for (T2<Integer, GridCacheVersion> val : vals) {
counts.putIfAbsent(val, 0);
counts.compute(val, (k, v) -> v + 1);
}
int[] sorted = counts.values().stream().sorted(Comparator.reverseOrder()).mapToInt(v -> v).toArray();
int max = sorted[0];
if (sorted.length > 1 && sorted[1] == max)
repairable = false;
if (repairable)
for (Map.Entry<T2<Integer, GridCacheVersion>, Integer> count : counts.entrySet()) if (count.getValue().equals(max)) {
fixed = count.getKey().getKey();
break;
}
break;
case REMOVE:
fixed = null;
break;
case CHECK_ONLY:
repairable = false;
// Should never be returned.
fixed = Integer.MIN_VALUE;
break;
default:
throw new UnsupportedOperationException(strategy.toString());
}
}
return new InconsistentMapping(mapping, primVal, fixed, repairable, consistent);
}
use of org.apache.ignite.internal.processors.cache.GridCacheAdapter in project ignite by apache.
the class IgniteTxPessimisticOriginatingNodeFailureAbstractSelfTest method checkPrimaryNodeCrash.
/**
* Checks tx data consistency in case when primary node crashes.
*
* @param commmit Whether to commit or rollback a transaction.
* @throws Exception If failed.
*/
private void checkPrimaryNodeCrash(final boolean commmit) throws Exception {
Set<Integer> keys = new HashSet<>();
for (int i = 0; i < 20; i++) keys.add(i);
final Collection<IgniteKernal> grids = new ArrayList<>();
ClusterNode primaryNode = grid(1).localNode();
for (int i = 0; i < gridCount(); i++) {
if (i != 1)
grids.add((IgniteKernal) grid(i));
}
failingNodeId = primaryNode.id();
final Map<Integer, String> map = new HashMap<>();
final String initVal = "initialValue";
for (Integer key : keys) {
grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
map.put(key, String.valueOf(key));
}
Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
IgniteCache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
info("Failing node ID: " + grid(1).localNode().id());
for (Integer key : keys) {
Collection<ClusterNode> nodes = new ArrayList<>();
nodes.addAll(affinity(cache).mapKeyToPrimaryAndBackups(key));
nodes.remove(primaryNode);
nodeMap.put(key, nodes);
}
info("Starting tx [values=" + map + ", topVer=" + grid(1).context().discovery().topologyVersion() + ']');
assertNotNull(cache);
try (Transaction tx = grid(0).transactions().txStart()) {
cache.getAll(keys);
// Should not send any messages.
cache.putAll(map);
TransactionProxyImpl txProxy = (TransactionProxyImpl) tx;
GridNearTxLocal txEx = txProxy.tx();
assertTrue(txEx.pessimistic());
if (commmit) {
txEx.prepare(true);
// Fail the node in the middle of transaction.
info(">>> Stopping primary node " + primaryNode);
G.stop(Ignition.ignite(primaryNode.id()).name(), true);
info(">>> Stopped originating node, finishing transaction: " + primaryNode.id());
tx.commit();
} else {
// Fail the node in the middle of transaction.
info(">>> Stopping primary node " + primaryNode);
G.stop(G.ignite(primaryNode.id()).name(), true);
info(">>> Stopped originating node, finishing transaction: " + primaryNode.id());
tx.rollback();
}
}
boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (IgniteKernal g : grids) {
GridCacheAdapter<?, ?> cache = g.internalCache(DEFAULT_CACHE_NAME);
IgniteTxManager txMgr = cache.isNear() ? ((GridNearCacheAdapter) cache).dht().context().tm() : cache.context().tm();
int txNum = txMgr.idMapSize();
if (txNum != 0)
return false;
}
return true;
}
}, 10000);
assertTrue(txFinished);
info("Transactions finished.");
for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
final Integer key = e.getKey();
final String val = map.get(key);
assertFalse(e.getValue().isEmpty());
if (atomicityMode() == TRANSACTIONAL_SNAPSHOT)
continue;
for (ClusterNode node : e.getValue()) {
final UUID checkNodeId = node.id();
compute(G.ignite(checkNodeId).cluster().forNode(node)).call(new IgniteCallable<Void>() {
/**
*/
@IgniteInstanceResource
private Ignite ignite;
@Override
public Void call() throws Exception {
IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
assertNotNull(cache);
assertEquals("Failed to check entry value on node: " + checkNodeId, !commmit ? initVal : val, cache.localPeek(key));
return null;
}
});
}
}
awaitPartitionMapExchange();
for (Map.Entry<Integer, String> e : map.entrySet()) {
long cntr0 = -1;
for (Ignite g : G.allGrids()) {
Integer key = e.getKey();
assertEquals(!commmit ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(key));
if (g.affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(((IgniteEx) g).localNode(), key)) {
long nodeCntr = updateCoutner(g, key);
if (cntr0 == -1)
cntr0 = nodeCntr;
assertEquals(cntr0, nodeCntr);
}
}
}
}
use of org.apache.ignite.internal.processors.cache.GridCacheAdapter in project ignite by apache.
the class IgniteCacheClientNodePartitionsExchangeTest method clientOnlyCacheStart.
/**
* @param nearCache If {@code true} creates near cache on client.
* @param srvNode If {@code true} creates client cache on server node.
* @throws Exception If failed.
*/
private void clientOnlyCacheStart(boolean nearCache, boolean srvNode) throws Exception {
Ignite ignite0 = startGrid(0);
Ignite ignite1 = startGrid(1);
boolean lateAff = ignite1.configuration().isLateAffinityAssignment();
waitForTopologyUpdate(2, new AffinityTopologyVersion(2, lateAff ? 1 : 0));
final String CACHE_NAME1 = "cache1";
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setName(CACHE_NAME1);
if (srvNode)
ccfg.setNodeFilter(new TestFilter(getTestIgniteInstanceName(2)));
ignite0.createCache(ccfg);
Ignite ignite2;
ignite2 = !srvNode ? startClientGrid(2) : startGrid(2);
int minorVer = srvNode && lateAff ? 1 : 0;
waitForTopologyUpdate(3, new AffinityTopologyVersion(3, minorVer));
TestCommunicationSpi spi0 = (TestCommunicationSpi) ignite0.configuration().getCommunicationSpi();
TestCommunicationSpi spi1 = (TestCommunicationSpi) ignite1.configuration().getCommunicationSpi();
TestCommunicationSpi spi2 = (TestCommunicationSpi) ignite2.configuration().getCommunicationSpi();
spi0.reset();
spi1.reset();
spi2.reset();
assertNull(((IgniteKernal) ignite2).context().cache().context().cache().internalCache(CACHE_NAME1));
if (nearCache)
ignite2.getOrCreateNearCache(CACHE_NAME1, new NearCacheConfiguration<>());
else
ignite2.cache(CACHE_NAME1);
GridCacheAdapter cache = ((IgniteKernal) ignite2).context().cache().context().cache().internalCache(CACHE_NAME1);
assertNotNull(cache);
assertEquals(nearCache, cache.context().isNear());
assertEquals(0, spi0.partitionsSingleMessages());
assertEquals(0, spi0.partitionsFullMessages());
assertEquals(0, spi1.partitionsSingleMessages());
assertEquals(0, spi1.partitionsFullMessages());
assertEquals(0, spi2.partitionsSingleMessages());
assertEquals(0, spi2.partitionsFullMessages());
final ClusterNode clientNode = ((IgniteKernal) ignite2).localNode();
for (Ignite ignite : Ignition.allGrids()) {
final GridDiscoveryManager disco = ((IgniteKernal) ignite).context().discovery();
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return disco.cacheNode(clientNode, CACHE_NAME1);
}
}, 5000);
assertTrue(disco.cacheNode(clientNode, CACHE_NAME1));
assertFalse(disco.cacheAffinityNode(clientNode, CACHE_NAME1));
assertEquals(nearCache, disco.cacheNearNode(clientNode, CACHE_NAME1));
}
spi0.reset();
spi1.reset();
spi2.reset();
if (!srvNode) {
log.info("Close client cache: " + CACHE_NAME1);
ignite2.cache(CACHE_NAME1).close();
assertNull(((IgniteKernal) ignite2).context().cache().context().cache().internalCache(CACHE_NAME1));
assertEquals(0, spi0.partitionsSingleMessages());
assertEquals(0, spi0.partitionsFullMessages());
assertEquals(0, spi1.partitionsSingleMessages());
assertEquals(0, spi1.partitionsFullMessages());
assertEquals(0, spi2.partitionsSingleMessages());
assertEquals(0, spi2.partitionsFullMessages());
}
final String CACHE_NAME2 = "cache2";
ccfg = new CacheConfiguration(CACHE_NAME2);
log.info("Create new cache: " + CACHE_NAME2);
ignite2.createCache(ccfg);
waitForTopologyUpdate(3, new AffinityTopologyVersion(3, ++minorVer));
assertEquals(0, spi0.partitionsSingleMessages());
assertEquals(2, spi0.partitionsFullMessages());
assertEquals(1, spi1.partitionsSingleMessages());
assertEquals(0, spi1.partitionsFullMessages());
assertEquals(1, spi2.partitionsSingleMessages());
assertEquals(0, spi2.partitionsFullMessages());
}
use of org.apache.ignite.internal.processors.cache.GridCacheAdapter in project ignite by apache.
the class IgnitePartitionedQueueNoBackupsTest method testCollocation.
/**
* @throws Exception If failed.
*/
@Test
public void testCollocation() throws Exception {
IgniteQueue<Integer> queue = grid(0).queue("queue", 0, config(true));
for (int i = 0; i < 1000; i++) assertTrue(queue.add(i));
assertEquals(1000, queue.size());
GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx");
UUID setNodeId = null;
for (int i = 0; i < gridCount(); i++) {
IgniteKernal grid = (IgniteKernal) grid(i);
GridCacheAdapter cache = grid.context().cache().internalCache(cctx.name());
Iterator<GridCacheMapEntry> entries = cache.map().entries(cache.context().cacheId()).iterator();
if (entries.hasNext()) {
if (setNodeId == null)
setNodeId = grid.localNode().id();
else
fail("For collocated queue all items should be stored on single node.");
}
}
}
use of org.apache.ignite.internal.processors.cache.GridCacheAdapter in project ignite by apache.
the class IgnitePartitionedSetNoBackupsSelfTest method testCollocation.
/**
* @throws Exception If failed.
*/
@Test
public void testCollocation() throws Exception {
Set<Integer> set0 = grid(0).set(SET_NAME, config(true));
for (int i = 0; i < 1000; i++) assertTrue(set0.add(i));
assertEquals(1000, set0.size());
GridCacheContext cctx = GridTestUtils.getFieldValue(set0, "cctx");
UUID setNodeId = null;
for (int i = 0; i < gridCount(); i++) {
IgniteKernal grid = (IgniteKernal) grid(i);
GridCacheAdapter cache = grid.context().cache().internalCache(cctx.name());
Iterator<GridCacheMapEntry> entries = cache.map().entries(cache.context().cacheId()).iterator();
if (entries.hasNext()) {
if (setNodeId == null)
setNodeId = grid.localNode().id();
else
fail("For collocated set all items should be stored on single node.");
}
}
}
Aggregations