Search in sources :

Example 6 with CX1

use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.

the class GridDhtColocatedCache method getAsync.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteInternalFuture<V> getAsync(final K key, boolean forcePrimary, boolean skipTx, String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean needVer) {
    ctx.checkSecurity(SecurityPermission.CACHE_READ);
    GridNearTxLocal tx = checkCurrentTx();
    final CacheOperationContext opCtx = ctx.operationContextPerCall();
    final boolean recovery = opCtx != null && opCtx.recovery();
    final ReadRepairStrategy readRepairStrategy = opCtx != null ? opCtx.readRepairStrategy() : null;
    // Get operation bypass Tx in Mvcc mode.
    if (!ctx.mvccEnabled() && tx != null && !tx.implicit() && !skipTx) {
        return asyncOp(tx, new AsyncOp<V>() {

            @Override
            public IgniteInternalFuture<V> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
                IgniteInternalFuture<Map<Object, Object>> fut = tx.getAllAsync(ctx, readyTopVer, Collections.singleton(ctx.toCacheKeyObject(key)), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, readRepairStrategy, needVer);
                return fut.chain(new CX1<IgniteInternalFuture<Map<Object, Object>>, V>() {

                    @Override
                    public V applyx(IgniteInternalFuture<Map<Object, Object>> e) throws IgniteCheckedException {
                        Map<Object, Object> map = e.get();
                        assert map.isEmpty() || map.size() == 1 : map.size();
                        if (skipVals) {
                            Boolean val = map.isEmpty() ? false : (Boolean) F.firstValue(map);
                            return (V) (val);
                        }
                        return (V) F.firstValue(map);
                    }
                });
            }
        }, opCtx, /*retry*/
        false);
    }
    MvccSnapshot mvccSnapshot = null;
    MvccQueryTracker mvccTracker = null;
    if (ctx.mvccEnabled()) {
        try {
            if (tx != null)
                mvccSnapshot = MvccUtils.requestSnapshot(tx);
            else {
                mvccTracker = MvccUtils.mvccTracker(ctx, null);
                mvccSnapshot = mvccTracker.snapshot();
            }
            assert mvccSnapshot != null;
        } catch (IgniteCheckedException ex) {
            return new GridFinishedFuture<>(ex);
        }
    }
    AffinityTopologyVersion topVer;
    if (tx != null)
        topVer = tx.topologyVersion();
    else if (mvccTracker != null)
        topVer = mvccTracker.topologyVersion();
    else
        topVer = ctx.affinity().affinityTopologyVersion();
    if (readRepairStrategy != null) {
        return new GridNearReadRepairCheckOnlyFuture(ctx, Collections.singleton(ctx.toCacheKeyObject(key)), readRepairStrategy, opCtx == null || !opCtx.skipStore(), taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, false, tx).single();
    }
    GridPartitionedSingleGetFuture fut = new GridPartitionedSingleGetFuture(ctx, ctx.toCacheKeyObject(key), topVer, opCtx == null || !opCtx.skipStore(), forcePrimary, taskName, deserializeBinary, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, /*keepCacheObjects*/
    false, opCtx != null && opCtx.recovery(), null, mvccSnapshot);
    fut.init();
    if (mvccTracker != null) {
        final MvccQueryTracker mvccTracker0 = mvccTracker;
        fut.listen(new CI1<IgniteInternalFuture<Object>>() {

            @Override
            public void apply(IgniteInternalFuture<Object> future) {
                if (future.isDone())
                    mvccTracker0.onDone();
            }
        });
    }
    return (IgniteInternalFuture<V>) fut;
}
Also used : MvccSnapshot(org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot) GridPartitionedSingleGetFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridPartitionedSingleGetFuture) CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ReadRepairStrategy(org.apache.ignite.cache.ReadRepairStrategy) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) GridNearReadRepairCheckOnlyFuture(org.apache.ignite.internal.processors.cache.distributed.near.consistency.GridNearReadRepairCheckOnlyFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CX1(org.apache.ignite.internal.util.typedef.CX1) Map(java.util.Map) GridCacheConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap)

Example 7 with CX1

use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.

the class GridFutureAdapterSelfTest method checkChaining.

/**
 * @param exec Executor for chain callback.
 * @throws Exception If failed.
 */
