Search in sources :

Example 91 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class GridCacheEntryVersionSelfTest method checkVersion.

/**
 * @throws Exception If failed.
 */
private void checkVersion() throws Exception {
    startGridsMultiThreaded(3);
    try {
        Map<Integer, Integer> map = F.asMap(1, 1, 2, 2, 3, 3);
        for (Integer key : map.keySet()) {
            info("Affinity nodes [key=" + key + ", nodes=" + F.viewReadOnly(grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key), F.node2id()) + ']');
        }
        grid(0).cache(DEFAULT_CACHE_NAME).putAll(map);
        for (int g = 0; g < 3; g++) {
            IgniteKernal grid = (IgniteKernal) grid(g);
            for (Integer key : map.keySet()) {
                GridCacheAdapter<Object, Object> cache = grid.internalCache(DEFAULT_CACHE_NAME);
                GridCacheEntryEx entry = cache.peekEx(key);
                if (entry != null) {
                    GridCacheVersion ver = entry.version();
                    long order = grid.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key).order();
                    // Check topology version.
                    assertEquals(3, ver.topologyVersion() - (grid.context().discovery().gridStartTime() - TOP_VER_BASE_TIME) / 1000);
                    // Check node order.
                    assertEquals("Failed for key: " + key, order, ver.nodeOrder());
                }
            }
        }
        startGrid(3);
        grid(0).cache(DEFAULT_CACHE_NAME).putAll(map);
        for (int g = 0; g < 4; g++) {
            IgniteKernal grid = (IgniteKernal) grid(g);
            for (Integer key : map.keySet()) {
                GridCacheAdapter<Object, Object> cache = grid.internalCache(DEFAULT_CACHE_NAME);
                GridCacheEntryEx entry = cache.peekEx(key);
                if (entry != null) {
                    GridCacheVersion ver = entry.version();
                    long order = grid.affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key).order();
                    // Check topology version.
                    assertEquals(4, ver.topologyVersion() - (grid.context().discovery().gridStartTime() - TOP_VER_BASE_TIME) / 1000);
                    // Check node order.
                    assertEquals("Failed for key: " + key, order, ver.nodeOrder());
                }
            }
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion)

Example 92 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class GridCacheMixedPartitionExchangeSelfTest method testNodeJoinLeave.

/**
 * @throws Exception If failed.
 */
