Search in sources :

Example 76 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class ClientComputeImpl method executeAsync0.

/**
 * @param taskName Task name.
 * @param arg Argument.
 * @param clusterGrp Cluster group.
 * @param flags Flags.
 * @param timeout Timeout.
 */
private <T, R> IgniteClientFuture<R> executeAsync0(String taskName, @Nullable T arg, ClientClusterGroupImpl clusterGrp, byte flags, long timeout) throws ClientException {
    Collection<UUID> nodeIds = clusterGrp.nodeIds();
    if (F.isEmpty(taskName))
        throw new ClientException("Task name can't be null or empty.");
    if (nodeIds != null && nodeIds.isEmpty())
        throw new ClientException("Cluster group is empty.");
    Consumer<PayloadOutputChannel> payloadWriter = ch -> writeExecuteTaskRequest(ch, taskName, arg, nodeIds, flags, timeout);
    Function<PayloadInputChannel, ClientComputeTask<R>> payloadReader = ch -> {
        Long taskId = ch.in().readLong();
        ClientComputeTask<R> task = new ClientComputeTask<>(utils, ch.clientChannel(), taskId);
        ch.clientChannel().addNotificationListener(COMPUTE_TASK_FINISHED, taskId, task);
        return task;
    };
    IgniteClientFuture<ClientComputeTask<R>> initFut = ch.serviceAsync(COMPUTE_TASK_EXECUTE, payloadWriter, payloadReader);
    CompletableFuture<R> resFut = new CompletableFuture<>();
    AtomicReference<Object> cancellationToken = new AtomicReference<>();
    initFut.handle((task, err) -> handleExecuteInitFuture(resFut, cancellationToken, task, err));
    return new IgniteClientFutureImpl<>(resFut, mayInterruptIfRunning -> {
        // 2. initFut has completed - cancel compute future.
        if (!cancellationToken.compareAndSet(null, mayInterruptIfRunning)) {
            GridFutureAdapter<?> fut = (GridFutureAdapter<?>) cancellationToken.get();
            if (!cancelGridFuture(fut, mayInterruptIfRunning))
                return false;
        }
        resFut.cancel(mayInterruptIfRunning);
        return true;
    });
}
Also used : ClientClusterGroup(org.apache.ignite.client.ClientClusterGroup) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) U(org.apache.ignite.internal.util.typedef.internal.U) CompletableFuture(java.util.concurrent.CompletableFuture) RESOURCE_CLOSE(org.apache.ignite.internal.client.thin.ClientOperation.RESOURCE_CLOSE) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) ClientException(org.apache.ignite.client.ClientException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) COMPUTE_TASK_FINISHED(org.apache.ignite.internal.client.thin.ClientNotificationType.COMPUTE_TASK_FINISHED) ClientStatus(org.apache.ignite.internal.processors.platform.client.ClientStatus) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) F(org.apache.ignite.internal.util.typedef.F) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClientFuture(org.apache.ignite.client.IgniteClientFuture) UUID(java.util.UUID) COMPUTE_TASK_EXECUTE(org.apache.ignite.internal.client.thin.ClientOperation.COMPUTE_TASK_EXECUTE) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) ClientCompute(org.apache.ignite.client.ClientCompute) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompletableFuture(java.util.concurrent.CompletableFuture) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ClientException(org.apache.ignite.client.ClientException) UUID(java.util.UUID)

