use of javax.cache.Cache in project ignite by apache.
the class GridCacheAdapter method localEntries.
/** {@inheritDoc} */
@Override
public final Iterable<Cache.Entry<K, V>> localEntries(CachePeekMode[] peekModes) throws IgniteCheckedException {
assert peekModes != null;
ctx.checkSecurity(SecurityPermission.CACHE_READ);
PeekModes modes = parsePeekModes(peekModes, false);
Collection<Iterator<Cache.Entry<K, V>>> its = new ArrayList<>();
final boolean keepBinary = ctx.keepBinary();
if (ctx.isLocal()) {
modes.primary = true;
modes.backup = true;
}
if (modes.offheap) {
if (modes.heap && modes.near && ctx.isNear())
its.add(ctx.near().nearEntries().iterator());
if (modes.primary || modes.backup) {
AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
IgniteCacheOffheapManager offheapMgr = ctx.isNear() ? ctx.near().dht().context().offheap() : ctx.offheap();
its.add(offheapMgr.<K, V>entriesIterator(modes.primary, modes.backup, topVer, ctx.keepBinary()));
}
} else if (modes.heap) {
if (modes.near && ctx.isNear())
its.add(ctx.near().nearEntries().iterator());
if (modes.primary || modes.backup) {
GridDhtCacheAdapter<K, V> cache = ctx.isNear() ? ctx.near().dht() : ctx.dht();
its.add(cache.localEntriesIterator(modes.primary, modes.backup, keepBinary));
}
}
final Iterator<Cache.Entry<K, V>> it = F.flatIterators(its);
return new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return it;
}
public String toString() {
return "CacheLocalEntries []";
}
};
}
use of javax.cache.Cache in project ignite by apache.
the class GridCacheQueryManager method runQuery.
/**
* Processes cache query request.
*
* @param qryInfo Query info.
*/
@SuppressWarnings("unchecked")
protected void runQuery(GridCacheQueryInfo qryInfo) {
assert qryInfo != null;
assert qryInfo.query().type() != SCAN || !qryInfo.local() : qryInfo;
if (!enterBusy()) {
if (cctx.localNodeId().equals(qryInfo.senderId()))
throw new IllegalStateException("Failed to process query request (grid is stopping).");
// Ignore remote requests when when node is stopping.
return;
}
try {
boolean loc = qryInfo.local();
QueryResult<K, V> res = null;
if (log.isDebugEnabled())
log.debug("Running query: " + qryInfo);
boolean rmvIter = true;
try {
// Preparing query closures.
IgniteClosure<Cache.Entry<K, V>, Object> trans = (IgniteClosure<Cache.Entry<K, V>, Object>) qryInfo.transformer();
IgniteReducer<Cache.Entry<K, V>, Object> rdc = (IgniteReducer<Cache.Entry<K, V>, Object>) qryInfo.reducer();
injectResources(trans);
injectResources(rdc);
GridCacheQueryAdapter<?> qry = qryInfo.query();
int pageSize = qry.pageSize();
boolean incBackups = qry.includeBackups();
String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
IgniteSpiCloseableIterator<IgniteBiTuple<K, V>> iter;
GridCacheQueryType type;
res = loc ? executeQuery(qry, qryInfo.arguments(), loc, qry.subjectId(), taskName, recipient(qryInfo.senderId(), qryInfo.requestId())) : queryResult(qryInfo, taskName);
if (res == null)
return;
iter = res.iterator(recipient(qryInfo.senderId(), qryInfo.requestId()));
type = res.type();
final GridCacheAdapter<K, V> cache = cctx.cache();
if (log.isDebugEnabled())
log.debug("Received index iterator [iterHasNext=" + iter.hasNext() + ", cacheSize=" + cache.size() + ']');
int cnt = 0;
boolean stop = false;
boolean pageSent = false;
Collection<Object> data = new ArrayList<>(pageSize);
AffinityTopologyVersion topVer = cctx.affinity().affinityTopologyVersion();
final boolean statsEnabled = cctx.config().isStatisticsEnabled();
final boolean readEvt = cctx.gridEvents().isRecordable(EVT_CACHE_QUERY_OBJECT_READ);
while (!Thread.currentThread().isInterrupted() && iter.hasNext()) {
long start = statsEnabled ? System.nanoTime() : 0L;
IgniteBiTuple<K, V> row = iter.next();
// Query is cancelled.
if (row == null) {
onPageReady(loc, qryInfo, null, true, null);
break;
}
final K key = row.getKey();
// Other types are filtered in indexing manager.
if (!cctx.isReplicated() && qry.type() == SCAN && qry.partition() == null && cctx.config().getCacheMode() != LOCAL && !incBackups && !cctx.affinity().primaryByKey(cctx.localNode(), key, topVer)) {
if (log.isDebugEnabled())
log.debug("Ignoring backup element [row=" + row + ", cacheMode=" + cctx.config().getCacheMode() + ", incBackups=" + incBackups + ", primary=" + cctx.affinity().primaryByKey(cctx.localNode(), key, topVer) + ']');
continue;
}
V val = row.getValue();
if (log.isDebugEnabled()) {
ClusterNode primaryNode = cctx.affinity().primaryByKey(key, cctx.affinity().affinityTopologyVersion());
log.debug(S.toString("Record", "key", key, true, "val", val, true, "incBackups", incBackups, false, "priNode", primaryNode != null ? U.id8(primaryNode.id()) : null, false, "node", U.id8(cctx.localNode().id()), false));
}
if (val == null) {
if (log.isDebugEnabled())
log.debug(S.toString("Unsuitable record value", "val", val, true));
continue;
}
if (statsEnabled) {
CacheMetricsImpl metrics = cctx.cache().metrics0();
metrics.onRead(true);
metrics.addGetTimeNanos(System.nanoTime() - start);
}
K key0 = null;
V val0 = null;
if (readEvt) {
key0 = (K) cctx.unwrapBinaryIfNeeded(key, qry.keepBinary());
val0 = (V) cctx.unwrapBinaryIfNeeded(val, qry.keepBinary());
switch(type) {
case SQL:
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "SQL query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SQL.name(), cctx.name(), qry.queryClassName(), qry.clause(), null, null, qryInfo.arguments(), qry.subjectId(), taskName, key0, val0, null, null));
break;
case TEXT:
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "Full text query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.FULL_TEXT.name(), cctx.name(), qry.queryClassName(), qry.clause(), null, null, null, qry.subjectId(), taskName, key0, val0, null, null));
break;
case SCAN:
cctx.gridEvents().record(new CacheQueryReadEvent<>(cctx.localNode(), "Scan query entry read.", EVT_CACHE_QUERY_OBJECT_READ, CacheQueryType.SCAN.name(), cctx.name(), null, null, qry.scanFilter(), null, null, qry.subjectId(), taskName, key0, val0, null, null));
break;
}
}
if (rdc != null || trans != null) {
if (key0 == null)
key0 = (K) cctx.unwrapBinaryIfNeeded(key, qry.keepBinary());
if (val0 == null)
val0 = (V) cctx.unwrapBinaryIfNeeded(val, qry.keepBinary());
Cache.Entry<K, V> entry = new CacheEntryImpl(key0, val0);
// Reduce.
if (rdc != null) {
if (!rdc.collect(entry) || !iter.hasNext()) {
onPageReady(loc, qryInfo, Collections.singletonList(rdc.reduce()), true, null);
pageSent = true;
break;
} else
continue;
}
data.add(trans != null ? trans.apply(entry) : !loc ? new GridCacheQueryResponseEntry<>(key, val) : F.t(key, val));
} else
data.add(!loc ? new GridCacheQueryResponseEntry<>(key, val) : F.t(key, val));
if (!loc) {
if (++cnt == pageSize || !iter.hasNext()) {
boolean finished = !iter.hasNext();
onPageReady(loc, qryInfo, data, finished, null);
pageSent = true;
if (!finished)
rmvIter = false;
if (!qryInfo.allPages())
return;
data = new ArrayList<>(pageSize);
if (stop)
// while
break;
}
}
}
if (!pageSent) {
if (rdc == null)
onPageReady(loc, qryInfo, data, true, null);
else
onPageReady(loc, qryInfo, Collections.singletonList(rdc.reduce()), true, null);
}
} catch (Throwable e) {
if (!X.hasCause(e, GridDhtUnreservedPartitionException.class))
U.error(log, "Failed to run query [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
onPageReady(loc, qryInfo, null, true, e);
if (e instanceof Error)
throw (Error) e;
} finally {
if (loc) {
// Local iterators are always removed.
if (res != null) {
try {
res.closeIfNotShared(recipient(qryInfo.senderId(), qryInfo.requestId()));
} catch (IgniteCheckedException e) {
if (!X.hasCause(e, GridDhtUnreservedPartitionException.class))
U.error(log, "Failed to close local iterator [qry=" + qryInfo + ", node=" + cctx.nodeId() + "]", e);
}
}
} else if (rmvIter)
removeQueryResult(qryInfo.senderId(), qryInfo.requestId());
}
} finally {
leaveBusy();
}
}
use of javax.cache.Cache in project ignite by apache.
the class PlatformDotNetEntityFrameworkCacheExtension method removeOldEntries.
/**
* Removes old cache entries locally.
*
* @param ignite Ignite.
* @param dataCacheName Cache name.
* @param currentVersions Current versions.
*/
private static void removeOldEntries(final Ignite ignite, final String dataCacheName, final Map<String, EntryProcessorResult<Long>> currentVersions) {
IgniteCache<PlatformDotNetEntityFrameworkCacheKey, PlatformDotNetEntityFrameworkCacheEntry> cache = ignite.cache(dataCacheName);
Set<PlatformDotNetEntityFrameworkCacheKey> keysToRemove = new TreeSet<>();
ClusterNode localNode = ignite.cluster().localNode();
for (Cache.Entry<PlatformDotNetEntityFrameworkCacheKey, PlatformDotNetEntityFrameworkCacheEntry> cacheEntry : cache.localEntries(CachePeekMode.ALL)) {
// and we don't want to process backup entries.
if (!ignite.affinity(dataCacheName).isPrimary(localNode, cacheEntry.getKey()))
continue;
long[] versions = cacheEntry.getKey().versions();
String[] entitySets = cacheEntry.getValue().entitySets();
for (int i = 0; i < entitySets.length; i++) {
EntryProcessorResult<Long> curVer = currentVersions.get(entitySets[i]);
if (curVer != null && versions[i] < curVer.get())
keysToRemove.add(cacheEntry.getKey());
}
}
cache.removeAll(keysToRemove);
}
use of javax.cache.Cache in project ignite by apache.
the class H2IndexingAbstractGeoSelfTest method checkGeoMultithreaded.
/**
* Check geo indexing multithreaded with dynamic index creation.
*
* @param dynamic Whether index should be created dynamically.
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void checkGeoMultithreaded(boolean dynamic) throws Exception {
final IgniteCache<Integer, EnemyCamp> cache1 = createCache("camp", true, Integer.class, EnemyCamp.class, dynamic);
final IgniteCache<Integer, EnemyCamp> cache2 = grid(1).cache("camp");
final IgniteCache<Integer, EnemyCamp> cache3 = grid(2).cache("camp");
try {
final String[] points = new String[CNT];
WKTReader r = new WKTReader();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int idx = 0; idx < CNT; idx++) {
int x = rnd.nextInt(1, 100);
int y = rnd.nextInt(1, 100);
cache1.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
points[idx] = Integer.toString(idx);
}
Thread.sleep(200);
final AtomicBoolean stop = new AtomicBoolean();
final AtomicReference<Exception> err = new AtomicReference<>();
IgniteInternalFuture<?> putFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
WKTReader r = new WKTReader();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!stop.get()) {
int cacheIdx = rnd.nextInt(0, 3);
IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
int idx = rnd.nextInt(CNT);
int x = rnd.nextInt(1, 100);
int y = rnd.nextInt(1, 100);
cache.getAndPut(idx, new EnemyCamp(r.read("POINT(" + x + " " + y + ")"), Integer.toString(idx)));
U.sleep(50);
}
return null;
}
}, Runtime.getRuntime().availableProcessors(), "put-thread");
IgniteInternalFuture<?> qryFut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
WKTReader r = new WKTReader();
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (!stop.get()) {
try {
int cacheIdx = rnd.nextInt(0, 3);
IgniteCache<Integer, EnemyCamp> cache = cacheIdx == 0 ? cache1 : cacheIdx == 1 ? cache2 : cache3;
SqlQuery<Integer, EnemyCamp> qry = new SqlQuery<>(EnemyCamp.class, "coords && ?");
Collection<Cache.Entry<Integer, EnemyCamp>> res = cache.query(qry.setArgs(r.read("POLYGON((0 0, 0 100, 100 100, 100 0, 0 0))"))).getAll();
checkPoints(res, points);
U.sleep(5);
} catch (Exception e) {
err.set(e);
stop.set(true);
break;
}
}
return null;
}
}, 4, "qry-thread");
U.sleep(6000L);
stop.set(true);
putFut.get();
qryFut.get();
Exception err0 = err.get();
if (err0 != null)
throw err0;
} finally {
destroy(cache1, grid(0), dynamic);
}
}
use of javax.cache.Cache in project ignite by apache.
the class GridCacheAbstractSelfTest method afterTest.
/** {@inheritDoc} */
@Override
protected void afterTest() throws Exception {
Transaction tx = jcache().unwrap(Ignite.class).transactions().tx();
if (tx != null) {
tx.close();
fail("Cache transaction remained after test completion: " + tx);
}
for (int i = 0; i < gridCount(); i++) {
info("Checking grid: " + i);
while (true) {
try {
final int fi = i;
assertTrue("Cache is not empty: " + " localSize = " + jcache(fi).localSize(CachePeekMode.ALL) + ", local entries " + entrySet(jcache(fi).localEntries()), GridTestUtils.waitForCondition(// Preloading may happen as nodes leave, so we need to wait.
new GridAbsPredicateX() {
@Override
public boolean applyx() throws IgniteCheckedException {
jcache(fi).removeAll();
if (jcache(fi).size(CachePeekMode.ALL) > 0) {
for (Cache.Entry<String, ?> k : jcache(fi).localEntries()) jcache(fi).remove(k.getKey());
}
return jcache(fi).localSize(CachePeekMode.ALL) == 0;
}
}, getTestTimeout()));
int primaryKeySize = jcache(i).localSize(CachePeekMode.PRIMARY);
int keySize = jcache(i).localSize();
int size = jcache(i).localSize();
int globalSize = jcache(i).size();
int globalPrimarySize = jcache(i).size(CachePeekMode.PRIMARY);
info("Size after [idx=" + i + ", size=" + size + ", keySize=" + keySize + ", primarySize=" + primaryKeySize + ", globalSize=" + globalSize + ", globalPrimarySize=" + globalPrimarySize + ", entrySet=" + jcache(i).localEntries() + ']');
assertEquals("Cache is not empty [idx=" + i + ", entrySet=" + jcache(i).localEntries() + ']', 0, jcache(i).localSize(CachePeekMode.ALL));
break;
} catch (Exception e) {
if (X.hasCause(e, ClusterTopologyCheckedException.class)) {
info("Got topology exception while tear down (will retry in 1000ms).");
U.sleep(1000);
} else
throw e;
}
}
}
assert jcache().unwrap(Ignite.class).transactions().tx() == null;
assertEquals("Cache is not empty", 0, jcache().localSize(CachePeekMode.ALL));
if (storeStgy != null)
storeStgy.resetStore();
}
Aggregations