Search in sources :

Example 61 with GridCacheEntryEx

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

the class GridCacheNearOneNodeSelfTest method testOptimisticTxWriteThrough.

/**
 * Test Optimistic repeatable read write-through.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings({ "ConstantConditions" })
public void testOptimisticTxWriteThrough() throws Exception {
    IgniteCache<Object, Object> near = jcache();
    GridCacheAdapter<Integer, String> dht = dht();
    try (Transaction tx = grid().transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
        near.put(2, "2");
        near.put(3, "3");
        assert "2".equals(near.get(2));
        assert "3".equals(near.get(3));
        GridCacheEntryEx entry = dht.peekEx(2);
        assert entry == null || entry.rawGet() == null : "Invalid entry: " + entry;
        tx.commit();
    }
    assert "2".equals(near.get(2));
    assert "3".equals(near.get(3));
    assert "2".equals(dht.get(2));
    assert "3".equals(dht.get(3));
    assertEquals(2, near.size());
    assertEquals(2, near.size());
    assertEquals(2, dht.size());
    assertEquals(2, dht.size());
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) Transaction(org.apache.ignite.transactions.Transaction)

Example 62 with GridCacheEntryEx

use of org.apache.ignite.internal.processors.cache.GridCacheEntryEx 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)

Example 63 with GridCacheEntryEx

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

the class GridCachePartitionedMultiNodeCounterSelfTest method checkNearAndPrimaryMultiNode.

/**
 * @param gridCnt Grid count.
 * @throws Exception If failed.
 */
