Search in sources :

Example 6 with GridNearCacheAdapter

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));
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter) UUID(java.util.UUID)

Example 7 with GridNearCacheAdapter

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);
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter)

Example 8 with GridNearCacheAdapter

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;
}
Also used : GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter)

Example 9 with GridNearCacheAdapter

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);
        }
    }
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter)

Example 10 with GridNearCacheAdapter

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);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) GridNearCacheEntry(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry) GridNearCacheAdapter(org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter)

Aggregations

GridNearCacheAdapter (org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter)14 IgniteKernal (org.apache.ignite.internal.IgniteKernal)8 Ignite (org.apache.ignite.Ignite)6 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)4 Transaction (org.apache.ignite.transactions.Transaction)4 UUID (java.util.UUID)3 IgniteException (org.apache.ignite.IgniteException)3 GridNearCacheEntry (org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)2 GridDhtCacheEntry (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry)2 IgniteTxManager (org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager)2 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)2