Search in sources :

Example 11 with AffinityFunction

use of org.apache.ignite.cache.affinity.AffinityFunction 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 12 with AffinityFunction

use of org.apache.ignite.cache.affinity.AffinityFunction 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 13 with AffinityFunction

use of org.apache.ignite.cache.affinity.AffinityFunction 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 14 with AffinityFunction

use of org.apache.ignite.cache.affinity.AffinityFunction 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)

Example 15 with AffinityFunction

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

the class GridOffHeapPartitionedMapAbstractSelfTest method testPutRandomKeys.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void testPutRandomKeys() throws Exception {
    map = newMap();
    AffinityFunction aff = new RendezvousAffinityFunction(parts, null);
    getTestResources().inject(aff);
    GridByteArrayWrapper[] keys = new GridByteArrayWrapper[512];
    Random rnd = new Random();
    for (int i = 0; i < keys.length; i++) {
        byte[] key = new byte[rnd.nextInt(64) + 1];
        rnd.nextBytes(key);
        keys[i] = new GridByteArrayWrapper(key);
    }
    for (int i = 0; i < 10000; i++) {
        int idx = rnd.nextInt(keys.length);
        GridByteArrayWrapper key = keys[idx];
        map.put(aff.partition(key), key.hashCode(), key.array(), new byte[64]);
    }
}
Also used : Random(java.util.Random) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Aggregations

AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)27 ArrayList (java.util.ArrayList)10 List (java.util.List)10 ClusterNode (org.apache.ignite.cluster.ClusterNode)10 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 AffinityFunctionContext (org.apache.ignite.cache.affinity.AffinityFunctionContext)8 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)8 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Ignite (org.apache.ignite.Ignite)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 Random (java.util.Random)3 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 HashSet (java.util.HashSet)2 UUID (java.util.UUID)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 PlatformAffinityFunction (org.apache.ignite.internal.processors.platform.cache.affinity.PlatformAffinityFunction)2