Search in sources :

Example 1 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class GridNearTxLocal method checkMissed.

/**
 * @param cacheCtx Cache context.
 * @param topVer Topology version.
 * @param map Return map.
 * @param missedMap Missed keys.
 * @param deserializeBinary Deserialize binary flag.
 * @param skipVals Skip values flag.
 * @param keepCacheObjects Keep cache objects flag.
 * @param skipStore Skip store flag.
 * @param expiryPlc Expiry policy.
 * @return Loaded key-value pairs.
 */
private <K, V> IgniteInternalFuture<Map<K, V>> checkMissed(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final Map<K, V> map, final Map<KeyCacheObject, GridCacheVersion> missedMap, final boolean deserializeBinary, final boolean skipVals, final boolean keepCacheObjects, final boolean skipStore, final boolean recovery, final boolean needVer, final ExpiryPolicy expiryPlc) {
    if (log.isDebugEnabled())
        log.debug("Loading missed values for missed map: " + missedMap);
    final boolean needReadVer = (serializable() && optimistic()) || needVer;
    return new GridEmbeddedFuture<>(new C2<Void, Exception, Map<K, V>>() {

        @Override
        public Map<K, V> apply(Void v, Exception e) {
            if (e != null) {
                setRollbackOnly();
                throw new GridClosureException(e);
            }
            return map;
        }
    }, loadMissing(cacheCtx, topVer, !skipStore, false, missedMap.keySet(), skipVals, needReadVer, !deserializeBinary, recovery, expiryPlc, new GridInClosure3<KeyCacheObject, Object, GridCacheVersion>() {

        @Override
        public void apply(KeyCacheObject key, Object val, GridCacheVersion loadVer) {
            if (isRollbackOnly()) {
                if (log.isDebugEnabled())
                    log.debug("Ignoring loaded value for read because transaction was rolled back: " + GridNearTxLocal.this);
                return;
            }
            CacheObject cacheVal = cacheCtx.toCacheObject(val);
            CacheObject visibleVal = cacheVal;
            IgniteTxKey txKey = cacheCtx.txKey(key);
            IgniteTxEntry txEntry = entry(txKey);
            if (txEntry != null) {
                if (!readCommitted())
                    txEntry.readValue(cacheVal);
                if (!F.isEmpty(txEntry.entryProcessors()))
                    visibleVal = txEntry.applyEntryProcessors(visibleVal);
            }
            assert txEntry != null || readCommitted() || skipVals;
            GridCacheEntryEx e = txEntry == null ? entryEx(cacheCtx, txKey, topVer) : txEntry.cached();
            if (readCommitted() || skipVals) {
                cacheCtx.evicts().touch(e, topologyVersion());
                if (visibleVal != null) {
                    cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0);
                }
            } else {
                assert txEntry != null;
                txEntry.setAndMarkValid(cacheVal);
                if (needReadVer) {
                    assert loadVer != null;
                    txEntry.entryReadVersion(loadVer);
                }
                if (visibleVal != null) {
                    cacheCtx.addResult(map, key, visibleVal, skipVals, keepCacheObjects, deserializeBinary, false, needVer ? loadVer : null, 0, 0);
                }
            }
        }
    }));
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) 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) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 2 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class GridNearTxLocal method loadMissing.

/**
 * @param cacheCtx Cache context.
 * @param keys Keys to load.
 * @param filter Filter.
 * @param ret Return value.
 * @param needReadVer Read version flag.
 * @param singleRmv {@code True} for single remove operation.
 * @param hasFilters {@code True} if filters not empty.
 * @param readThrough Read through flag.
 * @param retval Return value flag.
 * @param expiryPlc Expiry policy.
 * @return Load future.
 */