private void checkNearAndPrimaryMultiNode(int gridCnt) throws Exception {
    Affinity<String> aff = affinity(grid(0).<String, Integer>cache(DEFAULT_CACHE_NAME));
    Collection<ClusterNode> affNodes = aff.mapKeyToPrimaryAndBackups(CNTR_KEY);
    assertEquals(1 + backups, affNodes.size());
    Ignite pri = G.ignite(F.first(affNodes).id());
    // Initialize.
    pri.cache(DEFAULT_CACHE_NAME).put(CNTR_KEY, 0);
    assertNull(near(pri).peekEx(CNTR_KEY));
    GridCacheEntryEx dhtEntry = dht(pri).entryEx(CNTR_KEY);
    assertNotNull(dhtEntry);
    dhtEntry.unswap();
    assertEquals(Integer.valueOf(0), dhtEntry.rawGet().value(dhtEntry.context().cacheObjectContext(), false));
    startLatchMultiNode = new CountDownLatch(gridCnt);
    globalCntrMultiNode = new AtomicInteger(0);
    lockedMultiNode.set(false);
    // Execute task on all grid nodes.
    pri.compute().broadcast(new IncrementItemJob(pri.name()));
    info("*** ");
    for (int i = 0; i < gridCnt; i++) {
        Ignite g = grid(i);
        IgniteCache<String, Integer> cache = grid(i).cache(DEFAULT_CACHE_NAME);
        int cntr = cache.localPeek(CNTR_KEY);
        info("*** Cache counter [igniteInstanceName=" + g.name() + ", cntr=" + cntr + ']');
        assertEquals(RETRIES * gridCnt, cntr);
    }
    info("*** ");
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 64 with GridCacheEntryEx

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

the class GridCacheColocatedDebugTest method checkPutsMultithreaded.

/**
 * @param loc Local puts.
 * @param remote Remote puts.
 * @param maxIterCnt Number of iterations.
 * @throws Exception If failed.
 */
public void checkPutsMultithreaded(boolean loc, boolean remote, final long maxIterCnt) throws Exception {
    storeEnabled = false;
    assert loc || remote;
    startGridsMultiThreaded(3);
    try {
        final Ignite g0 = grid(0);
        Ignite g1 = grid(1);
        final Collection<Integer> keys = new ConcurrentLinkedQueue<>();
        if (loc) {
            Integer key = -1;
            for (int i = 0; i < 20; i++) {
                key = forPrimary(g0, key);
                keys.add(key);
            }
        }
        if (remote) {
            Integer key = -1;
            for (int i = 0; i < 20; i++) {
                key = forPrimary(g1, key);
                keys.add(key);
            }
        }
        final AtomicLong iterCnt = new AtomicLong();
        final int keysCnt = 10;
        IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {

            @Override
            public void run() {
                // Make thread-local copy to shuffle keys.
                List<Integer> threadKeys = new ArrayList<>(keys);
                long threadId = Thread.currentThread().getId();
                long itNum;
                while ((itNum = iterCnt.getAndIncrement()) < maxIterCnt) {
                    Collections.shuffle(threadKeys);
                    List<Integer> iterKeys = threadKeys.subList(0, keysCnt);
                    Collections.sort(iterKeys);
                    Map<Integer, String> vals = U.newLinkedHashMap(keysCnt);
                    for (Integer key : iterKeys) vals.put(key, String.valueOf(key) + threadId);
                    jcache(0).putAll(vals);
                    if (itNum > 0 && itNum % 5000 == 0)
                        info(">>> " + itNum + " iterations completed.");
                }
            }
        }, THREAD_CNT);
        fut.get();
        Thread.sleep(1000);
        // Check that all transactions are committed.
        for (int i = 0; i < 3; i++) {
            GridCacheAdapter<Object, Object> cache = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            for (Integer key : keys) {
                GridCacheEntryEx entry = cache.peekEx(key);
                if (entry != null) {
                    Collection<GridCacheMvccCandidate> locCands = entry.localCandidates();
                    Collection<GridCacheMvccCandidate> rmtCands = entry.remoteMvccSnapshot();
                    assert locCands == null || locCands.isEmpty() : "Local candidates is not empty [idx=" + i + ", entry=" + entry + ']';
                    assert rmtCands == null || rmtCands.isEmpty() : "Remote candidates is not empty [idx=" + i + ", entry=" + entry + ']';
                }
            }
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicLong(java.util.concurrent.atomic.AtomicLong) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Map(java.util.Map) GridCacheMvccCandidate(org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)

Example 65 with GridCacheEntryEx

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

the class GridCacheReplicatedPreloadSelfTest method testIntegrity.

/**
 * @throws Exception If test failed.
 */
public void testIntegrity() throws Exception {
    preloadMode = SYNC;
    try {
        Ignite g1 = startGrid(1);
        GridCacheAdapter<Integer, String> cache1 = ((IgniteKernal) g1).internalCache(DEFAULT_CACHE_NAME);
        // Cache rebalancing events should not be fired for this cache.
        CacheConfiguration ccfg = cacheConfiguration(((IgniteKernal) g1).getInstanceName()).setName(DEFAULT_CACHE_NAME + "_evts_disabled").setEventsDisabled(true);
        g1.getOrCreateCache(ccfg);
        cache1.getAndPut(1, "val1");
        cache1.getAndPut(2, "val2");
        GridCacheEntryEx e1 = cache1.entryEx(1);
        assertNotNull(e1);
        e1.unswap();
        Ignite g2 = startGrid(2);
        Collection<Event> evts = null;
        for (int i = 0; i < 3; i++) {
            evts = g2.events().localQuery(F.<Event>alwaysTrue(), EVT_CACHE_REBALANCE_STARTED, EVT_CACHE_REBALANCE_STOPPED);
            if (evts.size() != 2) {
                info("Wrong events collection size (will retry in 1000 ms): " + evts.size());
                Thread.sleep(1000);
            } else
                break;
        }
        assertNotNull(evts);
        assertEquals("Wrong events received: " + evts, 2, evts.size());
        Iterator<Event> iter = evts.iterator();
        assertEquals(EVT_CACHE_REBALANCE_STARTED, iter.next().type());
        assertEquals(EVT_CACHE_REBALANCE_STOPPED, iter.next().type());
        IgniteCache<Integer, String> cache2 = g2.cache(DEFAULT_CACHE_NAME);
        assertEquals("val1", cache2.localPeek(1));
        assertEquals("val2", cache2.localPeek(2));
        GridCacheAdapter<Integer, String> cacheAdapter2 = ((IgniteKernal) g2).internalCache(DEFAULT_CACHE_NAME);
        GridCacheEntryEx e2 = cacheAdapter2.entryEx(1);
        assertNotNull(e2);
        assertNotSame(e2, e1);
        e2.unswap();
        assertNotNull(e2.version());
        assertEquals(e1.version(), e2.version());
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)70 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)42 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)33 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)32 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)26 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)23 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 IgniteTxEntry (org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry)14 GridDhtInvalidPartitionException (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException)12 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)11 IgniteKernal (org.apache.ignite.internal.IgniteKernal)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 UUID (java.util.UUID)8 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)7 IgniteTxKey (org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey)7 Ignite (org.apache.ignite.Ignite)6