use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class CacheLateAffinityAssignmentTest method checkCaches.
/**
*/
private void checkCaches() {
List<Ignite> nodes = G.allGrids();
assertFalse(nodes.isEmpty());
for (Ignite node : nodes) {
Collection<String> cacheNames = node.cacheNames();
assertFalse(cacheNames.isEmpty());
for (String cacheName : cacheNames) {
try {
IgniteCache<Object, Object> cache = node.cache(cacheName);
assertNotNull(cache);
Long val = System.currentTimeMillis();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < 100; i++) {
int key = rnd.nextInt(100_000);
cache.put(key, val);
assertEquals(val, cache.get(key));
cache.remove(key);
assertNull(cache.get(key));
}
} catch (Exception e) {
assertTrue("Unexpected error: " + e, X.hasCause(e, ClusterTopologyServerNotFoundException.class));
Affinity<Object> aff = node.affinity(cacheName);
assert aff.partitions() > 0;
for (int p = 0; p > aff.partitions(); p++) {
Collection<ClusterNode> partNodes = aff.mapPartitionToPrimaryAndBackups(p);
assertTrue(partNodes.isEmpty());
}
}
}
}
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class AbstractDeadlockDetectionTest method findPrimaryKeys.
/**
* @param cache Cache.
* @param cnt Keys count.
* @param startFrom Start value for keys search.
* @return Collection of keys for which given node is primary.
*/
private <T> List<T> findPrimaryKeys(IgniteCache<?, ?> cache, final int cnt, final T startFrom) {
A.ensure(cnt > 0, "cnt");
final List<T> found = new ArrayList<>(cnt);
final ClusterNode locNode = localNode(cache);
final Affinity<T> aff = (Affinity<T>) affinity(cache);
try {
GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
T key = startFrom;
for (int i = 0; i < 100_000; i++) {
if (aff.isPrimary(locNode, key)) {
if (!found.contains(key))
found.add(key);
if (found.size() == cnt)
return true;
}
key = (T) incrementKey(key, 1);
}
return false;
}
}, 5000);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
if (found.size() != cnt)
throw new IgniteException("Unable to find " + cnt + " required keys.");
return found;
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class GridCommonAbstractTest method findKeys.
/**
* @param node Node.
* @param cache Cache.
* @param cnt Keys count.
* @param startFrom Start value for keys search.
* @return Collection of keys for which given cache is primary.
*/
protected List<Integer> findKeys(@Nullable ClusterNode node, IgniteCache<?, ?> cache, final int cnt, final int startFrom, final int type) {
assert cnt > 0 : cnt;
final List<Integer> found = new ArrayList<>(cnt);
final ClusterNode node0 = node != null ? node : localNode(cache);
final Affinity<Integer> aff = (Affinity<Integer>) affinity(cache);
try {
GridTestUtils.waitForCondition(new PA() {
@Override
public boolean apply() {
for (int i = startFrom; i < startFrom + 100_000; i++) {
Integer key = i;
boolean ok;
if (type == 0)
ok = aff.isPrimary(node0, key);
else if (type == 1)
ok = aff.isBackup(node0, key);
else if (type == 2)
ok = !aff.isPrimaryOrBackup(node0, key);
else {
fail();
return false;
}
if (ok) {
if (!found.contains(key))
found.add(key);
if (found.size() == cnt)
return true;
}
}
return false;
}
}, 5000);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
if (found.size() != cnt)
throw new IgniteException("Unable to find " + cnt + " requied keys.");
return found;
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class CacheMvccTxRecoveryTest method checkRecoveryNearFailure.
/**
*/
private void checkRecoveryNearFailure(TxEndResult endRes, NodeMode nearNodeMode) throws Exception {
int gridCnt = 4;
int baseCnt = gridCnt - 1;
boolean commit = endRes == COMMIT;
startGridsMultiThreaded(baseCnt);
// tweak client/server near
client = nearNodeMode == CLIENT;
IgniteEx nearNode = startGrid(baseCnt);
IgniteCache<Object, Object> cache = nearNode.getOrCreateCache(basicCcfg().setBackups(1));
Affinity<Object> aff = nearNode.affinity(DEFAULT_CACHE_NAME);
List<Integer> keys = new ArrayList<>();
for (int i = 0; i < 100; i++) {
if (aff.isPrimary(grid(0).localNode(), i) && aff.isBackup(grid(1).localNode(), i)) {
keys.add(i);
break;
}
}
for (int i = 0; i < 100; i++) {
if (aff.isPrimary(grid(1).localNode(), i) && aff.isBackup(grid(2).localNode(), i)) {
keys.add(i);
break;
}
}
assert keys.size() == 2;
TestRecordingCommunicationSpi nearComm = (TestRecordingCommunicationSpi) nearNode.configuration().getCommunicationSpi();
if (!commit)
nearComm.blockMessages(GridNearTxPrepareRequest.class, grid(1).name());
GridTestUtils.runAsync(() -> {
// run in separate thread to exclude tx from thread-local map
GridNearTxLocal nearTx = ((TransactionProxyImpl) nearNode.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)).tx();
for (Integer k : keys) cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(k));
List<IgniteInternalTx> txs = IntStream.range(0, baseCnt).mapToObj(i -> txsOnNode(grid(i), nearTx.xidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
IgniteInternalFuture<?> prepareFut = nearTx.prepareNearTxLocal();
if (commit)
prepareFut.get();
else
assertConditionEventually(() -> txs.stream().anyMatch(tx -> tx.state() == PREPARED));
// drop near
nearNode.close();
assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == (commit ? COMMITTED : ROLLED_BACK)));
return null;
}).get();
if (commit) {
assertConditionEventually(() -> {
int rowsCnt = grid(0).cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll().size();
return rowsCnt == keys.size();
});
} else {
int rowsCnt = G.allGrids().get(0).cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll().size();
assertEquals(0, rowsCnt);
}
assertPartitionCountersAreConsistent(keys, grids(baseCnt, i -> true));
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class CacheMvccTxRecoveryTest method testCountersNeighborcastServerFailed.
/**
* @throws Exception if failed.
*/
@Test
public void testCountersNeighborcastServerFailed() throws Exception {
// Reopen https://issues.apache.org/jira/browse/IGNITE-10766 if starts failing
int srvCnt = 4;
startGridsMultiThreaded(srvCnt);
client = true;
IgniteEx ign = startGrid(srvCnt);
IgniteCache<Object, Object> cache = ign.getOrCreateCache(basicCcfg().setBackups(2));
ArrayList<Integer> keys = new ArrayList<>();
int vid = 3;
IgniteEx victim = grid(vid);
Affinity<Object> aff = ign.affinity(DEFAULT_CACHE_NAME);
for (int i = 0; i < 100; i++) {
if (aff.isPrimary(victim.localNode(), i) && !aff.isBackup(grid(0).localNode(), i)) {
keys.add(i);
break;
}
}
for (int i = 0; i < 100; i++) {
if (aff.isPrimary(victim.localNode(), i) && !aff.isBackup(grid(1).localNode(), i)) {
keys.add(i);
break;
}
}
assert keys.size() == 2 && !keys.contains(99);
// prevent prepare on one backup
((TestRecordingCommunicationSpi) victim.configuration().getCommunicationSpi()).blockMessages(GridDhtTxPrepareRequest.class, grid(0).name());
GridNearTxLocal nearTx = ((TransactionProxyImpl) ign.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)).tx();
for (Integer k : keys) cache.query(new SqlFieldsQuery("insert into Integer(_key, _val) values(?, 42)").setArgs(k));
List<IgniteInternalTx> txs = IntStream.range(0, srvCnt).mapToObj(this::grid).filter(g -> g != victim).map(g -> txsOnNode(g, nearTx.xidVersion())).flatMap(Collection::stream).collect(Collectors.toList());
nearTx.commitAsync();
// await tx partially prepared
assertConditionEventually(() -> txs.stream().anyMatch(tx -> tx.state() == PREPARED));
CountDownLatch latch1 = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(1);
IgniteInternalFuture<Object> backgroundTxFut = GridTestUtils.runAsync(() -> {
try (Transaction ignored = ign.transactions().txStart()) {
boolean upd = false;
for (int i = 100; i < 200; i++) {
if (!aff.isPrimary(victim.localNode(), i)) {
cache.put(i, 11);
upd = true;
break;
}
}
assert upd;
latch1.countDown();
latch2.await(getTestTimeout(), TimeUnit.MILLISECONDS);
}
return null;
});
latch1.await(getTestTimeout(), TimeUnit.MILLISECONDS);
// drop primary
victim.close();
// do all assertions before rebalance
assertConditionEventually(() -> txs.stream().allMatch(tx -> tx.state() == ROLLED_BACK));
List<IgniteEx> liveNodes = grids(srvCnt, i -> i != vid);
assertPartitionCountersAreConsistent(keys, liveNodes);
latch2.countDown();
backgroundTxFut.get(getTestTimeout());
assertTrue(liveNodes.stream().map(node -> node.cache(DEFAULT_CACHE_NAME).query(new SqlFieldsQuery("select * from Integer")).getAll()).allMatch(Collection::isEmpty));
}
Aggregations