use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.
the class TxCrossCacheMapOnInvalidTopologyTest method doTestCrossCacheTxMapOnInvalidTopology.
/**
* Test scenario: cross-cache tx is started when node is left in the middle of rebalance, first cache is rebalanced
* and second is partially rebalanced.
*
* First cache map request will trigger client compatible remap for pessimistic txs,
* second cache map request should use new topology version.
*
* For optimistic tx remap is enforced if more than one mapping in transaction or all enlisted caches have compatible
* assignments.
*
* Success: tx is finished on ideal topology version over all mapped nodes.
*
* @param concurrency Concurrency.
* @param isolation Isolation.
*/
private void doTestCrossCacheTxMapOnInvalidTopology(TransactionConcurrency concurrency, TransactionIsolation isolation) throws Exception {
try {
IgniteEx crd = startGrid(0);
IgniteEx g1 = startGrid(1);
awaitPartitionMapExchange();
IgniteEx client = startClientGrid("client");
assertNotNull(client.cache(CACHE1));
assertNotNull(client.cache(CACHE2));
try (IgniteDataStreamer<Object, Object> streamer = crd.dataStreamer(CACHE1)) {
// Put 500 keys per partition.
for (int k = 0; k < PARTS_CNT * 500; k++) streamer.addData(k, new byte[10]);
}
try (IgniteDataStreamer<Object, Object> streamer = crd.dataStreamer(CACHE2)) {
// Put 500 keys per partition.
for (int k = 0; k < PARTS_CNT * 500; k++) streamer.addData(k, new byte[10]);
}
TestRecordingCommunicationSpi crdSpi = TestRecordingCommunicationSpi.spi(crd);
final AffinityTopologyVersion joinVer = new AffinityTopologyVersion(4, 0);
AffinityTopologyVersion leftVer = new AffinityTopologyVersion(5, 0);
AffinityTopologyVersion idealVer = new AffinityTopologyVersion(5, 1);
AtomicReference<Set<Integer>> full = new AtomicReference<>();
GridConcurrentSkipListSet<Integer> leftVerParts = new GridConcurrentSkipListSet<>();
crdSpi.blockMessages((node, m) -> {
if (m instanceof GridDhtPartitionSupplyMessage) {
GridDhtPartitionSupplyMessage msg = (GridDhtPartitionSupplyMessage) m;
// Allow full rebalance for cache 1 and system cache.
if (msg.groupId() != CU.cacheId(CACHE2))
return false;
// Allow only first batch for cache 2.
if (msg.topologyVersion().equals(joinVer)) {
if (full.get() == null) {
Map<Integer, Long> last = U.field(msg, "last");
full.set(last.keySet());
return false;
}
return true;
}
if (msg.topologyVersion().equals(leftVer)) {
Map<Integer, Long> last = U.field(msg, "last");
leftVerParts.addAll(last.keySet());
return true;
}
} else if (m instanceof GridDhtPartitionsFullMessage) {
GridDhtPartitionsFullMessage msg = (GridDhtPartitionsFullMessage) m;
// Delay full message for ideal topology switch.
GridDhtPartitionExchangeId exchId = msg.exchangeId();
if (exchId != null && exchId.topologyVersion().equals(idealVer))
return true;
}
return false;
});
TestRecordingCommunicationSpi g1Spi = TestRecordingCommunicationSpi.spi(g1);
g1Spi.blockMessages((node, msg) -> {
if (msg instanceof GridDhtPartitionSupplyMessage) {
GridDhtPartitionSupplyMessage m = (GridDhtPartitionSupplyMessage) msg;
return m.groupId() == CU.cacheId(CACHE2);
}
return false;
});
startGrid(2);
crdSpi.waitForBlocked();
g1Spi.waitForBlocked();
// Wait partial owning.
assertTrue("Timed out while waiting for rebalance", GridTestUtils.waitForCondition(() -> {
// Await full rebalance for cache 2.
GridDhtPartitionTopology top0 = grid(2).cachex(CACHE1).context().topology();
for (int p = 0; p < PARTS_CNT; p++) {
if (top0.localPartition(p).state() != OWNING)
return false;
}
// Await partial rebalance for cache 1.
GridDhtPartitionTopology top1 = grid(2).cachex(CACHE2).context().topology();
for (Integer part : full.get()) {
if (top1.localPartition(part).state() != OWNING)
return false;
}
return true;
}, 10_000));
// At this point cache 1 is fully rebalanced and cache 2 is partially rebalanced.
// Stop supplier in the middle of rebalance.
g1.close();
// Wait for topologies and calculate required partitions.
grid(0).cachex(CACHE1).context().affinity().affinityReadyFuture(leftVer).get();
grid(2).cachex(CACHE1).context().affinity().affinityReadyFuture(leftVer).get();
grid(0).cachex(CACHE2).context().affinity().affinityReadyFuture(leftVer).get();
grid(2).cachex(CACHE2).context().affinity().affinityReadyFuture(leftVer).get();
AffinityAssignment assignment0 = grid(0).cachex(CACHE1).context().affinity().assignment(leftVer);
AffinityAssignment assignment = grid(0).cachex(CACHE2).context().affinity().assignment(leftVer);
// Search for a partition with incompatible assignment.
// Partition for cache1 which is mapped for both late and ideal topologies to the same primary.
int stablePart = -1;
// Partition for cache2 which is mapped for both late and ideal topologies on different primaries.
int movingPart = -1;
for (int p = 0; p < assignment0.assignment().size(); p++) {
List<ClusterNode> curr = assignment.assignment().get(p);
List<ClusterNode> ideal = assignment.idealAssignment().get(p);
if (curr.equals(ideal) && curr.get(0).order() == 1) {
stablePart = p;
break;
}
}
assertFalse(stablePart == -1);
for (int p = 0; p < assignment.assignment().size(); p++) {
List<ClusterNode> curr = assignment.assignment().get(p);
List<ClusterNode> ideal = assignment.idealAssignment().get(p);
if (!curr.equals(ideal) && curr.get(0).order() == 1) {
movingPart = p;
break;
}
}
assertFalse(movingPart == -1);
TestRecordingCommunicationSpi.spi(client).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
@Override
public boolean apply(ClusterNode node, Message msg) {
if (concurrency == PESSIMISTIC)
return msg instanceof GridNearLockRequest;
else
return msg instanceof GridNearTxPrepareRequest;
}
});
final int finalStablePart = stablePart;
final int finalMovingPart = movingPart;
IgniteInternalFuture<?> txFut = multithreadedAsync(() -> {
try (Transaction tx = client.transactions().txStart(concurrency, isolation)) {
// Will map on crd(order=1).
client.cache(CACHE1).put(finalStablePart, 0);
// Next request will remap to ideal topology, but it's not ready on other node except crd.
client.cache(CACHE2).put(finalMovingPart, 0);
tx.commit();
}
}, 1, "tx-thread");
// Wait until all missing supply messages are blocked.
assertTrue(GridTestUtils.waitForCondition(() -> leftVerParts.size() == PARTS_CNT - full.get().size(), 5_000));
// Delay first lock request on late topology.
TestRecordingCommunicationSpi.spi(client).waitForBlocked();
// At this point only supply messages should be blocked.
// Unblock to continue rebalance and trigger ideal topology switch.
crdSpi.stopBlock(true, null, false, true);
// Wait until ideal topology is ready on crd.
crd.context().cache().context().exchange().affinityReadyFuture(idealVer).get(10_000);
// Other node must wait for full message.
assertFalse(GridTestUtils.waitForCondition(() -> grid(2).context().cache().context().exchange().affinityReadyFuture(idealVer).isDone(), 1_000));
// Map on unstable topology (PME is in progress on other node).
TestRecordingCommunicationSpi.spi(client).stopBlock();
// Capture local transaction.
IgniteInternalTx tx0 = client.context().cache().context().tm().activeTransactions().iterator().next();
// Expected behavior: tx must hang (both pessimistic and optimistic) because topology is not ready.
try {
txFut.get(3_000);
fail("TX must not complete");
} catch (IgniteFutureTimeoutCheckedException e) {
// Expected.
}
crdSpi.stopBlock();
txFut.get();
// Check transaction map version. Should be mapped on ideal topology.
assertEquals(tx0.topologyVersionSnapshot(), idealVer);
awaitPartitionMapExchange();
checkFutures();
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.
the class IgniteTxCachePrimarySyncTest method checkOnePhaseMessages.
/**
* @param client Node executing cache operation.
* @param ccfg Cache configuration.
* @param c Cache update closure.
* @throws Exception If failed.
*/
private void checkOnePhaseMessages(Ignite client, final CacheConfiguration<Object, Object> ccfg, final IgniteBiInClosure<Integer, IgniteCache<Object, Object>> c) throws Exception {
Ignite ignite = ignite(0);
assertNotSame(ignite, client);
TestRecordingCommunicationSpi commSpiClient = (TestRecordingCommunicationSpi) client.configuration().getCommunicationSpi();
TestRecordingCommunicationSpi commSpi0 = (TestRecordingCommunicationSpi) ignite.configuration().getCommunicationSpi();
IgniteCache<Object, Object> cache = ignite.cache(ccfg.getName());
final Integer key = primaryKey(cache);
cache.remove(key);
waitKeyRemoved(ccfg.getName(), key);
final IgniteCache<Object, Object> clientCache = client.cache(ccfg.getName());
commSpi0.record(GridNearTxFinishResponse.class, GridNearTxPrepareResponse.class);
commSpiClient.record(GridNearTxPrepareRequest.class, GridNearTxFinishRequest.class);
c.apply(key, clientCache);
List<Object> srvMsgs = commSpi0.recordedMessages(true);
assertEquals("Unexpected messages: " + srvMsgs, 1, srvMsgs.size());
assertTrue("Unexpected message: " + srvMsgs.get(0), srvMsgs.get(0) instanceof GridNearTxPrepareResponse);
List<Object> clientMsgs = commSpiClient.recordedMessages(true);
assertEquals("Unexpected messages: " + clientMsgs, 1, clientMsgs.size());
assertTrue("Unexpected message: " + clientMsgs.get(0), clientMsgs.get(0) instanceof GridNearTxPrepareRequest);
GridNearTxPrepareRequest req = (GridNearTxPrepareRequest) clientMsgs.get(0);
assertTrue(req.onePhaseCommit());
for (Ignite ignite0 : G.allGrids()) assertEquals(key, ignite0.cache(cache.getName()).get(key));
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.
the class IgniteLogicalRecoveryWithParamsTest method testPartiallyCommitedTx.
/**
* Tests concurrent tx with node stop and further recovery.
*/
private void testPartiallyCommitedTx() throws Exception {
final String cacheName = "recovery";
int itmsCount = 30_000;
AtomicBoolean failFileIO = new AtomicBoolean();
List<Integer> keys;
CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<Integer, Integer>(cacheName).setCacheMode(CacheMode.PARTITIONED).setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL).setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC).setBackups(backups).setAffinity(new RendezvousAffinityFunction(false, 32));
try {
final IgniteEx srv = (IgniteEx) startGridsMultiThreaded(numSrvNodes);
G.allGrids().forEach(n -> setWalIOFactory(n, failFileIO));
IgniteEx clnt = startClientGrid("client");
TestRecordingCommunicationSpi nearComm = TestRecordingCommunicationSpi.spi(clnt);
srv.cluster().state(ClusterState.ACTIVE);
final IgniteCache cache = clnt.getOrCreateCache(cfg);
final CountDownLatch commitStart = new CountDownLatch(1);
forceCheckpoint();
nearComm.blockMessages((node, msg) -> msg instanceof GridNearTxPrepareRequest);
if (singleNodeTx)
keys = primaryKeys(srv.cache(cacheName), itmsCount, 0);
else
keys = IntStream.range(0, itmsCount).boxed().collect(Collectors.toList());
Thread t = new Thread(() -> {
try (Transaction tx = clnt.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
keys.forEach(k -> cache.put(k, k));
commitStart.countDown();
tx.commit();
}
});
t.start();
commitStart.await();
nearComm.waitForBlocked();
nearComm.stopBlock();
assertTrue(waitForWalUpdates(G.allGrids().stream().filter(g -> !g.configuration().isClientMode()).collect(Collectors.toList())));
} finally {
failFileIO.set(true);
stopAllGrids(true);
assertTrue(G.allGrids().isEmpty());
}
final IgniteEx srv = (IgniteEx) startGridsMultiThreaded(numSrvNodes);
srv.cluster().state(ClusterState.ACTIVE);
IgniteCache<Integer, Integer> cache = srv.cache(cacheName);
int cSize = cache.size();
boolean pr = cache.get(keys.get(0)) == null;
for (int i : keys) {
Object res = cache.get(i);
if (pr != (res == null))
assertEquals("ethalon=" + pr + ", current=" + res + ", key=" + i, pr, res == null);
}
assert (cSize == itmsCount || cSize == 0) : "unexpected cache size: " + cSize;
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.
the class IgniteCacheClientNodeChangingTopologyTest method optimisticSerializableTx.
/**
* @param nearCfg Near cache configuration.
* @throws Exception If failed.
*/
private void optimisticSerializableTx(NearCacheConfiguration nearCfg) throws Exception {
ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setCacheMode(PARTITIONED);
ccfg.setBackups(1);
ccfg.setAtomicityMode(TRANSACTIONAL);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setRebalanceMode(SYNC);
ccfg.setNearConfiguration(nearCfg);
IgniteEx ignite0 = startGrid(0);
IgniteEx ignite1 = startGrid(1);
awaitPartitionMapExchange();
final Ignite ignite2 = startClientGrid(2);
assertTrue(ignite2.configuration().isClientMode());
final Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < 100; i++) map.put(i, i);
TestCommunicationSpi spi = (TestCommunicationSpi) ignite2.configuration().getCommunicationSpi();
spi.blockMessages(GridNearTxPrepareRequest.class, ignite0.localNode().id());
spi.blockMessages(GridNearTxPrepareRequest.class, ignite1.localNode().id());
spi.record(GridNearTxPrepareRequest.class);
final IgniteCache<Integer, Integer> cache = ignite2.cache(DEFAULT_CACHE_NAME);
IgniteInternalFuture<?> putFut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.currentThread().setName("put-thread");
try (Transaction tx = ignite2.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.putAll(map);
tx.commit();
}
return null;
}
});
assertFalse(putFut.isDone());
IgniteEx ignite3 = startGrid(3);
awaitPartitionMapExchange();
log.info("Stop block1.");
spi.stopBlock();
putFut.get();
spi.record(null);
checkData(map, null, cache, 4);
List<Object> msgs = spi.recordedMessages();
for (Object msg : msgs) assertTrue(((GridNearTxPrepareRequest) msg).firstClientRequest());
assertEquals(5, msgs.size());
ignite3.close();
awaitPartitionMapExchange();
for (int i = 0; i < 100; i++) map.put(i, i + 1);
spi.blockMessages(GridNearTxPrepareRequest.class, ignite0.localNode().id());
spi.blockMessages(GridNearTxPrepareRequest.class, ignite1.localNode().id());
spi.record(GridNearTxPrepareRequest.class);
putFut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.currentThread().setName("put-thread");
try (Transaction tx = ignite2.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
tx.commit();
}
return null;
}
});
startGrid(3);
awaitPartitionMapExchange();
log.info("Stop block2.");
spi.stopBlock();
putFut.get();
spi.record(null);
msgs = spi.recordedMessages();
for (Object msg : msgs) assertTrue(((GridNearTxPrepareRequest) msg).firstClientRequest());
assertEquals(5, msgs.size());
checkData(map, null, cache, 4);
for (int i = 0; i < 100; i++) map.put(i, i + 2);
try (Transaction tx = ignite2.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
cache.putAll(map);
tx.commit();
}
checkData(map, null, cache, 4);
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.
the class TxRollbackOnTimeoutTest method doTestRollbackOnTimeoutTxRemap.
/**
* @param concurrency Concurrency.
* @param isolation Isolation.
* @param clientWait {@code True} to wait remap on client, otherwise wait remap on server.
*/
private void doTestRollbackOnTimeoutTxRemap(TransactionConcurrency concurrency, TransactionIsolation isolation, boolean clientWait) throws Exception {
IgniteEx client = (IgniteEx) startClient();
Ignite crd = grid(0);
assertTrue(crd.cluster().localNode().order() == 1);
List<Integer> keys = movingKeysAfterJoin(grid(1), CACHE_NAME, 1);
// Delay exchange finish on server nodes if clientWait=true, or on all nodes otherwise (excluding joining node).
spi(crd).blockMessages((node, msg) -> node.order() < 5 && msg instanceof GridDhtPartitionsFullMessage && (!clientWait || node.order() != grid(1).cluster().localNode().order()));
// Delay prepare until exchange is finished.
spi(client).blockMessages((node, msg) -> {
boolean block = false;
if (concurrency == PESSIMISTIC) {
if (msg instanceof GridNearLockRequest) {
block = true;
assertEquals(GRID_CNT + 1, ((GridNearLockRequest) msg).topologyVersion().topologyVersion());
}
} else {
if (msg instanceof GridNearTxPrepareRequest) {
block = true;
assertEquals(GRID_CNT + 1, ((GridNearTxPrepareRequest) msg).topologyVersion().topologyVersion());
}
}
return block;
});
// Start tx and map on topver=GRID_CNT + 1
// Delay map until exchange.
// Start new node.
IgniteInternalFuture fut0 = runAsync(new Runnable() {
@Override
public void run() {
try (Transaction tx = client.transactions().txStart(concurrency, isolation, 5000, 1)) {
client.cache(CACHE_NAME).put(keys.get(0), 0);
tx.commit();
fail();
} catch (Exception e) {
assertTrue(X.hasCause(e, TransactionTimeoutException.class));
}
}
});
IgniteInternalFuture fut1 = runAsync(new Runnable() {
@Override
public void run() {
try {
// TX is trying to prepare on prev top ver.
spi(client).waitForBlocked();
startGrid(GRID_CNT);
} catch (Exception e) {
fail(e.getMessage());
}
}
});
IgniteInternalFuture fut2 = runAsync(new Runnable() {
@Override
public void run() {
try {
// Wait for all full messages to be ready.
spi(crd).waitForBlocked(GRID_CNT + (clientWait ? 0 : 1));
// Trigger remap.
spi(client).stopBlock();
} catch (Exception e) {
fail(e.getMessage());
}
}
});
fut0.get(30_000);
fut1.get(30_000);
fut2.get(30_000);
spi(crd).stopBlock();
// FIXME: If using awaitPartitionMapExchange for waiting it some times fail while waiting for owners.
IgniteInternalFuture<?> topFut = ((IgniteEx) client).context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(GRID_CNT + 2, 1));
assertNotNull(topFut);
topFut.get(10_000);
checkFutures();
}
Aggregations