Search in sources :

Example 1 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.

the class GridCacheAffinityApiSelfTest method testMapPartitionsToNodeArray.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testMapPartitionsToNodeArray() throws Exception {
    Map<Integer, ClusterNode> map = grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionsToNodes(F.asList(0, 1, 5, 19, 12));
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
    AffinityFunction aff = affinity();
    List<List<ClusterNode>> assignment = aff.assignPartitions(ctx);
    for (Map.Entry<Integer, ClusterNode> e : map.entrySet()) assert F.eqNodes(F.first(nodes(assignment, aff, e.getKey())), e.getValue());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) Map(java.util.Map)

Example 2 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.

the class CacheLateAffinityAssignmentTest method testDelayedAffinityCalculation.

/**
 * Checks that new joined primary is not assigned immediately.
 *
 * @throws Exception If failed.
 */
public void testDelayedAffinityCalculation() throws Exception {
    Ignite ignite0 = startServer(0, 1);
    checkAffinity(1, topVer(1, 0), true);
    GridCacheContext cctx = ((IgniteKernal) ignite0).context().cache().internalCache(CACHE_NAME1).context();
    AffinityFunction func = cctx.config().getAffinity();
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(ignite0.cluster().nodes()), null, null, topVer(1, 0), cctx.config().getBackups());
    List<List<ClusterNode>> calcAff1_0 = func.assignPartitions(ctx);
    startServer(1, 2);
    ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(ignite0.cluster().nodes()), calcAff1_0, null, topVer(1, 0), cctx.config().getBackups());
    List<List<ClusterNode>> calcAff2_0 = func.assignPartitions(ctx);
    checkAffinity(2, topVer(2, 0), false);
    List<List<ClusterNode>> aff2_0 = affinity(ignite0, topVer(2, 0), CACHE_NAME1);
    for (int p = 0; p < calcAff1_0.size(); p++) {
        List<ClusterNode> a1 = calcAff1_0.get(p);
        List<ClusterNode> a2 = calcAff2_0.get(p);
        List<ClusterNode> a = aff2_0.get(p);
        // Primary did not change.
        assertEquals(a1.get(0), a.get(0));
        // New primary is backup.
        if (!a1.get(0).equals(a2.get(0)))
            assertTrue(a.contains(a2.get(0)));
    }
    checkAffinity(2, topVer(2, 1), true);
    List<List<ClusterNode>> aff2_1 = affinity(ignite0, topVer(2, 1), CACHE_NAME1);
    assertEquals(calcAff2_0, aff2_1);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) ArrayList(java.util.ArrayList) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext)

Example 3 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.

the class IgniteCacheClientNodeChangingTopologyTest method findKeys.

/**
 * Tries to find keys for two partitions: for one partition assignment should not change after node join,
 * for another primary node should change.
 *
 * @param ignite Ignite.
 * @param nodes Current nodes.
 * @return Found keys.
 */
private IgniteBiTuple<Integer, Integer> findKeys(Ignite ignite, ClusterNode... nodes) {
    ClusterNode newNode = new TcpDiscoveryNode();
    GridTestUtils.setFieldValue(newNode, "consistentId", getTestIgniteInstanceName(4));
    GridTestUtils.setFieldValue(newNode, "id", UUID.randomUUID());
    List<ClusterNode> topNodes = new ArrayList<>();
    Collections.addAll(topNodes, nodes);
    topNodes.add(newNode);
    DiscoveryEvent discoEvt = new DiscoveryEvent(newNode, "", EventType.EVT_NODE_JOINED, newNode);
    final long topVer = ignite.cluster().topologyVersion();
    GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(topNodes, null, discoEvt, new AffinityTopologyVersion(topVer + 1), 1);
    AffinityFunction affFunc = ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getAffinity();
    List<List<ClusterNode>> newAff = affFunc.assignPartitions(ctx);
    List<List<ClusterNode>> curAff = ((IgniteKernal) ignite).context().cache().internalCache(DEFAULT_CACHE_NAME).context().affinity().assignments(new AffinityTopologyVersion(topVer));
    Integer key1 = null;
    Integer key2 = null;
    Affinity<Integer> aff = ignite.affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < curAff.size(); i++) {
        if (key1 == null) {
            List<ClusterNode> oldNodes = curAff.get(i);
            List<ClusterNode> newNodes = newAff.get(i);
            if (oldNodes.equals(newNodes))
                key1 = findKey(aff, i);
        }
        if (key2 == null) {
            ClusterNode oldPrimary = F.first(curAff.get(i));
            ClusterNode newPrimary = F.first(newAff.get(i));
            if (!oldPrimary.equals(newPrimary))
                key2 = findKey(aff, i);
        }
        if (key1 != null && key2 != null)
            break;
    }
    if (key1 == null || key2 == null)
        fail("Failed to find nodes required for test.");
    return new IgniteBiTuple<>(key1, key2);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) List(java.util.List) ArrayList(java.util.ArrayList) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.

the class GridCacheAffinityApiSelfTest method testMapPartitionToNode.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testMapPartitionToNode() throws Exception {
    int part = RND.nextInt(affinity().partitions());
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
    AffinityFunction aff = affinity();
    List<List<ClusterNode>> assignment = aff.assignPartitions(ctx);
    assertEquals(F.first(nodes(assignment, aff, part)), grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionToNode(part));
}
Also used : GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Example 5 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.

the class GridCacheAffinityApiSelfTest method testPrimaryPartitionsOneNode.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testPrimaryPartitionsOneNode() throws Exception {
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
    List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx);
    for (ClusterNode node : grid(0).cluster().nodes()) {
        int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).primaryPartitions(node);
        assert !F.isEmpty(parts);
        for (int p : parts) {
            Collection<ClusterNode> owners = nodes(assignment, p);
            assert !F.isEmpty(owners);
            ClusterNode primary = F.first(owners);
            assert F.eqNodes(node, primary);
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext)

Aggregations

GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)17 ArrayList (java.util.ArrayList)15 List (java.util.List)15 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 AffinityFunctionContext (org.apache.ignite.cache.affinity.AffinityFunctionContext)11 LinkedList (java.util.LinkedList)8 AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)8 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)7 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 GridTestNode (org.apache.ignite.testframework.GridTestNode)3 Ignite (org.apache.ignite.Ignite)2 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)2 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 UUID (java.util.UUID)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)1