Example 77 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter 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;
        final CX1 clo = new CX1<IgniteInternalFuture<T>, T>() {

            @Override
            public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
                try {
                    return tFut.get();
                } catch (IgniteTxTimeoutCheckedException | IgniteTxRollbackCheckedException | NodeStoppingException | IgniteConsistencyViolationException e) {
                    throw e;
                } catch (IgniteCheckedException e1) {
                    try {
                        tx0.rollbackNearTxLocalAsync();
                    } catch (Throwable e2) {
                        if (e1 != e2)
                            e1.addSuppressed(e2);
                    }
                    throw e1;
                } finally {
                    ctx.shared().txContextReset();
                }
            }
        };
        if (fut != null && !fut.isDone()) {
            IgniteInternalFuture<T> f = new GridEmbeddedFuture(fut, (IgniteOutClosure<IgniteInternalFuture>) () -> {
                GridFutureAdapter resFut = new GridFutureAdapter();
                ctx.kernalContext().closure().runLocalSafe((GridPlainRunnable) () -> {
                    IgniteInternalFuture fut0;
                    if (ctx.kernalContext().isStopping())
                        fut0 = new GridFinishedFuture<>(new IgniteCheckedException("Operation has been cancelled (node or cache is stopping)."));
                    else if (ctx.gate().isStopped())
                        fut0 = new GridFinishedFuture<>(new CacheStoppedException(ctx.name()));
                    else {
                        ctx.operationContextPerCall(opCtx);
                        ctx.shared().txContextReset();
                        try {
                            fut0 = op.op(tx0).chain(clo);
                        } finally {
                            // It is necessary to clear tx context in this thread as well.
                            ctx.shared().txContextReset();
                            ctx.operationContextPerCall(null);
                        }
                    }
                    fut0.listen((IgniteInClosure<IgniteInternalFuture>) fut01 -> {
                        try {
                            resFut.onDone(fut01.get());
                        } catch (Throwable ex) {
                            resFut.onDone(ex);
                        }
                    });
                }, true);
                return resFut;
            });
            saveFuture(holder, f, retry);
            return f;
        }
        /**
         * Wait for concurrent tx operation to finish.
         * See {@link GridDhtTxLocalAdapter#updateLockFuture(IgniteInternalFuture, IgniteInternalFuture)}
         */
        if (!tx0.txState().implicitSingle())
            tx0.txState().awaitLastFuture(ctx.shared());
        IgniteInternalFuture<T> f;
        try {
            f = op.op(tx).chain(clo);
        } finally {
            // It is necessary to clear tx context in this thread as well.
            ctx.shared().txContextReset();
        }
        saveFuture(holder, f, retry);
        if (tx.implicit())
            ctx.tm().resetContext();
        return f;
    } finally {
        holder.unlock();
    }
}
Also used : IgnitionEx(org.apache.ignite.internal.IgnitionEx) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) IgniteTxLocalAdapter(org.apache.ignite.internal.processors.cache.transactions.IgniteTxLocalAdapter) EntryProcessor(javax.cache.processor.EntryProcessor) GridSerializableMap(org.apache.ignite.internal.util.GridSerializableMap) MetricUtils.cacheMetricsRegistryName(org.apache.ignite.internal.processors.metric.impl.MetricUtils.cacheMetricsRegistryName) Map(java.util.Map) Cache(javax.cache.Cache) InvalidObjectException(java.io.InvalidObjectException) DFLT_ALLOW_ATOMIC_OPS_IN_TX(org.apache.ignite.internal.processors.cache.CacheOperationContext.DFLT_ALLOW_ATOMIC_OPS_IN_TX) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) IgniteKernal(org.apache.ignite.internal.IgniteKernal) TC_NO_FAILOVER(org.apache.ignite.internal.processors.task.GridTaskThreadContextKey.TC_NO_FAILOVER) ComputeJobContext(org.apache.ignite.compute.ComputeJobContext) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Externalizable(java.io.Externalizable) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) CIX2(org.apache.ignite.internal.util.typedef.CIX2) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CIX3(org.apache.ignite.internal.util.typedef.CIX3) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) READ_COMMITTED(org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED) ComputeTaskInternalFuture(org.apache.ignite.internal.ComputeTaskInternalFuture) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) Executors(java.util.concurrent.Executors) IgniteCache(org.apache.ignite.IgniteCache) GridCacheAffinityImpl(org.apache.ignite.internal.processors.cache.affinity.GridCacheAffinityImpl) GridCacheRawVersionedEntry(org.apache.ignite.internal.processors.cache.version.GridCacheRawVersionedEntry) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) PlatformCacheEntryFilter(org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) ComputeJob(org.apache.ignite.compute.ComputeJob) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) Affinity(org.apache.ignite.cache.affinity.Affinity) IgniteTxLocalEx(org.apache.ignite.internal.processors.cache.transactions.IgniteTxLocalEx) U(org.apache.ignite.internal.util.typedef.internal.U) Callable(java.util.concurrent.Callable) IgniteLogger(org.apache.ignite.IgniteLogger) IgniteDrDataStreamerCacheUpdater(org.apache.ignite.internal.processors.dr.IgniteDrDataStreamerCacheUpdater) SecurityPermission(org.apache.ignite.plugin.security.SecurityPermission) Supplier(java.util.function.Supplier) EntryProcessorException(javax.cache.processor.EntryProcessorException) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) CI1(org.apache.ignite.internal.util.typedef.CI1) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) MvccUtils(org.apache.ignite.internal.processors.cache.mvcc.MvccUtils) S(org.apache.ignite.internal.util.typedef.internal.S) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) GridInternal(org.apache.ignite.internal.processors.task.GridInternal) C2(org.apache.ignite.internal.util.typedef.C2) C1(org.apache.ignite.internal.util.typedef.C1) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) A(org.apache.ignite.internal.util.typedef.internal.A) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) DataStreamerImpl(org.apache.ignite.internal.processors.datastreamer.DataStreamerImpl) T2(org.apache.ignite.internal.util.typedef.T2) ObjectStreamException(java.io.ObjectStreamException) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) TransactionCheckedException(org.apache.ignite.internal.transactions.TransactionCheckedException) MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) BROADCAST(org.apache.ignite.internal.GridClosureCallMode.BROADCAST) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) CX1(org.apache.ignite.internal.util.typedef.CX1) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteExternalizableExpiryPolicy(org.apache.ignite.internal.processors.cache.distributed.IgniteExternalizableExpiryPolicy) SERIALIZABLE(org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE) SortedSet(java.util.SortedSet) ObjectOutput(java.io.ObjectOutput) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) DR_LOAD(org.apache.ignite.internal.processors.dr.GridDrType.DR_LOAD) ComputeJobResultPolicy(org.apache.ignite.compute.ComputeJobResultPolicy) X(org.apache.ignite.internal.util.typedef.X) OperationType(org.apache.ignite.internal.processors.performancestatistics.OperationType) GridPlainCallable(org.apache.ignite.internal.util.lang.GridPlainCallable) LoggerResource(org.apache.ignite.resources.LoggerResource) GridDhtTopologyFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture) CachePeekMode(org.apache.ignite.cache.CachePeekMode) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) Collection(java.util.Collection) IgniteException(org.apache.ignite.IgniteException) CacheEntry(org.apache.ignite.cache.CacheEntry) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) TransactionConfiguration(org.apache.ignite.configuration.TransactionConfiguration) IgniteClusterEx(org.apache.ignite.internal.cluster.IgniteClusterEx) OPTIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC) OWNING(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState.OWNING) UUID(java.util.UUID) CI2(org.apache.ignite.internal.util.typedef.CI2) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CacheMetricsMXBean(org.apache.ignite.mxbean.CacheMetricsMXBean) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CU(org.apache.ignite.internal.util.typedef.internal.CU) JobContextResource(org.apache.ignite.resources.JobContextResource) IGNITE_CACHE_RETRIES_COUNT(org.apache.ignite.IgniteSystemProperties.IGNITE_CACHE_RETRIES_COUNT) ObjectInput(java.io.ObjectInput) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) NotNull(org.jetbrains.annotations.NotNull) SortedMap(java.util.SortedMap) DataStreamerEntry(org.apache.ignite.internal.processors.datastreamer.DataStreamerEntry) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) DR_NONE(org.apache.ignite.internal.processors.dr.GridDrType.DR_NONE) ComputeTaskAdapter(org.apache.ignite.compute.ComputeTaskAdapter) AbstractSet(java.util.AbstractSet) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashMap(java.util.HashMap) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) IgniteFeatures(org.apache.ignite.internal.IgniteFeatures) Function(java.util.function.Function) LT(org.apache.ignite.internal.util.typedef.internal.LT) CacheInterceptor(org.apache.ignite.cache.CacheInterceptor) IgniteCallable(org.apache.ignite.lang.IgniteCallable) HashSet(java.util.HashSet) GridCompoundReadRepairFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridCompoundReadRepairFuture) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) GPC(org.apache.ignite.internal.util.typedef.internal.GPC) IgniteClusterNode(org.apache.ignite.internal.managers.discovery.IgniteClusterNode) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) TC_SUBGRID(org.apache.ignite.internal.processors.task.GridTaskThreadContextKey.TC_SUBGRID) F(org.apache.ignite.internal.util.typedef.F) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTxHeuristicCheckedException(org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Semaphore(java.util.concurrent.Semaphore) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) TimeUnit(java.util.concurrent.TimeUnit) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) IgniteTransactionsEx(org.apache.ignite.internal.IgniteTransactionsEx) ComputeJobAdapter(org.apache.ignite.compute.ComputeJobAdapter) CacheMetrics(org.apache.ignite.cache.CacheMetrics) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) Collections(java.util.Collections) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) 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) LT(org.apache.ignite.internal.util.typedef.internal.LT) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) CX1(org.apache.ignite.internal.util.typedef.CX1)

