Search in sources :

Example 21 with GridNearTxLocal

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

the class IgfsMetaManager method format.

/**
     * Deletes (moves to TRASH) all elements under the root folder.
     *
     * @return The new Id if the artificially created folder containing all former root
     * elements moved to TRASH folder.
     * @throws IgniteCheckedException On error.
     */
@SuppressWarnings("RedundantCast")
IgniteUuid format() throws IgniteCheckedException {
    if (busyLock.enterBusy()) {
        try {
            validTxState(false);
            IgniteUuid trashId = IgfsUtils.randomTrashId();
            try (GridNearTxLocal tx = startTx()) {
                // NB: We may lock root because its id is less than any other id:
                final IgfsEntryInfo rootInfo = lockIds(IgfsUtils.ROOT_ID, trashId).get(IgfsUtils.ROOT_ID);
                assert rootInfo != null;
                Map<String, IgfsListingEntry> rootListingMap = rootInfo.listing();
                assert rootListingMap != null;
                if (rootListingMap.isEmpty())
                    // Root is empty, nothing to do.
                    return null;
                // Construct new info and move locked entries from root to it.
                Map<String, IgfsListingEntry> transferListing = new HashMap<>(rootListingMap);
                IgfsEntryInfo newInfo = IgfsUtils.createDirectory(IgniteUuid.randomUuid(), transferListing, (Map<String, String>) null);
                createNewEntry(newInfo, trashId, newInfo.id().toString());
                // Remove listing entries from root.
                // Note that root directory properties and other attributes are preserved:
                id2InfoPrj.put(IgfsUtils.ROOT_ID, rootInfo.listing(null));
                tx.commit();
                signalDeleteWorker();
                return newInfo.id();
            }
        } finally {
            busyLock.leaveBusy();
        }
    } else
        throw new IllegalStateException("Failed to perform format because Grid is stopping.");
}
Also used : HashMap(java.util.HashMap) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 22 with GridNearTxLocal

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

the class IgniteCacheSystemTransactionsSelfTest method testSystemTxInsideUserTx.

/**
     * @throws Exception If failed.
     */
public void testSystemTxInsideUserTx() throws Exception {
    IgniteKernal ignite = (IgniteKernal) grid(0);
    IgniteCache<Object, Object> jcache = ignite.cache(DEFAULT_CACHE_NAME);
    try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        jcache.get("1");
        jcache.put("1", "11");
        IgniteInternalCache<Object, Object> utilityCache = ignite.context().cache().utilityCache();
        utilityCache.getAndPutIfAbsent("2", "2");
        try (GridNearTxLocal itx = utilityCache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) {
            assertEquals(null, utilityCache.get("1"));
            assertEquals("2", utilityCache.get("2"));
            assertEquals(null, utilityCache.get("3"));
            utilityCache.getAndPut("3", "3");
            itx.commit();
        }
        jcache.put("2", "22");
        tx.commit();
    }
    checkTransactionsCommitted();
    checkEntries(DEFAULT_CACHE_NAME, "1", "11", "2", "22", "3", null);
    checkEntries(CU.UTILITY_CACHE_NAME, "1", null, "2", "2", "3", "3");
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Transaction(org.apache.ignite.transactions.Transaction) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 23 with GridNearTxLocal

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

the class HibernateReadWriteAccessStrategy method unlock.

/**
     *
     * @param ctx Transaction context.
     * @param key Key.
     */
private void unlock(TxContext ctx, Object key) {
    if (ctx.unlocked(key)) {
        // Finish transaction if last key is unlocked.
        txCtx.remove();
        GridNearTxLocal tx = cache.tx();
        assert tx != null;
        try {
            tx.proxy().commit();
        } finally {
            tx.proxy().close();
        }
        assert cache.tx() == null;
    }
}
Also used : GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 24 with GridNearTxLocal

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

the class GridCacheAdapter method asyncOp.

/**
     * @param op Cache operation.
     * @param <T> Return type.
     * @return Future.
     */
