use of org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage in project ignite by apache.
the class GridCacheDatabaseSharedManager method readCheckpointAndRestoreMemory.
/**
* {@inheritDoc}
*/
@Override
public void readCheckpointAndRestoreMemory(List<DynamicCacheDescriptor> cachesToStart) throws IgniteCheckedException {
assert !cctx.localNode().isClient();
checkpointReadLock();
try {
if (!F.isEmpty(cachesToStart)) {
for (DynamicCacheDescriptor desc : cachesToStart) {
if (CU.affinityNode(cctx.localNode(), desc.cacheConfiguration().getNodeFilter()))
storeMgr.initializeForCache(desc.groupDescriptor(), new StoredCacheData(desc.cacheConfiguration()));
}
}
CheckpointStatus status = readCheckpointStatus();
cctx.pageStore().initializeForMetastorage();
metaStorage = new MetaStorage(cctx, dataRegionMap.get(METASTORE_DATA_REGION_NAME), (DataRegionMetricsImpl) memMetricsMap.get(METASTORE_DATA_REGION_NAME));
WALPointer restore = restoreMemory(status);
// First, bring memory to the last consistent checkpoint state if needed.
// This method should return a pointer to the last valid record in the WAL.
cctx.wal().resumeLogging(restore);
WALPointer ptr = cctx.wal().log(new MemoryRecoveryRecord(U.currentTimeMillis()));
if (ptr != null) {
cctx.wal().fsync(ptr);
nodeStart(ptr);
}
metaStorage.init(this);
notifyMetastorageReadyForReadWrite();
} catch (StorageException e) {
throw new IgniteCheckedException(e);
} finally {
checkpointReadUnlock();
}
}
use of org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage in project ignite by apache.
the class GridCacheDatabaseSharedManager method restoreMemory.
/**
* @param status Checkpoint status.
* @param storeOnly If {@code True} restores Metastorage only.
*/
private WALPointer restoreMemory(CheckpointStatus status, boolean storeOnly, PageMemoryEx storePageMem) throws IgniteCheckedException {
assert !storeOnly || storePageMem != null;
if (log.isInfoEnabled())
log.info("Checking memory state [lastValidPos=" + status.endPtr + ", lastMarked=" + status.startPtr + ", lastCheckpointId=" + status.cpStartId + ']');
boolean apply = status.needRestoreMemory();
if (apply) {
U.quietAndWarn(log, "Ignite node stopped in the middle of checkpoint. Will restore memory state and " + "finish checkpoint on node start.");
cctx.pageStore().beginRecover();
} else
cctx.wal().allowCompressionUntil(status.startPtr);
long start = U.currentTimeMillis();
int applied = 0;
WALPointer lastRead = null;
Collection<Integer> ignoreGrps = storeOnly ? Collections.emptySet() : initiallyWalDisabledGrps;
try (WALIterator it = cctx.wal().replay(status.endPtr)) {
while (it.hasNextX()) {
IgniteBiTuple<WALPointer, WALRecord> tup = it.nextX();
WALRecord rec = tup.get2();
lastRead = tup.get1();
switch(rec.type()) {
case CHECKPOINT_RECORD:
CheckpointRecord cpRec = (CheckpointRecord) rec;
// We roll memory up until we find a checkpoint start record registered in the status.
if (F.eq(cpRec.checkpointId(), status.cpStartId)) {
log.info("Found last checkpoint marker [cpId=" + cpRec.checkpointId() + ", pos=" + tup.get1() + ']');
apply = false;
} else if (!F.eq(cpRec.checkpointId(), status.cpEndId))
U.warn(log, "Found unexpected checkpoint marker, skipping [cpId=" + cpRec.checkpointId() + ", expCpId=" + status.cpStartId + ", pos=" + tup.get1() + ']');
break;
case PAGE_RECORD:
if (apply) {
PageSnapshot pageRec = (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 grpId = pageRec.fullPageId().groupId();
if (storeOnly && grpId != METASTORAGE_CACHE_ID)
continue;
if (!ignoreGrps.contains(grpId)) {
long pageId = pageRec.fullPageId().pageId();
PageMemoryEx pageMem = grpId == METASTORAGE_CACHE_ID ? storePageMem : getPageMemoryForCacheGroup(grpId);
long page = pageMem.acquirePage(grpId, pageId, true);
try {
long pageAddr = pageMem.writeLock(grpId, pageId, page);
try {
PageUtils.putBytes(pageAddr, 0, pageRec.pageData());
} finally {
pageMem.writeUnlock(grpId, pageId, page, null, true, true);
}
} finally {
pageMem.releasePage(grpId, pageId, page);
}
applied++;
}
}
break;
case PARTITION_DESTROY:
PartitionDestroyRecord destroyRec = (PartitionDestroyRecord) rec;
final int gId = destroyRec.groupId();
if (storeOnly && gId != METASTORAGE_CACHE_ID)
continue;
if (!ignoreGrps.contains(gId)) {
final int pId = destroyRec.partitionId();
PageMemoryEx pageMem = gId == METASTORAGE_CACHE_ID ? storePageMem : getPageMemoryForCacheGroup(gId);
pageMem.clearAsync((grpId, pageId) -> grpId == gId && PageIdUtils.partId(pageId) == pId, true).get();
}
break;
default:
if (apply && rec instanceof PageDeltaRecord) {
PageDeltaRecord r = (PageDeltaRecord) rec;
int grpId = r.groupId();
if (storeOnly && grpId != METASTORAGE_CACHE_ID)
continue;
if (!ignoreGrps.contains(grpId)) {
long pageId = r.pageId();
PageMemoryEx pageMem = grpId == METASTORAGE_CACHE_ID ? storePageMem : getPageMemoryForCacheGroup(grpId);
// 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.
long page = pageMem.acquirePage(grpId, pageId, true);
try {
long pageAddr = pageMem.writeLock(grpId, pageId, page);
try {
r.applyDelta(pageMem, pageAddr);
} finally {
pageMem.writeUnlock(grpId, pageId, page, null, true, true);
}
} finally {
pageMem.releasePage(grpId, pageId, page);
}
applied++;
}
}
}
}
}
if (storeOnly)
return null;
if (status.needRestoreMemory()) {
if (apply)
throw new IgniteCheckedException("Failed to restore memory state (checkpoint marker is present " + "on disk, but checkpoint record is missed in WAL) " + "[cpStatus=" + status + ", lastRead=" + lastRead + "]");
log.info("Finished applying memory changes [changesApplied=" + applied + ", time=" + (U.currentTimeMillis() - start) + "ms]");
if (applied > 0)
finalizeCheckpointOnRecovery(status.cpStartTs, status.cpStartId, status.startPtr);
}
checkpointHist.loadHistory(cpDir);
return lastRead == null ? null : lastRead.next();
}
use of org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage in project ignite by apache.
the class IgniteWalRecoveryTest method testMetastorageWalRestore.
/**
* @throws Exception If fail.
*/
public void testMetastorageWalRestore() throws Exception {
try {
int cnt = 2000;
IgniteEx ignite0 = (IgniteEx) startGrid(0);
ignite0.active(true);
GridCacheSharedContext<Object, Object> sharedCtx0 = ignite0.context().cache().context();
MetaStorage storage = sharedCtx0.database().metaStorage();
assert storage != null;
for (int i = 0; i < cnt; i++) {
sharedCtx0.database().checkpointReadLock();
try {
storage.putData(String.valueOf(i), new byte[] { 1, 2, 3 });
} finally {
sharedCtx0.database().checkpointReadUnlock();
}
}
for (int i = 0; i < cnt; i++) {
byte[] value = storage.getData(String.valueOf(i));
assert value != null;
assert value.length == 3;
}
stopGrid(0);
ignite0 = startGrid(0);
ignite0.active(true);
sharedCtx0 = ignite0.context().cache().context();
storage = sharedCtx0.database().metaStorage();
assert storage != null;
for (int i = 0; i < cnt; i++) {
byte[] value = storage.getData(String.valueOf(i));
assert value != null;
}
} finally {
stopAllGrids();
}
}
use of org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage in project ignite by apache.
the class IgniteWalRecoveryTest method testMetastorage.
/**
* @throws Exception If fail.
*/
public void testMetastorage() throws Exception {
try {
int cnt = 5000;
IgniteEx ignite0 = (IgniteEx) startGrid("node1");
IgniteEx ignite1 = (IgniteEx) startGrid("node2");
ignite1.active(true);
GridCacheSharedContext<Object, Object> sharedCtx0 = ignite0.context().cache().context();
GridCacheSharedContext<Object, Object> sharedCtx1 = ignite1.context().cache().context();
MetaStorage storage0 = sharedCtx0.database().metaStorage();
MetaStorage storage1 = sharedCtx1.database().metaStorage();
assert storage0 != null;
for (int i = 0; i < cnt; i++) {
sharedCtx0.database().checkpointReadLock();
try {
storage0.putData(String.valueOf(i), new byte[] { (byte) (i % 256), 2, 3 });
} finally {
sharedCtx0.database().checkpointReadUnlock();
}
byte[] b1 = new byte[i + 3];
b1[0] = 1;
b1[1] = 2;
b1[2] = 3;
sharedCtx1.database().checkpointReadLock();
try {
storage1.putData(String.valueOf(i), b1);
} finally {
sharedCtx1.database().checkpointReadUnlock();
}
}
for (int i = 0; i < cnt; i++) {
byte[] d1 = storage0.getData(String.valueOf(i));
assertEquals(3, d1.length);
assertEquals((byte) (i % 256), d1[0]);
assertEquals(2, d1[1]);
assertEquals(3, d1[2]);
byte[] d2 = storage1.getData(String.valueOf(i));
assertEquals(i + 3, d2.length);
assertEquals(1, d2[0]);
assertEquals(2, d2[1]);
assertEquals(3, d2[2]);
}
} finally {
stopAllGrids();
}
}
Aggregations