Search in sources :

Example 6 with GridDhtAssignmentFetchFuture

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

the class CacheAffinitySharedManager method initCoordinatorCaches.

/**
 * @param fut Exchange future.
 * @param newAff {@code True} if there are no older nodes with affinity info available.
 * @return Future completed when caches initialization is done.
 * @throws IgniteCheckedException If failed.
 */
public IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut, final boolean newAff) throws IgniteCheckedException {
    boolean locJoin = fut.firstEvent().eventNode().isLocal();
    if (!locJoin)
        return null;
    final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = Collections.synchronizedList(new ArrayList<>());
    final AffinityTopologyVersion topVer = fut.initialVersion();
    forAllRegisteredCacheGroups(new IgniteInClosureX<CacheGroupDescriptor>() {

        @Override
        public void applyx(CacheGroupDescriptor desc) throws IgniteCheckedException {
            CacheGroupHolder grpHolder = getOrCreateGroupHolder(topVer, desc);
            if (grpHolder.affinity().idealAssignmentRaw() != null)
                return;
            // Need initialize holders and affinity if this node became coordinator during this exchange.
            int grpId = desc.groupId();
            CacheGroupContext grp = cctx.cache().cacheGroup(grpId);
            if (grp == null) {
                grpHolder = CacheGroupNoAffOrFilteredHolder.create(cctx, desc, topVer, null);
                final GridAffinityAssignmentCache aff = grpHolder.affinity();
                if (newAff) {
                    if (!aff.lastVersion().equals(topVer))
                        calculateAndInit(fut.events(), aff, topVer);
                    grpHolder.topology(fut.context().events().discoveryCache()).beforeExchange(fut, true, false);
                } else {
                    List<GridDhtPartitionsExchangeFuture> exchFuts = cctx.exchange().exchangeFutures();
                    int idx = exchFuts.indexOf(fut);
                    assert idx >= 0 && idx < exchFuts.size() - 1 : "Invalid exchange futures state [cur=" + idx + ", total=" + exchFuts.size() + ']';
                    GridDhtPartitionsExchangeFuture futureToFetchAffinity = null;
                    for (int i = idx + 1; i < exchFuts.size(); i++) {
                        GridDhtPartitionsExchangeFuture prev = exchFuts.get(i);
                        assert prev.isDone() && prev.topologyVersion().compareTo(topVer) < 0;
                        if (prev.isMerged())
                            continue;
                        futureToFetchAffinity = prev;
                        break;
                    }
                    if (futureToFetchAffinity == null)
                        throw new IgniteCheckedException("Failed to find completed exchange future to fetch affinity.");
                    if (log.isDebugEnabled()) {
                        log.debug("Need initialize affinity on coordinator [" + "cacheGrp=" + desc.cacheOrGroupName() + "prevAff=" + futureToFetchAffinity.topologyVersion() + ']');
                    }
                    GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc.groupId(), futureToFetchAffinity.topologyVersion(), futureToFetchAffinity.events().discoveryCache());
                    fetchFut.init(false);
                    final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
                    final GridDhtPartitionsExchangeFuture futureToFetchAffinity0 = futureToFetchAffinity;
                    fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {

                        @Override
                        public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
                            fetchAffinity(futureToFetchAffinity0.topologyVersion(), futureToFetchAffinity0.events(), futureToFetchAffinity0.events().discoveryCache(), aff, (GridDhtAssignmentFetchFuture) fetchFut);
                            aff.calculate(topVer, fut.events(), fut.events().discoveryCache());
                            affFut.onDone(topVer);
                            cctx.exchange().exchangerUpdateHeartbeat();
                        }
                    });
                    futs.add(affFut);
                }
            } else {
                grpHolder = new CacheGroupAffNodeHolder(grp);
                if (newAff) {
                    GridAffinityAssignmentCache aff = grpHolder.affinity();
                    if (!aff.lastVersion().equals(topVer))
                        calculateAndInit(fut.events(), aff, topVer);
                    grpHolder.topology(fut.context().events().discoveryCache()).beforeExchange(fut, true, false);
                }
            }
            grpHolders.put(grpHolder.groupId(), grpHolder);
            cctx.exchange().exchangerUpdateHeartbeat();
            fut.timeBag().finishLocalStage("Coordinator affinity cache init " + "[grp=" + desc.cacheOrGroupName() + "]");
        }
    });
    if (!futs.isEmpty()) {
        GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
        for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
        affFut.markInitialized();
        return affFut;
    }
    return null;
}
Also used : GridDhtPartitionsExchangeFuture(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteInClosureX(org.apache.ignite.internal.util.lang.IgniteInClosureX) GridCompoundFuture(org.apache.ignite.internal.util.future.GridCompoundFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAffinityAssignmentCache(org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) GridDhtAssignmentFetchFuture(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse) GridLongList(org.apache.ignite.internal.util.GridLongList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

GridDhtAssignmentFetchFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 GridAffinityAssignmentCache (org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache)3 GridDhtAffinityAssignmentResponse (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)3 GridDhtPartitionsExchangeFuture (org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture)3 GridCompoundFuture (org.apache.ignite.internal.util.future.GridCompoundFuture)3 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)3 IgniteInClosureX (org.apache.ignite.internal.util.lang.IgniteInClosureX)3 UUID (java.util.UUID)2 GridLongList (org.apache.ignite.internal.util.GridLongList)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1