Example 78 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridNearTxLocal method getAllAsync.

/**
 * @param cacheCtx Cache context.
 * @param keys Keys to get.
 * @param deserializeBinary Deserialize binary flag.
 * @param skipVals Skip values flag.
 * @param keepCacheObjects Keep cache objects
 * @param skipStore Skip store flag.
 * @param readRepairStrategy Read Repair strategy.
 * @return Future for this get.
 */
@SuppressWarnings("unchecked")
public <K, V> IgniteInternalFuture<Map<K, V>> getAllAsync(final GridCacheContext cacheCtx, @Nullable final AffinityTopologyVersion entryTopVer, final Collection<KeyCacheObject> keys, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final ReadRepairStrategy readRepairStrategy, final boolean needVer) {
    if (F.isEmpty(keys))
        return new GridFinishedFuture<>(Collections.<K, V>emptyMap());
    if (cacheCtx.mvccEnabled() && !isOperationAllowed(true))
        return txTypeMismatchFinishFuture();
    init();
    int keysCnt = keys.size();
    boolean single = keysCnt == 1;
    try {
        checkValid();
        GridFutureAdapter<?> enlistFut = new GridFutureAdapter<>();
        if (!updateLockFuture(null, enlistFut))
            return new GridFinishedFuture<>(timedOut() ? timeoutException() : rollbackException());
        final Map<K, V> retMap = new GridLeanMap<>(keysCnt);
        final Map<KeyCacheObject, GridCacheVersion> missed = new GridLeanMap<>(pessimistic() ? keysCnt : 0);
        CacheOperationContext opCtx = cacheCtx.operationContextPerCall();
        ExpiryPolicy expiryPlc = opCtx != null ? opCtx.expiry() : null;
        final Collection<KeyCacheObject> lockKeys;
        try {
            lockKeys = enlistRead(cacheCtx, entryTopVer, keys, expiryPlc, retMap, missed, keysCnt, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, readRepairStrategy, needVer);
        } catch (IgniteCheckedException e) {
            return new GridFinishedFuture<>(e);
        } finally {
            finishFuture(enlistFut, null, true);
        }
        if (isRollbackOnly())
            return new GridFinishedFuture<>(timedOut() ? timeoutException() : rollbackException());
        if (single && missed.isEmpty())
            return new GridFinishedFuture<>(retMap);
        // Handle locks.
        if (pessimistic() && !readCommitted() && !skipVals) {
            if (expiryPlc == null)
                expiryPlc = cacheCtx.expiry();
            long accessTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForAccess()) : CU.TTL_NOT_CHANGED;
            long createTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForCreation()) : CU.TTL_NOT_CHANGED;
            long timeout = remainingTime();
            if (timeout == -1)
                return new GridFinishedFuture<>(timeoutException());
            IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(lockKeys, timeout, this, true, true, isolation, isInvalidate(), createTtl, accessTtl);
            final ExpiryPolicy expiryPlc0 = expiryPlc;
            PLC2<Map<K, V>> plc2 = new PLC2<Map<K, V>>() {

                @Override
                public IgniteInternalFuture<Map<K, V>> postLock() throws IgniteCheckedException {
                    if (log.isDebugEnabled())
                        log.debug("Acquired transaction lock for read on keys: " + lockKeys);
                    // Load keys only after the locks have been acquired.
                    for (KeyCacheObject cacheKey : lockKeys) {
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, true, null));
                        if (retMap.containsKey(keyVal))
                            // We already have a return value.
                            continue;
                        IgniteTxKey txKey = cacheCtx.txKey(cacheKey);
                        IgniteTxEntry txEntry = entry(txKey);
                        assert txEntry != null;
                        // Check if there is cached value.
                        while (true) {
                            GridCacheEntryEx cached = txEntry.cached();
                            CacheObject val = null;
                            GridCacheVersion readVer = null;
                            EntryGetResult getRes = null;
                            try {
                                Object transformClo = (!F.isEmpty(txEntry.entryProcessors()) && cctx.gridEvents().isRecordable(EVT_CACHE_OBJECT_READ)) ? F.first(txEntry.entryProcessors()) : null;
                                if (needVer) {
                                    getRes = cached.innerGetVersioned(null, GridNearTxLocal.this, /*update-metrics*/
                                    true, /*event*/
                                    !skipVals, transformClo, resolveTaskName(), null, txEntry.keepBinary(), null);
                                    if (getRes != null) {
                                        val = getRes.value();
                                        readVer = getRes.version();
                                    }
                                } else {
                                    val = cached.innerGet(null, GridNearTxLocal.this, /*read through*/
                                    false, /*metrics*/
                                    true, /*events*/
                                    !skipVals, transformClo, resolveTaskName(), null, txEntry.keepBinary());
                                }
                                // If value is in cache and passed the filter.
                                if (val != null) {
                                    missed.remove(cacheKey);
                                    txEntry.setAndMarkValid(val);
                                    if (!F.isEmpty(txEntry.entryProcessors()))
                                        val = txEntry.applyEntryProcessors(val);
                                    cacheCtx.addResult(retMap, cacheKey, val, skipVals, keepCacheObjects, deserializeBinary, false, getRes, readVer, 0, 0, needVer, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                                    if (readVer != null)
                                        txEntry.entryReadVersion(readVer);
                                }
                                // While.
                                break;
                            } catch (GridCacheEntryRemovedException ignore) {
                                if (log.isDebugEnabled())
                                    log.debug("Got removed exception in get postLock (will retry): " + cached);
                                txEntry.cached(entryEx(cacheCtx, txKey, topologyVersion()));
                            }
                        }
                    }
                    if (!missed.isEmpty() && cacheCtx.isLocal()) {
                        AffinityTopologyVersion topVer = topologyVersionSnapshot();
                        if (topVer == null)
                            topVer = entryTopVer;
                        return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, readRepairStrategy, needVer, expiryPlc0);
                    }
                    if (readRepairStrategy != null) {
                        // Checking and repairing each locked entry (if necessary).
                        return new GridNearReadRepairFuture(topVer != null ? topVer : topologyVersion(), cacheCtx, keys, readRepairStrategy, !skipStore, taskName, deserializeBinary, recovery, cacheCtx.cache().expiryPolicy(expiryPlc0), GridNearTxLocal.this).chain((fut) -> {
                            try {
                                // For every fixed entry.
                                for (Map.Entry<KeyCacheObject, EntryGetResult> entry : fut.get().entrySet()) {
                                    EntryGetResult getRes = entry.getValue();
                                    KeyCacheObject key = entry.getKey();
                                    enlistWrite(cacheCtx, entryTopVer, key, getRes != null ? getRes.value() : null, expiryPlc0, null, null, false, false, null, null, skipStore, false, !deserializeBinary, recovery, null);
                                    // Rewriting fixed, initially filled by explicit lock operation.
                                    if (getRes != null)
                                        cacheCtx.addResult(retMap, key, getRes.value(), skipVals, keepCacheObjects, deserializeBinary, false, getRes, getRes.version(), 0, 0, needVer, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                                    else
                                        retMap.remove(keepCacheObjects ? key : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(key, !deserializeBinary, false, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId)));
                                }
                                return Collections.emptyMap();
                            } catch (Exception e) {
                                throw new GridClosureException(e);
                            }
                        });
                    }
                    return new GridFinishedFuture<>(Collections.emptyMap());
                }
            };
            FinishClosure<Map<K, V>> finClos = new FinishClosure<Map<K, V>>() {

                @Override
                Map<K, V> finish(Map<K, V> loaded) {
                    retMap.putAll(loaded);
                    return retMap;
                }
            };
            if (fut.isDone()) {
                try {
                    IgniteInternalFuture<Map<K, V>> fut1 = plc2.apply(fut.get(), null);
                    return nonInterruptable(fut1.isDone() ? new GridFinishedFuture<>(finClos.apply(fut1.get(), null)) : new GridEmbeddedFuture<>(finClos, fut1));
                } catch (GridClosureException e) {
                    return new GridFinishedFuture<>(e.unwrap());
                } catch (IgniteCheckedException e) {
                    try {
                        return nonInterruptable(plc2.apply(false, e));
                    } catch (Exception e1) {
                        return new GridFinishedFuture<>(e1);
                    }
                }
            } else {
                return nonInterruptable(new GridEmbeddedFuture<>(fut, plc2, finClos));
            }
        } else {
            assert optimistic() || readCommitted() || skipVals;
            if (!missed.isEmpty()) {
                if (!readCommitted())
                    for (Iterator<KeyCacheObject> it = missed.keySet().iterator(); it.hasNext(); ) {
                        KeyCacheObject cacheKey = it.next();
                        K keyVal = (K) (keepCacheObjects ? cacheKey : cacheCtx.cacheObjectContext().unwrapBinaryIfNeeded(cacheKey, !deserializeBinary, false, null));
                        if (retMap.containsKey(keyVal))
                            it.remove();
                    }
                if (missed.isEmpty())
                    return new GridFinishedFuture<>(retMap);
                AffinityTopologyVersion topVer = topologyVersionSnapshot();
                if (topVer == null)
                    topVer = entryTopVer;
                return checkMissed(cacheCtx, topVer != null ? topVer : topologyVersion(), retMap, missed, deserializeBinary, skipVals, keepCacheObjects, skipStore, recovery, readRepairStrategy, needVer, expiryPlc);
            }
            return new GridFinishedFuture<>(retMap);
        }
    } catch (IgniteCheckedException e) {
        setRollbackOnly();
        return new GridFinishedFuture<>(e);
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) MARKED_ROLLBACK(org.apache.ignite.transactions.TransactionState.MARKED_ROLLBACK) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) Iterator(java.util.Iterator) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) CacheException(javax.cache.CacheException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridNearReadRepairFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairFuture) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap)

