Search in sources :

Example 21 with AffinityFunction

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

the class RendezvousAffinityFunctionSimpleBenchmark method testAffinityBenchmarkChangeLast.

/**
 */
public void testAffinityBenchmarkChangeLast() {
    mode = TopologyModificationMode.CHANGE_LAST_NODE;
    AffinityFunction aff0 = new RendezvousAffinityFunctionOld(true, 1024);
    GridTestUtils.setFieldValue(aff0, "ignite", ignite);
    affinityBenchmark(aff0, new RendezvousAffinityFunction(true, 1024));
}
Also used : AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Example 22 with AffinityFunction

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

the class CacheLateAffinityAssignmentTest method calculateAffinity.

/**
 * @param topVer Topology version.
 * @param filterByRcvd If {@code true} filters caches by 'receivedFrom' property.
 * @param cur Optional current affinity.
 * @throws Exception If failed.
 * @return {@code True} if some primary node changed comparing to given affinity.
 */
private boolean calculateAffinity(long topVer, boolean filterByRcvd, @Nullable Map<String, List<List<ClusterNode>>> cur) throws Exception {
    List<Ignite> all = G.allGrids();
    IgniteKernal ignite = (IgniteKernal) Collections.min(all, new Comparator<Ignite>() {

        @Override
        public int compare(Ignite n1, Ignite n2) {
            return Long.compare(n1.cluster().localNode().order(), n2.cluster().localNode().order());
        }
    });
    assert all.size() > 0;
    Map<Integer, List<List<ClusterNode>>> assignments = idealAff.get(topVer);
    if (assignments == null)
        idealAff.put(topVer, assignments = new HashMap<>());
    GridKernalContext ctx = ignite.context();
    GridCacheSharedContext cctx = ctx.cache().context();
    AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer);
    cctx.discovery().topologyFuture(topVer).get();
    List<GridDhtPartitionsExchangeFuture> futs = cctx.exchange().exchangeFutures();
    DiscoveryEvent evt = null;
    long stopTime = System.currentTimeMillis() + 10_000;
    boolean primaryChanged = false;
    do {
        for (int i = futs.size() - 1; i >= 0; i--) {
            GridDhtPartitionsExchangeFuture fut = futs.get(i);
            if (fut.initialVersion().equals(topVer0)) {
                evt = fut.firstEvent();
                break;
            }
        }
        if (evt == null) {
            U.sleep(500);
            futs = cctx.exchange().exchangeFutures();
        } else
            break;
    } while (System.currentTimeMillis() < stopTime);
    assertNotNull("Failed to find exchange future:", evt);
    Collection<ClusterNode> allNodes = ctx.discovery().serverNodes(topVer0);
    for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors().values()) {
        if (assignments.get(cacheDesc.cacheId()) != null)
            continue;
        if (filterByRcvd && cacheDesc.receivedFrom() != null && ctx.discovery().node(topVer0, cacheDesc.receivedFrom()) == null)
            continue;
        AffinityFunction func = cacheDesc.cacheConfiguration().getAffinity();
        func = cctx.cache().clone(func);
        cctx.kernalContext().resource().injectGeneric(func);
        List<ClusterNode> affNodes = new ArrayList<>();
        IgnitePredicate<ClusterNode> filter = cacheDesc.cacheConfiguration().getNodeFilter();
        for (ClusterNode n : allNodes) {
            if (!CU.clientNode(n) && (filter == null || filter.apply(n)))
                affNodes.add(n);
        }
        Collections.sort(affNodes, NodeOrderComparator.getInstance());
        AffinityFunctionContext affCtx = new GridAffinityFunctionContextImpl(affNodes, previousAssignment(topVer, cacheDesc.cacheId()), evt, topVer0, cacheDesc.cacheConfiguration().getBackups());
        List<List<ClusterNode>> assignment = func.assignPartitions(affCtx);
        if (cur != null) {
            List<List<ClusterNode>> prev = cur.get(cacheDesc.cacheConfiguration().getName());
            assertEquals(prev.size(), assignment.size());
            if (!primaryChanged) {
                for (int p = 0; p < prev.size(); p++) {
                    List<ClusterNode> nodes0 = prev.get(p);
                    List<ClusterNode> nodes1 = assignment.get(p);
                    if (nodes0.size() > 0 && nodes1.size() > 0) {
                        ClusterNode p0 = nodes0.get(0);
                        ClusterNode p1 = nodes1.get(0);
                        if (allNodes.contains(p0) && !p0.equals(p1)) {
                            primaryChanged = true;
                            log.info("Primary changed [cache=" + cacheDesc.cacheConfiguration().getName() + ", part=" + p + ", prev=" + F.nodeIds(nodes0) + ", new=" + F.nodeIds(nodes1) + ']');
                            break;
                        }
                    }
                }
            }
        }
        assignments.put(cacheDesc.cacheId(), assignment);
    }
    return primaryChanged;
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) NodeOrderComparator(org.apache.ignite.internal.cluster.NodeOrderComparator) NodeOrderLegacyComparator(org.apache.ignite.internal.cluster.NodeOrderLegacyComparator) Comparator(java.util.Comparator) Ignite(org.apache.ignite.Ignite) List(java.util.List) ArrayList(java.util.ArrayList) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DynamicCacheDescriptor(org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction)

