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;
}
Aggregations