Search in sources :

Example 1 with GridDhtTxPrepareFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture in project ignite by apache.

the class GridCommandHandlerAbstractTest method checkUserFutures.

/**
 * Checks if all non-system txs and non-system mvcc futures are finished.
 */
protected void checkUserFutures() {
    for (Ignite ignite : G.allGrids()) {
        IgniteEx ig = (IgniteEx) ignite;
        final Collection<GridCacheFuture<?>> futs = ig.context().cache().context().mvcc().activeFutures();
        boolean hasFutures = false;
        for (GridCacheFuture<?> fut : futs) {
            if (!fut.isDone()) {
                // skipping system tx futures if possible
                if (fut instanceof GridNearTxPrepareFutureAdapter && ((GridNearTxPrepareFutureAdapter) fut).tx().system())
                    continue;
                if (fut instanceof GridDhtTxPrepareFuture && ((GridDhtTxPrepareFuture) fut).tx().system())
                    continue;
                log.error("Expecting no active future [node=" + ig.localNode().id() + ", fut=" + fut + ']');
                hasFutures = true;
            }
        }
        if (hasFutures)
            fail("Some mvcc futures are not finished");
        Collection<IgniteInternalTx> txs = ig.context().cache().context().tm().activeTransactions().stream().filter(tx -> !tx.system()).collect(Collectors.toSet());
        for (IgniteInternalTx tx : txs) log.error("Expecting no active transaction [node=" + ig.localNode().id() + ", tx=" + tx + ']');
        if (!txs.isEmpty())
            fail("Some transaction are not finished");
    }
}
Also used : KEYSTORE_PATH(org.apache.ignite.internal.encryption.AbstractEncryptionTest.KEYSTORE_PATH) Arrays(java.util.Arrays) KeystoreEncryptionSpi(org.apache.ignite.spi.encryption.keystore.KeystoreEncryptionSpi) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) DirectoryStream(java.nio.file.DirectoryStream) DFLT_CHECKPOINT_FREQ(org.apache.ignite.configuration.DataStorageConfiguration.DFLT_CHECKPOINT_FREQ) ByteArrayInputStream(java.io.ByteArrayInputStream) IDLE_DUMP_FILE_PREFIX(org.apache.ignite.internal.processors.cache.verify.VerifyBackupPartitionsDumpTask.IDLE_DUMP_FILE_PREFIX) Arrays.asList(java.util.Arrays.asList) Path(java.nio.file.Path) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) WALMode(org.apache.ignite.configuration.WALMode) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridClientFactory(org.apache.ignite.internal.client.GridClientFactory) CommandHandler(org.apache.ignite.internal.commandline.CommandHandler) Collection(java.util.Collection) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) DFLT_REENCRYPTION_RATE_MBPS(org.apache.ignite.configuration.EncryptionConfiguration.DFLT_REENCRYPTION_RATE_MBPS) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) EncryptionConfiguration(org.apache.ignite.configuration.EncryptionConfiguration) Files.newDirectoryStream(java.nio.file.Files.newDirectoryStream) Handler(java.util.logging.Handler) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) Objects.nonNull(java.util.Objects.nonNull) Files.delete(java.nio.file.Files.delete) System.lineSeparator(java.lang.System.lineSeparator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) U(org.apache.ignite.internal.util.typedef.internal.U) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IdleVerify(org.apache.ignite.internal.commandline.cache.IdleVerify) String.join(java.lang.String.join) KEYSTORE_PASSWORD(org.apache.ignite.internal.encryption.AbstractEncryptionTest.KEYSTORE_PASSWORD) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) AtomicConfiguration(org.apache.ignite.configuration.AtomicConfiguration) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) GridCommandHandlerTestUtils.addSslParams(org.apache.ignite.util.GridCommandHandlerTestUtils.addSslParams) PrintStream(java.io.PrintStream) G(org.apache.ignite.internal.util.typedef.G) F(org.apache.ignite.internal.util.typedef.F) DFLT_REENCRYPTION_BATCH_SIZE(org.apache.ignite.configuration.EncryptionConfiguration.DFLT_REENCRYPTION_BATCH_SIZE) GridNearTxPrepareFutureAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareFutureAdapter) Ignite(org.apache.ignite.Ignite) File(java.io.File) GridCacheFuture(org.apache.ignite.internal.processors.cache.GridCacheFuture) WithSystemProperty(org.apache.ignite.testframework.junits.WithSystemProperty) ConnectorConfiguration(org.apache.ignite.configuration.ConnectorConfiguration) Paths(java.nio.file.Paths) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IGNITE_ENABLE_EXPERIMENTAL_COMMAND(org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND) EVT_CONSISTENCY_VIOLATION(org.apache.ignite.events.EventType.EVT_CONSISTENCY_VIOLATION) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) InputStream(java.io.InputStream) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) GridNearTxPrepareFutureAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareFutureAdapter) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) GridCacheFuture(org.apache.ignite.internal.processors.cache.GridCacheFuture)

Example 2 with GridDhtTxPrepareFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture in project ignite by apache.

the class GridNearTxLocal method prepareAsyncLocal.

/**
 * Prepares next batch of entries in dht transaction.
 *
 * @param req Prepare request.
 * @return Future that will be completed when locks are acquired.
 */