Example 23 with AffinityFunction

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

the class GridCacheAffinityApiSelfTest method testMapPartitionsToNodeCollection.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testMapPartitionsToNodeCollection() throws Exception {
    Collection<Integer> parts = new LinkedList<>();
    for (int p = 0; p < affinity().partitions(); p++) parts.add(p);
    Map<Integer, ClusterNode> map = grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionsToNodes(parts);
    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) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) AffinityFunctionContext(org.apache.ignite.cache.affinity.AffinityFunctionContext) LinkedList(java.util.LinkedList) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) Map(java.util.Map)

Example 24 with AffinityFunction

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

the class GridCachePartitionExchangeManager method clientTopology.

/**
 * @param grpId Cache group ID.
 * @param discoCache Discovery data cache.
 * @return Topology.
 */
public GridDhtPartitionTopology clientTopology(int grpId, DiscoCache discoCache) {
    GridClientPartitionTopology top = clientTops.get(grpId);
    if (top != null)
        return top;
    CacheGroupDescriptor grpDesc = cctx.affinity().cacheGroups().get(grpId);
    assert grpDesc != null : grpId;
    CacheConfiguration<?, ?> ccfg = grpDesc.config();
    AffinityFunction aff = ccfg.getAffinity();
    Object affKey = cctx.kernalContext().affinity().similaryAffinityKey(aff, ccfg.getNodeFilter(), ccfg.getBackups(), aff.partitions());
    GridClientPartitionTopology old = clientTops.putIfAbsent(grpId, top = new GridClientPartitionTopology(cctx, discoCache, grpId, aff.partitions(), affKey));
    return old != null ? old : top;
}
Also used : GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) GridClientPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridClientPartitionTopology)

Example 25 with AffinityFunction

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

the class PlatformConfigurationUtils method writeAffinityFunction.

/**
 * Writes the affinity functions.
 *
 * @param out Stream.
 * @param f Affinity.
 */
private static void writeAffinityFunction(BinaryRawWriter out, AffinityFunction f) {
    if (f instanceof PlatformDotNetAffinityFunction)
        f = ((PlatformDotNetAffinityFunction) f).getFunc();
    if (f instanceof RendezvousAffinityFunction) {
        out.writeByte((byte) 2);
        RendezvousAffinityFunction f0 = (RendezvousAffinityFunction) f;
        out.writeInt(f0.getPartitions());
        out.writeBoolean(f0.isExcludeNeighbors());
        // override flags
        out.writeByte((byte) 0);
        // user func
        out.writeObject(null);
    } else if (f instanceof PlatformAffinityFunction) {
        PlatformAffinityFunction f0 = (PlatformAffinityFunction) f;
        AffinityFunction baseFunc = f0.getBaseFunc();
        if (baseFunc instanceof RendezvousAffinityFunction) {
            out.writeByte((byte) 2);
            out.writeInt(f0.partitions());
            out.writeBoolean(((RendezvousAffinityFunction) baseFunc).isExcludeNeighbors());
            out.writeByte(f0.getOverrideFlags());
            out.writeObject(f0.getUserFunc());
        } else {
            out.writeByte((byte) 3);
            out.writeInt(f0.partitions());
            // exclude neighbors
            out.writeBoolean(false);
            out.writeByte(f0.getOverrideFlags());
            out.writeObject(f0.getUserFunc());
        }
    } else
        out.writeByte((byte) 0);
}
Also used : PlatformAffinityFunction(org.apache.ignite.internal.processors.platform.cache.affinity.PlatformAffinityFunction) PlatformDotNetAffinityFunction(org.apache.ignite.platform.dotnet.PlatformDotNetAffinityFunction) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) PlatformAffinityFunction(org.apache.ignite.internal.processors.platform.cache.affinity.PlatformAffinityFunction) PlatformDotNetAffinityFunction(org.apache.ignite.platform.dotnet.PlatformDotNetAffinityFunction)

Aggregations

AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)32 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)12 ArrayList (java.util.ArrayList)11 List (java.util.List)10 ClusterNode (org.apache.ignite.cluster.ClusterNode)10 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)10 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 Map (java.util.Map)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Ignite (org.apache.ignite.Ignite)5 LinkedList (java.util.LinkedList)4 Random (java.util.Random)4 UUID (java.util.UUID)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 HashSet (java.util.HashSet)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2