Search in sources :

Example 16 with GridCacheAdapter

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

the class CachePartitionStateTest method checkNodePartitions.

/**
 * @param assign Assignments.
 * @param clusterNode Node.
 * @param cacheName Cache name.
 * @param expState Expected partitions state.
 */
private void checkNodePartitions(AffinityAssignment assign, ClusterNode clusterNode, String cacheName, GridDhtPartitionState expState) {
    Affinity<Object> aff = ignite(0).affinity(cacheName);
    Set<Integer> nodeParts = new HashSet<>();
    nodeParts.addAll(assign.primaryPartitions(clusterNode.id()));
    nodeParts.addAll(assign.backupPartitions(clusterNode.id()));
    log.info("Test state [node=" + clusterNode.id() + ", cache=" + cacheName + ", parts=" + nodeParts.size() + ", state=" + expState + ']');
    if (grid(0).context().discovery().cacheAffinityNode(clusterNode, cacheName))
        assertFalse(nodeParts.isEmpty());
    boolean check = false;
    for (Ignite node : G.allGrids()) {
        GridCacheAdapter cache = ((IgniteKernal) node).context().cache().internalCache(cacheName);
        if (cache != null) {
            check = true;
            GridDhtPartitionTopology top = cache.context().topology();
            GridDhtPartitionMap partsMap = top.partitions(clusterNode.id());
            for (int p = 0; p < aff.partitions(); p++) {
                if (nodeParts.contains(p)) {
                    assertNotNull(partsMap);
                    GridDhtPartitionState state = partsMap.get(p);
                    assertEquals("Unexpected state [checkNode=" + clusterNode.id() + ", node=" + node.name() + ", state=" + state + ']', expState, partsMap.get(p));
                } else {
                    if (partsMap != null) {
                        GridDhtPartitionState state = partsMap.get(p);
                        assertTrue("Unexpected state [checkNode=" + clusterNode.id() + ", node=" + node.name() + ", state=" + state + ']', state == null || state == EVICTED);
                    }
                }
            }
        } else {
            assertEquals(0, aff.primaryPartitions(((IgniteKernal) node).localNode()).length);
            assertEquals(0, aff.backupPartitions(((IgniteKernal) node).localNode()).length);
        }
    }
    assertTrue(check);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet)

Example 17 with GridCacheAdapter

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

the class CachePartitionStateTest method checkRebalance.

/**
 * @param cacheName Cache name.
 * @param expDone Expected rebalance finish flag.
 */
