Search in sources :

Example 1 with GridEmptyCloseableIterator

use of org.apache.ignite.internal.util.GridEmptyCloseableIterator in project ignite by apache.

the class GridCacheQueryManager method scanIterator.

/**
 * @param qry Query.
 * @param transformer Transformer.
 * @param locNode Local node.
 * @return Full-scan row iterator.
 * @throws IgniteCheckedException If failed to get iterator.
 */
@SuppressWarnings({ "unchecked" })
private GridCloseableIterator scanIterator(final GridCacheQueryAdapter<?> qry, IgniteClosure transformer, boolean locNode) throws IgniteCheckedException {
    assert !cctx.mvccEnabled() || qry.mvccSnapshot() != null;
    final IgniteBiPredicate<K, V> keyValFilter = qry.scanFilter();
    final InternalScanFilter<K, V> intFilter = keyValFilter != null ? new InternalScanFilter<>(keyValFilter) : null;
    try {
        if (keyValFilter instanceof PlatformCacheEntryFilter)
            ((PlatformCacheEntryFilter) keyValFilter).cacheContext(cctx);
        else
            injectResources(keyValFilter);
        Integer part = cctx.isLocal() ? null : qry.partition();
        if (part != null && (part < 0 || part >= cctx.affinity().partitions()))
            return new GridEmptyCloseableIterator() {

                @Override
                public void close() throws IgniteCheckedException {
                    if (intFilter != null)
                        intFilter.close();
                    super.close();
                }
            };
        AffinityTopologyVersion topVer = GridQueryProcessor.getRequestAffinityTopologyVersion();
        if (topVer == null)
            topVer = cctx.affinity().affinityTopologyVersion();
        final boolean backups = qry.includeBackups() || cctx.isReplicated();
        final GridDhtLocalPartition locPart;
        final GridIterator<CacheDataRow> it;
        if (part != null) {
            final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
            GridDhtLocalPartition locPart0 = dht.topology().localPartition(part, topVer, false);
            if (locPart0 == null || locPart0.state() != OWNING || !locPart0.reserve()) {
                throw locPart0 != null && locPart0.state() == LOST ? new CacheInvalidStateException("Failed to execute scan query because cache partition has been " + "lost [cacheName=" + cctx.name() + ", part=" + part + "]") : new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
            }
            locPart = locPart0;
            it = cctx.offheap().cachePartitionIterator(cctx.cacheId(), part, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
        } else {
            locPart = null;
            if (!cctx.isLocal()) {
                final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
                Set<Integer> lostParts = dht.topology().lostPartitions();
                if (!lostParts.isEmpty()) {
                    throw new CacheInvalidStateException("Failed to execute scan query because cache partition " + "has been lost [cacheName=" + cctx.name() + ", part=" + lostParts.iterator().next() + "]");
                }
            }
            it = cctx.offheap().cacheIterator(cctx.cacheId(), true, backups, topVer, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
        }
        ScanQueryIterator iter = new ScanQueryIterator(it, qry, topVer, locPart, SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteBiPredicate.class, keyValFilter), SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteClosure.class, transformer), locNode, locNode ? locIters : null, cctx, log);
        if (locNode) {
            ScanQueryIterator old = locIters.addx(iter);
            assert old == null;
        }
        return iter;
    } catch (IgniteCheckedException | RuntimeException e) {
        if (intFilter != null)
            intFilter.close();
        throw e;
    }
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridDhtCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) GridDhtUnreservedPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) PlatformCacheEntryFilter(org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException)

Example 2 with GridEmptyCloseableIterator

use of org.apache.ignite.internal.util.GridEmptyCloseableIterator in project ignite by apache.

the class IgniteCachePeekModesAbstractTest method swapKeys.

/**
 * @param nodeIdx Node index.
 * @return Tuple with primary and backup keys.
 */
private T2<List<Integer>, List<Integer>> swapKeys(int nodeIdx) {
    // TODO: GG-11148.
    // SwapSpaceSpi swap = ignite(nodeIdx).configuration().getSwapSpaceSpi();
    // 
    // IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
    IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
    assertNotNull(it);
    Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
    ClusterNode node = ignite(nodeIdx).cluster().localNode();
    List<Integer> primary = new ArrayList<>();
    List<Integer> backups = new ArrayList<>();
    CacheObjectContext coctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context().cacheObjectContext();
    while (it.hasNext()) {
        Integer key = it.next().value(coctx, false);
        if (aff.isPrimary(node, key))
            primary.add(key);
        else {
            assertTrue(aff.isBackup(node, key));
            backups.add(key);
        }
    }
    return new T2<>(primary, backups);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteEx(org.apache.ignite.internal.IgniteEx) ArrayList(java.util.ArrayList) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) Affinity(org.apache.ignite.cache.affinity.Affinity) T2(org.apache.ignite.internal.util.typedef.T2)

