use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.
the class IgniteTxMultiNodeAbstractTest method nearEntry.
/**
* @param nodeId Node ID.
* @param key Key.
* @return Near entry.
*/
@Nullable
private static GridCacheEntryEx nearEntry(UUID nodeId, Object key) {
Ignite g = G.ignite(nodeId);
GridNearCacheAdapter<Object, Integer> near = ((IgniteKernal) g).<Object, Integer>internalCache(DEFAULT_CACHE_NAME).context().near();
return near.peekEx(key);
}
use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.
the class GridCacheNearReadersSelfTest method testTwoNodesTwoKeysOneBackup.
/**
* @throws Exception If failed.
*/
public void testTwoNodesTwoKeysOneBackup() throws Exception {
aff.backups(1);
grids = 2;
aff.partitions(grids);
startGrids();
ClusterNode n1 = F.first(aff.nodes(aff.partition(1), grid(0).cluster().nodes()));
ClusterNode n2 = F.first(aff.nodes(aff.partition(2), grid(0).cluster().nodes()));
assertNotNull(n1);
assertNotNull(n2);
assertNotSame(n1, n2);
assertFalse("Nodes cannot be equal: " + n1, n1.equals(n2));
Ignite g1 = grid(n1.id());
Ignite g2 = grid(n2.id());
awaitPartitionMapExchange();
GridCacheContext ctx = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).context();
List<KeyCacheObject> cacheKeys = F.asList(ctx.toCacheKeyObject(1), ctx.toCacheKeyObject(2));
IgniteInternalFuture<Object> f1 = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME).preloader().request(ctx, cacheKeys, new AffinityTopologyVersion(2));
if (f1 != null)
f1.get();
IgniteInternalFuture<Object> f2 = ((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).preloader().request(((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME).context(), cacheKeys, new AffinityTopologyVersion(2));
if (f2 != null)
f2.get();
IgniteCache<Integer, String> cache1 = g1.cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);
assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1), g1.cluster().localNode());
assertFalse(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2).equals(g1.cluster().localNode()));
assertEquals(g1.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(2), g2.cluster().localNode());
assertFalse(g2.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(1).equals(g2.cluster().localNode()));
// Store first value in cache.
assertNull(cache1.getAndPut(1, "v1"));
assertTrue(cache1.containsKey(1));
assertTrue(cache2.containsKey(1));
assertEquals("v1", nearPeek(cache1, 1));
assertEquals("v1", nearPeek(cache2, 1));
assertEquals("v1", dhtPeek(cache1, 1));
assertEquals("v1", dhtPeek(cache2, 1));
assertNull(near(cache1).peekEx(1));
assertNull(near(cache2).peekEx(1));
GridDhtCacheEntry e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
// Store second value in cache.
assertNull(cache1.getAndPut(2, "v2"));
assertTrue(cache1.containsKey(2));
assertTrue(cache2.containsKey(2));
assertEquals("v2", nearPeek(cache1, 2));
assertEquals("v2", nearPeek(cache2, 2));
assertEquals("v2", dhtPeek(cache1, 2));
assertEquals("v2", dhtPeek(cache2, 2));
assertNull(near(cache1).peekEx(2));
assertNull(near(cache2).peekEx(2));
GridDhtCacheEntry c2e2 = (GridDhtCacheEntry) dht(cache2).entryEx(2);
// Nodes are backups of each other, so no readers should be added.
assertFalse(c2e2.readers().contains(n1.id()));
assertFalse(e1.readers().contains(n2.id()));
// Get key1 on node2 (value should come from local DHT cache, as it has a backup).
assertEquals("v1", cache2.get(1));
// Since DHT cache2 has the value, Near cache2 should not have it.
assertNull(near(cache2).peekEx(1));
e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
// Since v1 was retrieved locally from cache2, cache1 should not know about it.
assertFalse(e1.readers().contains(n2.id()));
// Evict locally from cache2.
// It should not be successful since it's not allowed to evict entry on backup node.
cache2.localEvict(Collections.singleton(1));
assertNull(near(cache2).peekEx(1));
assertEquals("v1", dhtPeek(cache2, 1));
assertEquals("v1", cache1.getAndPut(1, "z1"));
e1 = (GridDhtCacheEntry) dht(cache1).entryEx(1);
// Node 1 should not have node2 in readers map.
assertFalse(e1.readers().contains(n2.id()));
assertNull(near(cache2).peekEx(1));
assertEquals("z1", dhtPeek(cache2, 1));
}
use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.
the class GridCacheNearTxMultiNodeSelfTest method testTxCleanup.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "unchecked" })
public void testTxCleanup() throws Exception {
backups = 1;
Ignite ignite = startGrids(GRID_CNT);
try {
Integer mainKey = 0;
ClusterNode priNode = ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(mainKey);
ClusterNode backupNode = F.first(F.view(ignite.affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(mainKey), F.notIn(F.asList(priNode))));
ClusterNode otherNode = F.first(ignite.cluster().forPredicate(F.notIn(F.asList(priNode, backupNode))).nodes());
assert priNode != backupNode;
assert backupNode != otherNode;
assert priNode != otherNode;
final Ignite priIgnite = grid(priNode);
Ignite backupIgnite = grid(backupNode);
Ignite otherIgnite = grid(otherNode);
List<Ignite> ignites = F.asList(otherIgnite, priIgnite, backupIgnite);
int cntr = 0;
// Update main key from all nodes.
for (Ignite g : ignites) g.cache(DEFAULT_CACHE_NAME).put(mainKey, ++cntr);
info("Updated mainKey from all nodes.");
int keyCnt = 200;
Set<Integer> keys = new TreeSet<>();
// Populate cache from all nodes.
for (int i = 1; i <= keyCnt; i++) {
keys.add(i);
Ignite g = F.rand(ignites);
g.cache(DEFAULT_CACHE_NAME).put(new AffinityKey<>(i, mainKey), Integer.toString(cntr++));
}
IgniteCache cache = priIgnite.cache(DEFAULT_CACHE_NAME);
Transaction tx = priIgnite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache.get(mainKey);
cache.removeAll(keys);
cache.put(mainKey, ++cntr);
tx.commit();
} catch (Error | Exception e) {
error("Transaction failed: " + tx, e);
throw e;
} finally {
tx.close();
}
stopGrid(priIgnite.name(), true);
stopGrid(backupIgnite.name(), true);
Ignite newIgnite = startGrid(GRID_CNT);
ignites = F.asList(otherIgnite, newIgnite);
for (Ignite g : ignites) {
GridNearCacheAdapter near = ((IgniteKernal) g).internalCache(DEFAULT_CACHE_NAME).context().near();
GridDhtCacheAdapter dht = near.dht();
checkTm(g, near.context().tm());
checkTm(g, dht.context().tm());
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.
the class GridCachePartitionedTopologyChangeSelfTest method testExplicitLocks.
/**
* @throws Exception If failed.
*/
public void testExplicitLocks() throws Exception {
try {
startGridsMultiThreaded(2);
IgniteKernal[] nodes = new IgniteKernal[] { (IgniteKernal) grid(0), (IgniteKernal) grid(1) };
Collection<IgniteInternalFuture> futs = new ArrayList<>();
final CountDownLatch startLatch = new CountDownLatch(1);
for (final IgniteKernal node : nodes) {
List<Integer> parts = partitions(node, PARTITION_PRIMARY);
Map<Integer, Integer> keyMap = keysFor(node, parts);
for (final Integer key : keyMap.values()) {
futs.add(multithreadedAsync(new Runnable() {
@Override
public void run() {
try {
Lock lock = node.cache(DEFAULT_CACHE_NAME).lock(key);
lock.lock();
try {
info(">>> Acquired explicit lock for key: " + key);
startLatch.await();
info(">>> Acquiring explicit lock for key: " + key * 10);
Lock lock10 = node.cache(DEFAULT_CACHE_NAME).lock(key * 10);
lock10.lock();
try {
info(">>> Releasing locks [key1=" + key + ", key2=" + key * 10 + ']');
} finally {
lock10.unlock();
}
} finally {
lock.unlock();
}
} catch (CacheException e) {
info(">>> Failed to perform lock [key=" + key + ", e=" + e + ']');
} catch (InterruptedException ignored) {
info(">>> Interrupted while waiting for start latch.");
Thread.currentThread().interrupt();
}
}
}, 1));
}
}
IgniteInternalFuture<?> startFut = multithreadedAsync(new Runnable() {
@Override
public void run() {
try {
startGrid(2);
info(">>> Started grid2.");
} catch (Exception e) {
info(">>> Failed to start grid: " + e);
}
}
}, 1);
U.sleep(5000);
assertFalse(startFut.isDone());
info(">>> Waiting for all locks to be released.");
startLatch.countDown();
for (IgniteInternalFuture fut : futs) fut.get(1000);
startFut.get();
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.
the class IgniteCacheNearLockValueSelfTest method testDhtVersion.
/**
* @throws Exception If failed.
*/
public void testDhtVersion() throws Exception {
CacheConfiguration<Object, Object> pCfg = new CacheConfiguration<>("partitioned");
pCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
try (IgniteCache<Object, Object> cache = ignite(0).getOrCreateCache(pCfg, new NearCacheConfiguration<>())) {
cache.put("key1", "val1");
for (int i = 0; i < 3; i++) {
try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get("key1");
tx.commit();
}
TestRecordingCommunicationSpi comm = (TestRecordingCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
Collection<GridNearLockRequest> reqs = (Collection) comm.recordedMessages(false);
assertEquals(1, reqs.size());
GridCacheAdapter<Object, Object> primary = ((IgniteKernal) grid(1)).internalCache("partitioned");
GridCacheEntryEx dhtEntry = primary.peekEx(primary.context().toCacheKeyObject("key1"));
assertNotNull(dhtEntry);
GridNearLockRequest req = reqs.iterator().next();
assertEquals(dhtEntry.version(), req.dhtVersion(0));
// Check entry version in near cache after commit.
GridCacheAdapter<Object, Object> near = ((IgniteKernal) grid(0)).internalCache("partitioned");
GridNearCacheEntry nearEntry = (GridNearCacheEntry) near.peekEx(near.context().toCacheKeyObject("key1"));
assertNotNull(nearEntry);
assertEquals(dhtEntry.version(), nearEntry.dhtVersion());
}
}
}
Aggregations