Search in sources :

Example 1 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 2 with AffinityFunctionContext

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

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

Example 4 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 5 with AffinityFunctionContext

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

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 GridNodeOrderComparator (org.apache.ignite.internal.GridNodeOrderComparator)1