Search in sources :

Example 1 with GridCacheQueryManager

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

the class GridCacheProcessor method createCache.

/**
     * @param cfg Cache configuration to use to create cache.
     * @param pluginMgr Cache plugin manager.
     * @param desc Cache descriptor.
     * @param locStartTopVer Current topology version.
     * @param cacheObjCtx Cache object context.
     * @param affNode {@code True} if local node affinity node.
     * @param updatesAllowed Updates allowed flag.
     * @return Cache context.
     * @throws IgniteCheckedException If failed to create cache.
     */
private GridCacheContext createCache(CacheConfiguration<?, ?> cfg, @Nullable CachePluginManager pluginMgr, DynamicCacheDescriptor desc, AffinityTopologyVersion locStartTopVer, CacheObjectContext cacheObjCtx, boolean affNode, boolean updatesAllowed) throws IgniteCheckedException {
    assert cfg != null;
    if (cfg.getCacheStoreFactory() instanceof GridCacheLoaderWriterStoreFactory) {
        GridCacheLoaderWriterStoreFactory factory = (GridCacheLoaderWriterStoreFactory) cfg.getCacheStoreFactory();
        prepare(cfg, factory.loaderFactory(), false);
        prepare(cfg, factory.writerFactory(), false);
    } else
        prepare(cfg, cfg.getCacheStoreFactory(), false);
    CacheStore cfgStore = cfg.getCacheStoreFactory() != null ? cfg.getCacheStoreFactory().create() : null;
    validate(ctx.config(), cfg, desc.cacheType(), cfgStore);
    if (pluginMgr == null)
        pluginMgr = new CachePluginManager(ctx, cfg);
    pluginMgr.validate();
    sharedCtx.jta().registerCache(cfg);
    // Skip suggestions for internal caches.
    if (desc.cacheType().userCache())
        suggestOptimizations(cfg, cfgStore != null);
    Collection<Object> toPrepare = new ArrayList<>();
    if (cfgStore instanceof GridCacheLoaderWriterStore) {
        toPrepare.add(((GridCacheLoaderWriterStore) cfgStore).loader());
        toPrepare.add(((GridCacheLoaderWriterStore) cfgStore).writer());
    } else
        toPrepare.add(cfgStore);
    prepare(cfg, toPrepare);
    U.startLifecycleAware(lifecycleAwares(cfg, cfgStore));
    boolean nearEnabled = GridCacheUtils.isNearEnabled(cfg);
    GridCacheAffinityManager affMgr = new GridCacheAffinityManager();
    GridCacheEventManager evtMgr = new GridCacheEventManager();
    CacheEvictionManager evictMgr = (nearEnabled || cfg.isOnheapCacheEnabled()) ? new GridCacheEvictionManager() : new CacheOffheapEvictionManager();
    GridCacheQueryManager qryMgr = queryManager(cfg);
    CacheContinuousQueryManager contQryMgr = new CacheContinuousQueryManager();
    CacheDataStructuresManager dataStructuresMgr = new CacheDataStructuresManager();
    GridCacheTtlManager ttlMgr = new GridCacheTtlManager();
    CacheConflictResolutionManager rslvrMgr = pluginMgr.createComponent(CacheConflictResolutionManager.class);
    GridCacheDrManager drMgr = pluginMgr.createComponent(GridCacheDrManager.class);
    CacheStoreManager storeMgr = pluginMgr.createComponent(CacheStoreManager.class);
    IgniteCacheOffheapManager offheapMgr = pluginMgr.createComponent(IgniteCacheOffheapManager.class);
    storeMgr.initialize(cfgStore, sesHolders);
    String memPlcName = cfg.getMemoryPolicyName();
    MemoryPolicy memPlc = sharedCtx.database().memoryPolicy(memPlcName);
    FreeList freeList = sharedCtx.database().freeList(memPlcName);
    ReuseList reuseList = sharedCtx.database().reuseList(memPlcName);
    GridCacheContext<?, ?> cacheCtx = new GridCacheContext(ctx, sharedCtx, cfg, desc.cacheType(), locStartTopVer, desc.receivedFrom(), affNode, updatesAllowed, memPlc, freeList, reuseList, /*
             * Managers in starting order!
             * ===========================
             */
    evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, offheapMgr, rslvrMgr, pluginMgr, affMgr);
    cacheCtx.cacheObjectContext(cacheObjCtx);
    GridCacheAdapter cache = null;
    switch(cfg.getCacheMode()) {
        case LOCAL:
            {
                switch(cfg.getAtomicityMode()) {
                    case TRANSACTIONAL:
                        {
                            cache = new GridLocalCache(cacheCtx);
                            break;
                        }
                    case ATOMIC:
                        {
                            cache = new GridLocalAtomicCache(cacheCtx);
                            break;
                        }
                    default:
                        {
                            assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
                        }
                }
                break;
            }
        case PARTITIONED:
        case REPLICATED:
            {
                if (nearEnabled) {
                    switch(cfg.getAtomicityMode()) {
                        case TRANSACTIONAL:
                            {
                                cache = new GridNearTransactionalCache(cacheCtx);
                                break;
                            }
                        case ATOMIC:
                            {
                                cache = new GridNearAtomicCache(cacheCtx);
                                break;
                            }
                        default:
                            {
                                assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
                            }
                    }
                } else {
                    switch(cfg.getAtomicityMode()) {
                        case TRANSACTIONAL:
                            {
                                cache = cacheCtx.affinityNode() ? new GridDhtColocatedCache(cacheCtx) : new GridDhtColocatedCache(cacheCtx, new GridNoStorageCacheMap(cacheCtx));
                                break;
                            }
                        case ATOMIC:
                            {
                                cache = cacheCtx.affinityNode() ? new GridDhtAtomicCache(cacheCtx) : new GridDhtAtomicCache(cacheCtx, new GridNoStorageCacheMap(cacheCtx));
                                break;
                            }
                        default:
                            {
                                assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
                            }
                    }
                }
                break;
            }
        default:
            {
                assert false : "Invalid cache mode: " + cfg.getCacheMode();
            }
    }
    cacheCtx.cache(cache);
    GridCacheContext<?, ?> ret = cacheCtx;
    /*
         * Create DHT cache.
         * ================
         */
    if (cfg.getCacheMode() != LOCAL && nearEnabled) {
        /*
             * Specifically don't create the following managers
             * here and reuse the one from Near cache:
             * 1. GridCacheVersionManager
             * 2. GridCacheIoManager
             * 3. GridCacheDeploymentManager
             * 4. GridCacheQueryManager (note, that we start it for DHT cache though).
             * 5. CacheContinuousQueryManager (note, that we start it for DHT cache though).
             * 6. GridCacheDgcManager
             * 7. GridCacheTtlManager.
             * ===============================================
             */
        evictMgr = cfg.isOnheapCacheEnabled() ? new GridCacheEvictionManager() : new CacheOffheapEvictionManager();
        evtMgr = new GridCacheEventManager();
        pluginMgr = new CachePluginManager(ctx, cfg);
        drMgr = pluginMgr.createComponent(GridCacheDrManager.class);
        cacheCtx = new GridCacheContext(ctx, sharedCtx, cfg, desc.cacheType(), locStartTopVer, desc.receivedFrom(), affNode, true, memPlc, freeList, reuseList, /*
                 * Managers in starting order!
                 * ===========================
                 */
        evtMgr, storeMgr, evictMgr, qryMgr, contQryMgr, dataStructuresMgr, ttlMgr, drMgr, offheapMgr, rslvrMgr, pluginMgr, affMgr);
        cacheCtx.cacheObjectContext(cacheObjCtx);
        GridDhtCacheAdapter dht = null;
        switch(cfg.getAtomicityMode()) {
            case TRANSACTIONAL:
                {
                    assert cache instanceof GridNearTransactionalCache;
                    GridNearTransactionalCache near = (GridNearTransactionalCache) cache;
                    GridDhtCache dhtCache = cacheCtx.affinityNode() ? new GridDhtCache(cacheCtx) : new GridDhtCache(cacheCtx, new GridNoStorageCacheMap(cacheCtx));
                    dhtCache.near(near);
                    near.dht(dhtCache);
                    dht = dhtCache;
                    break;
                }
            case ATOMIC:
                {
                    assert cache instanceof GridNearAtomicCache;
                    GridNearAtomicCache near = (GridNearAtomicCache) cache;
                    GridDhtAtomicCache dhtCache = cacheCtx.affinityNode() ? new GridDhtAtomicCache(cacheCtx) : new GridDhtAtomicCache(cacheCtx, new GridNoStorageCacheMap(cacheCtx));
                    dhtCache.near(near);
                    near.dht(dhtCache);
                    dht = dhtCache;
                    break;
                }
            default:
                {
                    assert false : "Invalid cache atomicity mode: " + cfg.getAtomicityMode();
                }
        }
        cacheCtx.cache(dht);
    }
    if (!CU.isUtilityCache(cache.name()) && !CU.isSystemCache(cache.name())) {
        registerMbean(cache.localMxBean(), cache.name(), false);
        registerMbean(cache.clusterMxBean(), cache.name(), false);
    }
    return ret;
}
Also used : GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) ArrayList(java.util.ArrayList) CacheDataStructuresManager(org.apache.ignite.internal.processors.cache.datastructures.CacheDataStructuresManager) CacheStoreManager(org.apache.ignite.internal.processors.cache.store.CacheStoreManager) CacheContinuousQueryManager(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager) GridLocalAtomicCache(org.apache.ignite.internal.processors.cache.local.atomic.GridLocalAtomicCache) GridNearAtomicCache(org.apache.ignite.internal.processors.cache.distributed.near.GridNearAtomicCache) GridDhtColocatedCache(org.apache.ignite.internal.processors.cache.distributed.dht.colocated.GridDhtColocatedCache) CachePluginManager(org.apache.ignite.internal.processors.plugin.CachePluginManager) GridLocalCache(org.apache.ignite.internal.processors.cache.local.GridLocalCache) GridDhtCache(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCache) GridDhtAtomicCache(org.apache.ignite.internal.processors.cache.distributed.dht.atomic.GridDhtAtomicCache) FreeList(org.apache.ignite.internal.processors.cache.database.freelist.FreeList) GridNearTransactionalCache(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTransactionalCache) MemoryPolicy(org.apache.ignite.internal.processors.cache.database.MemoryPolicy) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) CacheStore(org.apache.ignite.cache.store.CacheStore) GridCacheDrManager(org.apache.ignite.internal.processors.cache.dr.GridCacheDrManager) ReuseList(org.apache.ignite.internal.processors.cache.database.tree.reuse.ReuseList)