@SuppressWarnings("unchecked")
private <T> IgniteInternalFuture<T> asyncOp(final AsyncOp<T> op) {
    try {
        checkJta();
    } catch (IgniteCheckedException e) {
        return new GridFinishedFuture<>(e);
    }
    if (log.isDebugEnabled())
        log.debug("Performing async op: " + op);
    GridNearTxLocal tx = ctx.tm().threadLocalTx(ctx);
    CacheOperationContext opCtx = ctx.operationContextPerCall();
    final TransactionConfiguration txCfg = CU.transactionConfiguration(ctx, ctx.kernalContext().config());
    if (tx == null || tx.implicit()) {
        // Save value of thread-local flag.
        boolean skipStore = ctx.skipStore();
        int retries = opCtx != null && opCtx.noRetries() ? 1 : MAX_RETRIES;
        if (retries == 1) {
            tx = ctx.tm().newTx(true, op.single(), ctx.systemTx() ? ctx : null, OPTIMISTIC, READ_COMMITTED, txCfg.getDefaultTxTimeout(), !skipStore, 0);
            return asyncOp(tx, op, opCtx, /*retry*/
            false);
        } else {
            AsyncOpRetryFuture<T> fut = new AsyncOpRetryFuture<>(op, retries, opCtx);
            fut.execute(/*retry*/
            false);
            return fut;
        }
    } else
        return asyncOp(tx, op, opCtx, /*retry*/
        false);
}
Also used : TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BROADCAST(org.apache.ignite.internal.GridClosureCallMode.BROADCAST) IGNITE_CACHE_RETRIES_COUNT(org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_RETRIES_COUNT) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)

Example 25 with GridNearTxLocal

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

the class GridCacheAdapter method asyncOp.

/**
     * @param tx Transaction.
     * @param op Cache operation.
     * @param opCtx Cache operation context.
     * @param <T> Return type.
     * @return Future.
     */
@SuppressWarnings("unchecked")
protected <T> IgniteInternalFuture<T> asyncOp(GridNearTxLocal tx, final AsyncOp<T> op, final CacheOperationContext opCtx, final boolean retry) {
    IgniteInternalFuture<T> fail = asyncOpAcquire(retry);
    if (fail != null)
        return fail;
    FutureHolder holder = lastFut.get();
    holder.lock();
    try {
        IgniteInternalFuture fut = holder.future();
        final GridNearTxLocal tx0 = tx;
        if (fut != null && !fut.isDone()) {
            IgniteInternalFuture<T> f = new GridEmbeddedFuture(fut, new IgniteOutClosure<IgniteInternalFuture>() {

                @Override
                public IgniteInternalFuture<T> apply() {
                    if (ctx.kernalContext().isStopping())
                        return new GridFinishedFuture<>(new IgniteCheckedException("Operation has been cancelled (node is stopping)."));
                    return op.op(tx0, opCtx).chain(new CX1<IgniteInternalFuture<T>, T>() {

                        @Override
                        public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
                            try {
                                return tFut.get();
                            } catch (IgniteTxRollbackCheckedException e) {
                                throw e;
                            } catch (IgniteCheckedException e1) {
                                tx0.rollbackNearTxLocalAsync();
                                throw e1;
                            } finally {
                                ctx.shared().txContextReset();
                            }
                        }
                    });
                }
            });
            saveFuture(holder, f, retry);
            return f;
        }
        final IgniteInternalFuture<T> f = op.op(tx, opCtx).chain(new CX1<IgniteInternalFuture<T>, T>() {

            @Override
            public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
                try {
                    return tFut.get();
                } catch (IgniteTxRollbackCheckedException e) {
                    throw e;
                } catch (IgniteCheckedException e1) {
                    tx0.rollbackNearTxLocalAsync();
                    throw e1;
                } finally {
                    ctx.shared().txContextReset();
                }
            }
        });
        saveFuture(holder, f, retry);
        if (tx.implicit())
            ctx.tm().resetContext();
        return f;
    } finally {
        holder.unlock();
    }
}
Also used : GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BROADCAST(org.apache.ignite.internal.GridClosureCallMode.BROADCAST) IGNITE_CACHE_RETRIES_COUNT(org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_RETRIES_COUNT) CX1(org.apache.ignite.internal.util.typedef.CX1)

Aggregations

GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)48 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)20 IgniteUuid (org.apache.ignite.lang.IgniteUuid)10 IgniteException (org.apache.ignite.IgniteException)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 GridClosureException (org.apache.ignite.internal.util.lang.GridClosureException)7 TreeSet (java.util.TreeSet)6 UUID (java.util.UUID)6 Callable (java.util.concurrent.Callable)6 Nullable (org.jetbrains.annotations.Nullable)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 IgfsException (org.apache.ignite.igfs.IgfsException)5 IgfsPathNotFoundException (org.apache.ignite.igfs.IgfsPathNotFoundException)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 IgfsPathAlreadyExistsException (org.apache.ignite.igfs.IgfsPathAlreadyExistsException)4 IgfsPathIsDirectoryException (org.apache.ignite.igfs.IgfsPathIsDirectoryException)4 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)4