private IgniteInternalFuture<Void> loadMissing(final GridCacheContext cacheCtx, final AffinityTopologyVersion topVer, final Set<KeyCacheObject> keys, final CacheEntryPredicate[] filter, final GridCacheReturn ret, final boolean needReadVer, final boolean singleRmv, final boolean hasFilters, final boolean readThrough, final boolean retval, final boolean keepBinary, final boolean recovery, final ExpiryPolicy expiryPlc) {
    GridInClosure3<KeyCacheObject, Object, GridCacheVersion> c = new GridInClosure3<KeyCacheObject, Object, GridCacheVersion>() {

        @Override
        public void apply(KeyCacheObject key, @Nullable Object val, @Nullable GridCacheVersion loadVer) {
            if (log.isDebugEnabled())
                log.debug("Loaded value from remote node [key=" + key + ", val=" + val + ']');
            IgniteTxEntry e = entry(new IgniteTxKey(key, cacheCtx.cacheId()));
            assert e != null;
            if (needReadVer) {
                assert loadVer != null;
                e.entryReadVersion(singleRmv && val != null ? SER_READ_NOT_EMPTY_VER : loadVer);
            }
            if (singleRmv) {
                assert !hasFilters && !retval;
                assert val == null || Boolean.TRUE.equals(val) : val;
                ret.set(cacheCtx, null, val != null, keepBinary, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
            } else {
                CacheObject cacheVal = cacheCtx.toCacheObject(val);
                if (e.op() == TRANSFORM) {
                    GridCacheVersion ver;
                    e.readValue(cacheVal);
                    try {
                        ver = e.cached().version();
                    } catch (GridCacheEntryRemovedException ex) {
                        assert optimistic() : e;
                        if (log.isDebugEnabled())
                            log.debug("Failed to get entry version: [msg=" + ex.getMessage() + ']');
                        ver = null;
                    }
                    addInvokeResult(e, cacheVal, ret, ver);
                } else {
                    boolean success;
                    if (hasFilters) {
                        success = isAll(e.context(), key, cacheVal, filter);
                        if (!success) {
                            e.value(cacheVal, false, false);
                            e.op(READ);
                        }
                    } else
                        success = true;
                    ret.set(cacheCtx, cacheVal, success, keepBinary, U.deploymentClassLoader(cctx.kernalContext(), deploymentLdrId));
                }
            }
        }
    };
    return loadMissing(cacheCtx, topVer, readThrough, /*async*/
    true, keys, /*skipVals*/
    singleRmv, needReadVer, keepBinary, recovery, null, expiryPlc, c);
}
Also used : IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Nullable(org.jetbrains.annotations.Nullable) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3)

Example 3 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class GridNearTxLocal method loadMissing.

/**
 * @param cacheCtx Cache context.
 * @param readThrough Read through flag.
 * @param async if {@code True}, then loading will happen in a separate thread.
 * @param keys Keys.
 * @param skipVals Skip values flag.
 * @param needVer If {@code true} version is required for loaded values.
 * @param c Closure to be applied for loaded values.
 * @param readRepairStrategy Read Repair strategy.
 * @param expiryPlc Expiry policy.
 * @return Future with {@code True} value if loading took place.
 */
