use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState in project ignite by apache.
the class GridCacheDatabaseSharedManager method dumpPartitionsInfo.
/**
* Retrieves from page memory meta information about given {@code grp} group partitions
* and dumps this information to log INFO level.
*
* @param grp Cache group.
* @param log Logger.
* @throws IgniteCheckedException If failed.
*/
private static void dumpPartitionsInfo(CacheGroupContext grp, IgniteLogger log) throws IgniteCheckedException {
PageMemoryEx pageMem = (PageMemoryEx) grp.dataRegion().pageMemory();
IgnitePageStoreManager pageStore = grp.shared().pageStore();
assert pageStore != null : "Persistent cache should have initialize page store manager.";
for (int p = 0; p < grp.affinity().partitions(); p++) {
GridDhtLocalPartition part = grp.topology().localPartition(p);
if (part != null) {
if (log.isInfoEnabled())
log.info("Partition [grp=" + grp.cacheOrGroupName() + ", id=" + p + ", state=" + part.state() + ", counter=" + part.dataStore().partUpdateCounter() + ", size=" + part.fullSize() + "]");
continue;
}
if (!pageStore.exists(grp.groupId(), p))
continue;
pageStore.ensure(grp.groupId(), p);
if (pageStore.pages(grp.groupId(), p) <= 1) {
if (log.isInfoEnabled())
log.info("Partition [grp=" + grp.cacheOrGroupName() + ", id=" + p + ", state=N/A (only file header) ]");
continue;
}
long partMetaId = pageMem.partitionMetaPageId(grp.groupId(), p);
long partMetaPage = pageMem.acquirePage(grp.groupId(), partMetaId);
try {
long pageAddr = pageMem.readLock(grp.groupId(), partMetaId, partMetaPage);
try {
PagePartitionMetaIO io = PagePartitionMetaIO.VERSIONS.forPage(pageAddr);
GridDhtPartitionState partState = fromOrdinal(io.getPartitionState(pageAddr));
String state = partState != null ? partState.toString() : "N/A";
long updateCntr = io.getUpdateCounter(pageAddr);
long size = io.getSize(pageAddr);
if (log.isInfoEnabled())
log.info("Partition [grp=" + grp.cacheOrGroupName() + ", id=" + p + ", state=" + state + ", counter=" + updateCntr + ", size=" + size + "]");
} finally {
pageMem.readUnlock(grp.groupId(), partMetaId, partMetaPage);
}
} finally {
pageMem.releasePage(grp.groupId(), partMetaId, partMetaPage);
}
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState in project ignite by apache.
the class GridCacheDatabaseSharedManager method performBinaryMemoryRestore.
/**
* @param status Checkpoint status.
* @param cacheGroupsPredicate Cache groups to restore.
* @throws IgniteCheckedException If failed.
* @throws StorageException In case I/O error occurred during operations with storage.
*/
private RestoreBinaryState performBinaryMemoryRestore(CheckpointStatus status, IgnitePredicate<Integer> cacheGroupsPredicate, IgniteBiPredicate<WALRecord.RecordType, WALPointer> recordTypePredicate, boolean finalizeState) throws IgniteCheckedException {
if (log.isInfoEnabled())
log.info("Checking memory state [lastValidPos=" + status.endPtr + ", lastMarked=" + status.startPtr + ", lastCheckpointId=" + status.cpStartId + ']');
WALPointer recPtr = status.endPtr;
boolean apply = status.needRestoreMemory();
try {
WALRecord startRec = !CheckpointStatus.NULL_PTR.equals(status.startPtr) || apply ? cctx.wal().read(status.startPtr) : null;
if (apply) {
if (finalizeState)
U.quietAndWarn(log, "Ignite node stopped in the middle of checkpoint. Will restore memory state and " + "finish checkpoint on node start.");
cctx.cache().cacheGroupDescriptors().forEach((grpId, desc) -> {
if (!cacheGroupsPredicate.apply(grpId))
return;
try {
DataRegion region = cctx.database().dataRegion(desc.config().getDataRegionName());
if (region == null || !cctx.isLazyMemoryAllocation(region))
return;
region.pageMemory().start();
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
});
cctx.pageStore().beginRecover();
if (!(startRec instanceof CheckpointRecord))
throw new StorageException("Checkpoint marker doesn't point to checkpoint record " + "[ptr=" + status.startPtr + ", rec=" + startRec + "]");
WALPointer cpMark = ((CheckpointRecord) startRec).checkpointMark();
if (cpMark != null) {
if (log.isInfoEnabled())
log.info("Restoring checkpoint after logical recovery, will start physical recovery from " + "back pointer: " + cpMark);
recPtr = cpMark;
}
} else
cctx.wal().notchLastCheckpointPtr(status.startPtr);
} catch (NoSuchElementException e) {
throw new StorageException("Failed to read checkpoint record from WAL, persistence consistency " + "cannot be guaranteed. Make sure configuration points to correct WAL folders and WAL folder is " + "properly mounted [ptr=" + status.startPtr + ", walPath=" + persistenceCfg.getWalPath() + ", walArchive=" + persistenceCfg.getWalArchivePath() + "]");
}
AtomicReference<Throwable> applyError = new AtomicReference<>();
StripedExecutor exec = cctx.kernalContext().pools().getStripedExecutorService();
Semaphore semaphore = new Semaphore(semaphorePertmits(exec));
long start = U.currentTimeMillis();
long lastArchivedSegment = cctx.wal().lastArchivedSegment();
WALIterator it = cctx.wal().replay(recPtr, recordTypePredicate);
RestoreBinaryState restoreBinaryState = new RestoreBinaryState(status, it, lastArchivedSegment, cacheGroupsPredicate);
AtomicLong applied = new AtomicLong();
try {
while (restoreBinaryState.hasNext()) {
if (applyError.get() != null)
break;
WALRecord rec = restoreBinaryState.next();
if (rec == null)
break;
switch(rec.type()) {
case PAGE_RECORD:
if (restoreBinaryState.needApplyBinaryUpdate()) {
PageSnapshot pageSnapshot = (PageSnapshot) rec;
// Here we do not require tag check because we may be applying memory changes after
// several repetitive restarts and the same pages may have changed several times.
int groupId = pageSnapshot.fullPageId().groupId();
int partId = partId(pageSnapshot.fullPageId().pageId());
if (skipRemovedIndexUpdates(groupId, partId))
break;
stripedApplyPage((pageMem) -> {
try {
applyPageSnapshot(pageMem, pageSnapshot);
applied.incrementAndGet();
} catch (Throwable t) {
U.error(log, "Failed to apply page snapshot. rec=[" + pageSnapshot + ']');
applyError.compareAndSet(null, (t instanceof IgniteCheckedException) ? (IgniteCheckedException) t : new IgniteCheckedException("Failed to apply page snapshot", t));
}
}, groupId, partId, exec, semaphore);
}
break;
case PART_META_UPDATE_STATE:
PartitionMetaStateRecord metaStateRecord = (PartitionMetaStateRecord) rec;
{
int groupId = metaStateRecord.groupId();
int partId = metaStateRecord.partitionId();
stripedApplyPage((pageMem) -> {
GridDhtPartitionState state = fromOrdinal(metaStateRecord.state());
if (state == null || state == GridDhtPartitionState.EVICTED)
schedulePartitionDestroy(groupId, partId);
else {
try {
cancelOrWaitPartitionDestroy(groupId, partId);
} catch (Throwable t) {
U.error(log, "Failed to cancel or wait partition destroy. rec=[" + metaStateRecord + ']');
applyError.compareAndSet(null, (t instanceof IgniteCheckedException) ? (IgniteCheckedException) t : new IgniteCheckedException("Failed to cancel or wait partition destroy", t));
}
}
}, groupId, partId, exec, semaphore);
}
break;
case PARTITION_DESTROY:
PartitionDestroyRecord destroyRecord = (PartitionDestroyRecord) rec;
{
int groupId = destroyRecord.groupId();
int partId = destroyRecord.partitionId();
stripedApplyPage((pageMem) -> {
pageMem.invalidate(groupId, partId);
schedulePartitionDestroy(groupId, partId);
}, groupId, partId, exec, semaphore);
}
break;
default:
if (restoreBinaryState.needApplyBinaryUpdate() && rec instanceof PageDeltaRecord) {
PageDeltaRecord pageDelta = (PageDeltaRecord) rec;
int groupId = pageDelta.groupId();
int partId = partId(pageDelta.pageId());
if (skipRemovedIndexUpdates(groupId, partId))
break;
stripedApplyPage((pageMem) -> {
try {
applyPageDelta(pageMem, pageDelta, true);
applied.incrementAndGet();
} catch (Throwable t) {
U.error(log, "Failed to apply page delta. rec=[" + pageDelta + ']');
applyError.compareAndSet(null, (t instanceof IgniteCheckedException) ? (IgniteCheckedException) t : new IgniteCheckedException("Failed to apply page delta", t));
}
}, groupId, partId, exec, semaphore);
}
}
}
} finally {
it.close();
awaitApplyComplete(exec, applyError);
}
if (!finalizeState)
return null;
WALPointer lastReadPtr = restoreBinaryState.lastReadRecordPointer();
if (status.needRestoreMemory()) {
if (restoreBinaryState.needApplyBinaryUpdate())
throw new StorageException("Failed to restore memory state (checkpoint marker is present " + "on disk, but checkpoint record is missed in WAL) " + "[cpStatus=" + status + ", lastRead=" + lastReadPtr + "]");
if (log.isInfoEnabled())
log.info("Finished applying memory changes [changesApplied=" + applied + ", time=" + (U.currentTimeMillis() - start) + " ms]");
finalizeCheckpointOnRecovery(status.cpStartTs, status.cpStartId, status.startPtr, exec);
}
return restoreBinaryState;
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState in project ignite by apache.
the class GridDhtPartitionsExchangeFuture method assignPartitionSizes.
/**
* @param top Topology.
*/
private void assignPartitionSizes(GridDhtPartitionTopology top) {
Map<Integer, Long> partSizes = new HashMap<>();
for (Map.Entry<UUID, GridDhtPartitionsSingleMessage> e : msgs.entrySet()) {
GridDhtPartitionsSingleMessage singleMsg = e.getValue();
GridDhtPartitionMap partMap = singleMsg.partitions().get(top.groupId());
if (partMap == null)
continue;
for (Map.Entry<Integer, GridDhtPartitionState> e0 : partMap.entrySet()) {
int p = e0.getKey();
GridDhtPartitionState state = e0.getValue();
if (state == GridDhtPartitionState.OWNING)
partSizes.put(p, singleMsg.partitionSizes(top.groupId()).get(p));
}
}
for (GridDhtLocalPartition locPart : top.currentLocalPartitions()) {
if (locPart.state() == GridDhtPartitionState.OWNING)
partSizes.put(locPart.id(), locPart.fullSize());
}
top.globalPartSizes(partSizes);
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState in project ignite by apache.
the class SchemaIndexCachePartitionWorker method processPartition.
/**
* Process partition.
*
* @throws IgniteCheckedException If failed.
*/
private void processPartition() throws IgniteCheckedException {
if (stop())
return;
checkCancelled();
boolean reserved = false;
GridDhtPartitionState partState = locPart.state();
if (partState != EVICTED)
reserved = (partState == OWNING || partState == MOVING || partState == LOST) && locPart.reserve();
if (!reserved)
return;
try {
GridCursor<? extends CacheDataRow> cursor = locPart.dataStore().cursor(cctx.cacheId(), null, null, KEY_ONLY);
boolean locked = false;
try {
int cntr = 0;
while (!stop() && cursor.next()) {
KeyCacheObject key = cursor.get().key();
if (!locked) {
cctx.shared().database().checkpointReadLock();
locked = true;
}
processKey(key);
if (++cntr % batchSize == 0) {
cctx.shared().database().checkpointReadUnlock();
locked = false;
}
cctx.cache().metrics0().addIndexRebuildKeyProcessed(1);
if (locPart.state() == RENTING)
break;
}
wrappedClo.addNumberProcessedKeys(cntr);
} finally {
if (locked)
cctx.shared().database().checkpointReadUnlock();
}
} finally {
locPart.release();
if (partsCnt.getAndUpdate(v -> v > 0 ? v - 1 : 0) > 0)
cctx.group().metrics().decrementIndexBuildCountPartitionsLeft();
}
}
use of org.apache.ignite.internal.processors.cache.distributed.dht.topology.GridDhtPartitionState in project ignite by apache.
the class GridCacheDhtPreloadDelayedSelfTest method testAutomaticPreloadWithEmptyCache.
/**
* @throws Exception If failed.
*/
@Test
public void testAutomaticPreloadWithEmptyCache() throws Exception {
preloadMode = SYNC;
delay = 0;
Collection<Ignite> ignites = new ArrayList<>();
try {
for (int i = 0; i < 5; i++) {
ignites.add(startGrid(i));
if (persistenceEnabled()) {
if (i == 0)
grid(0).cluster().active(true);
else
resetBaselineTopology();
}
awaitPartitionMapExchange(true, true, null, false);
for (Ignite g : ignites) {
info(">>> Checking affinity for grid: " + g.name());
GridDhtPartitionTopology top = topology(g);
GridDhtPartitionFullMap fullMap = top.partitionMap(true);
for (Map.Entry<UUID, GridDhtPartitionMap> fe : fullMap.entrySet()) {
UUID nodeId = fe.getKey();
GridDhtPartitionMap m = fe.getValue();
for (Map.Entry<Integer, GridDhtPartitionState> e : m.entrySet()) {
int p = e.getKey();
GridDhtPartitionState state = e.getValue();
Collection<ClusterNode> nodes = affinityNodes(g, p);
Collection<UUID> nodeIds = U.nodeIds(nodes);
assert nodeIds.contains(nodeId) : "Invalid affinity mapping [nodeId=" + nodeId + ", part=" + p + ", state=" + state + ", igniteInstanceName=" + G.ignite(nodeId).name() + ", affNames=" + U.nodes2names(nodes) + ", affIds=" + nodeIds + ']';
}
}
}
}
} finally {
stopAllGrids();
}
}
Aggregations