use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.
the class CacheMetricsImpl method getEntriesStat.
/**
* Calculates entries count/partitions count metrics using one iteration over local partitions for all metrics
*/
public EntriesStatMetrics getEntriesStat() {
int owningPartCnt = 0;
int movingPartCnt = 0;
long offHeapEntriesCnt = 0L;
long offHeapPrimaryEntriesCnt = 0L;
long offHeapBackupEntriesCnt = 0L;
long heapEntriesCnt = 0L;
int size = 0;
long sizeLong = 0L;
boolean isEmpty;
try {
AffinityTopologyVersion topVer = cctx.affinity().affinityTopologyVersion();
if (AffinityTopologyVersion.NONE.equals(topVer))
return unknownEntriesStat();
final GridCacheAdapter<?, ?> cache = cctx.cache();
if (cache != null) {
offHeapEntriesCnt = cache.offHeapEntriesCount();
size = cache.localSize(null);
sizeLong = cache.localSizeLong(null);
}
if (cctx.isLocal()) {
if (cache != null) {
offHeapPrimaryEntriesCnt = offHeapEntriesCnt;
heapEntriesCnt = cache.sizeLong();
}
} else {
IntSet primaries = ImmutableIntSet.wrap(cctx.affinity().primaryPartitions(cctx.localNodeId(), topVer));
IntSet backups = ImmutableIntSet.wrap(cctx.affinity().backupPartitions(cctx.localNodeId(), topVer));
if (cctx.isNear() && cache != null)
heapEntriesCnt = cache.nearSize();
for (GridDhtLocalPartition part : cctx.topology().currentLocalPartitions()) {
// Partitions count.
GridDhtPartitionState partState = part.state();
if (partState == GridDhtPartitionState.OWNING)
owningPartCnt++;
if (partState == GridDhtPartitionState.MOVING)
movingPartCnt++;
// Offheap entries count
if (cache == null)
continue;
long cacheSize = part.dataStore().cacheSize(cctx.cacheId());
if (primaries.contains(part.id()))
offHeapPrimaryEntriesCnt += cacheSize;
else if (backups.contains(part.id()))
offHeapBackupEntriesCnt += cacheSize;
heapEntriesCnt += part.publicSize(cctx.cacheId());
}
}
} catch (Exception e) {
return unknownEntriesStat();
}
isEmpty = (offHeapEntriesCnt == 0);
EntriesStatMetrics stat = new EntriesStatMetrics();
stat.offHeapEntriesCount(offHeapEntriesCnt);
stat.offHeapPrimaryEntriesCount(offHeapPrimaryEntriesCnt);
stat.offHeapBackupEntriesCount(offHeapBackupEntriesCnt);
stat.heapEntriesCount(heapEntriesCnt);
stat.size(size);
stat.cacheSize(sizeLong);
stat.keySize(size);
stat.isEmpty(isEmpty);
stat.totalPartitionsCount(owningPartCnt + movingPartCnt);
stat.rebalancingPartitionsCount(movingPartCnt);
return stat;
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.
the class GridDhtPartitionsExchangeFuture method assignPartitionStates.
/**
* Collects and determines new owners of partitions for all nodes for given {@code top}.
*
* @param top Topology to assign.
* @param resetOwners True if need to reset partition state considering of counter, false otherwise.
* @return Partitions supply info list.
*/
private List<SupplyPartitionInfo> assignPartitionStates(GridDhtPartitionTopology top, boolean resetOwners) {
Map<Integer, CounterWithNodes> maxCntrs = new HashMap<>();
Map<Integer, TreeSet<Long>> varCntrs = new HashMap<>();
for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : msgs.entrySet()) {
CachePartitionPartialCountersMap nodeCntrs = e.getValue().partitionUpdateCounters(top.groupId(), top.partitions());
assert nodeCntrs != null;
for (int i = 0; i < nodeCntrs.size(); i++) {
int p = nodeCntrs.partitionAt(i);
UUID remoteNodeId = e.getKey();
GridDhtPartitionState state = top.partitionState(remoteNodeId, p);
if (state != GridDhtPartitionState.OWNING && state != GridDhtPartitionState.MOVING)
continue;
long cntr = state == GridDhtPartitionState.MOVING ? nodeCntrs.initialUpdateCounterAt(i) : nodeCntrs.updateCounterAt(i);
varCntrs.computeIfAbsent(p, key -> new TreeSet<>()).add(cntr);
if (state != GridDhtPartitionState.OWNING)
continue;
CounterWithNodes maxCntr = maxCntrs.get(p);
if (maxCntr == null || cntr > maxCntr.cnt)
maxCntrs.put(p, new CounterWithNodes(cntr, e.getValue().partitionSizes(top.groupId()).get(p), remoteNodeId));
else if (cntr == maxCntr.cnt)
maxCntr.nodes.add(remoteNodeId);
}
}
// Also must process counters from the local node.
for (GridDhtLocalPartition part : top.currentLocalPartitions()) {
GridDhtPartitionState state = top.partitionState(cctx.localNodeId(), part.id());
if (state != GridDhtPartitionState.OWNING && state != GridDhtPartitionState.MOVING)
continue;
final long cntr = state == GridDhtPartitionState.MOVING ? part.initialUpdateCounter() : part.updateCounter();
varCntrs.computeIfAbsent(part.id(), key -> new TreeSet<>()).add(cntr);
if (state != GridDhtPartitionState.OWNING)
continue;
CounterWithNodes maxCntr = maxCntrs.get(part.id());
if (maxCntr == null && cntr == 0) {
CounterWithNodes cntrObj = new CounterWithNodes(0, 0L, cctx.localNodeId());
for (UUID nodeId : msgs.keySet()) {
if (top.partitionState(nodeId, part.id()) == GridDhtPartitionState.OWNING)
cntrObj.nodes.add(nodeId);
}
maxCntrs.put(part.id(), cntrObj);
} else if (maxCntr == null || cntr > maxCntr.cnt)
maxCntrs.put(part.id(), new CounterWithNodes(cntr, part.fullSize(), cctx.localNodeId()));
else if (cntr == maxCntr.cnt)
maxCntr.nodes.add(cctx.localNodeId());
}
Set<Integer> haveHistory = new HashSet<>();
List<SupplyPartitionInfo> list = assignHistoricalSuppliers(top, maxCntrs, varCntrs, haveHistory);
if (resetOwners)
resetOwnersByCounter(top, maxCntrs, haveHistory);
return list;
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.
the class GridCacheQueryManager method scanIterator.
/**
* @param qry Query.
* @param transformer Transformer.
* @param locNode Local node.
* @return Full-scan row iterator.
* @throws IgniteCheckedException If failed to get iterator.
*/
@SuppressWarnings({ "unchecked" })
private GridCloseableIterator scanIterator(final GridCacheQueryAdapter<?> qry, IgniteClosure transformer, boolean locNode) throws IgniteCheckedException {
assert !cctx.mvccEnabled() || qry.mvccSnapshot() != null;
final IgniteBiPredicate<K, V> keyValFilter = qry.scanFilter();
final InternalScanFilter<K, V> intFilter = keyValFilter != null ? new InternalScanFilter<>(keyValFilter) : null;
try {
if (keyValFilter instanceof PlatformCacheEntryFilter)
((PlatformCacheEntryFilter) keyValFilter).cacheContext(cctx);
else
injectResources(keyValFilter);
Integer part = cctx.isLocal() ? null : qry.partition();
if (part != null && (part < 0 || part >= cctx.affinity().partitions()))
return new GridEmptyCloseableIterator() {
@Override
public void close() throws IgniteCheckedException {
if (intFilter != null)
intFilter.close();
super.close();
}
};
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 locPart0 != null && locPart0.state() == LOST ? new CacheInvalidStateException("Failed to execute scan query because cache partition has been " + "lost [cacheName=" + cctx.name() + ", part=" + part + "]") : new GridDhtUnreservedPartitionException(part, cctx.affinity().affinityTopologyVersion(), "Partition can not be reserved");
}
locPart = locPart0;
it = cctx.offheap().cachePartitionIterator(cctx.cacheId(), part, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
} else {
locPart = null;
if (!cctx.isLocal()) {
final GridDhtCacheAdapter dht = cctx.isNear() ? cctx.near().dht() : cctx.dht();
Set<Integer> lostParts = dht.topology().lostPartitions();
if (!lostParts.isEmpty()) {
throw new CacheInvalidStateException("Failed to execute scan query because cache partition " + "has been lost [cacheName=" + cctx.name() + ", part=" + lostParts.iterator().next() + "]");
}
}
it = cctx.offheap().cacheIterator(cctx.cacheId(), true, backups, topVer, qry.mvccSnapshot(), qry.isDataPageScanEnabled());
}
ScanQueryIterator iter = new ScanQueryIterator(it, qry, topVer, locPart, SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteBiPredicate.class, keyValFilter), SecurityUtils.sandboxedProxy(cctx.kernalContext(), IgniteClosure.class, transformer), locNode, locNode ? locIters : null, cctx, log);
if (locNode) {
ScanQueryIterator old = locIters.addx(iter);
assert old == null;
}
return iter;
} catch (IgniteCheckedException | RuntimeException e) {
if (intFilter != null)
intFilter.close();
throw e;
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.
the class GridCacheContext method checkAndReservePartition.
/**
* @param part Partition.
* @param topVer Topology version.
* @return {@code True} if partition is available locally.
*/
private boolean checkAndReservePartition(int part, AffinityTopologyVersion topVer) {
assert affinityNode();
GridDhtPartitionTopology top = topology();
if (isReplicated() && !group().persistenceEnabled()) {
boolean rebFinished = top.rebalanceFinished(topVer);
if (rebFinished)
return true;
GridDhtLocalPartition locPart = top.localPartition(part, topVer, false, false);
// No need to reserve a partition for REPLICATED cache because this partition cannot be evicted.
return locPart != null && locPart.state() == OWNING;
} else {
GridDhtLocalPartition locPart = top.localPartition(part, topVer, false, false);
if (locPart != null && locPart.reserve()) {
boolean canRead = true;
try {
canRead = locPart.state() == OWNING;
return canRead;
} finally {
if (!canRead)
locPart.release();
}
} else
return false;
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtLocalPartition in project ignite by apache.
the class GridCacheContext method releaseForFastLocalGet.
/**
* Releases the partition that was reserved by a call to
* {@link #reserveForFastLocalGet(int, AffinityTopologyVersion)}.
*
* @param part Partition to release.
* @param topVer Topology version.
*/
public void releaseForFastLocalGet(int part, AffinityTopologyVersion topVer) {
assert affinityNode();
if (!isReplicated() || group().persistenceEnabled()) {
GridDhtLocalPartition locPart = topology().localPartition(part, topVer, false);
assert locPart != null && locPart.state() == OWNING : "partition evicted after reserveForFastLocalGet " + "[part=" + part + ", locPart=" + locPart + ", topVer=" + topVer + ']';
locPart.release();
}
}
Aggregations