Search in sources :

Example 21 with GridDhtLocalPartition

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.

the class GridDhtCacheAdapter method processForceKeysRequest0.

/**
 * @param node Node originated request.
 * @param msg Force keys message.
 */
private void processForceKeysRequest0(ClusterNode node, GridDhtForceKeysRequest msg) {
    try {
        ClusterNode loc = ctx.localNode();
        GridDhtForceKeysResponse res = new GridDhtForceKeysResponse(ctx.cacheId(), msg.futureId(), msg.miniId(), ctx.deploymentEnabled());
        GridDhtPartitionTopology top = ctx.topology();
        for (KeyCacheObject k : msg.keys()) {
            int p = ctx.affinity().partition(k);
            GridDhtLocalPartition locPart = top.localPartition(p, AffinityTopologyVersion.NONE, false);
            // If this node is no longer an owner.
            if (locPart == null && !top.owners(p).contains(loc)) {
                res.addMissed(k);
                continue;
            }
            GridCacheEntryEx entry;
            while (true) {
                ctx.shared().database().checkpointReadLock();
                try {
                    entry = ctx.dht().entryEx(k);
                    entry.unswap();
                    if (ctx.mvccEnabled()) {
                        List<GridCacheEntryInfo> infos = entry.allVersionsInfo();
                        if (infos == null) {
                            assert entry.obsolete() : entry;
                            continue;
                        }
                        for (int i = 0; i < infos.size(); i++) res.addInfo(infos.get(i));
                    } else {
                        GridCacheEntryInfo info = entry.info();
                        if (info == null) {
                            assert entry.obsolete() : entry;
                            continue;
                        }
                        if (!info.isNew())
                            res.addInfo(info);
                    }
                    entry.touch();
                    break;
                } catch (GridCacheEntryRemovedException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Got removed entry: " + k);
                } catch (GridDhtInvalidPartitionException ignore) {
                    if (log.isDebugEnabled())
                        log.debug("Local node is no longer an owner: " + p);
                    res.addMissed(k);
                    break;
                } finally {
                    ctx.shared().database().checkpointReadUnlock();
                }
            }
        }
        if (log.isDebugEnabled())
            log.debug("Sending force key response [node=" + node.id() + ", res=" + res + ']');
        ctx.io().send(node, res, ctx.ioPolicy());
    } catch (ClusterTopologyCheckedException ignore) {
        if (log.isDebugEnabled())
            log.debug("Received force key request form failed node (will ignore) [nodeId=" + node.id() + ", req=" + msg + ']');
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to reply to force key request [nodeId=" + node.id() + ", req=" + msg + ']', e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtForceKeysResponse(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtForceKeysResponse) GridCacheEntryInfo(org.apache.ignite.internal.processors.cache.GridCacheEntryInfo) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 22 with GridDhtLocalPartition

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.

the class GridDhtCacheAdapter method loadEntry.

/**
 * @param key Key.
 * @param val Value.
 * @param ver Cache version.
 * @param p Optional predicate.
 * @param topVer Topology version.
 * @param replicate Replication flag.
 * @param plc Expiry policy.
 */
private void loadEntry(KeyCacheObject key, Object val, GridCacheVersion ver, @Nullable IgniteBiPredicate<K, V> p, AffinityTopologyVersion topVer, boolean replicate, @Nullable ExpiryPolicy plc) {
    if (p != null && !p.apply(key.<K>value(ctx.cacheObjectContext(), false), (V) val))
        return;
    try {
        GridDhtLocalPartition part = ctx.group().topology().localPartition(ctx.affinity().partition(key), AffinityTopologyVersion.NONE, true);
        // Reserve to make sure that partition does not get unloaded.
        if (part.reserve()) {
            GridCacheEntryEx entry = null;
            ctx.shared().database().checkpointReadLock();
            try {
                long ttl = CU.ttlForLoad(plc);
                if (ttl == CU.TTL_ZERO)
                    return;
                CacheObject cacheVal = ctx.toCacheObject(val);
                entry = entryEx(key);
                entry.initialValue(cacheVal, ver, ttl, CU.EXPIRE_TIME_CALCULATE, false, topVer, replicate ? DR_LOAD : DR_NONE, true, false);
            } catch (IgniteCheckedException e) {
                throw new IgniteException("Failed to put cache value: " + entry, e);
            } catch (GridCacheEntryRemovedException ignore) {
                if (log.isDebugEnabled())
                    log.debug("Got removed entry during loadCache (will ignore): " + entry);
            } finally {
                if (entry != null)
                    entry.touch();
                part.release();
                ctx.shared().database().checkpointReadUnlock();
            }
        } else if (log.isDebugEnabled())
            log.debug("Will node load entry into cache (partition is invalid): " + part);
    } catch (GridDhtInvalidPartitionException e) {
        if (log.isDebugEnabled())
            log.debug(S.toString("Ignoring entry for partition that does not belong", "key", key, true, "val", val, true, "err", e, false));
    }
}
Also used : GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtInvalidPartitionException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 23 with GridDhtLocalPartition

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.

the class CheckpointWorkflow method fillCacheGroupState.

/**
 * Fill cache group state in checkpoint record.
 *
 * @param cpRec Checkpoint record for filling.
 * @throws IgniteCheckedException if fail.
 */
private void fillCacheGroupState(CheckpointRecord cpRec) throws IgniteCheckedException {
    GridCompoundFuture grpHandleFut = checkpointCollectPagesInfoPool == null ? null : new GridCompoundFuture();
    for (CacheGroupContext grp : cacheGroupsContexts.get()) {
        if (grp.isLocal() || !grp.walEnabled())
            continue;
        Runnable r = () -> {
            ArrayList<GridDhtLocalPartition> parts = new ArrayList<>(grp.topology().localPartitions().size());
            for (GridDhtLocalPartition part : grp.topology().currentLocalPartitions()) parts.add(part);
            CacheState state = new CacheState(parts.size());
            for (GridDhtLocalPartition part : parts) {
                GridDhtPartitionState partState = part.state();
                if (partState == LOST)
                    partState = OWNING;
                state.addPartitionState(part.id(), part.dataStore().fullSize(), part.updateCounter(), (byte) partState.ordinal());
            }
            synchronized (cpRec) {
                cpRec.addCacheGroupState(grp.groupId(), state);
            }
        };
        if (checkpointCollectPagesInfoPool == null)
            r.run();
        else
            try {
                GridFutureAdapter<?> res = new GridFutureAdapter<>();
                checkpointCollectPagesInfoPool.execute(U.wrapIgniteFuture(r, res));
                grpHandleFut.add(res);
            } catch (RejectedExecutionException e) {
                assert false : "Task should never be rejected by async runner";
                // to protect from disabled asserts and call to failure handler
                throw new IgniteException(e);
            }
    }
    if (grpHandleFut != null) {
        grpHandleFut.markInitialized();
        grpHandleFut.get();
    }
}
Also used : IgniteException(org.apache.ignite.IgniteException) ArrayList(java.util.ArrayList) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheState(org.apache.ignite.internal.pagemem.wal.record.CacheState) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 24 with GridDhtLocalPartition

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.

the class CacheDhtLocalPartitionAfterRemoveSelfTest method testMemoryUsage.

/**
 * @throws Exception If failed.
 */
@Test
public void testMemoryUsage() throws Exception {
    assertEquals(10_000, GridDhtLocalPartition.MAX_DELETE_QUEUE_SIZE);
    IgniteCache<TestKey, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    for (int i = 0; i < 20_000; ++i) cache.put(new TestKey(String.valueOf(i)), i);
    for (int i = 0; i < 20_000; ++i) assertEquals((Object) i, cache.getAndRemove(new TestKey(String.valueOf(i))));
    assertEquals(0, cache.size());
    for (int g = 0; g < GRID_CNT; g++) {
        cache = grid(g).cache(DEFAULT_CACHE_NAME);
        for (GridDhtLocalPartition p : dht(cache).topology().localPartitions()) {
            long size = p.dataStore().fullSize();
            assertTrue("Unexpected size: " + size, size <= 32);
        }
    }
}
Also used : GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 25 with GridDhtLocalPartition

use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.

the class IgniteCacheOffheapManagerImpl method cacheData.

/**
 * @param primary Primary data flag.
 * @param backup Backup data flag.
 * @param topVer Topology version.
 * @return Data stores iterator.
 */
private Iterator<CacheDataStore> cacheData(boolean primary, boolean backup, AffinityTopologyVersion topVer) {
    assert primary || backup;
    if (grp.isLocal())
        return singletonIterator(locCacheDataStore);
    IgnitePredicate<GridDhtLocalPartition> filter;
    if (primary && backup)
        filter = F.alwaysTrue();
    else {
        IntSet parts = ImmutableIntSet.wrap(primary ? grp.affinity().primaryPartitions(ctx.localNodeId(), topVer) : grp.affinity().backupPartitions(ctx.localNodeId(), topVer));
        filter = part -> parts.contains(part.id());
    }
    return cacheDataStores(filter).iterator();
}
Also used : IntSet(org.apache.ignite.internal.util.collection.IntSet) ImmutableIntSet(org.apache.ignite.internal.util.collection.ImmutableIntSet) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)

Aggregations

GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)95 GridDhtPartitionTopology (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 IgniteEx (org.apache.ignite.internal.IgniteEx)19 CacheGroupContext (org.apache.ignite.internal.processors.cache.CacheGroupContext)19 ArrayList (java.util.ArrayList)18 Map (java.util.Map)18 Test (org.junit.Test)18 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)16 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)15 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 HashMap (java.util.HashMap)14 HashSet (java.util.HashSet)13 AtomicLong (java.util.concurrent.atomic.AtomicLong)13 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)13 Ignite (org.apache.ignite.Ignite)12 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)12 IgniteException (org.apache.ignite.IgniteException)11