Example 2 with GridCacheQueryManager

use of org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager 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 {
        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().oldestAliveCacheServerNode(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) {
                return new CacheEntryImpl<>(e.getKey(), e.getValue());
            }

            @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) 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 GridCacheQueryManager

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

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedSameQuery.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testMultiThreadedSameQuery() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 10;
    final int logMod = 5000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Integer> c = cache(Integer.class, Integer.class);
    for (int i = 0; i < keyCnt; i++) {
        c.put(i, i);
        c.localEvict(Arrays.asList(i));
    }
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            int iter = 0;
            while (!done.get() && !Thread.currentThread().isInterrupted()) {
                iter++;
                Collection<Cache.Entry<Integer, Integer>> entries = c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
                assert entries != null;
                assertEquals("Query results [entries=" + entries + ", aff=" + affinityNodes(entries, g) + ", iteration=" + iter + ']', keyCnt, entries.size());
                if (cnt.incrementAndGet() % logMod == 0) {
                    GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(c.getName()).context().queries();
                    assert qryMgr != null;
                    qryMgr.printMemoryStats();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    info("Finishing test...");
    done.set(true);
    fut.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 4 with GridCacheQueryManager

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

the class IgniteCacheQueryMultiThreadedSelfTest method testMultiThreadedNewQueries.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testMultiThreadedNewQueries() throws Exception {
    int threadCnt = 50;
    final int keyCnt = 10;
    final int logMod = 5000;
    final Ignite g = grid(0);
    // Put test values into cache.
    final IgniteCache<Integer, Integer> c = cache(Integer.class, Integer.class);
    for (int i = 0; i < keyCnt; i++) {
        c.put(i, i);
        c.localEvict(Arrays.asList(i));
    }
    final AtomicInteger cnt = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    IgniteInternalFuture<?> fut = multithreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            int iter = 0;
            while (!done.get() && !Thread.currentThread().isInterrupted()) {
                iter++;
                Collection<Cache.Entry<Integer, Integer>> entries = c.query(new SqlQuery(Integer.class, "_val >= 0")).getAll();
                assert entries != null;
                assertEquals("Entries count is not as expected on iteration: " + iter, keyCnt, entries.size());
                if (cnt.incrementAndGet() % logMod == 0) {
                    GridCacheQueryManager<Object, Object> qryMgr = ((IgniteKernal) g).internalCache(c.getName()).context().queries();
                    assert qryMgr != null;
                    qryMgr.printMemoryStats();
                }
            }
        }
    }, threadCnt);
    Thread.sleep(DURATION);
    done.set(true);
    fut.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CAX(org.apache.ignite.internal.util.typedef.CAX) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 5 with GridCacheQueryManager

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

the class GridCacheSetAbstractSelfTest method assertSetIteratorsCleared.

/**
     * Checks internal iterators maps are cleared.
     */
private void assertSetIteratorsCleared() {
    for (int i = 0; i < gridCount(); i++) {
        IgniteKernal grid = (IgniteKernal) grid(i);
        for (IgniteCache cache : grid.caches()) {
            GridCacheQueryManager queries = grid.internalCache(cache.getName()).context().queries();
            Map map = GridTestUtils.getFieldValue(queries, GridCacheQueryManager.class, "qryIters");
            for (Object obj : map.values()) assertEquals("Iterators not removed for grid " + i, 0, ((Map) obj).size());
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Map(java.util.Map)

Aggregations

GridCacheQueryManager (org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Cache (javax.cache.Cache)4 Ignite (org.apache.ignite.Ignite)4 IgniteCache (org.apache.ignite.IgniteCache)4 CAX (org.apache.ignite.internal.util.typedef.CAX)4 Collection (java.util.Collection)3 Map (java.util.Map)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 IgniteException (org.apache.ignite.IgniteException)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 CacheStore (org.apache.ignite.cache.store.CacheStore)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1