Search in sources :

Example 6 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 7 with GridAffinityFunctionContextImpl

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

the class GridCacheAffinityApiSelfTest method testBackupPartitions.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
public void testBackupPartitions() throws Exception {
    // Pick 2 nodes and create a projection over them.
    ClusterNode n0 = grid(0).localNode();
    // Get backup partitions without explicitly specified levels.
    int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).backupPartitions(n0);
    assert !F.isEmpty(parts);
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
    List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx);
    for (int p : parts) {
        Collection<ClusterNode> owners = new ArrayList<>(nodes(assignment, p));
        assert !F.isEmpty(owners);
        // Remove primary.
        Iterator<ClusterNode> iter = owners.iterator();
        iter.next();
        iter.remove();
        assert owners.contains(n0);
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext)

Example 8 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 9 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 10 with GridAffinityFunctionContextImpl

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

the class GridCommonAbstractTest method movingKeysAfterJoin.

/**
     * Return list of keys that are primary for given node on current topology,
     * but primary node will change after new node will be added.
     *
     * @param ign Ignite.
     * @param cacheName Cache name.
     * @param size Number of keys.
     * @return List of keys.
     */
protected final List<Integer> movingKeysAfterJoin(Ignite ign, String cacheName, int size) {
    assertEquals("Expected consistentId is set to node name", ign.name(), ign.cluster().localNode().consistentId());
    GridCacheContext<Object, Object> cctx = ((IgniteKernal) ign).context().cache().internalCache(cacheName).context();
    ArrayList<ClusterNode> nodes = new ArrayList<>(ign.cluster().nodes());
    AffinityFunction func = cctx.config().getAffinity();
    AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(nodes, null, null, AffinityTopologyVersion.NONE, cctx.config().getBackups());
    List<List<ClusterNode>> calcAff = func.assignPartitions(ctx);
    GridTestNode fakeNode = new GridTestNode(UUID.randomUUID(), null);
    fakeNode.consistentId(getTestIgniteInstanceName(nodes.size()));
    nodes.add(fakeNode);
    ctx = new GridAffinityFunctionContextImpl(nodes, null, null, AffinityTopologyVersion.NONE, cctx.config().getBackups());
    List<List<ClusterNode>> calcAff2 = func.assignPartitions(ctx);
    Set<Integer> movedParts = new HashSet<>();
    UUID locId = ign.cluster().localNode().id();
    for (int i = 0; i < calcAff.size(); i++) {
        if (calcAff.get(i).get(0).id().equals(locId) && !calcAff2.get(i).get(0).id().equals(locId))
            movedParts.add(i);
    }
    List<Integer> keys = new ArrayList<>();
    Affinity<Integer> aff = ign.affinity(cacheName);
    for (int i = 0; i < 10_000; i++) {
        int keyPart = aff.partition(i);
        if (movedParts.contains(keyPart)) {
            keys.add(i);
            if (keys.size() == size)
                break;
        }
    }
    assertEquals("Failed to find moving keys [movedPats=" + movedParts + ", keys=" + keys + ']', size, keys.size());
    return keys;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ArrayList(java.util.ArrayList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) GridTestNode(org.apache.ignite.testframework.GridTestNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) HashSet(java.util.HashSet)

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