Search in sources :

Example 11 with GridDhtPartitionsExchangeFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture in project ignite by apache.

the class GridCachePartitionExchangeManager method onExchangeDone.

/**
     * @param exchFut Exchange.
     * @param err Error.
     */
public void onExchangeDone(GridDhtPartitionsExchangeFuture exchFut, @Nullable Throwable err) {
    AffinityTopologyVersion topVer = exchFut.topologyVersion();
    if (log.isDebugEnabled())
        log.debug("Exchange done [topVer=" + topVer + ", fut=" + exchFut + ", err=" + err + ']');
    IgniteProductVersion minVer = cctx.localNode().version();
    IgniteProductVersion maxVer = cctx.localNode().version();
    if (err == null) {
        if (!F.isEmpty(exchFut.discoveryEvent().topologyNodes())) {
            for (ClusterNode node : exchFut.discoveryEvent().topologyNodes()) {
                IgniteProductVersion ver = node.version();
                if (ver.compareTo(minVer) < 0)
                    minVer = ver;
                if (ver.compareTo(maxVer) > 0)
                    maxVer = ver;
            }
        }
    }
    nodeVers.put(topVer, new IgnitePair<>(minVer, maxVer));
    AffinityTopologyVersion histVer = new AffinityTopologyVersion(topVer.topologyVersion() - 10, 0);
    for (AffinityTopologyVersion oldVer : nodeVers.headMap(histVer).keySet()) nodeVers.remove(oldVer);
    if (err == null) {
        while (true) {
            AffinityTopologyVersion readyVer = readyTopVer.get();
            if (readyVer.compareTo(topVer) >= 0)
                break;
            if (readyTopVer.compareAndSet(readyVer, topVer))
                break;
        }
        for (Map.Entry<AffinityTopologyVersion, AffinityReadyFuture> entry : readyFuts.entrySet()) {
            if (entry.getKey().compareTo(topVer) <= 0) {
                if (log.isDebugEnabled())
                    log.debug("Completing created topology ready future " + "[ver=" + topVer + ", fut=" + entry.getValue() + ']');
                entry.getValue().onDone(topVer);
            }
        }
    } else {
        for (Map.Entry<AffinityTopologyVersion, AffinityReadyFuture> entry : readyFuts.entrySet()) {
            if (entry.getKey().compareTo(topVer) <= 0) {
                if (log.isDebugEnabled())
                    log.debug("Completing created topology ready future with error " + "[ver=" + topVer + ", fut=" + entry.getValue() + ']');
                entry.getValue().onDone(err);
            }
        }
    }
    ExchangeFutureSet exchFuts0 = exchFuts;
    if (exchFuts0 != null) {
        int skipped = 0;
        for (GridDhtPartitionsExchangeFuture fut : exchFuts0.values()) {
            if (exchFut.exchangeId().topologyVersion().compareTo(fut.exchangeId().topologyVersion()) < 0)
                continue;
            skipped++;
            if (skipped > 10)
                fut.cleanUp();
        }
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) Map(java.util.Map) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridDhtPartitionFullMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionFullMap) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap)

Example 12 with GridDhtPartitionsExchangeFuture

use of org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture 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)

Aggregations

GridDhtPartitionsExchangeFuture (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture)12 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 ClusterNode (org.apache.ignite.cluster.ClusterNode)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteClientDisconnectedCheckedException (org.apache.ignite.internal.IgniteClientDisconnectedCheckedException)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 NavigableMap (java.util.NavigableMap)2 TreeMap (java.util.TreeMap)2 UUID (java.util.UUID)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 DiscoveryEvent (org.apache.ignite.events.DiscoveryEvent)2 IgniteFutureTimeoutCheckedException (org.apache.ignite.internal.IgniteFutureTimeoutCheckedException)2 IgniteNeedReconnectException (org.apache.ignite.internal.IgniteNeedReconnectException)2 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)2