Example 3 with GridEmptyCloseableIterator

use of org.apache.ignite.internal.util.GridEmptyCloseableIterator in project ignite by apache.

the class IgniteCachePeekModesAbstractTest method checkStorage.

/**
 * @param nodeIdx Node index.
 * @throws Exception If failed.
 */
private void checkStorage(int nodeIdx) throws Exception {
    if (// TODO GG-11148.
    true)
        return;
    IgniteCache<Integer, String> cache0 = jcache(nodeIdx);
    List<Integer> keys = primaryKeys(cache0, 100, 10_000);
    try {
        final String val = "test_value";
        for (Integer key : keys) cache0.put(key, val);
        Ignite ignite = ignite(nodeIdx);
        GridCacheAdapter<Integer, String> internalCache = ((IgniteKernal) ignite).context().cache().internalCache(DEFAULT_CACHE_NAME);
        CacheObjectContext coctx = internalCache.context().cacheObjectContext();
        Set<Integer> swapKeys = new HashSet<>();
        // TODO GG-11148.
        // SwapSpaceSpi swap = ignite.configuration().getSwapSpaceSpi();
        // 
        // IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
        IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
        assertNotNull(it);
        while (it.hasNext()) {
            KeyCacheObject next = it.next();
            assertTrue(swapKeys.add((Integer) next.value(coctx, false)));
        }
        assertFalse(swapKeys.isEmpty());
        assertTrue(swapKeys.size() + HEAP_ENTRIES < 100);
        Set<Integer> offheapKeys = new HashSet<>();
        // TODO GG-11148.
        Iterator<Map.Entry<Integer, String>> offheapIt = Collections.EMPTY_MAP.entrySet().iterator();
        while (offheapIt.hasNext()) {
            Map.Entry<Integer, String> e = offheapIt.next();
            assertTrue(offheapKeys.add(e.getKey()));
            assertFalse(swapKeys.contains(e.getKey()));
        }
        assertFalse(offheapKeys.isEmpty());
        Set<Integer> heapKeys = new HashSet<>(keys);
        heapKeys.removeAll(offheapKeys);
        heapKeys.removeAll(swapKeys);
        assertFalse(heapKeys.isEmpty());
        log.info("Keys [swap=" + swapKeys.size() + ", offheap=" + offheapKeys.size() + ", heap=" + heapKeys.size() + ']');
        assertEquals(100, swapKeys.size() + offheapKeys.size() + heapKeys.size());
        for (Integer key : swapKeys) {
            assertEquals(val, cache0.localPeek(key));
            assertEquals(val, cache0.localPeek(key, PRIMARY));
            assertEquals(val, cache0.localPeek(key, ONHEAP));
            assertEquals(val, cache0.localPeek(key, ONHEAP, OFFHEAP));
            assertEquals(val, cache0.localPeek(key, PRIMARY, ONHEAP));
            assertEquals(val, cache0.localPeek(key, PRIMARY, ONHEAP, OFFHEAP));
            if (cacheMode() == LOCAL) {
                assertEquals(val, cache0.localPeek(key, BACKUP));
                assertEquals(val, cache0.localPeek(key, NEAR));
            } else {
                assertNull(cache0.localPeek(key, BACKUP));
                assertNull(cache0.localPeek(key, NEAR));
            }
            assertNull(cache0.localPeek(key, ONHEAP));
            assertNull(cache0.localPeek(key, OFFHEAP));
        }
        for (Integer key : offheapKeys) {
            assertEquals(val, cache0.localPeek(key, OFFHEAP));
            assertEquals(val, cache0.localPeek(key, ONHEAP, OFFHEAP));
            assertEquals(val, cache0.localPeek(key, ONHEAP, OFFHEAP));
            assertEquals(val, cache0.localPeek(key, PRIMARY, OFFHEAP));
            if (cacheMode() == LOCAL) {
                assertEquals(val, cache0.localPeek(key, OFFHEAP, BACKUP));
                assertEquals(val, cache0.localPeek(key, OFFHEAP, NEAR));
            } else {
                assertNull(cache0.localPeek(key, OFFHEAP, BACKUP));
                assertNull(cache0.localPeek(key, OFFHEAP, NEAR));
            }
            assertNull(cache0.localPeek(key, ONHEAP));
            assertNull(cache0.localPeek(key));
        }
        for (Integer key : heapKeys) {
            assertEquals(val, cache0.localPeek(key, ONHEAP));
            assertEquals(val, cache0.localPeek(key, ONHEAP));
            assertEquals(val, cache0.localPeek(key, OFFHEAP, ONHEAP));
            assertEquals(val, cache0.localPeek(key, PRIMARY, ONHEAP));
            if (cacheMode() == LOCAL) {
                assertEquals(val, cache0.localPeek(key, ONHEAP, BACKUP));
                assertEquals(val, cache0.localPeek(key, ONHEAP, NEAR));
            } else {
                assertNull(cache0.localPeek(key, ONHEAP, BACKUP));
                assertNull(cache0.localPeek(key, ONHEAP, NEAR));
            }
            assertNull(cache0.localPeek(key));
            assertNull(cache0.localPeek(key, OFFHEAP));
        }
    } finally {
        cache0.removeAll(new HashSet<>(keys));
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with GridEmptyCloseableIterator

use of org.apache.ignite.internal.util.GridEmptyCloseableIterator in project ignite by apache.

the class GridCacheQueryAdapter method executeScanQuery.

/**
 * {@inheritDoc}
 */
@Override
public GridCloseableIterator executeScanQuery() throws IgniteCheckedException {
    assert type == SCAN : "Wrong processing of query: " + type;
    if (!cctx.isLocal()) {
        GridDhtCacheAdapter<?, ?> cacheAdapter = cctx.isNear() ? cctx.near().dht() : cctx.dht();
        Set<Integer> lostParts = cacheAdapter.topology().lostPartitions();
        if (!lostParts.isEmpty()) {
            if (part == null || lostParts.contains(part)) {
                throw new CacheException(new CacheInvalidStateException("Failed to execute query because cache partition " + "has been lostParts [cacheName=" + cctx.name() + ", part=" + (part == null ? lostParts.iterator().next() : part) + ']'));
            }
        }
    }
    // Affinity nodes snapshot.
    Collection<ClusterNode> nodes = new ArrayList<>(nodes());
    cctx.checkSecurity(SecurityPermission.CACHE_READ);
    if (nodes.isEmpty()) {
        if (part != null) {
            if (forceLocal) {
                throw new IgniteCheckedException("No queryable nodes for partition " + part + " [forced local query=" + this + "]");
            }
        }
        return new GridEmptyCloseableIterator();
    }
    if (log.isDebugEnabled())
        log.debug("Executing query [query=" + this + ", nodes=" + nodes + ']');
    if (cctx.deploymentEnabled())
        cctx.deploy().registerClasses(filter);
    taskHash = cctx.kernalContext().job().currentTaskNameHash();
    final GridCacheQueryManager qryMgr = cctx.queries();
    MvccQueryTracker mvccTracker = null;
    if (cctx.mvccEnabled() && mvccSnapshot == null) {
        GridNearTxLocal tx = cctx.tm().userTx();
        if (tx != null)
            mvccSnapshot = MvccUtils.requestSnapshot(tx);
        else {
            mvccTracker = MvccUtils.mvccTracker(cctx, null);
            mvccSnapshot = mvccTracker.snapshot();
        }
        assert mvccSnapshot != null;
    }
    boolean loc = nodes.size() == 1 && F.first(nodes).id().equals(cctx.localNodeId());
    GridCloseableIterator it;
    if (loc)
        it = qryMgr.scanQueryLocal(this, true);
    else if (part != null)
        it = new ScanQueryFallbackClosableIterator(part, this, qryMgr, cctx);
    else
        it = qryMgr.scanQueryDistributed(this, nodes);
    return mvccTracker != null ? new MvccTrackingIterator(it, mvccTracker) : it;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) CacheException(javax.cache.CacheException) ArrayList(java.util.ArrayList) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheInvalidStateException(org.apache.ignite.internal.processors.cache.CacheInvalidStateException)

Aggregations

GridEmptyCloseableIterator (org.apache.ignite.internal.util.GridEmptyCloseableIterator)4 ArrayList (java.util.ArrayList)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CacheInvalidStateException (org.apache.ignite.internal.processors.cache.CacheInvalidStateException)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 CacheException (javax.cache.CacheException)1 Ignite (org.apache.ignite.Ignite)1 Affinity (org.apache.ignite.cache.affinity.Affinity)1 IgniteEx (org.apache.ignite.internal.IgniteEx)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1 GridDhtCacheAdapter (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheAdapter)1 GridDhtUnreservedPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException)1 GridDhtLocalPartition (org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition)1 GridNearTxLocal (org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal)1 MvccQueryTracker (org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker)1 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)1 PlatformCacheEntryFilter (org.apache.ignite.internal.processors.platform.cache.PlatformCacheEntryFilter)1