Search in sources :

Example 76 with GridCacheEntryRemovedException

use of org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException in project ignite by apache.

the class IgniteCacheExpiryPolicyAbstractTest method checkTtl.

/**
     * @param key Key.
     * @param ttl TTL.
     * @param wait If {@code true} waits for ttl update.
     * @throws Exception If failed.
     */
private void checkTtl(Object key, final long ttl, boolean wait) throws Exception {
    boolean found = false;
    for (int i = 0; i < gridCount(); i++) {
        IgniteKernal grid = (IgniteKernal) grid(i);
        GridCacheAdapter<Object, Object> cache = grid.context().cache().internalCache(DEFAULT_CACHE_NAME);
        if (cache.context().isNear())
            cache = cache.context().near().dht();
        while (true) {
            try {
                GridCacheEntryEx e = cache.entryEx(key);
                if (e != null && e.deleted()) {
                    assertEquals(0, e.ttl());
                    assertFalse("Invalid entry [e=" + e + ", node=" + i + ']', cache.affinity().isPrimaryOrBackup(grid.localNode(), key));
                    continue;
                }
                if (e == null)
                    assertTrue("Not found " + key, !cache.affinity().isPrimaryOrBackup(grid.localNode(), key));
                else {
                    e.unswap();
                    found = true;
                    if (ttl > 0)
                        assertTrue(e.expireTime() > 0);
                    else
                        assertEquals(0, e.expireTime());
                }
                break;
            } catch (GridCacheEntryRemovedException ignore) {
            // Retry.
            } catch (GridDhtInvalidPartitionException ignore) {
                // No need to check.
                break;
            }
        }
    }
    assertTrue(found);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtInvalidPartitionException(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Example 77 with GridCacheEntryRemovedException

use of org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException in project ignite by apache.

the class GridCacheNearOnlyMultiNodeFullApiSelfTest method checkReaderTtl.

/**
     * @param inTx If {@code true} starts explicit transaction.
     * @throws Exception If failed.
     */
private void checkReaderTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(fullCache(), 1).get(0);
    c.put(key, 1);
    info("Finished first put.");
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertEquals((Integer) 1, c.get(key));
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    int fullIdx = nearIdx == 0 ? 1 : 0;
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        info("Checking grid: " + grid(i).localNode().id());
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(ttl, (long) entryTtl.get1());
            assertEquals(expireTimes[i], (long) entryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() throws IgniteCheckedException {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                if (!internalCache(c).context().deferredDelete()) {
                    GridCacheEntryEx e0 = internalCache(c).peekEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException ignored) {
                // If e0.valueBytes() thrown this exception then entry has been removed.
                return true;
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    // Ensure that old TTL and expire time are not longer "visible".
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    // Ensure that next update will not pick old expire time.
    tx = inTx ? transactions().txStart() : null;
    try {
        c.put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
}
Also used : Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Aggregations

GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)77 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)53 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)38 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)38 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)36 ClusterNode (org.apache.ignite.cluster.ClusterNode)19 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)18 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)17 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)15 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)14 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException)14 ArrayList (java.util.ArrayList)13 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)13 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)12 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)12 Map (java.util.Map)10 GridDistributedCacheEntry (org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry)10 UUID (java.util.UUID)9 GridCacheReturn (org.apache.ignite.internal.processors.cache.GridCacheReturn)9