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