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();
}
}
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;
}
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());
}
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();
}
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();
}
}
Aggregations