use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter in project ignite by apache.
the class GridCacheAtomicNearCacheSelfTest method checkEntry.
/**
* @param ignite Node.
* @param key Key.
* @param val Expected value.
* @param expectNear If {@code true} then near cache entry is expected.
* @param expReaders Expected readers.
* @throws Exception If failed.
*/
@SuppressWarnings("ConstantConditions")
private void checkEntry(Ignite ignite, Integer key, @Nullable Integer val, boolean expectNear, final UUID... expReaders) throws Exception {
GridCacheAdapter<Integer, Integer> near = ((IgniteKernal) ignite).internalCache(DEFAULT_CACHE_NAME);
assertTrue(near.isNear());
GridCacheEntryEx nearEntry = near.peekEx(key);
boolean expectDht = near.affinity().isPrimaryOrBackup(ignite.cluster().localNode(), key);
if (expectNear) {
assertNotNull("No near entry for: " + key + ", grid: " + ignite.name(), nearEntry);
assertEquals("Unexpected value for grid: " + ignite.name(), val, CU.value(nearEntry.info().value(), near.context(), false));
assertEquals("Unexpected value for grid: " + ignite.name(), val, ignite.cache(near.name()).localPeek(key, CachePeekMode.ONHEAP));
} else
assertNull("Unexpected near entry: " + nearEntry + ", grid: " + ignite.name(), nearEntry);
GridDhtCacheAdapter<Integer, Integer> dht = ((GridNearCacheAdapter<Integer, Integer>) near).dht();
if (expectDht) {
final GridDhtCacheEntry dhtEntry = (GridDhtCacheEntry) dht.entryEx(key);
assertNotNull("No dht entry for: " + key + ", grid: " + ignite.name(), dhtEntry);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
try {
return dhtEntry.readers().size() == expReaders.length;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}, 5000);
Collection<UUID> readers = dhtEntry.readers();
assertEquals(expReaders.length, readers.size());
for (UUID reader : expReaders) assertTrue(readers.contains(reader));
if (dhtEntry.peekVisibleValue() == null)
dhtEntry.unswap();
assertEquals("Unexpected value for grid: " + ignite.name(), val, CU.value(dhtEntry.info().value(), dht.context(), false));
assertEquals("Unexpected value for grid: " + ignite.name(), val, ignite.cache(near.name()).localPeek(key, CachePeekMode.ONHEAP));
} else {
GridDhtCacheEntry dhtEntry = (GridDhtCacheEntry) dht.peekEx(key);
assertNull("Unexpected dht entry: " + dhtEntry + ", grid: " + ignite.name(), dhtEntry);
}
if (!expectNear && !expectDht) {
assertNull("Unexpected peek value for grid: " + ignite.name(), ignite.cache(near.name()).localPeek(key, CachePeekMode.ONHEAP));
}
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter in project ignite by apache.
the class IgniteTxHandler method invalidateNearEntry.
/**
* @param cacheCtx Context.
* @param key Key
* @param ver Version.
* @throws IgniteCheckedException If invalidate failed.
*/
private void invalidateNearEntry(GridCacheContext cacheCtx, KeyCacheObject key, GridCacheVersion ver) throws IgniteCheckedException {
GridNearCacheAdapter near = cacheCtx.isNear() ? cacheCtx.near() : cacheCtx.dht().near();
GridCacheEntryEx nearEntry = near.peekEx(key);
if (nearEntry != null)
nearEntry.invalidate(ver);
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter in project ignite by apache.
the class GridCacheTtlManager method expire.
/**
* Processes specified amount of expired entries.
*
* @param amount Limit of processed entries by single call, {@code -1} for no limit.
* @return {@code True} if unprocessed expired entries remains.
*/
public boolean expire(int amount) {
long now = U.currentTimeMillis();
try {
if (pendingEntries != null) {
GridNearCacheAdapter nearCache = cctx.near();
GridCacheVersion obsoleteVer = null;
int limit = (-1 != amount) ? amount : pendingEntries.sizex();
for (int cnt = limit; cnt > 0; cnt--) {
EntryWrapper e = pendingEntries.firstx();
if (e == null || e.expireTime > now)
// All expired entries are processed.
break;
if (pendingEntries.remove(e)) {
if (obsoleteVer == null)
obsoleteVer = cctx.versions().next();
GridNearCacheEntry nearEntry = nearCache.peekExx(e.key);
if (nearEntry != null)
expireC.apply(nearEntry, obsoleteVer);
}
}
}
boolean more = cctx.offheap().expire(dhtCtx, expireC, amount);
if (more)
return true;
if (amount != -1 && pendingEntries != null) {
EntryWrapper e = pendingEntries.firstx();
return e != null && e.expireTime <= now;
}
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process entry expiration: " + e, e);
}
return false;
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter in project ignite by apache.
the class IgniteTxManager method removeObsolete.
/**
* @param tx Transaction.
*/
private void removeObsolete(IgniteInternalTx tx) {
Collection<IgniteTxEntry> entries = tx.local() ? tx.allEntries() : tx.writeEntries();
for (IgniteTxEntry entry : entries) {
GridCacheEntryEx cached = entry.cached();
GridCacheContext cacheCtx = entry.context();
if (cached == null)
cached = cacheCtx.cache().peekEx(entry.key());
if (cached.detached())
continue;
try {
if (cached.obsolete() || cached.markObsoleteIfEmpty(tx.xidVersion()))
cacheCtx.cache().removeEntry(cached);
if (!tx.near() && isNearEnabled(cacheCtx)) {
GridNearCacheAdapter near = cacheCtx.isNear() ? cacheCtx.near() : cacheCtx.dht().near();
GridNearCacheEntry e = near.peekExx(entry.key());
if (e != null && e.markObsoleteIfEmpty(null))
near.removeEntry(e);
}
} catch (IgniteCheckedException e) {
U.error(log, "Failed to remove obsolete entry from cache: " + cached, e);
}
}
}
use of org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter in project ignite by apache.
the class GridCacheVersionMultinodeTest method checkEntryVersion.
/**
* @param key Key.
* @throws Exception If failed.
*/
private void checkEntryVersion(String key) throws Exception {
GridCacheVersion ver = null;
boolean verified = false;
for (int i = 0; i < gridCount(); i++) {
IgniteKernal grid = (IgniteKernal) grid(i);
GridCacheAdapter<Object, Object> cache = grid.context().cache().internalCache(DEFAULT_CACHE_NAME);
GridCacheEntryEx e;
if (cache.affinity().isPrimaryOrBackup(grid.localNode(), key)) {
if (cache instanceof GridNearCacheAdapter)
cache = ((GridNearCacheAdapter<Object, Object>) cache).dht();
e = cache.entryEx(key);
e.unswap();
assertNotNull(e.rawGet());
} else
e = cache.peekEx(key);
if (e != null) {
if (ver != null) {
assertEquals("Non-equal versions for key: " + key, ver, e instanceof GridNearCacheEntry ? ((GridNearCacheEntry) e).dhtVersion() : e.version());
verified = true;
} else
ver = e instanceof GridNearCacheEntry ? ((GridNearCacheEntry) e).dhtVersion() : e.version();
}
}
assertTrue(verified);
}
Aggregations