Search in sources :

Example 1 with IgniteInternalCache

use of org.apache.ignite.internal.processors.cache.IgniteInternalCache in project ignite by apache.

the class GridCacheCommandHandler method executeCommand.

/**
     * Executes command on flagged cache projection. Checks {@code destId} to find if command could be performed locally
     * or routed to a remote node.
     *
     * @param destId Target node Id for the operation. If {@code null} - operation could be executed anywhere.
     * @param clientId Client ID.
     * @param cacheName Cache name.
     * @param skipStore Skip store.
     * @param key Key to set affinity mapping in the response.
     * @param op Operation to perform.
     * @return Operation result in future.
     * @throws IgniteCheckedException If failed
     */
private IgniteInternalFuture<GridRestResponse> executeCommand(@Nullable UUID destId, UUID clientId, final String cacheName, final boolean skipStore, final Object key, final CacheProjectionCommand op) throws IgniteCheckedException {
    final boolean locExec = destId == null || destId.equals(ctx.localNodeId()) || replicatedCacheAvailable(cacheName);
    if (locExec) {
        IgniteInternalCache<?, ?> prj = localCache(cacheName).forSubjectId(clientId).setSkipStore(skipStore);
        return op.apply((IgniteInternalCache<Object, Object>) prj, ctx).chain(resultWrapper((IgniteInternalCache<Object, Object>) prj, key));
    } else {
        ClusterGroup prj = ctx.grid().cluster().forPredicate(F.nodeForNodeId(destId));
        ctx.task().setThreadContext(TC_NO_FAILOVER, true);
        return ctx.closure().callAsync(BALANCE, new FlaggedCacheOperationCallable(clientId, cacheName, skipStore, op, key), prj.nodes());
    }
}
Also used : IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 2 with IgniteInternalCache

use of org.apache.ignite.internal.processors.cache.IgniteInternalCache in project ignite by apache.

the class GridServiceProcessor method serviceEntries.

/**
 * @param p Entry predicate used to execute query from client node.
 * @return Service deployment entries.
 */
