Search in sources :

Example 11 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl 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.topologyVersion().equals(topVer0)) {
                evt = fut.discoveryEvent();
                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().cacheNodes(topVer0);
    for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors()) {
        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, GridNodeOrderComparator.INSTANCE);
        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) GridNodeOrderComparator(org.apache.ignite.internal.GridNodeOrderComparator) 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 12 with GridAffinityFunctionContextImpl

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

the class RendezvousAffinityFunctionSimpleBenchmark method assignPartitions.

/**
     *
     * @param aff Affinity function.
     * @param nodes Topology.
     * @param iter Number of iteration.
     * @param prevAssignment Previous affinity assignment.
     * @param backups Backups count.
     * @return Tuple with affinity and time spend of the affinity calculation.
     */
private IgniteBiTuple<Long, List<List<ClusterNode>>> assignPartitions(AffinityFunction aff, List<ClusterNode> nodes, List<List<ClusterNode>> prevAssignment, int backups, int iter) {
    GridAffinityFunctionContextImpl ctx = null;
    switch(mode) {
        case CHANGE_LAST_NODE:
            ctx = nodesModificationChangeLast(nodes, prevAssignment, iter, backups);
            break;
        case CHANGE_FIRST_NODE:
            ctx = nodesModificationChangeFirst(nodes, prevAssignment, iter, backups);
            break;
        case ADD:
            ctx = new GridAffinityFunctionContextImpl(nodes, prevAssignment, addNode(nodes, iter), new AffinityTopologyVersion(nodes.size()), backups);
            break;
        case REMOVE_RANDOM:
            ctx = new GridAffinityFunctionContextImpl(nodes, prevAssignment, removeNode(nodes, nodes.size() - 1), new AffinityTopologyVersion(nodes.size()), backups);
            break;
        case NONE:
            ctx = new GridAffinityFunctionContextImpl(nodes, prevAssignment, new DiscoveryEvent(nodes.get(0), "", EventType.EVT_NODE_JOINED, nodes.get(nodes.size() - 1)), new AffinityTopologyVersion(nodes.size()), backups);
            break;
    }
    long start = System.currentTimeMillis();
    List<List<ClusterNode>> assignments = aff.assignPartitions(ctx);
    return F.t(System.currentTimeMillis() - start, assignments);
}
Also used : GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with GridAffinityFunctionContextImpl

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

the class AbstractAffinityFunctionSelfTest method checkNodeRemoved.

/**
     * @throws Exception If failed.
     */
protected void checkNodeRemoved(int backups, int neighborsPerHost, int neighborsPeriod) throws Exception {
    AffinityFunction aff = affinityFunction();
    int nodesCnt = 50;
    List<ClusterNode> nodes = new ArrayList<>(nodesCnt);
    List<List<ClusterNode>> prev = null;
    for (int i = 0; i < nodesCnt; i++) {
        info("======================================");
        info("Assigning partitions: " + i);
        info("======================================");
        ClusterNode node = new GridTestNode(UUID.randomUUID());
        if (neighborsPerHost > 0)
            node.attribute(MAC_PREF + ((i / neighborsPeriod) % (nodesCnt / neighborsPerHost)));
        nodes.add(node);
        DiscoveryEvent discoEvt = new DiscoveryEvent(node, "", EventType.EVT_NODE_JOINED, node);
        GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(nodes, prev, discoEvt, new AffinityTopologyVersion(i), backups);
        List<List<ClusterNode>> assignment = aff.assignPartitions(ctx);
        info("Assigned.");
        verifyAssignment(assignment, backups, aff.partitions(), nodes.size());
        prev = assignment;
    }
    info("======================================");
    info("Will remove nodes.");
    info("======================================");
    for (int i = 0; i < nodesCnt - 1; i++) {
        info("======================================");
        info("Assigning partitions: " + i);
        info("======================================");
        ClusterNode rmv = nodes.remove(nodes.size() - 1);
        DiscoveryEvent discoEvt = new DiscoveryEvent(rmv, "", EventType.EVT_NODE_LEFT, rmv);
        List<List<ClusterNode>> assignment = aff.assignPartitions(new GridAffinityFunctionContextImpl(nodes, prev, discoEvt, new AffinityTopologyVersion(i), backups));
        info("Assigned.");
        verifyAssignment(assignment, backups, aff.partitions(), nodes.size());
        prev = assignment;
    }
}
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) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) ArrayList(java.util.ArrayList) List(java.util.List) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 14 with GridAffinityFunctionContextImpl

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

the class RendezvousAffinityFunctionSimpleBenchmark method nodesModificationChangeFirst.

/**
     * Modify the topology by remove the first node / add new node
     *
     * @param nodes Topology.
     * @param prevAssignment Previous affinity.
     * @param iter Number of iteration.
     * @param backups Backups count.
     * @return Affinity context.
     */
private GridAffinityFunctionContextImpl nodesModificationChangeFirst(List<ClusterNode> nodes, List<List<ClusterNode>> prevAssignment, int iter, int backups) {
    DiscoveryEvent discoEvt;
    discoEvt = iter % 2 == 0 ? addNode(nodes, iter) : removeNode(nodes, 0);
    return new GridAffinityFunctionContextImpl(nodes, prevAssignment, discoEvt, new AffinityTopologyVersion(nodes.size()), backups);
}
Also used : GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent)

Example 15 with GridAffinityFunctionContextImpl

use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl 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)

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