Search in sources :

Example 11 with IgniteClosure

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

the class GridCacheQueryManager method scanQueryLocal.

/**
     * Process local scan query.
     *
     * @param qry Query.
     * @param updStatisticsIfNeeded Update statistics flag.
     */
@SuppressWarnings({ "unchecked", "serial" })
protected GridCloseableIterator scanQueryLocal(final GridCacheQueryAdapter qry, final boolean updStatisticsIfNeeded) throws IgniteCheckedException {
    if (!enterBusy())
        throw new IllegalStateException("Failed to process query request (grid is stopping).");
    final boolean statsEnabled = cctx.config().isStatisticsEnabled();
    boolean needUpdStatistics = updStatisticsIfNeeded && statsEnabled;
    long startTime = U.currentTimeMillis();
    final String namex = cctx.name();
    try {
        assert qry.type() == SCAN;
        if (log.isDebugEnabled())
            log.debug("Running local SCAN query: " + qry);
        final String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
        final IgniteBiPredicate filter = qry.scanFilter();
        final ClusterNode locNode = cctx.localNode();
        final UUID subjId = qry.subjectId();
        if (cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
            cctx.gridEvents().record(new CacheQueryExecutedEvent<>(locNode, "Scan query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SCAN.name(), namex, null, null, filter, null, null, subjId, taskName));
        }
        final GridCloseableIterator<IgniteBiTuple<K, V>> iter = scanIterator(qry, true);
        if (updStatisticsIfNeeded)
            needUpdStatistics = false;
        final boolean readEvt = cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
        return new GridCloseableIteratorAdapter<Object>() {

            @Override
            protected Object onNext() throws IgniteCheckedException {
                long start = statsEnabled ? System.nanoTime() : 0L;
                IgniteBiTuple<K, V> next = iter.nextX();
                if (statsEnabled) {
                    CacheMetricsImpl metrics = cctx.cache().metrics0();
                    metrics.onRead(true);
                    metrics.addGetTimeNanos(System.nanoTime() - start);
                }
                if (readEvt) {
                    cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "Scan query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SCAN.name(), namex, null, null, filter, null, null, subjId, taskName, next.getKey(), next.getValue(), null, null));
                }
                IgniteClosure transform = qry.transform();
                if (transform == null)
                    return next;
                Cache.Entry<K, V> entry;
                if (qry.keepBinary())
                    entry = cctx.cache().keepBinary().getEntry(next.getKey());
                else
                    entry = cctx.cache().getEntry(next.getKey());
                return transform.apply(entry);
            }

            @Override
            protected boolean onHasNext() throws IgniteCheckedException {
                return iter.hasNextX();
            }

            @Override
            protected void onClose() throws IgniteCheckedException {
                iter.close();
            }
        };
    } catch (Exception e) {
        if (needUpdStatistics)
            cctx.queries().collectMetrics(GridCacheQueryType.SCAN, namex, startTime, U.currentTimeMillis() - startTime, true);
        throw e;
    } finally {
        leaveBusy();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCloseableIteratorAdapter(org.apache.ignite.internal.util.GridCloseableIteratorAdapter) IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) CacheMetricsImpl(org.apache.ignite.internal.processors.cache.CacheMetricsImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) IOException(java.io.IOException) IgniteException(org.apache.ignite.IgniteException) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) NoSuchElementException(java.util.NoSuchElementException) GridDhtUnreservedPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtUnreservedPartitionException) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteInternalCache(org.apache.ignite.internal.processors.cache.IgniteInternalCache)

Example 12 with IgniteClosure

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

the class GridCacheQueryRequest method beforeLocalExecution.

/**
     * @param ctx Context.
     * @throws IgniteCheckedException In case of error.
     */
void beforeLocalExecution(GridCacheContext ctx) throws IgniteCheckedException {
    Marshaller marsh = ctx.marshaller();
    rdc = rdc != null ? U.<IgniteReducer<Object, Object>>unmarshal(marsh, U.marshal(marsh, rdc), U.resolveClassLoader(ctx.gridConfig())) : null;
    trans = trans != null ? U.<IgniteClosure<Object, Object>>unmarshal(marsh, U.marshal(marsh, trans), U.resolveClassLoader(ctx.gridConfig())) : null;
}
Also used : IgniteReducer(org.apache.ignite.lang.IgniteReducer) Marshaller(org.apache.ignite.marshaller.Marshaller) IgniteClosure(org.apache.ignite.lang.IgniteClosure)

Example 13 with IgniteClosure

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

the class GridCacheDistributedQueryManager method distributedQueryInfo.

/**
     * @param sndId Sender node id.
     * @param req Query request.
     * @return Query info.
     */
@Nullable
private GridCacheQueryInfo distributedQueryInfo(UUID sndId, GridCacheQueryRequest req) {
    IgniteReducer<Object, Object> rdc = req.reducer();
    IgniteClosure<Object, Object> trans = (IgniteClosure<Object, Object>) req.transformer();
    ClusterNode sndNode = cctx.node(sndId);
    if (sndNode == null)
        return null;
    GridCacheQueryAdapter<?> qry = new GridCacheQueryAdapter<>(cctx, req.type(), log, req.pageSize(), 0, false, req.includeBackups(), false, null, req.keyValueFilter(), req.partition() == -1 ? null : req.partition(), req.className(), req.clause(), req.includeMetaData(), req.keepBinary(), req.subjectId(), req.taskHash());
    return new GridCacheQueryInfo(false, trans, rdc, qry, null, sndId, req.id(), req.includeMetaData(), req.allPages(), req.arguments());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteClosure(org.apache.ignite.lang.IgniteClosure) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with IgniteClosure

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

the class IgniteLockAbstractSelfTest method testFairness.

/**
     * Tests if lock is evenly acquired among nodes when fair flag is set on.
     * Since exact ordering of lock acquisitions cannot be guaranteed because it also depends
     * on the OS thread scheduling, certain deviation from uniform distribution is tolerated.
     * @throws Exception If failed.
     */
public void testFairness() throws Exception {
    if (gridCount() == 1)
        return;
    // Total number of ops.
    final long opsCount = 10000;
    // Allowed deviation from uniform distribution.
    final double tolerance = 0.05;
    // Shared counter.
    final String OPS_COUNTER = "ops_counter";
    // Number of threads, one per node.
    final int threadCount = gridCount();
    final AtomicLong threadCounter = new AtomicLong(0);
    Ignite ignite = startGrid(gridCount());
    // Initialize reentrant lock.
    IgniteLock l = ignite.reentrantLock("lock", true, true, true);
    // Initialize OPS_COUNTER.
    ignite.getOrCreateCache(OPS_COUNTER).put(OPS_COUNTER, (long) 0);
    final Map<Integer, Long> counts = new ConcurrentHashMap<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final int localNodeId = (int) threadCounter.getAndIncrement();
            final Ignite grid = grid(localNodeId);
            IgniteClosure<Ignite, Long> closure = new IgniteClosure<Ignite, Long>() {

                @Override
                public Long apply(Ignite ignite) {
                    IgniteLock l = ignite.reentrantLock("lock", true, true, true);
                    long localCount = 0;
                    IgniteCountDownLatch latch = ignite.countDownLatch("latch", threadCount, false, true);
                    latch.countDown();
                    latch.await();
                    while (true) {
                        l.lock();
                        try {
                            long opsCounter = (long) ignite.getOrCreateCache(OPS_COUNTER).get(OPS_COUNTER);
                            if (opsCounter == opsCount)
                                break;
                            ignite.getOrCreateCache(OPS_COUNTER).put(OPS_COUNTER, ++opsCounter);
                            localCount++;
                            if (localCount > 1000) {
                                assertTrue(localCount < (1 + tolerance) * opsCounter / threadCount);
                                assertTrue(localCount > (1 - tolerance) * opsCounter / threadCount);
                            }
                            if (localCount % 100 == 0) {
                                info("Node [id=" + ignite.cluster().localNode().id() + "] acquired " + localCount + " times. " + "Total ops count: " + opsCounter + "/" + opsCount + "]");
                            }
                        } finally {
                            l.unlock();
                        }
                    }
                    return localCount;
                }
            };
            long localCount = closure.apply(grid);
            counts.put(localNodeId, localCount);
            return null;
        }
    }, threadCount);
    fut.get();
    long totalSum = 0;
    for (int i = 0; i < gridCount(); i++) {
        totalSum += counts.get(i);
        info("Node " + grid(i).localNode().id() + " acquired the lock " + counts.get(i) + " times. ");
    }
    assertEquals(totalSum, opsCount);
    l.close();
    ignite.close();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Ignite(org.apache.ignite.Ignite) IgniteLock(org.apache.ignite.IgniteLock) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 15 with IgniteClosure

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