@SuppressWarnings("unchecked")
private Iterator<Cache.Entry<Object, Object>> serviceEntries(IgniteBiPredicate<Object, Object> p) {
    try {
        IgniteInternalCache<Object, Object> cache = serviceCache();
        GridCacheQueryManager qryMgr = cache.context().queries();
        CacheQuery<Map.Entry<Object, Object>> qry = qryMgr.createScanQuery(p, null, false);
        qry.keepAll(false);
        if (!cache.context().affinityNode()) {
            ClusterNode oldestSrvNode = ctx.discovery().oldestAliveServerNode(AffinityTopologyVersion.NONE);
            if (oldestSrvNode == null)
                return new GridEmptyIterator<>();
            qry.projection(ctx.cluster().get().forNode(oldestSrvNode));
        } else
            qry.projection(ctx.cluster().get().forLocal());
        GridCloseableIterator<Map.Entry<Object, Object>> iter = qry.executeScanQuery();
        return cache.context().itHolder().iterator(iter, new CacheIteratorConverter<Cache.Entry<Object, Object>, Map.Entry<Object, Object>>() {

            @Override
            protected Cache.Entry<Object, Object> convert(Map.Entry<Object, Object> e) {
                // CacheQueryEntry implements both Map.Entry and Cache.Entry interfaces.
                return (Cache.Entry<Object, Object>) e;
            }

            @Override
            protected void remove(Cache.Entry<Object, Object> item) {
                throw new UnsupportedOperationException();
            }
        });
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) DiscoCache(org.apache.ignite.internal.managers.discovery.DiscoCache)

Example 3 with IgniteInternalCache

use of org.apache.ignite.internal.processors.cache.IgniteInternalCache in project ignite by apache.

the class DataStructuresProcessor method compatibleCache.

/**
 * @param cfg Collection configuration.
 * @return Cache name.
 * @param grpName Group name.
 * @throws IgniteCheckedException If failed.
 */
@Nullable
private IgniteInternalCache compatibleCache(CollectionConfiguration cfg, String grpName) throws IgniteCheckedException {
    String cacheName = DS_CACHE_NAME_PREFIX + cfg.getAtomicityMode() + "_" + cfg.getCacheMode() + "_" + cfg.getBackups() + "@" + grpName;
    IgniteInternalCache cache = ctx.cache().cache(cacheName);
    if (cache == null) {
        ctx.cache().dynamicStartCache(cacheConfiguration(cfg, cacheName, grpName), cacheName, null, CacheType.DATA_STRUCTURES, false, false, true, true).get();
    } else {
        IgnitePredicate<ClusterNode> cacheNodeFilter = cache.context().group().nodeFilter();
        String clsName1 = cacheNodeFilter != null ? cacheNodeFilter.getClass().getName() : CacheConfiguration.IgniteAllNodesPredicate.class.getName();
        String clsName2 = cfg.getNodeFilter() != null ? cfg.getNodeFilter().getClass().getName() : CacheConfiguration.IgniteAllNodesPredicate.class.getName();
        if (!clsName1.equals(clsName2))
            throw new IgniteCheckedException("Could not add collection to group " + grpName + " because of different node filters [existing=" + clsName1 + ", new=" + clsName2 + "]");
    }
    cache = ctx.cache().getOrStartCache(cacheName);
    assert cache != null;
    return cache;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with IgniteInternalCache

use of org.apache.ignite.internal.processors.cache.IgniteInternalCache in project ignite by apache.

the class IgfsSizeSelfTest method check.

/**
 * Ensure that IGFS cache size is calculated correctly.
 *
 * @throws Exception If failed.
 */
private void check() throws Exception {
    startUp();
    // Ensure that cache was marked as IGFS data cache.
    for (int i = 0; i < GRID_CNT; i++) {
        IgniteEx g = grid(i);
        IgniteInternalCache cache = g.cachex(g.igfsx(IGFS_NAME).configuration().getDataCacheConfiguration().getName()).cache();
        assert cache.isIgfsDataCache();
    }
    // Perform writes.
    Collection<IgfsFile> files = write();
    // Check sizes.
    Map<UUID, Integer> expSizes = new HashMap<>(GRID_CNT, 1.0f);
    for (IgfsFile file : files) {
        for (IgfsBlock block : file.blocks()) {
            Collection<UUID> ids = primaryOrBackups(block.key());
            for (UUID id : ids) {
                if (expSizes.get(id) == null)
                    expSizes.put(id, block.length());
                else
                    expSizes.put(id, expSizes.get(id) + block.length());
            }
        }
    }
    for (int i = 0; i < GRID_CNT; i++) {
        UUID id = grid(i).localNode().id();
        GridCacheAdapter<IgfsBlockKey, byte[]> cache = cache(id);
        int expSize = expSizes.get(id) != null ? expSizes.get(id) : 0;
        assert expSize == cache.igfsDataSpaceUsed();
    }
    // Perform reads which could potentially be non-local.
    byte[] buf = new byte[BLOCK_SIZE];
    for (IgfsFile file : files) {
        for (int i = 0; i < GRID_CNT; i++) {
            int total = 0;
            IgfsInputStream is = igfs(i).open(file.path());
            while (true) {
                int read = is.read(buf);
                if (read == -1)
                    break;
                else
                    total += read;
            }
            assert total == file.length() : "Not enough bytes read: [expected=" + file.length() + ", actual=" + total + ']';
            is.close();
        }
    }
    // Check sizes after read.
    if (cacheMode == PARTITIONED) {
        // No changes since the previous check for co-located cache.
        for (int i = 0; i < GRID_CNT; i++) {
            UUID id = grid(i).localNode().id();
            GridCacheAdapter<IgfsBlockKey, byte[]> cache = cache(id);
            int expSize = expSizes.get(id) != null ? expSizes.get(id) : 0;
            assert expSize == cache.igfsDataSpaceUsed();
        }
    } else {
        // All data must exist on each cache.
        int totalSize = 0;
        for (IgfsFile file : files) totalSize += file.length();
        for (int i = 0; i < GRID_CNT; i++) {
            UUID id = grid(i).localNode().id();
            GridCacheAdapter<IgfsBlockKey, byte[]> cache = cache(id);
            assertEquals(totalSize, cache.igfsDataSpaceUsed());
        }
    }
    // Delete data and ensure that all counters are 0 now.
    for (IgfsFile file : files) {
        igfs(0).delete(file.path(), false);
        // Await for actual delete to occur.
        for (IgfsBlock block : file.blocks()) {
            for (int i = 0; i < GRID_CNT; i++) {
                while (localPeek(cache(grid(i).localNode().id()), block.key()) != null) U.sleep(100);
            }
        }
    }
    for (int i = 0; i < GRID_CNT; i++) {
        GridCacheAdapter<IgfsBlockKey, byte[]> cache = cache(grid(i).localNode().id());
        assert 0 == cache.igfsDataSpaceUsed() : "Size counter is not 0: " + cache.igfsDataSpaceUsed();
    }
}
Also used : IgfsInputStream(org.apache.ignite.igfs.IgfsInputStream) HashMap(java.util.HashMap) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID)

Example 5 with IgniteInternalCache

use of org.apache.ignite.internal.processors.cache.IgniteInternalCache in project ignite by apache.

the class ViewCacheClosure method collectSequences.

/**
 * @param ctx Context.
 * @param compiled Compiled pattern.
 * @param cacheInfo Cache info.
 */
private void collectSequences(GridKernalContext ctx, Pattern compiled, List<CacheInfo> cacheInfo) throws IgniteCheckedException {
    String dsCacheName = DataStructuresProcessor.ATOMICS_CACHE_NAME + "@default-ds-group";
    IgniteInternalCache<GridCacheInternalKey, AtomicDataStructureValue> cache0 = ctx.cache().cache(dsCacheName);
    final Iterator<Cache.Entry<GridCacheInternalKey, AtomicDataStructureValue>> iter = cache0.scanIterator(false, null);
    while (iter.hasNext()) {
        Cache.Entry<GridCacheInternalKey, AtomicDataStructureValue> entry = iter.next();
        final AtomicDataStructureValue val = entry.getValue();
        if (val.type() == DataStructureType.ATOMIC_SEQ) {
            final String name = entry.getKey().name();
            if (compiled.matcher(name).find()) {
                CacheInfo ci = new CacheInfo();
                ci.setSeqName(name);
                ci.setSeqVal(((GridCacheAtomicSequenceValue) val).get());
                cacheInfo.add(ci);
            }
        }
    }
}
Also used : GridCacheInternalKey(org.apache.ignite.internal.processors.datastructures.GridCacheInternalKey) AtomicDataStructureValue(org.apache.ignite.internal.processors.datastructures.AtomicDataStructureValue) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache)

Aggregations

IgniteInternalCache (org.apache.ignite.internal.processors.cache.IgniteInternalCache)32 IgniteEx (org.apache.ignite.internal.IgniteEx)11 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 Test (org.junit.Test)8 Ignite (org.apache.ignite.Ignite)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)6 IgniteKernal (org.apache.ignite.internal.IgniteKernal)6 ArrayList (java.util.ArrayList)5 IgniteCache (org.apache.ignite.IgniteCache)5 File (java.io.File)4 Cache (javax.cache.Cache)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 List (java.util.List)3 GridKernalContext (org.apache.ignite.internal.GridKernalContext)3 GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)3 IgniteCacheOffheapManager (org.apache.ignite.internal.processors.cache.IgniteCacheOffheapManager)3 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)3