private void checkRebalance(String cacheName, boolean expDone) {
    for (Ignite node : G.allGrids()) {
        IgniteKernal node0 = (IgniteKernal) node;
        GridCacheAdapter cache = node0.context().cache().internalCache(cacheName);
        AffinityTopologyVersion topVer = node0.context().cache().context().exchange().readyAffinityVersion();
        if (cache != null)
            assertEquals(expDone, cache.context().topology().rebalanceFinished(topVer));
        else
            node0.context().discovery().cacheAffinityNode(node0.localNode(), cacheName);
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) Ignite(org.apache.ignite.Ignite)

Example 18 with GridCacheAdapter

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

the class EvictionPolicyFailureHandlerTest method testCacheMapDoesNotContainsWrongEntityAfterTransaction.

/**
 * We expect that localPeek produces an exception, but the entry evict returns false because the transaction locks
 * this entry. After transaction commit, the entry will be evicted.
 */
@Test
public void testCacheMapDoesNotContainsWrongEntityAfterTransaction() throws Exception {
    LogListener lsnr = LogListener.matches(s -> s.contains("The cache entry cannot be touched")).times(1).build();
    log.registerListener(lsnr);
    IgniteEx node = startGrid(0);
    IgniteEx client = startClientGrid(1);
    GridCacheAdapter<Object, Object> cache = ((IgniteKernal) node).internalCache(DEFAULT_CACHE_NAME);
    cache.put(1, 1);
    CountDownLatch locPeekFinished = new CountDownLatch(1);
    CountDownLatch txStarted = new CountDownLatch(1);
    CountDownLatch txFinished = new CountDownLatch(1);
    GridTestUtils.runAsync(() -> {
        IgniteCache<Object, Object> cache1 = client.cache(DEFAULT_CACHE_NAME);
        IgniteTransactions transactions = client.transactions();
        try (Transaction tx = transactions.txStart(PESSIMISTIC, REPEATABLE_READ)) {
            cache1.put(2.1, 2.4);
            txStarted.countDown();
            locPeekFinished.await();
            tx.commit();
        } catch (Exception ignore) {
        }
        txFinished.countDown();
    }, "tx-thread");
    txStarted.await();
    try {
        cache.localPeek(2.1, new CachePeekMode[] { CachePeekMode.ONHEAP });
    } catch (Exception ignore) {
    }
    locPeekFinished.countDown();
    assertTrue(lsnr.check(10_000));
    txFinished.await();
    assertFalse(cache.map().entrySet(cache.context().cacheId()).stream().anyMatch(e -> new Double(2.1).equals(e.key().value(null, false))));
    assertEquals(ACTIVE, node.cluster().state());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) Affinity(org.apache.ignite.cache.affinity.Affinity) Transaction(org.apache.ignite.transactions.Transaction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) Factory(javax.cache.configuration.Factory) IgniteKernal(org.apache.ignite.internal.IgniteKernal) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) CachePeekMode(org.apache.ignite.cache.CachePeekMode) FailureHandler(org.apache.ignite.failure.FailureHandler) Test(org.junit.Test) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) SortedEvictionPolicy(org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicy) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) SortedEvictionPolicyFactory(org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicyFactory) CountDownLatch(java.util.concurrent.CountDownLatch) EvictionPolicy(org.apache.ignite.cache.eviction.EvictionPolicy) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) IgniteTransactions(org.apache.ignite.IgniteTransactions) EvictableEntry(org.apache.ignite.cache.eviction.EvictableEntry) IgniteKernal(org.apache.ignite.internal.IgniteKernal) LogListener(org.apache.ignite.testframework.LogListener) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 19 with GridCacheAdapter

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

the class IgniteCacheClientNearCacheExpiryTest method testExpirationOnClient.

/**
 * @throws Exception If failed.
 */
@Test
public void testExpirationOnClient() throws Exception {
    Ignite ignite = grid(NODES - 1);
    // Check size of near entries via reflection because entries is filtered for size() API call.
    IgniteEx igniteEx = (IgniteEx) ignite;
    GridCacheAdapter internalCache = igniteEx.context().cache().internalCache(DEFAULT_CACHE_NAME);
    GridCacheLocalConcurrentMap map = GridTestUtils.getFieldValue(internalCache, GridCacheAdapter.class, "map");
    assertTrue(ignite.configuration().isClientMode());
    IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
    assertTrue(((IgniteCacheProxy) cache).context().isNear());
    for (int i = 0; i < KEYS_COUNT; i++) cache.put(i, i);
    CreatedExpiryPolicy plc = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, 500));
    IgniteCache<Object, Object> cacheWithExpiry = cache.withExpiryPolicy(plc);
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) {
        cacheWithExpiry.put(i, i);
        assertEquals(i, cacheWithExpiry.localPeek(i));
    }
    assertEquals(KEYS_COUNT * 2, map.publicSize(internalCache.context().cacheId()));
    U.sleep(1000);
    assertEquals(KEYS_COUNT, map.publicSize(internalCache.context().cacheId()));
    assertEquals(KEYS_COUNT, cache.size());
    for (int i = 0; i < KEYS_COUNT; i++) assertEquals(i, cacheWithExpiry.localPeek(i));
    for (int i = KEYS_COUNT; i < KEYS_COUNT * 2; i++) assertNull(cache.localPeek(i));
}
Also used : GridCacheLocalConcurrentMap(org.apache.ignite.internal.processors.cache.GridCacheLocalConcurrentMap) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite) Duration(javax.cache.expiry.Duration) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) IgniteCacheAbstractTest(org.apache.ignite.internal.processors.cache.IgniteCacheAbstractTest) Test(org.junit.Test)