Example 79 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridNearTxLocal method enlistWrite.

/**
 * @param cacheCtx Cache context.
 * @param cacheKey Key to enlist.
 * @param val Value.
 * @param expiryPlc Explicitly specified expiry policy for entry.
 * @param entryProcessor Entry processor (for invoke operation).
 * @param invokeArgs Optional arguments for EntryProcessor.
 * @param retval Flag indicating whether a value should be returned.
 * @param lockOnly If {@code true}, then entry will be enlisted as noop.
 * @param filter User filters.
 * @param ret Return value.
 * @param skipStore Skip store flag.
 * @param singleRmv {@code True} for single key remove operation ({@link Cache#remove(Object)}.
 * @param recovery Recovery flag.
 * @param dataCenterId Optional data center Id.
 * @return Future for entry values loading.
 */
private <K, V> IgniteInternalFuture<Void> enlistWrite(final GridCacheContext cacheCtx, @Nullable AffinityTopologyVersion entryTopVer, KeyCacheObject cacheKey, Object val, @Nullable ExpiryPolicy expiryPlc, @Nullable EntryProcessor<K, V, Object> entryProcessor, @Nullable Object[] invokeArgs, final boolean retval, boolean lockOnly, final CacheEntryPredicate[] filter, final GridCacheReturn ret, boolean skipStore, final boolean singleRmv, boolean keepBinary, boolean recovery, Byte dataCenterId) {
    try (TraceSurroundings ignored2 = MTC.support(context().kernalContext().tracing().create(TX_NEAR_ENLIST_WRITE, MTC.span()))) {
        GridFutureAdapter<Void> enlistFut = new GridFutureAdapter<>();
        try {
            if (!updateLockFuture(null, enlistFut))
                return finishFuture(enlistFut, timedOut() ? timeoutException() : rollbackException(), false);
            addActiveCache(cacheCtx, recovery);
            final boolean hasFilters = !F.isEmptyOrNulls(filter) && !F.isAlwaysTrue(filter);
            final boolean needVal = singleRmv || retval || hasFilters;
            final boolean needReadVer = needVal && (serializable() && optimistic());
            if (entryProcessor != null)
                transform = true;
            GridCacheVersion drVer = dataCenterId != null ? cacheCtx.cache().nextVersion(dataCenterId) : null;
            boolean loadMissed = enlistWriteEntry(cacheCtx, entryTopVer, cacheKey, val, entryProcessor, invokeArgs, expiryPlc, retval, lockOnly, filter, /*drVer*/
            drVer, /*drTtl*/
            -1L, /*drExpireTime*/
            -1L, ret, /*enlisted*/
            null, skipStore, singleRmv, hasFilters, needVal, needReadVer, keepBinary, recovery);
            if (loadMissed) {
                AffinityTopologyVersion topVer = topologyVersionSnapshot();
                if (topVer == null)
                    topVer = entryTopVer;
                IgniteInternalFuture<Void> loadFut = loadMissing(cacheCtx, topVer != null ? topVer : topologyVersion(), Collections.singleton(cacheKey), filter, ret, needReadVer, singleRmv, hasFilters, /*read through*/
                (entryProcessor != null || cacheCtx.config().isLoadPreviousValue()) && !skipStore, retval, keepBinary, recovery, expiryPlc);
                loadFut.listen(new IgniteInClosure<IgniteInternalFuture<Void>>() {

                    @Override
                    public void apply(IgniteInternalFuture<Void> fut) {
                        try {
                            fut.get();
                            finishFuture(enlistFut, null, true);
                        } catch (IgniteCheckedException e) {
                            finishFuture(enlistFut, e, true);
                        }
                    }
                });
                return enlistFut;
            }
            finishFuture(enlistFut, null, true);
            return enlistFut;
        } catch (IgniteCheckedException e) {
            return finishFuture(enlistFut, e, true);
        }
    }
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)