private IgniteInternalFuture<Void> loadMissing(final GridCacheContext cacheCtx, AffinityTopologyVersion topVer, boolean readThrough, boolean async, final Collection<KeyCacheObject> keys, final boolean skipVals, final boolean needVer, boolean keepBinary, boolean recovery, ReadRepairStrategy readRepairStrategy, final ExpiryPolicy expiryPlc, final GridInClosure3<KeyCacheObject, Object, GridCacheVersion> c) {
    IgniteCacheExpiryPolicy expiryPlc0 = optimistic() ? accessPolicy(cacheCtx, keys) : cacheCtx.cache().expiryPolicy(expiryPlc);
    if (cacheCtx.isNear()) {
        return cacheCtx.nearTx().txLoadAsync(this, topVer, keys, readThrough, needVer || !cacheCtx.config().isReadFromBackup() || (optimistic() && serializable() && readThrough), /*deserializeBinary*/
        false, recovery, expiryPlc0, skipVals, needVer).chain(new C1<IgniteInternalFuture<Map<Object, Object>>, Void>() {

            @Override
            public Void apply(IgniteInternalFuture<Map<Object, Object>> f) {
                try {
                    Map<Object, Object> map = f.get();
                    processLoaded(map, keys, needVer, c);
                    return null;
                } catch (Exception e) {
                    setRollbackOnly();
                    throw new GridClosureException(e);
                }
            }
        });
    } else if (cacheCtx.isColocated()) {
        if (readRepairStrategy != null) {
            return new GridNearReadRepairCheckOnlyFuture(cacheCtx, keys, readRepairStrategy, readThrough, taskName, false, recovery, expiryPlc0, skipVals, needVer, true, this).multi().chain((fut) -> {
                try {
                    Map<Object, Object> map = fut.get();
                    processLoaded(map, keys, needVer, c);
                    return null;
                } catch (IgniteConsistencyViolationException e) {
                    for (KeyCacheObject key : keys) // Will be recreated after repair.
                    txState().removeEntry(cacheCtx.txKey(key));
                    throw new GridClosureException(e);
                } catch (Exception e) {
                    setRollbackOnly();
                    throw new GridClosureException(e);
                }
            });
        }
        if (keys.size() == 1) {
            final KeyCacheObject key = F.first(keys);
            return cacheCtx.colocated().loadAsync(key, readThrough, needVer || !cacheCtx.config().isReadFromBackup() || (optimistic() && serializable() && readThrough), topVer, resolveTaskName(), /*deserializeBinary*/
            false, expiryPlc0, skipVals, needVer, /*keepCacheObject*/
            true, recovery, null, label()).chain(new C1<IgniteInternalFuture<Object>, Void>() {

                @Override
                public Void apply(IgniteInternalFuture<Object> f) {
                    try {
                        Object val = f.get();
                        processLoaded(key, val, needVer, skipVals, c);
                        return null;
                    } catch (Exception e) {
                        setRollbackOnly();
                        throw new GridClosureException(e);
                    }
                }
            });
        } else {
            return cacheCtx.colocated().loadAsync(keys, readThrough, needVer || !cacheCtx.config().isReadFromBackup() || (optimistic() && serializable() && readThrough), topVer, resolveTaskName(), /*deserializeBinary*/
            false, recovery, expiryPlc0, skipVals, needVer, /*keepCacheObject*/
            true, label(), null).chain(new C1<IgniteInternalFuture<Map<Object, Object>>, Void>() {

                @Override
                public Void apply(IgniteInternalFuture<Map<Object, Object>> f) {
                    try {
                        Map<Object, Object> map = f.get();
                        processLoaded(map, keys, needVer, c);
                        return null;
                    } catch (Exception e) {
                        setRollbackOnly();
                        throw new GridClosureException(e);
                    }
                }
            });
        }
    } else {
        assert cacheCtx.isLocal();
        return localCacheLoadMissing(cacheCtx, topVer, readThrough, async, keys, skipVals, needVer, keepBinary, recovery, expiryPlc, c);
    }
}
Also used : GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) GridDistributedTxMapping(org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping) EVT_CACHE_OBJECT_READ(org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ) EntryProcessor(javax.cache.processor.EntryProcessor) Map(java.util.Map) Cache(javax.cache.Cache) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) DELETE(org.apache.ignite.internal.processors.cache.GridCacheOperation.DELETE) GridStringBuilder(org.apache.ignite.internal.util.GridStringBuilder) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) SER_READ_NOT_EMPTY_VER(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry.SER_READ_NOT_EMPTY_VER) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) GridToStringExclude(org.apache.ignite.internal.util.tostring.GridToStringExclude) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) MvccSnapshotFuture(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshotFuture) MvccProcessor(org.apache.ignite.internal.processors.cache.mvcc.MvccProcessor) TransactionConcurrency(org.apache.ignite.transactions.TransactionConcurrency) Set(java.util.Set) GridCacheVersionedFuture(org.apache.ignite.internal.processors.cache.GridCacheVersionedFuture) GridDhtTxLocalAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocalAdapter) MTC(org.apache.ignite.internal.processors.tracing.MTC) TransactionProxy(org.apache.ignite.internal.processors.cache.transactions.TransactionProxy) TX_NEAR_ENLIST_READ(org.apache.ignite.internal.processors.tracing.SpanType.TX_NEAR_ENLIST_READ) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) TransactionIsolation(org.apache.ignite.transactions.TransactionIsolation) EntryGetResult(org.apache.ignite.internal.processors.cache.EntryGetResult) GridDhtDetachedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.colocated.GridDhtDetachedCacheEntry) U(org.apache.ignite.internal.util.typedef.internal.U) SecurityPermission(org.apache.ignite.plugin.security.SecurityPermission) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) COMMITTING(org.apache.ignite.transactions.TransactionState.COMMITTING) ClusterNode(org.apache.ignite.cluster.ClusterNode) CI1(org.apache.ignite.internal.util.typedef.CI1) IgniteInternalTx(org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MvccUtils(org.apache.ignite.internal.processors.cache.mvcc.MvccUtils) S(org.apache.ignite.internal.util.typedef.internal.S) C2(org.apache.ignite.internal.util.typedef.C2) IgniteTxManager(org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager) C1(org.apache.ignite.internal.util.typedef.C1) PREPARED(org.apache.ignite.transactions.TransactionState.PREPARED) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) MvccCoordinator(org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinator) IgniteTxOptimisticCheckedException(org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException) COMMITTED(org.apache.ignite.transactions.TransactionState.COMMITTED) PREPARING(org.apache.ignite.transactions.TransactionState.PREPARING) IgniteTxEntry(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry) GridNearReadRepairFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) CacheEntryPredicate(org.apache.ignite.internal.processors.cache.CacheEntryPredicate) GridDistributedCacheEntry(org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry) GridCacheDrInfo(org.apache.ignite.internal.processors.cache.dr.GridCacheDrInfo) CX1(org.apache.ignite.internal.util.typedef.CX1) IgniteUuid(org.apache.ignite.lang.IgniteUuid) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) TRANSFORM(org.apache.ignite.internal.processors.cache.GridCacheOperation.TRANSFORM) GridInvokeValue(org.apache.ignite.internal.processors.cache.distributed.dht.GridInvokeValue) UNKNOWN(org.apache.ignite.transactions.TransactionState.UNKNOWN) TransactionProxyRollbackOnlyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyRollbackOnlyImpl) CREATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.CREATE) TX_NEAR_ENLIST_WRITE(org.apache.ignite.internal.processors.tracing.SpanType.TX_NEAR_ENLIST_WRITE) GridDhtTxPrepareFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxPrepareFuture) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) Collection(java.util.Collection) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) UUID(java.util.UUID) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl) Instant(java.time.Instant) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CI2(org.apache.ignite.internal.util.typedef.CI2) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Nullable(org.jetbrains.annotations.Nullable) NOOP(org.apache.ignite.internal.processors.cache.GridCacheOperation.NOOP) IgniteTxKey(org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey) CU(org.apache.ignite.internal.util.typedef.internal.CU) ACTIVE(org.apache.ignite.transactions.TransactionState.ACTIVE) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) ROLLING_BACK(org.apache.ignite.transactions.TransactionState.ROLLING_BACK) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) GridCacheReturn(org.apache.ignite.internal.processors.cache.GridCacheReturn) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) HashMap(java.util.HashMap) IgniteBiClosure(org.apache.ignite.lang.IgniteBiClosure) SER_READ_EMPTY_ENTRY_VER(org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry.SER_READ_EMPTY_ENTRY_VER) HashSet(java.util.HashSet) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) CacheException(javax.cache.CacheException) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) F(org.apache.ignite.internal.util.typedef.F) Iterator(java.util.Iterator) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) ROLLED_BACK(org.apache.ignite.transactions.TransactionState.ROLLED_BACK) UPDATE(org.apache.ignite.internal.processors.cache.GridCacheOperation.UPDATE) MARKED_ROLLBACK(org.apache.ignite.transactions.TransactionState.MARKED_ROLLBACK) MvccCoordinatorChangeAware(org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinatorChangeAware) GridEmbeddedFuture(org.apache.ignite.internal.util.future.GridEmbeddedFuture) TransactionState(org.apache.ignite.transactions.TransactionState) GridDhtTxFinishFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxFinishFuture) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) EnlistOperation(org.apache.ignite.internal.processors.query.EnlistOperation) GridPlainRunnable(org.apache.ignite.internal.util.lang.GridPlainRunnable) Collections(java.util.Collections) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException) READ(org.apache.ignite.internal.processors.cache.GridCacheOperation.READ) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) 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) C1(org.apache.ignite.internal.util.typedef.C1) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) IgniteConsistencyViolationException(org.apache.ignite.internal.processors.cache.distributed.near.consistency.IgniteConsistencyViolationException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) GridLeanMap(org.apache.ignite.internal.util.GridLeanMap) IgniteCacheExpiryPolicy(org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 4 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccAbstractTest method accountsTxReadAll.

/**
 * @param srvs Number of server nodes.
 * @param clients Number of client nodes.
 * @param cacheBackups Number of cache backups.
 * @param cacheParts Number of cache partitions.
 * @param cfgC Optional closure applied to cache configuration.
 * @param withRmvs If {@code true} then in addition to puts tests also executes removes.
 * @param readMode Read mode.
 * @param writeMode Write mode.
 * @param testTime Test time.
 * @throws Exception If failed.
 */
final void accountsTxReadAll(final int srvs, final int clients, int cacheBackups, int cacheParts, @Nullable IgniteInClosure<CacheConfiguration> cfgC, final boolean withRmvs, final ReadMode readMode, final WriteMode writeMode, long testTime, RestartMode restartMode) throws Exception {
    final int ACCOUNTS = 20;
    final int ACCOUNT_START_VAL = 1000;
    final int writers = 4;
    final int readers = 4;
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            if (writeMode == WriteMode.PUT) {
                Map<Integer, MvccTestAccount> accounts = new HashMap<>();
                for (int i = 0; i < ACCOUNTS; i++) accounts.put(i, new MvccTestAccount(ACCOUNT_START_VAL, 1));
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache.putAll(accounts);
                    tx.commit();
                }
            } else if (writeMode == WriteMode.DML) {
                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    SqlFieldsQuery qry = new SqlFieldsQuery("insert into MvccTestAccount(_key, val, updateCnt) values " + "(?," + ACCOUNT_START_VAL + ",1)");
                    for (int i = 0; i < ACCOUNTS; i++) {
                        try (FieldsQueryCursor<List<?>> cur = cache.query(qry.setArgs(i))) {
                            assertEquals(1L, cur.iterator().next().get(0));
                        }
                        tx.commit();
                    }
                }
            } else
                assert false : "Unknown write mode";
        }
    };
    final RemovedAccountsTracker rmvdTracker = new RemovedAccountsTracker(ACCOUNTS);
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                try {
                    IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                    cnt++;
                    int i1 = rnd.nextInt(ACCOUNTS), i2 = rnd.nextInt(ACCOUNTS);
                    while (i2 == i1) i2 = rnd.nextInt(ACCOUNTS);
                    Integer id1 = Math.min(i1, i2);
                    Integer id2 = Math.max(i1, i2);
                    Set<Integer> keys = new HashSet<>();
                    keys.add(id1);
                    keys.add(id2);
                    Integer cntr1 = null;
                    Integer cntr2 = null;
                    Integer rmvd = null;
                    Integer inserted = null;
                    MvccTestAccount a1;
                    MvccTestAccount a2;
                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        tx.timeout(TX_TIMEOUT);
                        Map<Integer, MvccTestAccount> accounts = null;
                        if (writeMode == WriteMode.PUT)
                            accounts = cache.cache.getAll(keys);
                        else if (writeMode == WriteMode.DML)
                            accounts = getAllSql(cache);
                        else
                            assert false : "Unknown write mode";
                        a1 = accounts.get(id1);
                        a2 = accounts.get(id2);
                        if (!withRmvs) {
                            assertNotNull(a1);
                            assertNotNull(a2);
                            cntr1 = a1.updateCnt + 1;
                            cntr2 = a2.updateCnt + 1;
                            if (writeMode == WriteMode.PUT) {
                                cache.cache.put(id1, new MvccTestAccount(a1.val + 1, cntr1));
                                cache.cache.put(id2, new MvccTestAccount(a2.val - 1, cntr2));
                            } else if (writeMode == WriteMode.DML) {
                                updateSql(cache, id1, a1.val + 1, cntr1);
                                updateSql(cache, id2, a2.val - 1, cntr2);
                            } else
                                assert false : "Unknown write mode";
                        } else {
                            if (a1 != null || a2 != null) {
                                if (a1 != null && a2 != null) {
                                    if (rnd.nextInt(10) == 0) {
                                        if (rmvdTracker.size() < ACCOUNTS / 2) {
                                            rmvd = rnd.nextBoolean() ? id1 : id2;
                                            assertTrue(rmvdTracker.markRemoved(rmvd));
                                        }
                                    }
                                    if (rmvd != null) {
                                        if (writeMode == WriteMode.PUT) {
                                            if (rmvd.equals(id1)) {
                                                cache.cache.remove(id1);
                                                cache.cache.put(id2, new MvccTestAccount(a1.val + a2.val, 1));
                                            } else {
                                                cache.cache.put(id1, new MvccTestAccount(a1.val + a2.val, 1));
                                                cache.cache.remove(id2);
                                            }
                                        } else if (writeMode == WriteMode.DML) {
                                            if (rmvd.equals(id1)) {
                                                removeSql(cache, id1);
                                                updateSql(cache, id2, a1.val + a2.val, 1);
                                            } else {
                                                updateSql(cache, id1, a1.val + a2.val, 1);
                                                removeSql(cache, id2);
                                            }
                                        } else
                                            assert false : "Unknown write mode";
                                    } else {
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(a1.val + 1, 1));
                                            cache.cache.put(id2, new MvccTestAccount(a2.val - 1, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            updateSql(cache, id1, a1.val + 1, 1);
                                            updateSql(cache, id2, a2.val - 1, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    }
                                } else {
                                    if (a1 == null) {
                                        inserted = id1;
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(100, 1));
                                            cache.cache.put(id2, new MvccTestAccount(a2.val - 100, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            insertSql(cache, id1, 100, 1);
                                            updateSql(cache, id2, a2.val - 100, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    } else {
                                        inserted = id2;
                                        if (writeMode == WriteMode.PUT) {
                                            cache.cache.put(id1, new MvccTestAccount(a1.val - 100, 1));
                                            cache.cache.put(id2, new MvccTestAccount(100, 1));
                                        } else if (writeMode == WriteMode.DML) {
                                            updateSql(cache, id1, a1.val - 100, 1);
                                            insertSql(cache, id2, 100, 1);
                                        } else
                                            assert false : "Unknown write mode";
                                    }
                                }
                            }
                        }
                        tx.commit();
                        // In case of tx success mark inserted.
                        if (inserted != null) {
                            assert withRmvs;
                            assertTrue(rmvdTracker.unmarkRemoved(inserted));
                        }
                    } catch (Throwable e) {
                        if (rmvd != null) {
                            assert withRmvs;
                            // If tx fails, unmark removed.
                            assertTrue(rmvdTracker.unmarkRemoved(rmvd));
                        }
                        throw e;
                    }
                    if (!withRmvs) {
                        Map<Integer, MvccTestAccount> accounts = null;
                        if (writeMode == WriteMode.PUT)
                            accounts = cache.cache.getAll(keys);
                        else if (writeMode == WriteMode.DML)
                            accounts = getAllSql(cache);
                        else
                            assert false : "Unknown write mode";
                        a1 = accounts.get(id1);
                        a2 = accounts.get(id2);
                        assertNotNull(a1);
                        assertNotNull(a2);
                        assertTrue(a1.updateCnt >= cntr1);
                        assertTrue(a2.updateCnt >= cntr2);
                    }
                } catch (Exception e) {
                    handleTxException(e);
                } finally {
                    cache.readUnlock();
                }
            }
            info("Writer finished, updates: " + cnt);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Set<Integer> keys = new LinkedHashSet<>();
            Map<Integer, Integer> lastUpdateCntrs = new HashMap<>();
            SqlFieldsQuery sumQry = new SqlFieldsQuery("select sum(val) from MvccTestAccount");
            while (!stop.get()) {
                while (keys.size() < ACCOUNTS) keys.add(rnd.nextInt(ACCOUNTS));
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                Map<Integer, MvccTestAccount> accounts = null;
                try {
                    switch(readMode) {
                        case GET:
                            {
                                accounts = cache.cache.getAll(keys);
                                break;
                            }
                        case SCAN:
                            {
                                accounts = new HashMap<>();
                                Iterator<Cache.Entry<Integer, MvccTestAccount>> it = cache.cache.iterator();
                                try {
                                    for (; it.hasNext(); ) {
                                        IgniteCache.Entry<Integer, MvccTestAccount> e = it.next();
                                        MvccTestAccount old = accounts.put(e.getKey(), e.getValue());
                                        assertNull("new=" + e + ", old=" + old, old);
                                    }
                                } finally {
                                    U.closeQuiet((AutoCloseable) it);
                                }
                                break;
                            }
                        case SQL:
                            {
                                accounts = new HashMap<>();
                                if (rnd.nextBoolean()) {
                                    SqlQuery<Integer, MvccTestAccount> qry = new SqlQuery<>(MvccTestAccount.class, "_key >= 0");
                                    for (IgniteCache.Entry<Integer, MvccTestAccount> e : cache.cache.query(qry).getAll()) {
                                        MvccTestAccount old = accounts.put(e.getKey(), e.getValue());
                                        assertNull(old);
                                    }
                                } else {
                                    SqlFieldsQuery qry = new SqlFieldsQuery("select _key, val from MvccTestAccount");
                                    for (List<?> row : cache.cache.query(qry).getAll()) {
                                        Integer id = (Integer) row.get(0);
                                        Integer val = (Integer) row.get(1);
                                        MvccTestAccount old = accounts.put(id, new MvccTestAccount(val, 1));
                                        assertNull(old);
                                    }
                                }
                                break;
                            }
                        case SQL_SUM:
                            {
                                Long sum;
                                if (rnd.nextBoolean()) {
                                    List<List<?>> res = cache.cache.query(sumQry).getAll();
                                    assertEquals(1, res.size());
                                    sum = (Long) res.get(0).get(0);
                                } else {
                                    Map res = readAllByMode(cache.cache, keys, readMode, ACCOUNT_CODEC);
                                    sum = (Long) ((Map.Entry) res.entrySet().iterator().next()).getValue();
                                }
                                assertEquals(ACCOUNT_START_VAL * ACCOUNTS, sum.intValue());
                                break;
                            }
                        default:
                            {
                                fail();
                                return;
                            }
                    }
                } finally {
                    cache.readUnlock();
                }
                if (accounts != null) {
                    if (!withRmvs)
                        assertEquals(ACCOUNTS, accounts.size());
                    int sum = 0;
                    for (int i = 0; i < ACCOUNTS; i++) {
                        MvccTestAccount account = accounts.get(i);
                        if (account != null) {
                            sum += account.val;
                            Integer cntr = lastUpdateCntrs.get(i);
                            if (cntr != null)
                                assertTrue(cntr <= account.updateCnt);
                            lastUpdateCntrs.put(i, cntr);
                        } else
                            assertTrue(withRmvs);
                    }
                    assertEquals(ACCOUNTS * ACCOUNT_START_VAL, sum);
                }
            }
            if (idx == 0) {
                TestCache<Integer, MvccTestAccount> cache = randomCache(caches, rnd);
                Map<Integer, MvccTestAccount> accounts;
                ReadMode readMode0 = readMode == SQL_SUM ? SQL : readMode;
                try {
                    accounts = readAllByMode(cache.cache, keys, readMode0, ACCOUNT_CODEC);
                    ;
                } finally {
                    cache.readUnlock();
                }
                int sum = 0;
                for (int i = 0; i < ACCOUNTS; i++) {
                    MvccTestAccount account = accounts.get(i);
                    assertTrue(account != null || withRmvs);
                    info("Account [id=" + i + ", val=" + (account != null ? account.val : null) + ']');
                    if (account != null)
                        sum += account.val;
                }
                info("Sum: " + sum);
            }
        }
    };
    readWriteTest(restartMode, srvs, clients, cacheBackups, cacheParts, writers, readers, testTime, cfgC, init, writer, reader);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) Iterator(java.util.Iterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteTxRollbackCheckedException(org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException) IgniteFutureCancelledCheckedException(org.apache.ignite.internal.IgniteFutureCancelledCheckedException) IgniteTxTimeoutCheckedException(org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) TransactionSerializationException(org.apache.ignite.transactions.TransactionSerializationException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 5 with GridInClosure3

use of org.apache.ignite.internal.util.lang.GridInClosure3 in project ignite by apache.

the class CacheMvccSqlQueriesAbstractTest method joinTransactional.

/**
 * @param singleNode {@code True} for test with single node.
 * @param distributedJoin {@code True} to test distributed joins.
 * @throws Exception If failed.
 */
private void joinTransactional(boolean singleNode, final boolean distributedJoin) throws Exception {
    final int KEYS = 100;
    final int writers = 4;
    final int readers = 4;
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            int cnt = 0;
            while (!stop.get()) {
                TestCache<Object, Object> cache = randomCache(caches, rnd);
                IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                try {
                    while (true) {
                        try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            Integer key = rnd.nextInt(KEYS);
                            JoinTestChildKey childKey = new JoinTestChildKey(key);
                            JoinTestChild child = (JoinTestChild) cache.cache.get(childKey);
                            if (child == null) {
                                int parentKey = distributedJoin ? key + 100 : key;
                                child = new JoinTestChild(parentKey);
                                cache.cache.put(childKey, child);
                                JoinTestParent parent = new JoinTestParent(parentKey);
                                cache.cache.put(new JoinTestParentKey(parentKey), parent);
                            } else {
                                cache.cache.remove(childKey);
                                cache.cache.remove(new JoinTestParentKey(child.parentId));
                            }
                            tx.commit();
                            break;
                        } catch (CacheException e) {
                            MvccFeatureChecker.assertMvccWriteConflict(e);
                        }
                    }
                    cnt++;
                } finally {
                    cache.readUnlock();
                }
            }
            info("Writer finished, updates: " + cnt);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            List<SqlFieldsQuery> qrys = new ArrayList<>();
            qrys.add(new SqlFieldsQuery("select c.parentId, p.id from " + "JoinTestChild c left outer join JoinTestParent p on (c.parentId = p.id)").setDistributedJoins(distributedJoin));
            qrys.add(new SqlFieldsQuery("select c.parentId, p.id from " + "JoinTestChild c left outer join JoinTestParent p on (c.parentId = p.id) where p.id = 10").setDistributedJoins(distributedJoin));
            qrys.add(new SqlFieldsQuery("select c.parentId, p.id from " + "JoinTestChild c left outer join JoinTestParent p on (c.parentId = p.id) where p.id != 10").setDistributedJoins(distributedJoin));
            while (!stop.get()) {
                TestCache<Object, Object> cache = randomCache(caches, rnd);
                try {
                    for (SqlFieldsQuery qry : qrys) {
                        List<List<?>> res = cache.cache.query(qry).getAll();
                        if (!res.isEmpty()) {
                            for (List<?> resRow : res) {
                                Integer parentId = (Integer) resRow.get(1);
                                assertNotNull(parentId);
                            }
                        }
                    }
                } finally {
                    cache.readUnlock();
                }
            }
            if (idx == 0) {
                TestCache<Object, Object> cache = randomCache(caches, rnd);
                try {
                    List<List<?>> res = cache.cache.query(qrys.get(0)).getAll();
                    info("Reader finished, result: " + res);
                } finally {
                    cache.readUnlock();
                }
            }
        }
    };
    int srvs;
    int clients;
    if (singleNode) {
        srvs = 1;
        clients = 0;
    } else {
        srvs = 4;
        clients = 2;
    }
    readWriteTest(null, srvs, clients, 0, DFLT_PARTITION_COUNT, writers, readers, DFLT_TEST_TIME, new InitIndexing(JoinTestParentKey.class, JoinTestParent.class, JoinTestChildKey.class, JoinTestChild.class), null, writer, reader);
}
Also used : CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) IgniteTransactions(org.apache.ignite.IgniteTransactions) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Transaction(org.apache.ignite.transactions.Transaction) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite)

Aggregations

GridInClosure3 (org.apache.ignite.internal.util.lang.GridInClosure3)17 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)14 ArrayList (java.util.ArrayList)13 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 HashMap (java.util.HashMap)12 CacheException (javax.cache.CacheException)11 Ignite (org.apache.ignite.Ignite)11 IgniteTransactions (org.apache.ignite.IgniteTransactions)10 Transaction (org.apache.ignite.transactions.Transaction)10 Map (java.util.Map)9 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)9 LinkedHashSet (java.util.LinkedHashSet)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 IgniteCache (org.apache.ignite.IgniteCache)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)7 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6