use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteCacheTxRecoveryRollbackTest method nearTx2.
/**
* Stop both tx near node (client2) and primary node, near cache tx on client1 is invalidated.
*
* @param concurrency Tx concurrency or {@code null} for implicit transaction.
* @throws Exception If failed.
*/
private void nearTx2(final TransactionConcurrency concurrency) throws Exception {
startGrids(4);
Ignite srv0 = grid(0);
srv0.createCache(cacheConfiguration(2, false, false));
awaitPartitionMapExchange();
client = true;
Ignite client1 = startGrid(4);
final Ignite client2 = startGrid(5);
final Integer key = primaryKey(srv0.cache(DEFAULT_CACHE_NAME));
final IgniteCache<Integer, Integer> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
final IgniteCache<Integer, Integer> cache2 = client2.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
cache1.put(key, 1);
final Integer newVal = 2;
testSpi(client2).blockMessages(GridNearTxFinishRequest.class, srv0.name());
testSpi(srv0).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
@Override
public boolean apply(ClusterNode node, Message msg) {
return msg instanceof GridDhtTxFinishRequest;
}
});
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
log.info("Start put, concurrency: " + concurrency);
if (concurrency != null) {
try (Transaction tx = client2.transactions().txStart(concurrency, REPEATABLE_READ)) {
cache2.put(key, newVal);
tx.commit();
}
} else
cache2.put(key, newVal);
return null;
}
});
U.sleep(500);
assertFalse(fut.isDone());
testSpi(client2).waitForBlocked(GridNearTxFinishRequest.class, srv0.name());
stopGrid(client2.name());
stopGrid(srv0.name());
try {
fut.get();
} catch (IgniteCheckedException ignore) {
// No-op.
}
final IgniteCache<Integer, Integer> srvCache = grid(1).cache(DEFAULT_CACHE_NAME);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return newVal.equals(srvCache.get(key)) && newVal.equals(cache1.get(key));
}
}, 5000);
checkData(F.asMap(key, newVal));
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteCacheTxRecoveryRollbackTest method nearTx1.
/**
* Stop tx near node (client2), near cache tx on client1 is either committed
* by primary or invalidated.
*
* @param concurrency Tx concurrency or {@code null} for implicit transaction.
* @throws Exception If failed.
*/
private void nearTx1(final TransactionConcurrency concurrency) throws Exception {
startGrids(4);
Ignite srv0 = grid(0);
final IgniteCache<Integer, Integer> srvCache = srv0.createCache(cacheConfiguration(2, false, false));
awaitPartitionMapExchange();
client = true;
Ignite client1 = startGrid(4);
final Ignite client2 = startGrid(5);
final Integer key = primaryKey(srv0.cache(DEFAULT_CACHE_NAME));
final IgniteCache<Integer, Integer> cache1 = client1.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
final IgniteCache<Integer, Integer> cache2 = client2.createNearCache(DEFAULT_CACHE_NAME, new NearCacheConfiguration<Integer, Integer>());
cache1.put(key, 1);
final Integer newVal = 2;
testSpi(client2).blockMessages(GridNearTxFinishRequest.class, srv0.name());
IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
log.info("Start put, concurrency: " + concurrency);
if (concurrency != null) {
try (Transaction tx = client2.transactions().txStart(concurrency, REPEATABLE_READ)) {
cache2.put(key, newVal);
tx.commit();
}
} else
cache2.put(key, newVal);
return null;
}
});
U.sleep(500);
assertFalse(fut.isDone());
testSpi(client2).waitForBlocked(GridNearTxFinishRequest.class, srv0.name());
stopGrid(client2.name());
try {
fut.get();
} catch (IgniteCheckedException ignore) {
// No-op.
}
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return newVal.equals(srvCache.get(key)) && newVal.equals(cache1.get(key));
}
}, 5000);
checkData(F.asMap(key, newVal));
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class GridCacheAtomicInvalidPartitionHandlingSelfTest method checkRestarts.
/**
* @param writeSync Write synchronization mode to check.
* @throws Exception If failed.
*/
private void checkRestarts(CacheWriteSynchronizationMode writeSync) throws Exception {
this.writeSync = writeSync;
final int gridCnt = 6;
startGrids(gridCnt);
awaitPartitionMapExchange();
try {
assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
final IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
final int range = 100_000;
final Set<Integer> keys = new LinkedHashSet<>();
try (IgniteDataStreamer<Integer, Integer> streamer = grid(0).dataStreamer(DEFAULT_CACHE_NAME)) {
streamer.allowOverwrite(true);
for (int i = 0; i < range; i++) {
streamer.addData(i, 0);
keys.add(i);
if (i > 0 && i % 10_000 == 0)
System.err.println("Put: " + i);
}
}
final Affinity<Integer> aff = grid(0).affinity(DEFAULT_CACHE_NAME);
boolean putDone = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
Iterator<Integer> it = keys.iterator();
while (it.hasNext()) {
Integer key = it.next();
Collection<ClusterNode> affNodes = aff.mapKeyToPrimaryAndBackups(key);
for (int i = 0; i < gridCnt; i++) {
ClusterNode locNode = grid(i).localNode();
IgniteCache<Object, Object> cache = grid(i).cache(DEFAULT_CACHE_NAME);
Object val = cache.localPeek(key);
if (affNodes.contains(locNode)) {
if (val == null)
return false;
} else
assertNull(val);
}
it.remove();
}
return true;
}
}, 30_000);
assertTrue(putDone);
assertTrue(keys.isEmpty());
final AtomicBoolean done = new AtomicBoolean();
delay = true;
System.err.println("FINISHED PUTS");
// Start put threads.
IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Random rnd = new Random();
while (!done.get()) {
try {
int cnt = rnd.nextInt(5);
if (cnt < 2) {
int key = rnd.nextInt(range);
int val = rnd.nextInt();
cache.put(key, val);
} else {
Map<Integer, Integer> upd = new TreeMap<>();
for (int i = 0; i < cnt; i++) upd.put(rnd.nextInt(range), rnd.nextInt());
cache.putAll(upd);
}
} catch (CachePartialUpdateException ignored) {
// No-op.
}
}
return null;
}
}, 4, "putAll-thread");
Random rnd = new Random();
// Restart random nodes.
for (int r = 0; r < 20; r++) {
int idx0 = rnd.nextInt(gridCnt - 1) + 1;
stopGrid(idx0);
U.sleep(200);
startGrid(idx0);
}
done.set(true);
awaitPartitionMapExchange();
fut.get();
for (int k = 0; k < range; k++) {
Collection<ClusterNode> affNodes = affinity(cache).mapKeyToPrimaryAndBackups(k);
// Test is valid with at least one backup.
assert affNodes.size() >= 2;
Object val = null;
GridCacheVersion ver = null;
UUID nodeId = null;
for (int i = 0; i < gridCnt; i++) {
ClusterNode locNode = grid(i).localNode();
GridCacheAdapter<Object, Object> c = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
GridCacheEntryEx entry = null;
try {
entry = c.entryEx(k);
entry.unswap();
} catch (GridDhtInvalidPartitionException ignored) {
// Skip key.
}
for (int r = 0; r < 10; r++) {
try {
if (affNodes.contains(locNode)) {
assert c.affinity().isPrimaryOrBackup(locNode, k);
boolean primary = c.affinity().isPrimary(locNode, k);
assertNotNull("Failed to find entry on node for key [locNode=" + locNode.id() + ", key=" + k + ']', entry);
if (val == null) {
assertNull(ver);
val = CU.value(entry.rawGet(), entry.context(), false);
ver = entry.version();
nodeId = locNode.id();
} else {
assertNotNull(ver);
assertEquals("Failed to check value for key [key=" + k + ", node=" + locNode.id() + ", primary=" + primary + ", recNodeId=" + nodeId + ']', val, CU.value(entry.rawGet(), entry.context(), false));
assertEquals("Failed to check version for key [key=" + k + ", node=" + locNode.id() + ", primary=" + primary + ", recNodeId=" + nodeId + ']', ver, entry.version());
}
} else
assertTrue("Invalid entry: " + entry, entry == null || !entry.partitionValid());
} catch (AssertionError e) {
if (r == 9) {
info("Failed to verify cache contents: " + e.getMessage());
throw e;
}
info("Failed to verify cache contents, will retry: " + e.getMessage());
// Give some time to finish async updates.
U.sleep(1000);
}
}
}
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteCacheAtomicProtocolTest method testPutPrimarySync.
/**
* @throws Exception If failed.
*/
public void testPutPrimarySync() throws Exception {
startGrids(2);
client = true;
Ignite clientNode = startGrid(2);
client = false;
final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, PRIMARY_SYNC));
awaitPartitionMapExchange();
Ignite srv0 = grid(0);
final Ignite srv1 = grid(1);
final Integer key = primaryKey(srv0.cache(TEST_CACHE));
testSpi(srv0).blockMessages(GridDhtAtomicSingleUpdateRequest.class, srv1.name());
IgniteFuture<?> fut = nearCache.putAsync(key, key);
fut.get(5, TimeUnit.SECONDS);
assertEquals(key, srv0.cache(TEST_CACHE).get(key));
assertNull(srv1.cache(TEST_CACHE).localPeek(key));
testSpi(srv0).stopBlock(true);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return srv1.cache(TEST_CACHE).localPeek(key) != null;
}
}, 5000);
checkData(F.asMap(key, key));
}
use of org.apache.ignite.internal.util.lang.GridAbsPredicate in project ignite by apache.
the class IgniteCacheAtomicProtocolTest method fullAsyncRemap.
/**
* @param putAll Test putAll flag.
* @throws Exception If failed.
*/
private void fullAsyncRemap(boolean putAll) throws Exception {
Ignite srv0 = startGrid(0);
client = true;
Ignite clientNode = startGrid(1);
client = false;
final IgniteCache<Integer, Integer> nearCache = clientNode.createCache(cacheConfiguration(1, FULL_ASYNC));
List<Integer> keys = movingKeysAfterJoin(srv0, TEST_CACHE, putAll ? 10 : 1);
testSpi(clientNode).blockMessages(GridNearAtomicSingleUpdateRequest.class, srv0.name());
testSpi(clientNode).blockMessages(GridNearAtomicFullUpdateRequest.class, srv0.name());
final Map<Integer, Integer> map = new HashMap<>();
for (Integer key : keys) map.put(key, -key);
if (putAll)
nearCache.putAll(map);
else
nearCache.put(keys.get(0), map.get(keys.get(0)));
Affinity<Object> aff = clientNode.affinity(TEST_CACHE);
startGrid(2);
awaitPartitionMapExchange();
int keysMoved = 0;
for (Integer key : keys) {
if (!aff.isPrimary(srv0.cluster().localNode(), key))
keysMoved++;
}
assertEquals(keys.size(), keysMoved);
testSpi(clientNode).stopBlock(true);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (Integer key : map.keySet()) {
if (nearCache.get(key) == null)
return false;
}
return true;
}
}, 5000);
checkData(map);
}
Aggregations