public void testNodeJoinLeave() throws Exception {
    try {
        cache = true;
        startGrids(4);
        awaitPartitionMapExchange();
        final AtomicBoolean finished = new AtomicBoolean();
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new IgniteCallable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new Random();
                int keys = 100;
                while (!finished.get()) {
                    int g = rnd.nextInt(4);
                    int key = rnd.nextInt(keys);
                    IgniteCache<Integer, Integer> prj = grid(g).cache(DEFAULT_CACHE_NAME);
                    try {
                        try (Transaction tx = grid(g).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                            Integer val = prj.get(key);
                            val = val == null ? 1 : val + 1;
                            prj.put(key, val);
                            tx.commit();
                        }
                    } catch (Exception e) {
                        if (!X.hasCause(e, ClusterTopologyCheckedException.class))
                            throw e;
                    }
                }
                return null;
            }
        }, 4, "async-runner");
        cache = false;
        for (int r = 0; r < 3; r++) {
            for (int i = 4; i < 8; i++) startGrid(i);
            for (int i = 4; i < 8; i++) stopGrid(i);
        }
        // Check we can start more cache nodes after non-cache ones.
        cache = true;
        startGrid(4);
        U.sleep(500);
        finished.set(true);
        fut.get();
        AffinityTopologyVersion topVer = new AffinityTopologyVersion(grid(0).cluster().topologyVersion());
        assertEquals(29, topVer.topologyVersion());
        // Check all grids have all exchange futures completed.
        for (int i = 0; i < 4; i++) {
            IgniteKernal grid = (IgniteKernal) grid(i);
            GridCacheContext<Object, Object> cctx = grid.internalCache(DEFAULT_CACHE_NAME).context();
            IgniteInternalFuture<AffinityTopologyVersion> verFut = cctx.affinity().affinityReadyFuture(topVer);
            assertEquals(topVer, verFut.get());
            assertEquals(topVer, cctx.topologyVersionFuture().get());
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteCache(org.apache.ignite.IgniteCache) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 93 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class CacheOffheapMapEntrySelfTest method checkCacheMapEntry.

/**
 * @param atomicityMode Cache atomicity mode.
 * @param cacheMode Cache mode.
 * @param entryCls Class of cache map entry.
 * @throws Exception If failed.
 */
private void checkCacheMapEntry(CacheAtomicityMode atomicityMode, CacheMode cacheMode, Class<?> entryCls) throws Exception {
    log.info("Test cache [atomicityMode=" + atomicityMode + ", cacheMode=" + cacheMode + ']');
    CacheConfiguration cfg = cacheConfiguration(grid(0).name(), atomicityMode, cacheMode, "Cache");
    IgniteCache jcache = grid(0).getOrCreateCache(cfg);
    try {
        GridCacheAdapter<Integer, String> cache = ((IgniteKernal) grid(0)).internalCache(jcache.getName());
        Integer key = primaryKey(grid(0).cache(DEFAULT_CACHE_NAME));
        cache.put(key, "val");
        GridCacheEntryEx entry = cache.entryEx(key);
        entry.unswap(true);
        assertNotNull(entry);
        assertEquals(entryCls, entry.getClass());
    } finally {
        jcache.destroy();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 94 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class GridCacheAbstractMetricsSelfTest method checkTtl.

/**
 * @param inTx {@code true} for tx.
 * @throws Exception If failed.
 */
private void checkTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<Integer, Integer> c = grid(0).cache(DEFAULT_CACHE_NAME);
    final Integer key = primaryKeys(jcache(0), 1, 0).get(0);
    c.put(key, 1);
    GridCacheAdapter<Object, Object> c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    if (c0.isNear())
        c0 = c0.context().near().dht();
    GridCacheEntryEx entry = c0.entryEx(key);
    assert entry != null;
    assertEquals(0, entry.ttl());
    assertEquals(0, entry.expireTime());
    long startTime = U.currentTimeMillis();
    if (inTx) {
        // Rollback transaction for the first time.
        Transaction tx = grid(0).transactions().txStart();
        try {
            grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
        } finally {
            tx.rollback();
        }
        entry = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME).entryEx(key);
        assertEquals(0, entry.ttl());
        assertEquals(0, entry.expireTime());
    }
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
    } finally {
        if (tx != null)
            tx.commit();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 2);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // And one more direct update to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 3);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertTrue(curEntry.expireTime() >= startTime);
            expireTimes[i] = curEntry.expireTime();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    log.info("Put 4");
    tx = inTx ? grid(0).transactions().txStart() : null;
    try {
        c.put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    log.info("Put 4 done");
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            c0 = ((IgniteKernal) grid(i)).internalCache(DEFAULT_CACHE_NAME);
            if (c0.isNear())
                c0 = c0.context().near().dht();
            GridCacheEntryEx curEntry = c0.entryEx(key);
            curEntry.unswap();
            assertEquals(expireTimes[i], curEntry.expireTime());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() {
            try {
                if (c.get(key) != null)
                    return false;
                // Get "cache" field from GridCacheProxyImpl.
                GridCacheAdapter c0 = cacheFromCtx(c);
                if (!c0.context().deferredDelete()) {
                    GridCacheEntryEx e0 = c0.entryEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException e) {
                throw new RuntimeException(e);
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    c0 = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    if (c0.isNear())
        c0 = c0.context().near().dht();
    // Ensure that old TTL and expire time are not longer "visible".
    entry = c0.entryEx(key);
    assertEquals(0, entry.expireTime());
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Duration(javax.cache.expiry.Duration) Transaction(org.apache.ignite.transactions.Transaction) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy)

Example 95 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class CacheLateAffinityAssignmentTest method affFutures.

/**
 * @param expNodes Expected nodes number.
 * @param topVer Topology version.
 * @return Affinity futures.
 */
private List<IgniteInternalFuture<?>> affFutures(int expNodes, AffinityTopologyVersion topVer) {
    List<Ignite> nodes = G.allGrids();
    assertEquals(expNodes, nodes.size());
    List<IgniteInternalFuture<?>> futs = new ArrayList<>(nodes.size());
    for (Ignite node : nodes) {
        IgniteInternalFuture<?> fut = ((IgniteKernal) node).context().cache().context().exchange().affinityReadyFuture(topVer);
        futs.add(fut);
    }
    return futs;
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture)

Aggregations

IgniteKernal (org.apache.ignite.internal.IgniteKernal)181 Ignite (org.apache.ignite.Ignite)78 ClusterNode (org.apache.ignite.cluster.ClusterNode)35 ArrayList (java.util.ArrayList)34 Map (java.util.Map)30 Transaction (org.apache.ignite.transactions.Transaction)30 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)27 IgniteCache (org.apache.ignite.IgniteCache)26 UUID (java.util.UUID)22 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)22 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)22 IgniteException (org.apache.ignite.IgniteException)21 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)20 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)19 IgniteEx (org.apache.ignite.internal.IgniteEx)15 HashMap (java.util.HashMap)14 List (java.util.List)14 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13