Search in sources :

Example 56 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method assertBeforePutValue.

/**
 * @param key Key.
 * @param oldVal Expected old value.
 * @param newVal Expected new value.
 */
private void assertBeforePutValue(String key, @Nullable Object oldVal, @Nullable Object newVal) {
    IgniteBiTuple t = interceptor.beforePutMap.get(key);
    assertNotNull(t);
    assertEquals(t.get1(), oldVal);
    assertEquals(t.get2(), newVal);
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple)

Example 57 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheInterceptorAbstractSelfTest method testModifyUpdate.

/**
 * @param key Key.
 * @param op Operation type.
 * @throws Exception If failed.
 */
private void testModifyUpdate(String key, Operation op) throws Exception {
    // Interceptor returns incremented new value.
    CacheInterceptor retInterceptor = new PutIncrementInterceptor();
    // Execute update when value is null.
    interceptor.retInterceptor = retInterceptor;
    log.info("Update 1 " + op);
    update(0, op, key, 1, null);
    checkCacheValue(key, 2);
    // Check values passed to interceptor.
    assertEquals(1, interceptor.beforePutMap.size());
    IgniteBiTuple t = interceptor.beforePutMap.get(key);
    assertEquals(null, t.get1());
    assertEquals(1, t.get2());
    assertEquals(1, interceptor.afterPutMap.size());
    assertEquals(2, interceptor.afterPutMap.get(key));
    // Execute update when value is not null.
    interceptor.reset();
    interceptor.retInterceptor = retInterceptor;
    log.info("Update 2 " + op);
    update(0, op, key, 3, 2);
    checkCacheValue(key, 4);
    // Check values passed to interceptor.
    assertEquals(1, interceptor.beforePutMap.size());
    t = interceptor.beforePutMap.get(key);
    assertEquals(2, t.get1());
    assertEquals(3, t.get2());
    assertEquals(1, interceptor.afterPutMap.size());
    assertEquals(4, interceptor.afterPutMap.get(key));
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheInterceptor(org.apache.ignite.cache.CacheInterceptor)

Example 58 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestAtomicStamped.

/**
 * Tests atomic stamped value.
 *
 * @param topWorker Topology change worker.
 * @throws Exception If failed.
 */
private void doTestAtomicStamped(ConstantTopologyChangeWorker topWorker) throws Exception {
    try (IgniteAtomicStamped<Integer, Integer> s = grid(0).atomicStamped(STRUCTURE_NAME, 1, 1, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Object>() {

            @Override
            public Object apply(Ignite ignite) {
                IgniteBiTuple<Integer, Integer> t = ignite.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
                assert t.get1() > 0;
                assert t.get2() > 0;
                return null;
            }
        });
        int val = s.value();
        while (!fut.isDone()) {
            IgniteBiTuple<Integer, Integer> t = s.get();
            assertEquals(val, (int) t.get1());
            assertEquals(val, (int) t.get2());
            ++val;
            s.set(val, val);
        }
        fut.get();
        for (Ignite g : G.allGrids()) {
            IgniteBiTuple<Integer, Integer> t = g.atomicStamped(STRUCTURE_NAME, 1, 1, false).get();
            assertEquals(val, (int) t.get1());
            assertEquals(val, (int) t.get2());
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignite(org.apache.ignite.Ignite)

Example 59 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridCacheProcessor method onExchangeDone.

/**
 * Callback invoked when first exchange future for dynamic cache is completed.
 *
 * @param cacheStartVer Started caches version to create proxy for.
 * @param exchActions Change requests.
 * @param err Error.
 */
@SuppressWarnings("unchecked")
public void onExchangeDone(AffinityTopologyVersion cacheStartVer, @Nullable ExchangeActions exchActions, @Nullable Throwable err) {
    initCacheProxies(cacheStartVer, err);
    if (exchActions == null)
        return;
    if (exchActions.systemCachesStarting() && exchActions.stateChangeRequest() == null) {
        ctx.dataStructures().restoreStructuresState(ctx);
        ctx.service().updateUtilityCache();
    }
    if (err == null) {
        // Force checkpoint if there is any cache stop request
        if (exchActions.cacheStopRequests().size() > 0) {
            try {
                sharedCtx.database().waitForCheckpoint("caches stop");
            } catch (IgniteCheckedException e) {
                U.error(log, "Failed to wait for checkpoint finish during cache stop.", e);
            }
        }
        for (ExchangeActions.CacheActionData action : exchActions.cacheStopRequests()) {
            CacheGroupContext gctx = cacheGrps.get(action.descriptor().groupId());
            // Cancel all operations blocking gateway
            if (gctx != null) {
                final String msg = "Failed to wait for topology update, cache group is stopping.";
                // If snapshot operation in progress we must throw CacheStoppedException
                // for correct cache proxy restart. For more details see
                // IgniteCacheProxy.cacheException()
                gctx.affinity().cancelFutures(new CacheStoppedException(msg));
            }
            stopGateway(action.request());
            sharedCtx.database().checkpointReadLock();
            try {
                prepareCacheStop(action.request().cacheName(), action.request().destroy());
            } finally {
                sharedCtx.database().checkpointReadUnlock();
            }
        }
        sharedCtx.database().checkpointReadLock();
        try {
            // Do not invoke checkpoint listeners for groups are going to be destroyed to prevent metadata corruption.
            for (ExchangeActions.CacheGroupActionData action : exchActions.cacheGroupsToStop()) {
                Integer groupId = action.descriptor().groupId();
                CacheGroupContext grp = cacheGrps.get(groupId);
                if (grp != null && grp.persistenceEnabled() && sharedCtx.database() instanceof GridCacheDatabaseSharedManager) {
                    GridCacheDatabaseSharedManager mngr = (GridCacheDatabaseSharedManager) sharedCtx.database();
                    mngr.removeCheckpointListener((DbCheckpointListener) grp.offheap());
                }
            }
        } finally {
            sharedCtx.database().checkpointReadUnlock();
        }
        List<IgniteBiTuple<CacheGroupContext, Boolean>> stoppedGroups = new ArrayList<>();
        for (ExchangeActions.CacheGroupActionData action : exchActions.cacheGroupsToStop()) {
            Integer groupId = action.descriptor().groupId();
            if (cacheGrps.containsKey(groupId)) {
                stoppedGroups.add(F.t(cacheGrps.get(groupId), action.destroy()));
                stopCacheGroup(groupId);
            }
        }
        if (!sharedCtx.kernalContext().clientNode())
            sharedCtx.database().onCacheGroupsStopped(stoppedGroups);
        if (exchActions.deactivate())
            sharedCtx.deactivate();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheDatabaseSharedManager(org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList)

Example 60 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class GridLocalAtomicCache method updatePartialBatch.

/**
 * @param entries Entries to update.
 * @param ver Cache version.
 * @param writeVals Cache values.
 * @param putMap Values to put.
 * @param rmvKeys Keys to remove.
 * @param expiryPlc Expiry policy.
 * @param err Optional partial update exception.
 * @param subjId Subject ID.
 * @param taskName Task name.
 * @return Partial update exception.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions", "ForLoopReplaceableByForEach" })
@Nullable
private CachePartialUpdateCheckedException updatePartialBatch(List<GridCacheEntryEx> entries, final GridCacheVersion ver, @Nullable List<CacheObject> writeVals, @Nullable Map<KeyCacheObject, CacheObject> putMap, @Nullable Collection<KeyCacheObject> rmvKeys, @Nullable ExpiryPolicy expiryPlc, boolean keepBinary, @Nullable CachePartialUpdateCheckedException err, UUID subjId, String taskName) {
    assert putMap == null ^ rmvKeys == null;
    GridCacheOperation op;
    CacheStorePartialUpdateException storeErr = null;
    try {
        if (putMap != null) {
            try {
                Map<? extends KeyCacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>> view = F.viewReadOnly(putMap, new C1<CacheObject, IgniteBiTuple<? extends CacheObject, GridCacheVersion>>() {

                    @Override
                    public IgniteBiTuple<? extends CacheObject, GridCacheVersion> apply(CacheObject val) {
                        return F.t(val, ver);
                    }
                });
                ctx.store().putAll(null, view);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = UPDATE;
        } else {
            try {
                ctx.store().removeAll(null, rmvKeys);
            } catch (CacheStorePartialUpdateException e) {
                storeErr = e;
            }
            op = DELETE;
        }
    } catch (IgniteCheckedException e) {
        if (err == null)
            err = partialUpdateException();
        err.add(putMap != null ? putMap.keySet() : rmvKeys, e);
        return err;
    }
    boolean intercept = ctx.config().getInterceptor() != null;
    for (int i = 0; i < entries.size(); i++) {
        GridCacheEntryEx entry = entries.get(i);
        assert entry.lockedByCurrentThread();
        if (entry.obsolete() || (storeErr != null && storeErr.failedKeys().contains(entry.key().value(ctx.cacheObjectContext(), false))))
            continue;
        try {
            // We are holding java-level locks on entries at this point.
            CacheObject writeVal = op == UPDATE ? writeVals.get(i) : null;
            assert writeVal != null || op == DELETE : "null write value found.";
            GridTuple3<Boolean, Object, EntryProcessorResult<Object>> t = entry.innerUpdateLocal(ver, op, writeVal, null, false, false, false, keepBinary, expiryPlc, true, true, null, false, subjId, taskName);
            if (intercept) {
                if (op == UPDATE)
                    ctx.config().getInterceptor().onAfterPut(new CacheLazyEntry(ctx, entry.key(), writeVal, keepBinary));
                else
                    ctx.config().getInterceptor().onAfterRemove(new CacheLazyEntry(ctx, entry.key(), t.get2(), keepBinary));
            }
        } catch (GridCacheEntryRemovedException ignore) {
            assert false : "Entry cannot become obsolete while holding lock.";
        } catch (IgniteCheckedException e) {
            if (err == null)
                err = partialUpdateException();
            err.add(Collections.singleton(entry.key()), e);
        }
    }
    return err;
}
Also used : CacheLazyEntry(org.apache.ignite.internal.processors.cache.CacheLazyEntry) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheStorePartialUpdateException(org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCacheOperation(org.apache.ignite.internal.processors.cache.GridCacheOperation) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)93 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)42 ArrayList (java.util.ArrayList)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 HashMap (java.util.HashMap)20 List (java.util.List)20 IgniteException (org.apache.ignite.IgniteException)20 IOException (java.io.IOException)18 Map (java.util.Map)16 UUID (java.util.UUID)16 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 Collection (java.util.Collection)10 Collections (java.util.Collections)10 Iterator (java.util.Iterator)10 Random (java.util.Random)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 Collectors (java.util.stream.Collectors)9