@SuppressWarnings("ErrorNotRethrown")
private void checkChaining(ExecutorService exec) throws Exception {
    final CX1<IgniteInternalFuture<Object>, Object> passThrough = new CX1<IgniteInternalFuture<Object>, Object>() {

        @Override
        public Object applyx(IgniteInternalFuture<Object> f) throws IgniteCheckedException {
            return f.get();
        }
    };
    GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
    IgniteInternalFuture<Object> chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    assertFalse(fut.isDone());
    assertFalse(chain.isDone());
    try {
        chain.get(20);
        fail("Expects timeout exception.");
    } catch (IgniteFutureTimeoutCheckedException e) {
        info("Expected timeout exception: " + e.getMessage());
    }
    fut.onDone("result");
    assertEquals("result", exec == null ? chain.get(1) : chain.get());
    // Test exception re-thrown.
    fut = new GridFutureAdapter<>();
    chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    fut.onDone(new ClusterGroupEmptyCheckedException("test exception"));
    try {
        chain.get();
        fail("Expects failed with exception.");
    } catch (ClusterGroupEmptyCheckedException e) {
        info("Expected exception: " + e.getMessage());
    }
    // Test error re-thrown.
    fut = new GridFutureAdapter<>();
    chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
    try {
        fut.onDone(new StackOverflowError("test error"));
        if (exec == null)
            fail("Expects failed with error.");
    } catch (StackOverflowError e) {
        info("Expected error: " + e.getMessage());
    }
    try {
        chain.get();
        fail("Expects failed with error.");
    } catch (StackOverflowError e) {
        info("Expected error: " + e.getMessage());
    }
}
Also used : ClusterGroupEmptyCheckedException(org.apache.ignite.internal.cluster.ClusterGroupEmptyCheckedException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CX1(org.apache.ignite.internal.util.typedef.CX1)

Example 8 with CX1

use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.

the class GridCacheAbstractJobExecutionTest method checkTransactions.

/**
 * @param concur Concurrency.
 * @param isolation Isolation.
 * @param jobCnt Job count.
 * @throws Exception If fails.
 */
private void checkTransactions(final TransactionConcurrency concur, final TransactionIsolation isolation, final int jobCnt) throws Exception {
    info("Grid 0: " + grid(0).localNode().id());
    info("Grid 1: " + grid(1).localNode().id());
    info("Grid 2: " + grid(2).localNode().id());
    info("Grid 3: " + grid(3).localNode().id());
    Ignite ignite = grid(0);
    Collection<IgniteFuture<?>> futs = new LinkedList<>();
    final String key = "TestKey";
    info("Primary node for test key: " + grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key));
    for (int i = 0; i < jobCnt; i++) {
        futs.add(ignite.compute().applyAsync(new CX1<Integer, Void>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public Void applyx(final Integer i) {
                IgniteCache<String, int[]> cache = ignite.cache(DEFAULT_CACHE_NAME);
                try (Transaction tx = ignite.transactions().txStart(concur, isolation)) {
                    int[] arr = cache.get(key);
                    if (arr == null)
                        arr = new int[jobCnt];
                    arr[i] = 1;
                    cache.put(key, arr);
                    int c = cntr.getAndIncrement();
                    if (c % 50 == 0)
                        X.println("Executing transaction [i=" + i + ", c=" + c + ']');
                    tx.commit();
                }
                return null;
            }
        }, i));
    }
    for (IgniteFuture<?> fut : futs) // Wait for completion.
    fut.get();
    for (int i = 0; i < GRID_CNT; i++) {
        info("Running iteration: " + i);
        for (int g = 0; g < GRID_CNT; g++) {
            info("Will check grid: " + g);
            info("Value: " + grid(i).cache(DEFAULT_CACHE_NAME).localPeek(key));
        }
        IgniteCache<String, int[]> c = grid(i).cache(DEFAULT_CACHE_NAME);
        // which means that all previous transactions have committed.
        try (Transaction tx = grid(i).transactions().txStart(concur, isolation)) {
            int[] arr = c.get(key);
            assertNotNull(arr);
            assertEquals(jobCnt, arr.length);
            for (int j : arr) assertEquals(1, j);
            tx.commit();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) IgniteFuture(org.apache.ignite.lang.IgniteFuture) Ignite(org.apache.ignite.Ignite) CX1(org.apache.ignite.internal.util.typedef.CX1) LinkedList(java.util.LinkedList)

Aggregations

CX1 (org.apache.ignite.internal.util.typedef.CX1)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 Map (java.util.Map)4 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)3 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Ignite (org.apache.ignite.Ignite)2 ReadRepairStrategy (org.apache.ignite.cache.ReadRepairStrategy)2 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)2 GridCacheReturn (org.apache.ignite.internal.processors.cache.GridCacheReturn)2 MvccSnapshot (org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot)2 IgniteInternalTx (org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx)2 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)2 Transaction (org.apache.ignite.transactions.Transaction)2 Nullable (org.jetbrains.annotations.Nullable)2