use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.
the class GridCacheQueryManager method scanIterator.
/**
* @param qry Query.
* @param locNode Local node.
* @return Full-scan row iterator.
* @throws IgniteCheckedException If failed to get iterator.
*/
@SuppressWarnings({ "unchecked" })
private GridCloseableIterator<IgniteBiTuple<K, V>> scanIterator(final GridCacheQueryAdapter<?> qry, boolean locNode) throws IgniteCheckedException {
final IgniteBiPredicate<K, V> keyValFilter = qry.scanFilter();
try {
injectResources(keyValFilter);
Integer part = qry.partition();
if (cctx.isLocal())
part = null;
if (part != null && (part < 0 || part >= cctx.affinity().partitions()))
return new GridEmptyCloseableIterator<>();
final ExpiryPolicy plc = cctx.expiry();
AffinityTopologyVersion topVer = GridQueryProcessor.getRequestAffinityTopologyVersion();
if (topVer == null)
topVer = cctx.affinity().affinityTopologyVersion();
final boolean backups = qry.includeBackups() || cctx.isReplicated();
final GridDhtLocalPartition locPart;
final GridIterator<CacheDataRow> it;
if (part != null) {
final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
GridDhtLocalPartition locPart0 = dht.topology().localPartition(part, topVer, false);
if (locPart0 == null || locPart0.state() != OWNING || !locPart0.reserve())
throw new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
if (locPart0.state() != OWNING) {
locPart0.release();
throw new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
}
locPart = locPart0;
it = cctx.offheap().iterator(part);
} else {
locPart = null;
it = cctx.offheap().iterator(true, backups, topVer);
}
return new PeekValueExpiryAwareIterator(it, plc, topVer, keyValFilter, qry.keepBinary(), locNode) {
@Override
protected void onClose() {
super.onClose();
if (locPart != null)
locPart.release();
closeScanFilter(keyValFilter);
}
};
} catch (IgniteCheckedException | RuntimeException e) {
closeScanFilter(keyValFilter);
throw e;
}
}
use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.
the class GridClusterStateProcessor method start.
/** {@inheritDoc} */
@Override
public void start(boolean activeOnStart) throws IgniteCheckedException {
super.start(activeOnStart);
globalState = activeOnStart ? ACTIVE : INACTIVE;
cacheProc = ctx.cache();
sharedCtx = cacheProc.context();
sharedCtx.io().addHandler(0, GridChangeGlobalStateMessageResponse.class, new CI2<UUID, GridChangeGlobalStateMessageResponse>() {
@Override
public void apply(UUID nodeId, GridChangeGlobalStateMessageResponse msg) {
processChangeGlobalStateResponse(nodeId, msg);
}
});
ctx.discovery().setCustomEventListener(ChangeGlobalStateMessage.class, new CustomEventListener<ChangeGlobalStateMessage>() {
@Override
public void onCustomEvent(AffinityTopologyVersion topVer, ClusterNode snd, ChangeGlobalStateMessage msg) {
assert topVer != null;
assert snd != null;
assert msg != null;
boolean activate = msg.activate();
ChangeGlobalStateContext actx = lastCgsCtx;
if (actx != null && globalState == TRANSITION) {
GridChangeGlobalStateFuture f = cgsLocFut.get();
if (log.isDebugEnabled())
log.debug("Concurrent " + prettyStr(activate) + " [id=" + ctx.localNodeId() + " topVer=" + topVer + " actx=" + actx + ", msg=" + msg + "]");
if (f != null && f.requestId.equals(msg.requestId()))
f.onDone(new IgniteCheckedException("Concurrent change state, now in progress=" + (activate) + ", initiatingNodeId=" + actx.initiatingNodeId + ", you try=" + (prettyStr(activate)) + ", locNodeId=" + ctx.localNodeId()));
msg.concurrentChangeState();
} else {
if (log.isInfoEnabled())
log.info("Create " + prettyStr(activate) + " context [id=" + ctx.localNodeId() + " topVer=" + topVer + ", reqId=" + msg.requestId() + ", initiatingNodeId=" + msg.initiatorNodeId() + "]");
lastCgsCtx = new ChangeGlobalStateContext(msg.requestId(), msg.initiatorNodeId(), msg.getDynamicCacheChangeBatch(), msg.activate());
globalState = TRANSITION;
}
}
});
ctx.event().addLocalEventListener(lsr, EVT_NODE_LEFT, EVT_NODE_FAILED);
}
use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.
the class CacheAffinitySharedManager method initAffinityOnNodeJoin.
/**
* @param fut Exchange future.
* @param aff Affinity.
* @param rebalanceInfo Rebalance information.
* @param latePrimary If {@code true} delays primary assignment if it is not owner.
* @param affCache Already calculated assignments (to reduce data stored in history).
* @throws IgniteCheckedException If failed.
*/
private void initAffinityOnNodeJoin(GridDhtPartitionsExchangeFuture fut, GridAffinityAssignmentCache aff, WaitRebalanceInfo rebalanceInfo, boolean latePrimary, Map<Object, List<List<ClusterNode>>> affCache) throws IgniteCheckedException {
assert lateAffAssign;
AffinityTopologyVersion topVer = fut.topologyVersion();
AffinityTopologyVersion affTopVer = aff.lastVersion();
assert affTopVer.topologyVersion() > 0 : "Affinity is not initialized [cache=" + aff.cacheName() + ", topVer=" + affTopVer + ", node=" + cctx.localNodeId() + ']';
List<List<ClusterNode>> curAff = aff.assignments(affTopVer);
assert aff.idealAssignment() != null : "Previous assignment is not available.";
List<List<ClusterNode>> idealAssignment = aff.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
List<List<ClusterNode>> newAssignment = null;
if (latePrimary) {
for (int p = 0; p < idealAssignment.size(); p++) {
List<ClusterNode> newNodes = idealAssignment.get(p);
List<ClusterNode> curNodes = curAff.get(p);
ClusterNode curPrimary = curNodes.size() > 0 ? curNodes.get(0) : null;
ClusterNode newPrimary = newNodes.size() > 0 ? newNodes.get(0) : null;
if (curPrimary != null && newPrimary != null && !curPrimary.equals(newPrimary)) {
assert cctx.discovery().node(topVer, curPrimary.id()) != null : curPrimary;
List<ClusterNode> nodes0 = latePrimaryAssignment(aff, p, curPrimary, newNodes, rebalanceInfo);
if (newAssignment == null)
newAssignment = new ArrayList<>(idealAssignment);
newAssignment.set(p, nodes0);
}
}
}
if (newAssignment == null)
newAssignment = idealAssignment;
aff.initialize(fut.topologyVersion(), cachedAssignment(aff, newAssignment, affCache));
}
use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.
the class CacheAffinitySharedManager method fetchAffinity.
/**
* @param fut Exchange future.
* @param affCache Affinity.
* @param fetchFut Affinity fetch future.
* @throws IgniteCheckedException If failed.
*/
private void fetchAffinity(GridDhtPartitionsExchangeFuture fut, GridAffinityAssignmentCache affCache, GridDhtAssignmentFetchFuture fetchFut) throws IgniteCheckedException {
assert affCache != null;
AffinityTopologyVersion topVer = fut.topologyVersion();
GridDhtAffinityAssignmentResponse res = fetchFut.get();
if (res == null) {
List<List<ClusterNode>> aff = affCache.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
affCache.initialize(topVer, aff);
} else {
List<List<ClusterNode>> idealAff = res.idealAffinityAssignment(cctx.discovery());
if (idealAff != null)
affCache.idealAssignment(idealAff);
else {
assert !affCache.centralizedAffinityFunction() || !lateAffAssign;
affCache.calculate(topVer, fut.discoveryEvent(), fut.discoCache());
}
List<List<ClusterNode>> aff = res.affinityAssignment(cctx.discovery());
assert aff != null : res;
affCache.initialize(topVer, aff);
}
}
use of org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion in project ignite by apache.
the class CacheAffinitySharedManager method initCoordinatorCaches.
/**
* @param fut Exchange future.
* @throws IgniteCheckedException If failed.
* @return Future completed when caches initialization is done.
*/
private IgniteInternalFuture<?> initCoordinatorCaches(final GridDhtPartitionsExchangeFuture fut) throws IgniteCheckedException {
final List<IgniteInternalFuture<AffinityTopologyVersion>> futs = new ArrayList<>();
forAllRegisteredCaches(new IgniteInClosureX<DynamicCacheDescriptor>() {
@Override
public void applyx(DynamicCacheDescriptor desc) throws IgniteCheckedException {
CacheHolder cache = caches.get(desc.cacheId());
if (cache != null) {
if (cache.client())
cache.affinity().calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
return;
}
final Integer cacheId = desc.cacheId();
GridCacheContext cacheCtx = cctx.cacheContext(cacheId);
if (cacheCtx == null) {
cctx.io().addHandler(desc.cacheId(), GridDhtAffinityAssignmentResponse.class, new IgniteBiInClosure<UUID, GridDhtAffinityAssignmentResponse>() {
@Override
public void apply(UUID nodeId, GridDhtAffinityAssignmentResponse res) {
processAffinityAssignmentResponse(nodeId, res);
}
});
cache = CacheHolder2.create(cctx, desc, fut, null);
final GridAffinityAssignmentCache aff = cache.affinity();
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() + ']';
final GridDhtPartitionsExchangeFuture prev = exchFuts.get(idx + 1);
if (log.isDebugEnabled()) {
log.debug("Need initialize affinity on coordinator [" + "cache=" + desc.cacheConfiguration().getName() + "prevAff=" + prev.topologyVersion() + ']');
}
assert prev.topologyVersion().compareTo(fut.topologyVersion()) < 0 : prev;
GridDhtAssignmentFetchFuture fetchFut = new GridDhtAssignmentFetchFuture(cctx, desc, prev.topologyVersion(), prev.discoCache());
fetchFut.init();
final GridFutureAdapter<AffinityTopologyVersion> affFut = new GridFutureAdapter<>();
fetchFut.listen(new IgniteInClosureX<IgniteInternalFuture<GridDhtAffinityAssignmentResponse>>() {
@Override
public void applyx(IgniteInternalFuture<GridDhtAffinityAssignmentResponse> fetchFut) throws IgniteCheckedException {
fetchAffinity(prev, aff, (GridDhtAssignmentFetchFuture) fetchFut);
aff.calculate(fut.topologyVersion(), fut.discoveryEvent(), fut.discoCache());
affFut.onDone(fut.topologyVersion());
}
});
futs.add(affFut);
} else
cache = new CacheHolder1(cacheCtx, null);
CacheHolder old = caches.put(cache.cacheId(), cache);
assert old == null : old;
}
});
if (!futs.isEmpty()) {
GridCompoundFuture<AffinityTopologyVersion, ?> affFut = new GridCompoundFuture<>();
for (IgniteInternalFuture<AffinityTopologyVersion> f : futs) affFut.add(f);
affFut.markInitialized();
return affFut;
}
return null;
}
Aggregations