Search in sources :

Example 6 with AffinityFunctionContext

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

the class GridCacheAffinityApiSelfTest method testMapPartitionsToNode.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testMapPartitionsToNode() 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 AffinityFunctionContext

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

the class PlatformAffinityFunctionTarget method processOutStream.

/**
 * {@inheritDoc}
 */
@Override
public void processOutStream(int type, BinaryRawWriterEx writer) throws IgniteCheckedException {
    if (type == OP_ASSIGN_PARTITIONS) {
        AffinityFunctionContext affCtx = currentAffCtx.get();
        if (affCtx == null)
            throw new IgniteException("Thread-local AffinityFunctionContext is null. " + "This may indicate an unsupported call to the base AffinityFunction.");
        final List<List<ClusterNode>> partitions = baseFunc.assignPartitions(affCtx);
        PlatformAffinityUtils.writePartitionAssignment(partitions, writer, platformContext());
        return;
    }
    super.processOutStream(type, writer);
}
Also used : IgniteException(org.apache.ignite.IgniteException) List(java.util.List) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext)

Example 8 with AffinityFunctionContext

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

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

the class GridCachePartitionedPreloadEventsSelfTest method cacheConfiguration.

/** {@inheritDoc} */
@Override
protected CacheConfiguration cacheConfiguration() {
    CacheConfiguration cacheCfg = super.cacheConfiguration();
    if (replicatedAffinity)
        // replicate entries to all nodes
        cacheCfg.setAffinity(notSerializableProxy(new AffinityFunction() {

            /** {@inheritDoc} */
            @Override
            public void reset() {
            }

            /** {@inheritDoc} */
            @Override
            public int partitions() {
                return 1;
            }

            /** {@inheritDoc} */
            @Override
            public int partition(Object key) {
                return 0;
            }

            /** {@inheritDoc} */
            @Override
            public List<List<ClusterNode>> assignPartitions(AffinityFunctionContext affCtx) {
                List<ClusterNode> nodes = new ArrayList<>(affCtx.currentTopologySnapshot());
                return Collections.singletonList(nodes);
            }

            /** {@inheritDoc} */
            @Override
            public void removeNode(UUID nodeId) {
            }
        }, AffinityFunction.class));
    cacheCfg.setRebalanceDelay(rebalanceDelay);
    return cacheCfg;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 10 with AffinityFunctionContext

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

the class GridCacheAffinityApiSelfTest method testAllPartitions.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testAllPartitions() throws Exception {
    // Pick 2 nodes and create a projection over them.
    ClusterNode n0 = grid(0).localNode();
    int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).allPartitions(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 = nodes(assignment, p);
        assert !F.isEmpty(owners);
        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) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext)

Aggregations

List (java.util.List)13 AffinityFunctionContext (org.apache.ignite.cache.affinity.AffinityFunctionContext)13 ArrayList (java.util.ArrayList)12 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)11 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)9 LinkedList (java.util.LinkedList)8 AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)8 Map (java.util.Map)3 UUID (java.util.UUID)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 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 IgniteException (org.apache.ignite.IgniteException)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1