Search in sources :

Example 6 with GridNearTxPrepareRequest

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.

the class OnePhaseCommitAndNodeLeftTest method startTransactionAndFailPrimary.

/**
 * Stars cluster and stops exactly primary node for a cache operation specified in parameters.
 *
 * @param cacheClosure Closure for a cache operation.
 * @throws Exception If failed.
 */
private void startTransactionAndFailPrimary(CI3<Ignite, Integer, String> cacheClosure) throws Exception {
    Ignite ignite0 = startGrids(2);
    awaitPartitionMapExchange();
    IgniteCache cache = ignite0.cache(DEFAULT_CACHE_NAME);
    Integer key = primaryKey(ignite(1).cache(DEFAULT_CACHE_NAME));
    ClusterNode node1 = ignite0.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
    assertFalse("Found key is local: " + key + " on node " + node1, node1.isLocal());
    TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(ignite0);
    spi.blockMessages((node, msg) -> {
        if (msg instanceof GridNearTxPrepareRequest) {
            GridNearTxPrepareRequest putReq = (GridNearTxPrepareRequest) msg;
            if (!F.isEmpty(putReq.writes())) {
                int cacheId = putReq.writes().iterator().next().cacheId();
                if (cacheId == CU.cacheId(DEFAULT_CACHE_NAME)) {
                    assertTrue(putReq.onePhaseCommit());
                    String nodeName = node.attribute(ATTR_IGNITE_INSTANCE_NAME);
                    return true;
                }
            }
        }
        return false;
    });
    String testVal = "Tets value";
    IgniteInternalFuture putFut = GridTestUtils.runAsync(() -> {
        try {
            cacheClosure.apply(ignite0, key, testVal);
        // We are not sure, operation completed correctly or not when backups are zero.
        // Exception could be thrown or not.
        // assertTrue(((CacheConfiguration)cache.getConfiguration(CacheConfiguration.class)).getBackups() != 0);
        } catch (Exception e) {
            checkException(cache, e);
        }
    });
    spi.waitForBlocked();
    assertFalse(putFut.isDone());
    ignite(1).close();
    spi.stopBlock();
    try {
        putFut.get();
        try {
            assertEquals(testVal, cache.get(key));
            assertTrue(((CacheConfiguration) cache.getConfiguration(CacheConfiguration.class)).getBackups() != 0);
        } catch (Exception e) {
            checkException(cache, e);
        }
    } catch (Exception e) {
        if (X.hasCause(e, TransactionRollbackException.class))
            info("Transaction was rolled back [err=" + e.getMessage() + "]");
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) IgniteCache(org.apache.ignite.IgniteCache) Ignite(org.apache.ignite.Ignite) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException) IgniteException(org.apache.ignite.IgniteException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with GridNearTxPrepareRequest

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.

the class IgniteCacheClientNodeChangingTopologyTest method testOptimisticTxMessageClientFirstFlag.

/**
 * @throws Exception If failed.
 */
@Test
public void testOptimisticTxMessageClientFirstFlag() throws Exception {
    ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setCacheMode(PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setAtomicityMode(TRANSACTIONAL);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setRebalanceMode(SYNC);
    IgniteEx ignite0 = startGrid(0);
    IgniteEx ignite1 = startGrid(1);
    IgniteEx ignite2 = startGrid(2);
    awaitPartitionMapExchange();
    Ignite ignite3 = startClientGrid(3);
    assertTrue(ignite3.configuration().isClientMode());
    TestCommunicationSpi spi = (TestCommunicationSpi) ignite3.configuration().getCommunicationSpi();
    IgniteCache<Integer, Integer> cache = ignite3.cache(DEFAULT_CACHE_NAME);
    List<Integer> keys0 = primaryKeys(ignite0.cache(DEFAULT_CACHE_NAME), 2, 0);
    List<Integer> keys1 = primaryKeys(ignite1.cache(DEFAULT_CACHE_NAME), 2, 0);
    List<Integer> keys2 = primaryKeys(ignite2.cache(DEFAULT_CACHE_NAME), 2, 0);
    LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
    map.put(keys0.get(0), 1);
    map.put(keys1.get(0), 2);
    map.put(keys2.get(0), 3);
    map.put(keys0.get(1), 4);
    map.put(keys1.get(1), 5);
    map.put(keys2.get(1), 6);
    spi.record(GridNearTxPrepareRequest.class);
    try (Transaction tx = ignite3.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
        for (Map.Entry<Integer, Integer> e : map.entrySet()) cache.put(e.getKey(), e.getValue());
        tx.commit();
    }
    checkClientPrepareMessages(spi.recordedMessages(), 6);
    checkData(map, null, cache, 4);
    cache.putAll(map);
    checkClientPrepareMessages(spi.recordedMessages(), 6);
    spi.record(null);
    checkData(map, null, cache, 4);
    IgniteCache<Integer, Integer> cache0 = ignite0.cache(DEFAULT_CACHE_NAME);
    TestCommunicationSpi spi0 = (TestCommunicationSpi) ignite0.configuration().getCommunicationSpi();
    spi0.record(GridNearTxPrepareRequest.class);
    cache0.putAll(map);
    spi0.record(null);
    List<Object> msgs = spi0.recordedMessages();
    assertEquals(4, msgs.size());
    for (Object msg : msgs) assertFalse(((GridNearTxPrepareRequest) msg).firstClientRequest());
    checkData(map, null, cache, 4);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with GridNearTxPrepareRequest

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.

the class TxPartitionCounterStateAbstractTest method runOnPartition.

/**
 * Runs a scenario.
 *
 * @param partId Partition id.
 * @param part2Sup Optional second partition supplier.
 * @param nodesCnt Nodes count.
 * @param clo Callback closure which produces {@link TxCallback}.
 * @param sizes Sizes.
 */
protected Map<Integer, T2<Ignite, List<Ignite>>> runOnPartition(int partId, @Nullable Supplier<Integer> part2Sup, int backups, int nodesCnt, IgniteClosure<Map<Integer, T2<Ignite, List<Ignite>>>, TxCallback> clo, int[] sizes) throws Exception {
    this.backups = backups;
    IgniteEx crd = startGrids(nodesCnt);
    crd.cluster().active(true);
    configureBaselineAutoAdjust();
    assertEquals(0, crd.cache(DEFAULT_CACHE_NAME).size());
    int[][] ranges = new int[sizes.length][2];
    int totalKeys = 0;
    for (int i = 0; i < sizes.length; i++) {
        int size = sizes[i];
        ranges[i] = new int[] { totalKeys, size };
        totalKeys += size;
    }
    IgniteEx client = startClientGrid(CLIENT_GRID_NAME);
    // Preload one key to partition to enable historical rebalance.
    List<Integer> preloadKeys = loadDataToPartition(partId, "client", DEFAULT_CACHE_NAME, PRELOAD_KEYS_CNT, 0);
    forceCheckpoint();
    assertPartitionsSame(idleVerify(client, DEFAULT_CACHE_NAME));
    List<Integer> keys = partitionKeys(crd.cache(DEFAULT_CACHE_NAME), partId, totalKeys, PRELOAD_KEYS_CNT);
    assertFalse(preloadKeys.get(0).equals(keys.get(0)));
    Ignite prim = primaryNode(keys.get(0), DEFAULT_CACHE_NAME);
    List<Ignite> backupz = backups == 0 ? null : backups == 1 ? Collections.singletonList(backupNode(keys.get(0), DEFAULT_CACHE_NAME)) : backupNodes(keys.get(0), DEFAULT_CACHE_NAME);
    final TestRecordingCommunicationSpi clientWrappedSpi = spi(client);
    Map<IgniteUuid, GridCacheVersion> futMap = new ConcurrentHashMap<>();
    Map<GridCacheVersion, GridCacheVersion> nearToLocVerMap = new ConcurrentHashMap<>();
    Map<Integer, T2<Ignite, List<Ignite>>> txTop = new HashMap<>();
    txTop.put(partId, new T2<>(prim, backupz));
    List<Integer> keysPart2 = part2Sup == null ? null : partitionKeys(crd.cache(DEFAULT_CACHE_NAME), part2Sup.get(), sizes.length, 0);
    log.info("TX: topology [part1=" + partId + ", primary=" + prim.name() + ", backups=" + F.transform(backupz, Ignite::name));
    if (part2Sup != null) {
        int partId2 = part2Sup.get();
        Ignite prim2 = primaryNode(keysPart2.get(0), DEFAULT_CACHE_NAME);
        assertNotSame(prim, prim2);
        List<Ignite> backupz2 = backupNodes(keysPart2.get(0), DEFAULT_CACHE_NAME);
        txTop.put(partId2, new T2<>(prim2, backupz2));
        log.info("TX: topology [part2=" + partId2 + ", primary=" + prim2.name() + ", backups=" + F.transform(backupz2, Ignite::name));
    }
    TxCallback cb = clo.apply(txTop);
    clientWrappedSpi.blockMessages((node, msg) -> {
        if (msg instanceof GridNearTxPrepareRequest) {
            IgniteEx to = IgnitionEx.gridxx(node.id());
            GridNearTxPrepareRequest req = (GridNearTxPrepareRequest) msg;
            if (!req.last())
                return false;
            futMap.put(req.futureId(), req.version());
            return cb.beforePrimaryPrepare(to, req.version().asIgniteUuid(), createSendFuture(clientWrappedSpi, msg));
        } else if (msg instanceof GridNearTxFinishRequest) {
            IgniteEx to = IgnitionEx.gridxx(node.id());
            GridNearTxFinishRequest req = (GridNearTxFinishRequest) msg;
            futMap.put(req.futureId(), req.version());
            IgniteInternalTx tx = findTx(to, req.version(), true);
            assertNotNull(tx);
            return cb.beforePrimaryFinish(to, tx, createSendFuture(clientWrappedSpi, msg));
        }
        return false;
    });
    spi(prim).blockMessages(createPrimaryMessagePredicate(spi(prim), futMap, nearToLocVerMap, cb));
    if (part2Sup != null) {
        Ignite prim2 = txTop.get(part2Sup.get()).get1();
        spi(prim2).blockMessages(createPrimaryMessagePredicate(spi(prim2), futMap, nearToLocVerMap, cb));
    }
    if (backupz != null) {
        for (Ignite backup : backupz) spi(backup).blockMessages(createBackupMessagePredicate(spi(backup), futMap, cb));
        if (part2Sup != null) {
            for (Ignite backup : txTop.get(part2Sup.get()).get2()) spi(backup).blockMessages(createBackupMessagePredicate(spi(backup), futMap, cb));
        }
    }
    assertNotNull(client.cache(DEFAULT_CACHE_NAME));
    AtomicInteger idx = new AtomicInteger();
    CyclicBarrier b = new CyclicBarrier(sizes.length);
    IgniteInternalFuture<Long> fut = runMultiThreadedAsync(() -> {
        int txIdx = idx.getAndIncrement();
        int[] range = ranges[txIdx];
        String lb = "t" + txIdx;
        try (Transaction tx = client.transactions().withLabel(lb).txStart()) {
            cb.onTxStart(tx, txIdx);
            // Wait should always success.
            U.awaitQuiet(b);
            for (Integer key : keys.subList(range[0], range[0] + range[1])) client.cache(DEFAULT_CACHE_NAME).put(key, 0);
            if (keysPart2 != null) {
                // Force 2PC.
                client.cache(DEFAULT_CACHE_NAME).put(keysPart2.get(txIdx), 0);
            }
            tx.commit();
        } catch (Throwable ignored) {
        // No-op.
        }
    }, sizes.length, "tx-thread");
    try {
        // TODO verify all created futures.
        fut.get(TEST_TIMEOUT);
    } catch (IgniteCheckedException e) {
        Throwable err = testFailed.get();
        if (err != null)
            log.error("Test execution failed", err);
        fail("Test is timed out");
    }
    return txTop;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Ignite(org.apache.ignite.Ignite) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GridNearTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishRequest) T2(org.apache.ignite.internal.util.typedef.T2) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest)

Example 9 with GridNearTxPrepareRequest

use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest in project ignite by apache.

the class TxOnCachesStopTest method runTxOnCacheStop.

/**
 * @param conc Concurrency mode.
 * @param iso Isolation level.
 * @param ig Client node.
 * @param runConc {@code true} if a cache should be destroyed concurrently.
 * @throws Exception If Failed.
 */
private void runTxOnCacheStop(TransactionConcurrency conc, TransactionIsolation iso, Ignite ig, boolean runConc) throws Exception {
    if ((conc == TransactionConcurrency.OPTIMISTIC) && (MvccFeatureChecker.forcedMvcc()))
        return;
    if (log.isInfoEnabled()) {
        log.info("Starting runTxOnCacheStop " + "[concurrency=" + conc + ", isolation=" + iso + ", blockPrepareRequests=" + !runConc + ']');
    }
    CountDownLatch destroyLatch = new CountDownLatch(1);
    final IgniteCache<Integer, byte[]> cache = ig.getOrCreateCache(destroyCacheCfg);
    final IgniteCache<Integer, byte[]> cache2 = ig.getOrCreateCache(surviveCacheCfg);
    TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(ig);
    IgniteInternalFuture f0 = GridTestUtils.runAsync(() -> {
        try {
            destroyLatch.await();
            IgniteInternalFuture f = GridTestUtils.runAsync(() -> {
                doSleep(rnd.nextInt(500));
                spi.stopBlock();
            });
            cache.destroy();
            f.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    spi.blockMessages((node, msg) -> {
        if (msg instanceof GridNearTxPrepareRequest) {
            destroyLatch.countDown();
            return runConc;
        }
        return false;
    });
    IgniteInternalFuture f1 = GridTestUtils.runAsync(() -> {
        byte[] val = new byte[1024];
        try (Transaction tx = ig.transactions().txStart(conc, iso, 1_000, 2)) {
            cache.put(100, val);
            cache2.put(100, val);
            tx.commit();
        } catch (IgniteException e) {
            assertTrue(X.hasCause(e, IgniteTxTimeoutCheckedException.class) || X.hasCause(e, CacheInvalidStateException.class) || X.hasCause(e, IgniteException.class));
        }
    });
    f1.get();
    f0.get();
    try {
        assertEquals(cache2.get(100), cache.get(100));
    } catch (IllegalStateException e) {
        assertTrue(X.hasCause(e, CacheStoppedException.class));
    }
    spi.stopBlock();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) GridNearTxPrepareRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteFutureTimeoutException(org.apache.ignite.lang.IgniteFutureTimeoutException) CacheException(javax.cache.CacheException) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheStoppedException(org.apache.ignite.internal.processors.cache.CacheStoppedException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException)

Aggregations

GridNearTxPrepareRequest (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest)9 Ignite (org.apache.ignite.Ignite)7 Transaction (org.apache.ignite.transactions.Transaction)7 IgniteEx (org.apache.ignite.internal.IgniteEx)6 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteException (org.apache.ignite.IgniteException)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 HashMap (java.util.HashMap)3 CacheException (javax.cache.CacheException)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IgniteCache (org.apache.ignite.IgniteCache)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)2