use of org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionsReservation in project ignite by apache.
the class GridMapQueryExecutor method reservePartitions.
/**
* @param cacheIds Cache IDs.
* @param topVer Topology version.
* @param explicitParts Explicit partitions list.
* @param reserved Reserved list.
* @return {@code true} If all the needed partitions successfully reserved.
* @throws IgniteCheckedException If failed.
*/
private boolean reservePartitions(@Nullable List<Integer> cacheIds, AffinityTopologyVersion topVer, final int[] explicitParts, List<GridReservable> reserved) throws IgniteCheckedException {
assert topVer != null;
if (F.isEmpty(cacheIds))
return true;
Collection<Integer> partIds = wrap(explicitParts);
for (int i = 0; i < cacheIds.size(); i++) {
GridCacheContext<?, ?> cctx = ctx.cache().context().cacheContext(cacheIds.get(i));
if (// Cache was not found, probably was not deployed yet.
cctx == null)
return false;
if (cctx.isLocal() || !cctx.rebalanceEnabled())
continue;
// For replicated cache topology version does not make sense.
final T2<String, AffinityTopologyVersion> grpKey = new T2<>(cctx.name(), cctx.isReplicated() ? null : topVer);
GridReservable r = reservations.get(grpKey);
if (explicitParts == null && r != null) {
// Try to reserve group partition if any and no explicits.
if (r != ReplicatedReservation.INSTANCE) {
if (!r.reserve())
// We need explicit partitions here -> retry.
return false;
reserved.add(r);
}
} else {
// Try to reserve partitions one by one.
int partsCnt = cctx.affinity().partitions();
if (cctx.isReplicated()) {
// Check all the partitions are in owning state for replicated cache.
if (r == null) {
// Check only once.
for (int p = 0; p < partsCnt; p++) {
GridDhtLocalPartition part = partition(cctx, p);
// We don't need to reserve partitions because they will not be evicted in replicated caches.
if (part == null || part.state() != OWNING)
return false;
}
// Mark that we checked this replicated cache.
reservations.putIfAbsent(grpKey, ReplicatedReservation.INSTANCE);
}
} else {
// Reserve primary partitions for partitioned cache (if no explicit given).
if (explicitParts == null)
partIds = cctx.affinity().primaryPartitions(ctx.localNodeId(), topVer);
for (int partId : partIds) {
GridDhtLocalPartition part = partition(cctx, partId);
if (part == null || part.state() != OWNING || !part.reserve())
return false;
reserved.add(part);
// Double check that we are still in owning state and partition contents are not cleared.
if (part.state() != OWNING)
return false;
}
if (explicitParts == null) {
// We reserved all the primary partitions for cache, attempt to add group reservation.
GridDhtPartitionsReservation grp = new GridDhtPartitionsReservation(topVer, cctx, "SQL");
if (grp.register(reserved.subList(reserved.size() - partIds.size(), reserved.size()))) {
if (reservations.putIfAbsent(grpKey, grp) != null)
throw new IllegalStateException("Reservation already exists.");
grp.onPublish(new CI1<GridDhtPartitionsReservation>() {
@Override
public void apply(GridDhtPartitionsReservation r) {
reservations.remove(grpKey, r);
}
});
}
}
}
}
}
return true;
}
Aggregations