Search in sources :

Example 36 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.
 */
@Test
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) Test(org.junit.Test)

Example 37 with AffinityFunction

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

the class CacheLateAffinityAssignmentTest method testSinglePartitionCacheNodeLeft.

/**
 * Since we have only 1 partition, at each node left it will be lost (no rebalance needed) or it will be still
 * located at second node (thanks to special affinity) (no rebalance neeeded). So, LAA should never happen.
 *
 * @param ownerLeft Kill owner flag.
 */
private void testSinglePartitionCacheNodeLeft(boolean ownerLeft) throws Exception {
    String cacheName = "single-partitioned";
    cacheC = new IgniteClosure<String, CacheConfiguration[]>() {

        @Override
        public CacheConfiguration[] apply(String igniteInstanceName) {
            CacheConfiguration ccfg = new CacheConfiguration();
            AffinityFunction aff;
            ccfg.setName(cacheName);
            ccfg.setWriteSynchronizationMode(FULL_SYNC);
            ccfg.setBackups(0);
            aff = ownerLeft ? affinityFunction(1) : new MapSinglePartitionToSecondNodeAffinityFunction();
            ccfg.setAffinity(aff);
            return new CacheConfiguration[] { ccfg };
        }
    };
    int top = 0;
    int nodes = 0;
    startServer(nodes++, ++top);
    checkAffinity(nodes, topVer(top, 0), true);
    // Checks LAA is absent on initial topology.
    checkNoExchange(nodes, topVer(top, 1));
    for (int i = 0; i < 10; i++) startServer(nodes++, ++top);
    awaitPartitionMapExchange();
    Ignite primary = primaryNode(0, cacheName);
    boolean laaOnJoin = primary.cluster().localNode().order() != 1;
    boolean leftHappen = false;
    while (nodes > 1) {
        Map<String, List<List<ClusterNode>>> aff = checkAffinity(nodes, topVer(top, leftHappen ? 0 : (laaOnJoin ? 1 : 0)), true);
        ClusterNode owner = aff.get(cacheName).get(/*part*/
        0).get(/*primary*/
        0);
        Ignite actualOwner = primaryNode(0, cacheName);
        assertEquals(actualOwner.cluster().localNode().order(), owner.order());
        for (Ignite node : G.allGrids()) {
            ClusterNode locNode = node.cluster().localNode();
            boolean equals = locNode.order() == owner.order();
            if (equals == ownerLeft) {
                if (!ownerLeft)
                    assertNotSame(locNode.order(), 2);
                grid(locNode).close();
                calculateAffinity(++top);
                leftHappen = true;
                break;
            }
        }
        checkAffinity(--nodes, topVer(top, 0), true);
        checkNoExchange(nodes, topVer(top, 1));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) 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) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 38 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.isEmpty();
    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 (!n.isClient() && (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.isEmpty() && !nodes1.isEmpty()) {
                        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) 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 39 with AffinityFunction

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

the class IgnitePdsSingleNodeWithIndexingAndGroupPutGetPersistenceSelfTest method configure.

/**
 * {@inheritDoc}
 */
@Override
protected void configure(IgniteConfiguration cfg) {
    super.configure(cfg);
    for (CacheConfiguration ccfg : cfg.getCacheConfiguration()) {
        AffinityFunction aff = ccfg.getAffinity();
        int parts = aff != null ? aff.partitions() : RendezvousAffinityFunction.DFLT_PARTITION_COUNT;
        ccfg.setGroupName("testGroup-parts" + parts);
    }
}
Also used : AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 40 with AffinityFunction

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

the class GridCommonAbstractTest method awaitPartitionMapExchange.

/**
 * @param waitEvicts If {@code true} will wait for evictions finished.
 * @param waitNode2PartUpdate If {@code true} will wait for nodes node2part info update finished.
 * @param nodes Optional nodes. If {@code null} method will wait for all nodes, for non null collection nodes will
 *      be filtered
 * @param printPartState If {@code true} will print partition state if evictions not happened.
 * @param cacheNames Wait for specific caches.
 * @throws InterruptedException If interrupted.
 */
@SuppressWarnings("BusyWait")
protected void awaitPartitionMapExchange(boolean waitEvicts, boolean waitNode2PartUpdate, @Nullable Collection<ClusterNode> nodes, boolean printPartState, @Nullable Set<String> cacheNames) throws InterruptedException {
    long timeout = getPartitionMapExchangeTimeout();
    long startTime = -1;
    Set<String> names = new HashSet<>();
    Ignite crd = null;
    for (Ignite g : G.allGrids()) {
        ClusterNode node = g.cluster().localNode();
        if (crd == null || node.order() < crd.cluster().localNode().order()) {
            crd = g;
            if (node.order() == 1)
                break;
        }
    }
    if (crd == null)
        return;
    AffinityTopologyVersion waitTopVer = ((IgniteKernal) crd).context().discovery().topologyVersionEx();
    if (waitTopVer.topologyVersion() <= 0)
        waitTopVer = new AffinityTopologyVersion(1, 0);
    for (Ignite g : G.allGrids()) {
        if (nodes != null && !nodes.contains(g.cluster().localNode()))
            continue;
        IgniteKernal g0 = (IgniteKernal) g;
        names.add(g0.configuration().getIgniteInstanceName());
        if (startTime != -1) {
            if (startTime != g0.context().discovery().gridStartTime())
                fail("Found nodes from different clusters, probable some test does not stop nodes " + "[allNodes=" + names + ']');
        } else
            startTime = g0.context().discovery().gridStartTime();
        if (g.cluster().localNode().isDaemon())
            continue;
        IgniteInternalFuture<?> exchFut = g0.context().cache().context().exchange().affinityReadyFuture(waitTopVer);
        if (exchFut != null && !exchFut.isDone()) {
            try {
                exchFut.get(timeout);
            } catch (IgniteCheckedException e) {
                log.error("Failed to wait for exchange [topVer=" + waitTopVer + ", node=" + g0.name() + ']', e);
            }
        }
        for (IgniteCacheProxy<?, ?> c : g0.context().cache().jcaches()) {
            CacheConfiguration cfg = c.context().config();
            if (cfg == null || cacheNames != null && !cacheNames.contains(cfg.getName()))
                continue;
            if (cfg.getCacheMode() != LOCAL && cfg.getRebalanceMode() != NONE && g.cluster().nodes().size() > 1) {
                AffinityFunction aff = cfg.getAffinity();
                GridDhtCacheAdapter<?, ?> dht = dht(c);
                GridDhtPartitionTopology top = dht.topology();
                for (int p = 0; p < aff.partitions(); p++) {
                    long start = 0;
                    for (int i = 0; ; i++) {
                        boolean match = false;
                        GridCachePartitionExchangeManager<?, ?> exchMgr = dht.context().shared().exchange();
                        AffinityTopologyVersion readyVer = exchMgr.readyAffinityVersion();
                        // Otherwise, there may be an assertion when printing top.readyTopologyVersion().
                        try {
                            IgniteInternalFuture<?> fut = exchMgr.affinityReadyFuture(readyVer);
                            if (fut != null)
                                fut.get();
                        } catch (IgniteCheckedException e) {
                            throw new IgniteException(e);
                        }
                        if (readyVer.topologyVersion() > 0 && c.context().started()) {
                            // Must map on updated version of topology.
                            List<ClusterNode> affNodes = dht.context().affinity().assignment(readyVer).idealAssignment().get(p);
                            int affNodesCnt = affNodes.size();
                            GridDhtTopologyFuture topFut = top.topologyVersionFuture();
                            Collection<ClusterNode> owners = (topFut != null && topFut.isDone()) ? top.owners(p, AffinityTopologyVersion.NONE) : Collections.<ClusterNode>emptyList();
                            int ownerNodesCnt = owners.size();
                            GridDhtLocalPartition loc = top.localPartition(p, readyVer, false);
                            boolean notPrimary = !affNodes.isEmpty() && !affNodes.get(0).equals(dht.context().affinity().primaryByPartition(p, readyVer));
                            if (affNodesCnt != ownerNodesCnt || !affNodes.containsAll(owners) || (waitEvicts && loc != null && loc.state() != OWNING) || notPrimary) {
                                if (i % 50 == 0)
                                    LT.warn(log(), "Waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", p=" + p + ", affNodesCnt=" + affNodesCnt + ", ownersCnt=" + ownerNodesCnt + ", affNodes=" + F.nodeIds(affNodes) + ", owners=" + F.nodeIds(owners) + ", topFut=" + topFut + ", locNode=" + g.cluster().localNode() + ']');
                            } else
                                match = true;
                        } else {
                            if (i % 50 == 0)
                                LT.warn(log(), "Waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", started=" + dht.context().started() + ", p=" + p + ", readVer=" + readyVer + ", locNode=" + g.cluster().localNode() + ']');
                        }
                        if (!match) {
                            if (i == 0)
                                start = System.currentTimeMillis();
                            if (System.currentTimeMillis() - start > timeout) {
                                U.dumpThreads(log);
                                if (printPartState)
                                    printPartitionState(c);
                                throw new IgniteException("Timeout of waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", p=" + p + ", readVer=" + readyVer + ", locNode=" + g.cluster().localNode() + ']');
                            }
                            // Busy wait.
                            Thread.sleep(20);
                            continue;
                        }
                        if (i > 0)
                            log().warning("Finished waiting for topology map update [igniteInstanceName=" + g.name() + ", p=" + p + ", duration=" + (System.currentTimeMillis() - start) + "ms]");
                        break;
                    }
                }
                if (waitNode2PartUpdate) {
                    long start = System.currentTimeMillis();
                    boolean failed = true;
                    while (failed) {
                        failed = false;
                        for (GridDhtPartitionMap pMap : top.partitionMap(true).values()) {
                            if (failed)
                                break;
                            for (Map.Entry entry : pMap.entrySet()) {
                                if (System.currentTimeMillis() - start > timeout) {
                                    U.dumpThreads(log);
                                    throw new IgniteException("Timeout of waiting for partition state update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", locNode=" + g.cluster().localNode() + ']');
                                }
                                if (entry.getValue() != OWNING) {
                                    LT.warn(log(), "Waiting for correct partition state part=" + entry.getKey() + ", should be OWNING [state=" + entry.getValue() + "], node=" + g.name() + ", cache=" + c.getName());
                                    // Busy wait.
                                    Thread.sleep(200);
                                    failed = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    log.info("awaitPartitionMapExchange finished");
}
Also used : GridDhtPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionTopology) GridDhtTopologyFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) GridDhtLocalPartition(org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) Map(java.util.Map) GridDhtPartitionFullMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionFullMap) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) TreeMap(java.util.TreeMap) IgniteDhtDemandedPartitionsMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.IgniteDhtDemandedPartitionsMap)

Aggregations

AffinityFunction (org.apache.ignite.cache.affinity.AffinityFunction)40 Test (org.junit.Test)16 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)14 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)13 ArrayList (java.util.ArrayList)12 List (java.util.List)11 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)10 AffinityFunctionContext (org.apache.ignite.cache.affinity.AffinityFunctionContext)9 GridAffinityFunctionContextImpl (org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl)9 Map (java.util.Map)7 Ignite (org.apache.ignite.Ignite)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 LinkedList (java.util.LinkedList)4 Random (java.util.Random)4 UUID (java.util.UUID)4 IgniteKernal (org.apache.ignite.internal.IgniteKernal)4 HashSet (java.util.HashSet)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3