public IgniteInternalFuture<GridNearTxPrepareResponse> prepareAsyncLocal(GridNearTxPrepareRequest req) {
    long timeout = remainingTime();
    if (state() != PREPARING) {
        if (timeout == -1)
            return new GridFinishedFuture<>(timeoutException());
        setRollbackOnly();
        return new GridFinishedFuture<>(rollbackException());
    }
    if (timeout == -1)
        return new GridFinishedFuture<>(timeoutException());
    init();
    GridDhtTxPrepareFuture fut = new GridDhtTxPrepareFuture(cctx, this, timeout, 0, Collections.<IgniteTxKey, GridCacheVersion>emptyMap(), req.last(), needReturnValue() && implicit());
    try {
        userPrepare((serializable() && optimistic()) ? F.concat(false, req.writes(), req.reads()) : req.writes());
        // Make sure to add future before calling prepare on it.
        cctx.mvcc().addFuture(fut);
        if (isSystemInvalidate())
            fut.complete();
        else
            fut.prepare(req);
    } catch (IgniteTxTimeoutCheckedException | IgniteTxOptimisticCheckedException e) {
        fut.onError(e);
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        fut.onError(new IgniteTxRollbackCheckedException("Failed to prepare transaction: " + this, e));
    }
    return chainOnePhasePrepare(fut);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 3 with GridDhtTxPrepareFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture in project ignite by apache.

the class GridNearTxLocal method prepareAsyncLocal.

/**
     * Prepares next batch of entries in dht transaction.
     *
     * @param reads Read entries.
     * @param writes Write entries.
     * @param txNodes Transaction nodes mapping.
     * @param last {@code True} if this is last prepare request.
     * @return Future that will be completed when locks are acquired.
     */
@SuppressWarnings("TypeMayBeWeakened")
public IgniteInternalFuture<GridNearTxPrepareResponse> prepareAsyncLocal(@Nullable Collection<IgniteTxEntry> reads, @Nullable Collection<IgniteTxEntry> writes, Map<UUID, Collection<UUID>> txNodes, boolean last) {
    long timeout = remainingTime();
    if (state() != PREPARING) {
        if (timeout == -1)
            return new GridFinishedFuture<>(new IgniteTxTimeoutCheckedException("Transaction timed out: " + this));
        setRollbackOnly();
        return new GridFinishedFuture<>(new IgniteCheckedException("Invalid transaction state for prepare [state=" + state() + ", tx=" + this + ']'));
    }
    if (timeout == -1)
        return new GridFinishedFuture<>(timeoutException());
    init();
    GridDhtTxPrepareFuture fut = new GridDhtTxPrepareFuture(cctx, this, timeout, 0, Collections.<IgniteTxKey, GridCacheVersion>emptyMap(), last, needReturnValue() && implicit());
    try {
        userPrepare((serializable() && optimistic()) ? F.concat(false, writes, reads) : writes);
        // Make sure to add future before calling prepare on it.
        cctx.mvcc().addFuture(fut);
        if (isSystemInvalidate())
            fut.complete();
        else
            fut.prepare(reads, writes, txNodes);
    } catch (IgniteTxTimeoutCheckedException | IgniteTxOptimisticCheckedException e) {
        fut.onError(e);
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        fut.onError(new IgniteTxRollbackCheckedException("Failed to prepare transaction: " + this, e));
        try {
            rollback();
        } catch (IgniteTxOptimisticCheckedException e1) {
            if (log.isDebugEnabled())
                log.debug("Failed optimistically to prepare transaction [tx=" + this + ", e=" + e1 + ']');
            fut.onError(e);
        } catch (IgniteCheckedException e1) {
            U.error(log, "Failed to rollback transaction: " + this, e1);
        }
    }
    return chainOnePhasePrepare(fut);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture)

Example 4 with GridDhtTxPrepareFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture in project ignite by apache.

the class IgniteTxHandler method processDhtTxPrepareResponse.

/**
 * @param nodeId Node ID.
 * @param res Response.
 */
private void processDhtTxPrepareResponse(UUID nodeId, GridDhtTxPrepareResponse res) {
    try (TraceSurroundings ignored = MTC.support(ctx.kernalContext().tracing().create(TX_PROCESS_DHT_PREPARE_RESP, MTC.span()))) {
        GridDhtTxPrepareFuture fut = (GridDhtTxPrepareFuture) ctx.mvcc().versionedFuture(res.version(), res.futureId());
        if (fut == null) {
            if (txPrepareMsgLog.isDebugEnabled()) {
                txPrepareMsgLog.debug("Failed to find future for dht prepare response [txId=null" + ", dhtTxId=" + res.version() + ", node=" + nodeId + ", res=" + res + ']');
            }
            return;
        } else if (txPrepareMsgLog.isDebugEnabled())
            txPrepareMsgLog.debug("Received dht prepare response [txId=" + fut.tx().nearXidVersion() + ", node=" + nodeId + ']');
        IgniteInternalTx tx = fut.tx();
        assert tx != null;
        res.txState(tx.txState());
        fut.onResult(nodeId, res);
    }
}
Also used : GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 5 with GridDhtTxPrepareFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture 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

GridDhtTxPrepareFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Ignite (org.apache.ignite.Ignite)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)2 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)2 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 String.join (java.lang.String.join)1 System.lineSeparator (java.lang.System.lineSeparator)1 DirectoryStream (java.nio.file.DirectoryStream)1 Files.delete (java.nio.file.Files.delete)1