Example 20 with GridCacheAdapter

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

the class IgniteStandByClusterTest method testStaticCacheStartAfterActivationWithCacheFilter.

/**
 * @throws Exception if fail.
 */
@Test
public void testStaticCacheStartAfterActivationWithCacheFilter() throws Exception {
    String cache1 = "cache1";
    String cache2 = "cache2";
    String cache3 = "cache3";
    IgniteConfiguration cfg1 = getConfiguration("node1");
    cfg1.setCacheConfiguration(new CacheConfiguration(cache1).setNodeFilter(new NodeFilterIgnoreByName("node2")));
    IgniteConfiguration cfg2 = getConfiguration("node2");
    cfg2.setCacheConfiguration(new CacheConfiguration(cache2).setNodeFilter(new NodeFilterIgnoreByName("node3")));
    IgniteConfiguration cfg3 = getConfiguration("node3");
    cfg3.setCacheConfiguration(new CacheConfiguration(cache3).setNodeFilter(new NodeFilterIgnoreByName("node1")));
    IgniteEx ig1 = startGrid(cfg1);
    IgniteEx ig2 = startGrid(cfg2);
    IgniteEx ig3 = startGrid(cfg3);
    assertTrue(!ig1.active());
    assertTrue(!ig2.active());
    assertTrue(!ig3.active());
    ig3.active(true);
    assertTrue(ig1.active());
    assertTrue(ig2.active());
    assertTrue(ig3.active());
    for (IgniteEx ig : Arrays.asList(ig1, ig2, ig3)) {
        Map<String, DynamicCacheDescriptor> desc = U.field((Object) U.field(ig.context().cache(), "cachesInfo"), "registeredCaches");
        assertEquals(4, desc.size());
        Map<String, GridCacheAdapter<?, ?>> caches = U.field(ig.context().cache(), "caches");
        assertEquals(3, caches.keySet().size());
    }
    Map<String, GridCacheAdapter<?, ?>> caches1 = U.field(ig1.context().cache(), "caches");
    Assert.assertNotNull(caches1.get(cache1));
    Assert.assertNotNull(caches1.get(cache2));
    Assert.assertNull(caches1.get(cache3));
    Map<String, GridCacheAdapter<?, ?>> caches2 = U.field(ig2.context().cache(), "caches");
    Assert.assertNull(caches2.get(cache1));
    Assert.assertNotNull(caches2.get(cache2));
    Assert.assertNotNull(caches2.get(cache3));
    Map<String, GridCacheAdapter<?, ?>> caches3 = U.field(ig3.context().cache(), "caches");
    Assert.assertNotNull(caches3.get(cache1));
    Assert.assertNull(caches3.get(cache2));
    Assert.assertNotNull(caches3.get(cache3));
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) IgniteEx(org.apache.ignite.internal.IgniteEx) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Aggregations

GridCacheAdapter (org.apache.ignite.internal.processors.cache.GridCacheAdapter)50 IgniteEx (org.apache.ignite.internal.IgniteEx)18 Map (java.util.Map)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 Ignite (org.apache.ignite.Ignite)12 IgniteException (org.apache.ignite.IgniteException)12 IgniteKernal (org.apache.ignite.internal.IgniteKernal)12 ArrayList (java.util.ArrayList)11 UUID (java.util.UUID)11 Test (org.junit.Test)11 IgniteCache (org.apache.ignite.IgniteCache)10 HashMap (java.util.HashMap)9 ClusterNode (org.apache.ignite.cluster.ClusterNode)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)9 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)8 HashSet (java.util.HashSet)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7 DynamicCacheDescriptor (org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor)7