use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class IgniteCacheOffheapManagerImpl method rebalanceIterator.
/**
* {@inheritDoc}
*/
@Override
public IgniteRebalanceIterator rebalanceIterator(IgniteDhtDemandedPartitionsMap parts, AffinityTopologyVersion topVer) throws IgniteCheckedException {
TreeMap<Integer, GridCloseableIterator<CacheDataRow>> iterators = new TreeMap<>();
Set<Integer> missing = new HashSet<>();
for (Integer p : parts.fullSet()) {
GridCloseableIterator<CacheDataRow> partIter = reservedIterator(p, topVer);
if (partIter == null) {
missing.add(p);
continue;
}
iterators.put(p, partIter);
}
IgniteHistoricalIterator historicalIterator = historicalIterator(parts.historicalMap(), missing);
IgniteRebalanceIterator iter = new IgniteRebalanceIteratorImpl(iterators, historicalIterator);
for (Integer p : missing) iter.setPartitionMissing(p);
return iter;
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class IgniteSnapshotManager method partitionRowIterator.
/**
* @param snpName Snapshot name.
* @param folderName The node folder name, usually it's the same as the U.maskForFileName(consistentId).
* @param grpName Cache group name.
* @param partId Partition id.
* @return Iterator over partition.
* @throws IgniteCheckedException If and error occurs.
*/
public GridCloseableIterator<CacheDataRow> partitionRowIterator(GridKernalContext ctx, String snpName, String folderName, String grpName, int partId) throws IgniteCheckedException {
File snpDir = resolveSnapshotDir(snpName);
File nodePath = new File(snpDir, databaseRelativePath(folderName));
if (!nodePath.exists())
throw new IgniteCheckedException("Consistent id directory doesn't exists: " + nodePath.getAbsolutePath());
List<File> grps = cacheDirectories(nodePath, name -> name.equals(grpName));
if (F.isEmpty(grps)) {
throw new IgniteCheckedException("The snapshot cache group not found [dir=" + snpDir.getAbsolutePath() + ", grpName=" + grpName + ']');
}
if (grps.size() > 1) {
throw new IgniteCheckedException("The snapshot cache group directory cannot be uniquely identified [dir=" + snpDir.getAbsolutePath() + ", grpName=" + grpName + ']');
}
File snpPart = getPartitionFile(new File(snapshotLocalDir(snpName), databaseRelativePath(folderName)), grps.get(0).getName(), partId);
int grpId = CU.cacheId(grpName);
FilePageStore pageStore = (FilePageStore) storeMgr.getPageStoreFactory(grpId, cctx.cache().isEncrypted(grpId)).createPageStore(getTypeByPartId(partId), snpPart::toPath, val -> {
});
GridCloseableIterator<CacheDataRow> partIter = partitionRowIterator(ctx, grpName, partId, pageStore);
return new GridCloseableIteratorAdapter<CacheDataRow>() {
/**
* {@inheritDoc}
*/
@Override
protected CacheDataRow onNext() throws IgniteCheckedException {
return partIter.nextX();
}
/**
* {@inheritDoc}
*/
@Override
protected boolean onHasNext() throws IgniteCheckedException {
return partIter.hasNextX();
}
/**
* {@inheritDoc}
*/
@Override
protected void onClose() {
U.closeQuiet(pageStore);
}
};
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridCacheQueryManager method scanQueryLocal.
/**
* Process local scan query.
*
* @param qry Query.
* @param updateStatistics Update statistics flag.
*/
@SuppressWarnings({ "unchecked" })
protected GridCloseableIterator scanQueryLocal(final GridCacheQueryAdapter qry, boolean updateStatistics) throws IgniteCheckedException {
if (!enterBusy())
throw new IllegalStateException("Failed to process query request (grid is stopping).");
final boolean statsEnabled = cctx.statisticsEnabled();
updateStatistics &= statsEnabled;
long startTime = U.currentTimeMillis();
final String namex = cctx.name();
final InternalScanFilter<K, V> intFilter = qry.scanFilter() != null ? new InternalScanFilter<>(qry.scanFilter()) : null;
try {
assert qry.type() == SCAN;
if (log.isDebugEnabled())
log.debug("Running local SCAN query: " + qry);
final String taskName = cctx.kernalContext().task().resolveTaskName(qry.taskHash());
final ClusterNode locNode = cctx.localNode();
if (cctx.events().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
cctx.gridEvents().record(new CacheQueryExecutedEvent<>(locNode, "Scan query executed.", EVT_CACHE_QUERY_EXECUTED, CacheQueryType.SCAN.name(), namex, null, null, intFilter != null ? intFilter.scanFilter() : null, null, null, securitySubjectId(cctx), taskName));
}
IgniteClosure transformer = qry.transform();
injectResources(transformer);
GridCloseableIterator it = scanIterator(qry, transformer, true);
updateStatistics = false;
return it;
} catch (Exception e) {
if (intFilter != null)
intFilter.close();
if (updateStatistics)
cctx.queries().collectMetrics(GridCacheQueryType.SCAN, namex, startTime, U.currentTimeMillis() - startTime, true);
throw e;
} finally {
leaveBusy();
}
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridCacheDistributedQueryManager method scanQueryDistributed.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "unchecked" })
@Override
public GridCloseableIterator scanQueryDistributed(final GridCacheQueryAdapter qry, Collection<ClusterNode> nodes) throws IgniteCheckedException {
assert !cctx.isLocal() : cctx.name();
assert qry.type() == GridCacheQueryType.SCAN : qry;
assert qry.mvccSnapshot() != null || !cctx.mvccEnabled();
boolean performanceStatsEnabled = cctx.kernalContext().performanceStatistics().enabled();
long startTime = performanceStatsEnabled ? System.currentTimeMillis() : 0;
long startTimeNanos = performanceStatsEnabled ? System.nanoTime() : 0;
GridCloseableIterator locIter0 = null;
for (ClusterNode node : nodes) {
if (node.isLocal()) {
locIter0 = scanQueryLocal(qry, false);
Collection<ClusterNode> rmtNodes = new ArrayList<>(nodes.size() - 1);
for (ClusterNode n : nodes) {
// Equals by reference can be used here.
if (n != node)
rmtNodes.add(n);
}
nodes = rmtNodes;
break;
}
}
final GridCloseableIterator locIter = locIter0;
final GridCacheQueryBean bean = new GridCacheQueryBean(qry, null, qry.<K, V>transform(), null);
final CacheQueryFuture fut = queryDistributed(bean, nodes);
return new GridCloseableIteratorAdapter() {
/**
*/
private Object cur;
/**
* Logical reads.
*/
private long logicalReads;
/**
* Physical reads.
*/
private long physicalReads;
@Override
protected Object onNext() throws IgniteCheckedException {
if (!onHasNext())
throw new NoSuchElementException();
Object e = cur;
cur = null;
return e;
}
@Override
protected boolean onHasNext() throws IgniteCheckedException {
if (cur != null)
return true;
if (locIter != null) {
if (performanceStatsEnabled)
IoStatisticsQueryHelper.startGatheringQueryStatistics();
try {
if (locIter.hasNextX())
cur = locIter.nextX();
} finally {
if (performanceStatsEnabled) {
IoStatisticsHolder stat = IoStatisticsQueryHelper.finishGatheringQueryStatistics();
logicalReads += stat.logicalReads();
physicalReads += stat.physicalReads();
}
}
}
return cur != null || (cur = convert(fut.next())) != null;
}
/**
* @param obj Entry to convert.
* @return Cache entry
*/
private Object convert(Object obj) {
if (qry.transform() != null)
return obj;
Map.Entry e = (Map.Entry) obj;
return e == null ? null : new CacheQueryEntry(e.getKey(), e.getValue());
}
@Override
protected void onClose() throws IgniteCheckedException {
super.onClose();
if (locIter != null)
locIter.close();
if (fut != null)
fut.cancel();
if (performanceStatsEnabled) {
cctx.kernalContext().performanceStatistics().query(SCAN, cctx.name(), ((GridCacheDistributedQueryFuture) fut).requestId(), startTime, System.nanoTime() - startTimeNanos, true);
if (logicalReads > 0 || physicalReads > 0) {
cctx.kernalContext().performanceStatistics().queryReads(SCAN, cctx.localNodeId(), ((GridCacheDistributedQueryFuture) fut).requestId(), logicalReads, physicalReads);
}
}
}
};
}
use of org.apache.ignite.internal.util.lang.GridCloseableIterator in project ignite by apache.
the class GridCacheQueryAdapter method executeScanQuery.
/**
* {@inheritDoc}
*/
@Override
public GridCloseableIterator executeScanQuery() throws IgniteCheckedException {
assert type == SCAN : "Wrong processing of query: " + type;
if (!cctx.isLocal()) {
GridDhtCacheAdapter<?, ?> cacheAdapter = cctx.isNear() ? cctx.near().dht() : cctx.dht();
Set<Integer> lostParts = cacheAdapter.topology().lostPartitions();
if (!lostParts.isEmpty()) {
if (part == null || lostParts.contains(part)) {
throw new CacheException(new CacheInvalidStateException("Failed to execute query because cache partition " + "has been lostParts [cacheName=" + cctx.name() + ", part=" + (part == null ? lostParts.iterator().next() : part) + ']'));
}
}
}
// Affinity nodes snapshot.
Collection<ClusterNode> nodes = new ArrayList<>(nodes());
cctx.checkSecurity(SecurityPermission.CACHE_READ);
if (nodes.isEmpty()) {
if (part != null) {
if (forceLocal) {
throw new IgniteCheckedException("No queryable nodes for partition " + part + " [forced local query=" + this + "]");
}
}
return new GridEmptyCloseableIterator();
}
if (log.isDebugEnabled())
log.debug("Executing query [query=" + this + ", nodes=" + nodes + ']');
if (cctx.deploymentEnabled())
cctx.deploy().registerClasses(filter);
taskHash = cctx.kernalContext().job().currentTaskNameHash();
final GridCacheQueryManager qryMgr = cctx.queries();
MvccQueryTracker mvccTracker = null;
if (cctx.mvccEnabled() && mvccSnapshot == null) {
GridNearTxLocal tx = cctx.tm().userTx();
if (tx != null)
mvccSnapshot = MvccUtils.requestSnapshot(tx);
else {
mvccTracker = MvccUtils.mvccTracker(cctx, null);
mvccSnapshot = mvccTracker.snapshot();
}
assert mvccSnapshot != null;
}
boolean loc = nodes.size() == 1 && F.first(nodes).id().equals(cctx.localNodeId());
GridCloseableIterator it;
if (loc)
it = qryMgr.scanQueryLocal(this, true);
else if (part != null)
it = new ScanQueryFallbackClosableIterator(part, this, qryMgr, cctx);
else
it = qryMgr.scanQueryDistributed(this, nodes);
return mvccTracker != null ? new MvccTrackingIterator(it, mvccTracker) : it;
}
Aggregations