Example 80 with GridFutureAdapter

use of org.apache.ignite.internal.util.future.GridFutureAdapter in project ignite by apache.

the class GridNearTxLocal method chainFinishFuture.

/**
 * @param fut Already started finish future.
 * @param commit Commit flag.
 * @param clearThreadMap Clear thread map.
 * @return Finish future.
 */
private IgniteInternalFuture<IgniteInternalTx> chainFinishFuture(final NearTxFinishFuture fut, final boolean commit, final boolean clearThreadMap, final boolean onTimeout) {
    assert fut != null;
    if (fut.commit() != commit) {
        final GridNearTxLocal tx = this;
        if (!commit) {
            final GridNearTxFinishFuture rollbackFut = new GridNearTxFinishFuture<>(cctx, this, false);
            fut.listen(new IgniteInClosure<IgniteInternalFuture<IgniteInternalTx>>() {

                @Override
                public void apply(IgniteInternalFuture<IgniteInternalTx> fut0) {
                    if (FINISH_FUT_UPD.compareAndSet(tx, fut, rollbackFut)) {
                        switch(tx.state()) {
                            case COMMITTED:
                                if (log.isDebugEnabled())
                                    log.debug("Failed to rollback, transaction is already committed: " + tx);
                            case ROLLED_BACK:
                                rollbackFut.forceFinish();
                                assert rollbackFut.isDone() : rollbackFut;
                                break;
                            default:
                                // First finish attempt was unsuccessful. Try again.
                                rollbackFut.finish(false, clearThreadMap, onTimeout);
                        }
                    } else {
                        finishFut.listen(new IgniteInClosure<IgniteInternalFuture<IgniteInternalTx>>() {

                            @Override
                            public void apply(IgniteInternalFuture<IgniteInternalTx> fut) {
                                try {
                                    fut.get();
                                    rollbackFut.markInitialized();
                                } catch (IgniteCheckedException e) {
                                    rollbackFut.onDone(e);
                                }
                            }
                        });
                    }
                }
            });
            return rollbackFut;
        } else {
            final GridFutureAdapter<IgniteInternalTx> fut0 = new GridFutureAdapter<>();
            fut.listen(new IgniteInClosure<IgniteInternalFuture<IgniteInternalTx>>() {

                @Override
                public void apply(IgniteInternalFuture<IgniteInternalTx> fut) {
                    if (timedOut())
                        fut0.onDone(new IgniteTxTimeoutCheckedException("Failed to commit transaction, " + "transaction is concurrently rolled back on timeout: " + tx));
                    else
                        fut0.onDone(new IgniteTxRollbackCheckedException("Failed to commit transaction, " + "transaction is concurrently rolled back: " + tx));
                }
            });
            return fut0;
        }
    }
    return fut;
}
Also used : IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)

Aggregations

GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)110 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)59 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)34 IgniteException (org.apache.ignite.IgniteException)23 ArrayList (java.util.ArrayList)21 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)21 List (java.util.List)20 UUID (java.util.UUID)20 ClusterNode (org.apache.ignite.cluster.ClusterNode)20 IgniteEx (org.apache.ignite.internal.IgniteEx)20 HashMap (java.util.HashMap)19 Map (java.util.Map)19 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)18 Test (org.junit.Test)18 Nullable (org.jetbrains.annotations.Nullable)17 Ignite (org.apache.ignite.Ignite)15 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)15 HashSet (java.util.HashSet)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 T2 (org.apache.ignite.internal.util.typedef.T2)13