Search in sources :

Example 36 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteCachePeekModesAbstractTest method offheapKeys.

/**
 * @param nodeIdx Node index.
 * @return Tuple with primary and backup keys.
 */
private T2<List<Integer>, List<Integer>> offheapKeys(int nodeIdx) {
    GridCacheAdapter<Integer, String> internalCache = ((IgniteKernal) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME);
    // TODO GG-11148.
    Iterator<Map.Entry<Integer, String>> offheapIt = Collections.EMPTY_MAP.entrySet().iterator();
    // if (internalCache.context().isNear())
    // offheapIt = internalCache.context().near().dht().context().swap().lazyOffHeapIterator(false);
    // else
    // offheapIt = internalCache.context().swap().lazyOffHeapIterator(false);
    Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
    ClusterNode node = ignite(nodeIdx).cluster().localNode();
    List<Integer> primary = new ArrayList<>();
    List<Integer> backups = new ArrayList<>();
    while (offheapIt.hasNext()) {
        Map.Entry<Integer, String> e = offheapIt.next();
        if (aff.isPrimary(node, e.getKey()))
            primary.add(e.getKey());
        else {
            assertTrue(aff.isBackup(node, e.getKey()));
            backups.add(e.getKey());
        }
    }
    return new T2<>(primary, backups);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) ArrayList(java.util.ArrayList) Affinity(org.apache.ignite.cache.affinity.Affinity) Map(java.util.Map) T2(org.apache.ignite.internal.util.typedef.T2)

Example 37 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class IgniteTxMultiNodeAbstractTest method primaryId.

/**
 * @param ignite Grid
 * @param key Key.
 * @return Primary node id.
 */
@SuppressWarnings("unchecked")
private static UUID primaryId(Ignite ignite, Object key) {
    Affinity aff = ignite.affinity(DEFAULT_CACHE_NAME);
    Collection<ClusterNode> affNodes = aff.mapPartitionToPrimaryAndBackups(aff.partition(key));
    ClusterNode first = F.first(affNodes);
    assert first != null;
    return first.id();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) Affinity(org.apache.ignite.cache.affinity.Affinity)

Example 38 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class GridCachePartitionedReloadAllAbstractSelfTest method testReloadAll.

/**
 * @throws Exception If test failed.
 */
@Test
public void testReloadAll() throws Exception {
    // Fill caches with values.
    for (IgniteCache<Integer, String> cache : caches) {
        Iterable<Integer> keys = primaryKeys(cache, 100);
        info("Values [cache=" + caches.indexOf(cache) + ", size=" + F.size(keys.iterator()) + ", keys=" + keys + "]");
        for (Integer key : keys) map.put(key, "val" + key);
    }
    CompletionListenerFuture fut = new CompletionListenerFuture();
    caches.get(0).loadAll(map.keySet(), false, fut);
    fut.get();
    Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
    for (IgniteCache<Integer, String> cache : caches) {
        for (Integer key : map.keySet()) {
            if (aff.isPrimaryOrBackup(grid(caches.indexOf(cache)).localNode(), key))
                assertEquals(map.get(key), cache.localPeek(key));
            else
                assertNull(cache.localPeek(key));
        }
    }
}
Also used : Affinity(org.apache.ignite.cache.affinity.Affinity) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 39 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class EvictionPolicyFailureHandlerTest method testFailureHandlerShouldNotCallOnRuntimeException.

/**
 */
@Test
public void testFailureHandlerShouldNotCallOnRuntimeException() throws Exception {
    IgniteEx node1 = startGrid(0);
    IgniteEx node2 = startGrid(1);
    GridCacheAdapter<Object, Object> cache = ((IgniteKernal) node2).internalCache(DEFAULT_CACHE_NAME);
    Affinity<Object> affinity = cache.affinity();
    for (int i = 0; i < 1000; i++) {
        if (affinity.isPrimary(node1.localNode(), i))
            cache.put(i, 1);
    }
    for (int i = 0; i < 1000; i++) {
        if (affinity.isPrimary(node1.localNode(), (double) i))
            cache.put((double) i, 1);
    }
    assertFalse(cache.map().entrySet(cache.context().cacheId()).stream().anyMatch(e -> e.key().value(null, false) instanceof Double));
    assertFalse(nodeFailure.get());
}
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) IgniteEx(org.apache.ignite.internal.IgniteEx) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 40 with Affinity

use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.

the class ClientCacheNodePartitionsRequest method process.

/**
 * {@inheritDoc}
 */
@Override
public ClientResponse process(ClientConnectionContext ctx) {
    IgniteCache cache = cache(ctx);
    GridDiscoveryManager discovery = ctx.kernalContext().discovery();
    Collection<ClusterNode> nodes = discovery.discoCache().cacheNodes(cache.getName());
    Affinity aff = ctx.kernalContext().affinity().affinityProxy(cache.getName());
    ArrayList<ClientConnectableNodePartitions> res = new ArrayList<>();
    for (ClusterNode node : nodes) {
        Integer port = node.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT);
        if (port == null)
            continue;
        Collection<String> addrs = node.addresses();
        int[] parts = aff.primaryPartitions(node);
        res.add(new ClientConnectableNodePartitions(port, addrs, parts));
    }
    return new ClientCacheNodePartitionsResponse(requestId(), res);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCache(org.apache.ignite.IgniteCache) ArrayList(java.util.ArrayList) GridDiscoveryManager(org.apache.ignite.internal.managers.discovery.GridDiscoveryManager) ClientConnectableNodePartitions(org.apache.ignite.internal.processors.platform.client.ClientConnectableNodePartitions) Affinity(org.apache.ignite.cache.affinity.Affinity)

Aggregations

Affinity (org.apache.ignite.cache.affinity.Affinity)49 Ignite (org.apache.ignite.Ignite)30 IgniteCache (org.apache.ignite.IgniteCache)29 ClusterNode (org.apache.ignite.cluster.ClusterNode)28 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)18 Collection (java.util.Collection)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Transaction (org.apache.ignite.transactions.Transaction)16 List (java.util.List)15 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)14 Map (java.util.Map)13 Cache (javax.cache.Cache)13 HashMap (java.util.HashMap)10 IgniteEx (org.apache.ignite.internal.IgniteEx)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 Collections (java.util.Collections)8 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)8 Ignition (org.apache.ignite.Ignition)8 ScanQuery (org.apache.ignite.cache.query.ScanQuery)8