Search in sources :

Example 1 with OPTIMISTIC

use of org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC in project ignite by apache.

the class TxRecoveryWithConcurrentRollbackTest method testTxDoesntBecomePreparedAfterError.

/**
 * Start 3 servers,
 * start 2 clients,
 * start two OPTIMISTIC transactions with the same key from different client nodes,
 * trying to transfer both to PREPARED state,
 * stop one client node.
 */
@Test
public void testTxDoesntBecomePreparedAfterError() throws Exception {
    backups = 2;
    persistence = true;
    syncMode = FULL_ASYNC;
    final IgniteEx node0 = startGrids(3);
    node0.cluster().state(ACTIVE);
    final IgniteEx client1 = startGrid("client1");
    final IgniteEx client2 = startGrid("client2");
    awaitPartitionMapExchange();
    final IgniteCache<Object, Object> cache = client1.cache(DEFAULT_CACHE_NAME);
    final IgniteCache<Object, Object> cache2 = client2.cache(DEFAULT_CACHE_NAME);
    final Integer pk = primaryKey(node0.cache(DEFAULT_CACHE_NAME));
    CountDownLatch txPrepareLatch = new CountDownLatch(1);
    GridTestUtils.runMultiThreadedAsync(() -> {
        try (final Transaction tx = client1.transactions().withLabel("tx1").txStart(OPTIMISTIC, READ_COMMITTED, 5000, 1)) {
            cache.put(pk, Boolean.TRUE);
            TransactionProxyImpl p = (TransactionProxyImpl) tx;
            // To prevent tx rollback on exit from try-with-resource block, this should cause another tx timeout fail.
            spi(client1).blockMessages((node, msg) -> msg instanceof GridNearTxFinishRequest);
            log.info("Test, preparing tx: xid=" + tx.xid() + ", tx=" + tx);
            // Doing only prepare to try to lock the key, commit is not needed here.
            p.tx().prepareNearTxLocal();
            p.tx().currentPrepareFuture().listen(fut -> txPrepareLatch.countDown());
        } catch (Exception e) {
        // No-op.
        }
    }, 1, "tx1-thread");
    try (final Transaction tx = client2.transactions().withLabel("tx2").txStart(OPTIMISTIC, READ_COMMITTED, 5000, 1)) {
        cache2.put(pk, Boolean.TRUE);
        TransactionProxyImpl p = (TransactionProxyImpl) tx;
        log.info("Test, preparing tx: xid=" + tx.xid() + ", tx=" + tx);
        p.tx().prepareNearTxLocal();
        p.tx().currentPrepareFuture().listen(fut -> txPrepareLatch.countDown());
        txPrepareLatch.await(6, TimeUnit.SECONDS);
        if (txPrepareLatch.getCount() > 0)
            fail("Failed to await for tx prepare.");
        AtomicReference<GridDhtTxLocal> dhtTxLocRef = new AtomicReference<>();
        assertTrue(waitForCondition(() -> {
            dhtTxLocRef.set((GridDhtTxLocal) txs(node0).stream().filter(t -> t.state() == TransactionState.PREPARING).findFirst().orElse(null));
            return dhtTxLocRef.get() != null;
        }, 6_000));
        assertNotNull(dhtTxLocRef.get());
        UUID clientNodeToFail = dhtTxLocRef.get().eventNodeId();
        GridDhtTxPrepareFuture prep = GridTestUtils.getFieldValue(dhtTxLocRef.get(), "prepFut");
        prep.get();
        List<IgniteInternalTx> txs = txs(node0);
        String txsStr = txs.stream().map(Object::toString).collect(Collectors.joining(", "));
        log.info("Transactions check point [count=" + txs.size() + ", txs=" + txsStr + "]");
        if (clientNodeToFail.equals(client1.localNode().id()))
            client1.close();
        else if (clientNodeToFail.equals(client2.localNode().id()))
            client2.close();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    U.sleep(500);
    assertEquals(3, grid(1).context().discovery().aliveServerNodes().size());
    assertEquals(txs(client1).toString() + ", " + txs(client2).toString(), 1, txs(client1).size() + txs(client2).size());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) GridNearTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishRequest) Transaction(org.apache.ignite.transactions.Transaction) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) FULL_ASYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_ASYNC) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) GridDhtTxLocal(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocal) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) GridDhtTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishRequest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) OPTIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC) READ_COMMITTED(org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED) Test(org.junit.Test) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) FULL_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC) IgniteCache(org.apache.ignite.IgniteCache) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) StopNodeFailureHandler(org.apache.ignite.failure.StopNodeFailureHandler) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridCacheTxRecoveryRequest(org.apache.ignite.internal.processors.cache.distributed.GridCacheTxRecoveryRequest) TransactionState(org.apache.ignite.transactions.TransactionState) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) PRIMARY_SYNC(org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC) GridDhtTxLocal(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) GridNearTxFinishRequest(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxFinishRequest) UUID(java.util.UUID) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 TRANSACTIONAL (org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL)1 PARTITIONED (org.apache.ignite.cache.CacheMode.PARTITIONED)1 CacheWriteSynchronizationMode (org.apache.ignite.cache.CacheWriteSynchronizationMode)1 FULL_ASYNC (org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_ASYNC)1 FULL_SYNC (org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC)1 PRIMARY_SYNC (org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC)1 ACTIVE (org.apache.ignite.cluster.ClusterState.ACTIVE)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)1 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1