Search in sources :

Example 6 with GridDhtAffinityAssignmentResponse

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

Example 7 with GridDhtAffinityAssignmentResponse

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

the class CacheAffinitySharedManager method fetchAffinity.

/**
 * @param topVer Topology version.
 * @param events Discovery events.
 * @param discoCache Discovery data cache.
 * @param affCache Affinity.
 * @param fetchFut Affinity fetch future.
 * @return Affinity assignment response.
 * @throws IgniteCheckedException If failed.
 */
private GridDhtAffinityAssignmentResponse fetchAffinity(AffinityTopologyVersion topVer, @Nullable ExchangeDiscoveryEvents events, DiscoCache discoCache, GridAffinityAssignmentCache affCache, GridDhtAssignmentFetchFuture fetchFut) throws IgniteCheckedException {
    assert affCache != null;
    GridDhtAffinityAssignmentResponse res = fetchFut.get();
    if (res == null) {
        List<List<ClusterNode>> aff = affCache.calculate(topVer, events, discoCache).assignment();
        affCache.initialize(topVer, aff);
    } else {
        List<List<ClusterNode>> idealAff = res.idealAffinityAssignment(discoCache);
        if (idealAff != null)
            affCache.idealAssignment(topVer, idealAff);
        else {
            assert !affCache.centralizedAffinityFunction();
            affCache.calculate(topVer, events, discoCache);
        }
        List<List<ClusterNode>> aff = res.affinityAssignment(discoCache);
        assert aff != null : res;
        affCache.initialize(topVer, aff);
    }
    return res;
}
Also used : 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)

Example 8 with GridDhtAffinityAssignmentResponse

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

the class CacheGroupContext method processAffinityAssignmentRequest0.

/**
 * @param nodeId Node ID.
 * @param req Request.
 */
private void processAffinityAssignmentRequest0(UUID nodeId, final GridDhtAffinityAssignmentRequest req) {
    AffinityTopologyVersion topVer = req.topologyVersion();
    if (log.isDebugEnabled())
        log.debug("Affinity is ready for topology version, will send response [topVer=" + topVer + ", node=" + nodeId + ']');
    AffinityAssignment assignment;
    GridDhtAffinityAssignmentResponse res;
    try {
        assignment = aff.cachedAffinity(topVer);
        res = new GridDhtAffinityAssignmentResponse(req.futureId(), grpId, topVer, assignment.assignment());
        if (aff.centralizedAffinityFunction()) {
            assert assignment.idealAssignment() != null;
            res.idealAffinityAssignment(assignment.idealAssignment());
        }
        if (req.sendPartitionsState())
            res.partitionMap(top.partitionMap(true));
    } catch (IllegalStateException err) {
        res = new GridDhtAffinityAssignmentResponse(req.futureId(), grpId, topVer, Collections.emptyList());
        res.affinityAssignmentsError(new IgniteCheckedException("Failed to prepare the required affinity assignment " + "[nodeId=" + nodeId + ", topVer=" + topVer + ']', err));
    }
    try {
        ctx.io().send(nodeId, res, AFFINITY_POOL);
    } catch (IgniteCheckedException e) {
        U.error(log, "Failed to send affinity assignment response to remote node [node=" + nodeId + ']', e);
    }
}
Also used : AffinityAssignment(org.apache.ignite.internal.processors.affinity.AffinityAssignment) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtAffinityAssignmentResponse(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)

Aggregations

GridDhtAffinityAssignmentResponse (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAffinityAssignmentResponse)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 GridLongList (org.apache.ignite.internal.util.GridLongList)4 AffinityAssignment (org.apache.ignite.internal.processors.affinity.AffinityAssignment)3 GridAffinityAssignmentCache (org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache)3 GridDhtAssignmentFetchFuture (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtAssignmentFetchFuture)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 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