the class GridCacheQueryTransformerSelfTest method testLocal.

/**
     * @throws Exception If failed.
     */
public void testLocal() throws Exception {
    IgniteCache<Integer, Value> cache = grid().createCache("test-cache");
    try {
        for (int i = 0; i < 50; i++) cache.put(i, new Value("str" + i, i * 100));
        Collection<List<Integer>> lists = grid().compute().broadcast(new IgniteCallable<List<Integer>>() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public List<Integer> call() throws Exception {
                IgniteClosure<Cache.Entry<Integer, Value>, Integer> transformer = new IgniteClosure<Cache.Entry<Integer, Value>, Integer>() {

                    @Override
                    public Integer apply(Cache.Entry<Integer, Value> e) {
                        return e.getValue().idx;
                    }
                };
                return ignite.cache("test-cache").query(new ScanQuery<Integer, Value>().setLocal(true), transformer).getAll();
            }
        });
        List<Integer> res = new ArrayList<>(F.flatCollections(lists));
        assertEquals(50, res.size());
        Collections.sort(res);
        for (int i = 0; i < 50; i++) assertEquals(i * 100, res.get(i).intValue());
    } finally {
        cache.destroy();
    }
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) ArrayList(java.util.ArrayList) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) ArrayList(java.util.ArrayList) List(java.util.List) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

IgniteClosure (org.apache.ignite.lang.IgniteClosure)17 Ignite (org.apache.ignite.Ignite)9 Cache (javax.cache.Cache)6 ArrayList (java.util.ArrayList)5 IgniteCache (org.apache.ignite.IgniteCache)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 IgniteException (org.apache.ignite.IgniteException)5 List (java.util.List)4 ScanQuery (org.apache.ignite.cache.query.ScanQuery)4 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 IOException (java.io.IOException)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)3 UUID (java.util.UUID)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)2 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)2 IgniteLock (org.apache.ignite.IgniteLock)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)2