Search in sources :

Example 6 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class IgniteCacheContainsKeyAbstractSelfTest method txContainsKey.

/**
 * Checks if transaction has given key enlisted.
 *
 * @param tx Transaction to check.
 * @param key Key to check.
 * @return {@code True} if key was enlisted.
 */
private boolean txContainsKey(Transaction tx, String key) {
    TransactionProxyImpl<String, Integer> proxy = (TransactionProxyImpl<String, Integer>) tx;
    IgniteInternalTx txEx = proxy.tx();
    IgniteTxEntry entry = txEx.entry(context(0).txKey(context(0).toCacheKeyObject(key)));
    return entry != null;
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 7 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class IgniteCachePrimaryNodeFailureRecoveryAbstractTest method primaryAndOriginatingNodeFailure.

/**
 * @param locBackupKey If {@code true} uses one key which is backup for originating node.
 * @param rollback If {@code true} tests rollback after primary node failure.
 * @param optimistic If {@code true} tests optimistic transaction.
 * @throws Exception If failed.
 */
private void primaryAndOriginatingNodeFailure(final boolean locBackupKey, final boolean rollback, boolean optimistic) throws Exception {
    // TODO IGNITE-6174: when exchanges can be merged test fails because of IGNITE-6174.
    System.setProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1, "true");
    try {
        int orig = 0;
        IgniteCache<Integer, Integer> origCache = jcache(orig);
        Affinity<Integer> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
        Integer key0 = null;
        for (int key = 0; key < 10_000; key++) {
            if (aff.isPrimary(ignite(1).cluster().localNode(), key)) {
                if (locBackupKey == aff.isBackup(ignite(orig).cluster().localNode(), key)) {
                    key0 = key;
                    break;
                }
            }
        }
        assertNotNull(key0);
        final Integer key1 = key0;
        final Integer key2 = primaryKey(jcache(2));
        int backups = origCache.getConfiguration(CacheConfiguration.class).getBackups();
        final Collection<ClusterNode> key1Nodes = (locBackupKey && backups < 2) ? Collections.emptyList() : aff.mapKeyToPrimaryAndBackups(key1);
        final Collection<ClusterNode> key2Nodes = aff.mapKeyToPrimaryAndBackups(key2);
        TestCommunicationSpi commSpi = (TestCommunicationSpi) ignite(orig).configuration().getCommunicationSpi();
        IgniteTransactions txs = ignite(orig).transactions();
        Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ);
        log.info("Put key1 [key1=" + key1 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key1)) + ']');
        origCache.put(key1, key1);
        log.info("Put key2 [key2=" + key2 + ", nodes=" + U.nodeIds(aff.mapKeyToPrimaryAndBackups(key2)) + ']');
        origCache.put(key2, key2);
        log.info("Start prepare.");
        GridNearTxLocal txEx = ((TransactionProxyImpl) tx).tx();
        // Do not allow to finish prepare for key2.
        commSpi.blockMessages(ignite(2).cluster().localNode().id());
        IgniteInternalFuture<?> prepFut = txEx.prepareNearTxLocal();
        waitPrepared(ignite(1));
        log.info("Stop one primary node.");
        stopGrid(1);
        // Wait some time to catch possible issues in tx recovery.
        U.sleep(1000);
        if (!rollback) {
            commSpi.stopBlock();
            prepFut.get(10_000);
        }
        log.info("Stop originating node.");
        stopGrid(orig);
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                try {
                    checkKey(key1, rollback, key1Nodes, 0);
                    checkKey(key2, rollback, key2Nodes, 0);
                    return true;
                } catch (AssertionError e) {
                    log.info("Check failed: " + e);
                    return false;
                }
            }
        }, 5000);
        checkKey(key1, rollback, key1Nodes, 0);
        checkKey(key2, rollback, key2Nodes, 0);
    } finally {
        System.clearProperty(IGNITE_EXCHANGE_COMPATIBILITY_VER_1);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 8 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class IgniteCachePrimaryNodeFailureRecoveryAbstractTest method primaryNodeFailure.

/**
 * @param locBackupKey If {@code true} uses one key which is backup for originating node.
 * @param rollback If {@code true} tests rollback after primary node failure.
 * @param optimistic If {@code true} tests optimistic transaction.
 * @throws Exception If failed.
 */
private void primaryNodeFailure(boolean locBackupKey, final boolean rollback, boolean optimistic) throws Exception {
    IgniteCache<Integer, Integer> cache0 = jcache(0);
    IgniteCache<Integer, Integer> cache2 = jcache(2);
    Affinity<Integer> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    Integer key0 = null;
    for (int key = 0; key < 10_000; key++) {
        if (aff.isPrimary(ignite(1).cluster().localNode(), key)) {
            if (locBackupKey == aff.isBackup(ignite(0).cluster().localNode(), key)) {
                key0 = key;
                break;
            }
        }
    }
    assertNotNull(key0);
    final Integer key1 = key0;
    final Integer key2 = primaryKey(cache2);
    final Collection<ClusterNode> key1Nodes = aff.mapKeyToPrimaryAndBackups(key1);
    final Collection<ClusterNode> key2Nodes = aff.mapKeyToPrimaryAndBackups(key2);
    TestCommunicationSpi commSpi = (TestCommunicationSpi) ignite(0).configuration().getCommunicationSpi();
    IgniteTransactions txs = ignite(0).transactions();
    try (Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ)) {
        log.info("Put key1: " + key1);
        cache0.put(key1, key1);
        log.info("Put key2: " + key2);
        cache0.put(key2, key2);
        log.info("Start prepare.");
        GridNearTxLocal txEx = ((TransactionProxyImpl) tx).tx();
        // Do not allow to finish prepare for key2.
        commSpi.blockMessages(ignite(2).cluster().localNode().id());
        IgniteInternalFuture<?> prepFut = txEx.prepareNearTxLocal();
        waitPrepared(ignite(1));
        log.info("Stop one primary node.");
        stopGrid(1);
        // Wait some time to catch possible issues in tx recovery.
        U.sleep(1000);
        commSpi.stopBlock();
        prepFut.get(10_000);
        if (rollback) {
            log.info("Rollback.");
            tx.rollback();
        } else {
            log.info("Commit.");
            tx.commit();
        }
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                checkKey(key1, rollback, key1Nodes, 0);
                checkKey(key2, rollback, key2Nodes, 0);
                return true;
            } catch (AssertionError e) {
                log.info("Check failed: " + e);
                return false;
            }
        }
    }, 5000);
    checkKey(key1, rollback, key1Nodes, 0);
    checkKey(key2, rollback, key2Nodes, 0);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTransactions(org.apache.ignite.IgniteTransactions) Transaction(org.apache.ignite.transactions.Transaction) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 9 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class TransactionsMXBeanImplTest method checkLongOperationsDumpTimeoutViaTxMxBean.

/**
 * Checking changes and receiving lrt through MXBean.
 *
 * @param defTimeout Default lrt timeout.
 * @param newTimeout New lrt timeout.
 * @param waitTimeTx Waiting time for a lrt.
 * @param expectTx Expect or not a lrt to log.
 * @throws Exception If failed.
 */
private void checkLongOperationsDumpTimeoutViaTxMxBean(long defTimeout, long newTimeout, long waitTimeTx, boolean expectTx) throws Exception {
    IgniteEx ignite = startGrid(0);
    IgniteEx ignite1 = startGrid(1);
    ignite.cluster().state(ACTIVE);
    TransactionsMXBean txMXBean = txMXBean(0);
    TransactionsMXBean txMXBean1 = txMXBean(1);
    assertEquals(defTimeout, txMXBean.getLongOperationsDumpTimeout());
    assertEquals(defTimeout, txMXBean1.getLongOperationsDumpTimeout());
    Transaction tx = ignite.transactions().txStart();
    LogListener lrtLogLsnr = matches("First 10 long running transactions [total=1]").build();
    LogListener txLogLsnr = matches(((TransactionProxyImpl) tx).tx().xidVersion().toString()).build();
    testLog.registerListener(lrtLogLsnr);
    testLog.registerListener(txLogLsnr);
    txMXBean.setLongOperationsDumpTimeout(newTimeout);
    assertEquals(newTimeout, ignite.context().cache().context().tm().longOperationsDumpTimeout());
    assertEquals(newTimeout, ignite1.context().cache().context().tm().longOperationsDumpTimeout());
    if (expectTx)
        assertTrue(waitForCondition(() -> lrtLogLsnr.check() && txLogLsnr.check(), waitTimeTx));
    else
        assertFalse(waitForCondition(() -> lrtLogLsnr.check() && txLogLsnr.check(), waitTimeTx));
}
Also used : TransactionsMXBean(org.apache.ignite.mxbean.TransactionsMXBean) Transaction(org.apache.ignite.transactions.Transaction) LogListener(org.apache.ignite.testframework.LogListener) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 10 with TransactionProxyImpl

use of org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl in project ignite by apache.

the class CacheTxFastFinishTest method checkNormalTxFinish.

/**
 * @param tx Transaction.
 * @param commit Commit flag.
 * @param readOnly {@code true} if checked tx did no writes.
 */
protected void checkNormalTxFinish(Transaction tx, boolean commit, boolean readOnly) {
    IgniteInternalTx tx0 = ((TransactionProxyImpl) tx).tx();
    if (commit) {
        tx.commit();
        checkNormalCommittedTx(tx0, readOnly);
    } else {
        tx.rollback();
        assertNull(prepareFuture(tx0));
        assertNotNull(finishFuture(tx0));
    }
}
Also used : IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Aggregations

TransactionProxyImpl (org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)19 Transaction (org.apache.ignite.transactions.Transaction)13 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)12 ArrayList (java.util.ArrayList)9 Ignite (org.apache.ignite.Ignite)9 TestRecordingCommunicationSpi (org.apache.ignite.internal.TestRecordingCommunicationSpi)9 IgniteInternalTx (org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IgniteCache (org.apache.ignite.IgniteCache)8 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 Test (org.junit.Test)8 Collection (java.util.Collection)7 IgniteEx (org.apache.ignite.internal.IgniteEx)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)6 List (java.util.List)5 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)5 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)5 Optional (java.util.Optional)4