use of org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO in project ignite by apache.
the class IgniteIndexReader method scanFileStore.
/**
* Scans given file page store and executes closure for each page.
*
* @param partId Partition id.
* @param flag Flag.
* @param store Page store.
* @param c Closure that accepts page id, page address, page IO. If it returns false, scan stops.
* @return List of errors that occured while scanning.
* @throws IgniteCheckedException If failed.
*/
private List<Throwable> scanFileStore(int partId, byte flag, FilePageStore store, GridClosure3<Long, Long, PageIO, Boolean> c) throws IgniteCheckedException {
return doWithBuffer((buf, addr) -> {
List<Throwable> errors = new ArrayList<>();
long pagesNum = isNull(store) ? 0 : (store.size() - store.headerSize()) / pageSize;
for (int i = 0; i < pagesNum; i++) {
buf.rewind();
try {
long pageId = PageIdUtils.pageId(partId, flag, i);
readPage(store, pageId, buf);
PageIO io = PageIO.getPageIO(addr);
if (!c.apply(pageId, addr, io))
break;
} catch (Throwable e) {
String err = "Exception occurred on step " + i + ": " + e.getMessage();
errors.add(new IgniteException(err, e));
}
}
return errors;
});
}
use of org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO in project ignite by apache.
the class IgniteIndexReader method getTreeNode.
/**
* Gets tree node and all its children.
*
* @param pageId Page id, where tree node is located.
* @param nodeCtx Tree traverse context.
* @return Tree node.
*/
private TreeNode getTreeNode(long pageId, TreeTraverseContext nodeCtx) {
PageContent pageContent;
PageIOProcessor ioProcessor;
try {
final ByteBuffer buf = allocateBuffer(pageSize);
try {
readPage(nodeCtx.store, pageId, buf);
final long addr = bufferAddress(buf);
final PageIO io = PageIO.getPageIO(addr);
nodeCtx.ioStat.compute(io.getClass(), (k, v) -> v == null ? 1 : v + 1);
ioProcessor = getIOProcessor(io);
pageContent = ioProcessor.getContent(io, addr, pageId, nodeCtx);
} finally {
freeBuffer(buf);
}
return ioProcessor.getNode(pageContent, pageId, nodeCtx);
} catch (Throwable e) {
nodeCtx.errors.computeIfAbsent(pageId, k -> new LinkedList<>()).add(e);
return new TreeNode(pageId, null, "exception: " + e.getMessage(), Collections.emptyList());
}
}
use of org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO in project ignite by apache.
the class PageMemoryTracker method comparePages.
/**
* Compare pages content.
*
* @param fullPageId Full page ID.
* @param expPage Expected page.
* @param actualPageAddr Actual page address.
* @return {@code True} if pages are equals, {@code False} otherwise.
* @throws IgniteCheckedException If fails.
*/
private boolean comparePages(FullPageId fullPageId, DirectMemoryPage expPage, long actualPageAddr) throws IgniteCheckedException {
long expPageAddr = expPage.address();
GridCacheProcessor cacheProc = gridCtx.cache();
ByteBuffer locBuf = GridUnsafe.wrapPointer(expPageAddr, pageSize);
ByteBuffer rmtBuf = GridUnsafe.wrapPointer(actualPageAddr, pageSize);
PageIO pageIo = PageIO.getPageIO(actualPageAddr);
if (pageIo.getType() == T_DATA_REF_MVCC_LEAF || pageIo.getType() == T_CACHE_ID_DATA_REF_MVCC_LEAF) {
assert cacheProc.cacheGroup(fullPageId.groupId()).mvccEnabled();
AbstractDataLeafIO io = (AbstractDataLeafIO) pageIo;
int cnt = io.getMaxCount(actualPageAddr, pageSize);
// Reset lock info as there is no sense to log it into WAL.
for (int i = 0; i < cnt; i++) {
io.setMvccLockCoordinatorVersion(expPageAddr, i, io.getMvccLockCoordinatorVersion(actualPageAddr, i));
io.setMvccLockCounter(expPageAddr, i, io.getMvccLockCounter(actualPageAddr, i));
}
}
// Compare only meaningful data.
if (pageIo instanceof CompactablePageIO) {
tmpBuf1.clear();
tmpBuf2.clear();
((CompactablePageIO) pageIo).compactPage(locBuf, tmpBuf1, pageSize);
((CompactablePageIO) pageIo).compactPage(rmtBuf, tmpBuf2, pageSize);
locBuf = tmpBuf1;
rmtBuf = tmpBuf2;
}
if (!locBuf.equals(rmtBuf)) {
log.error("Page buffers are not equals [fullPageId=" + fullPageId + ", pageIo=" + pageIo + ']');
dumpDiff(locBuf, rmtBuf);
dumpHistory(expPage);
return false;
}
return true;
}
use of org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO in project ignite by apache.
the class IgnitePdsCheckpointSimulationWithRealCpDisabledTest method runCheckpointing.
/**
* @param mem Memory to use.
* @param storeMgr Store manager.
* @param cacheId Cache ID.
* @return Result map of random operations.
* @throws Exception If failure occurred.
*/
private IgniteBiTuple<Map<FullPageId, Integer>, WALPointer> runCheckpointing(final IgniteEx ig, final PageMemoryImpl mem, final IgnitePageStoreManager storeMgr, final IgniteWriteAheadLogManager wal, final int cacheId) throws Exception {
final ConcurrentMap<FullPageId, Integer> resMap = new ConcurrentHashMap<>();
final FullPageId[] pages = new FullPageId[TOTAL_PAGES];
Set<FullPageId> allocated = new HashSet<>();
IgniteCacheDatabaseSharedManager db = ig.context().cache().context().database();
PageIO pageIO = new DummyPageIO();
for (int i = 0; i < TOTAL_PAGES; i++) {
FullPageId fullId;
db.checkpointReadLock();
try {
fullId = new FullPageId(mem.allocatePage(cacheId, 0, PageIdAllocator.FLAG_DATA), cacheId);
initPage(mem, pageIO, fullId);
} finally {
db.checkpointReadUnlock();
}
resMap.put(fullId, -1);
pages[i] = fullId;
allocated.add(fullId);
}
final AtomicBoolean run = new AtomicBoolean(true);
// Simulate transaction lock.
final ReadWriteLock updLock = new ReentrantReadWriteLock();
// Mark the start position.
CheckpointRecord cpRec = new CheckpointRecord(null);
WALPointer start = wal.log(cpRec);
wal.flush(start, false);
IgniteInternalFuture<Long> updFut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
while (true) {
FullPageId fullId = pages[ThreadLocalRandom.current().nextInt(TOTAL_PAGES)];
updLock.readLock().lock();
try {
if (!run.get())
return null;
ig.context().cache().context().database().checkpointReadLock();
try {
long page = mem.acquirePage(fullId.groupId(), fullId.pageId());
try {
long pageAddr = mem.writeLock(fullId.groupId(), fullId.pageId(), page);
PageIO.setPageId(pageAddr, fullId.pageId());
try {
int state = resMap.get(fullId);
if (state != -1) {
if (VERBOSE)
info("Verify page [fullId=" + fullId + ", state=" + state + ", buf=" + pageAddr + ", bhc=" + U.hexLong(System.identityHashCode(pageAddr)) + ", page=" + U.hexLong(System.identityHashCode(page)) + ']');
for (int i = PageIO.COMMON_HEADER_END; i < mem.realPageSize(fullId.groupId()); i++) {
assertEquals("Verify page failed [fullId=" + fullId + ", i=" + i + ", state=" + state + ", buf=" + pageAddr + ", bhc=" + U.hexLong(System.identityHashCode(pageAddr)) + ", page=" + U.hexLong(System.identityHashCode(page)) + ']', state & 0xFF, PageUtils.getByte(pageAddr, i) & 0xFF);
}
}
state = (state + 1) & 0xFF;
if (VERBOSE)
info("Write page [fullId=" + fullId + ", state=" + state + ", buf=" + pageAddr + ", bhc=" + U.hexLong(System.identityHashCode(pageAddr)) + ", page=" + U.hexLong(System.identityHashCode(page)) + ']');
for (int i = PageIO.COMMON_HEADER_END; i < mem.realPageSize(fullId.groupId()); i++) PageUtils.putByte(pageAddr, i, (byte) state);
resMap.put(fullId, state);
} finally {
mem.writeUnlock(fullId.groupId(), fullId.pageId(), page, null, true);
}
} finally {
mem.releasePage(fullId.groupId(), fullId.pageId(), page);
}
} finally {
ig.context().cache().context().database().checkpointReadUnlock();
}
} finally {
updLock.readLock().unlock();
}
}
}
}, 8, "update-thread");
int checkpoints = 20;
while (checkpoints > 0) {
Map<FullPageId, Integer> snapshot = null;
Collection<FullPageId> pageIds;
updLock.writeLock().lock();
try {
snapshot = new HashMap<>(resMap);
pageIds = mem.beginCheckpoint(new GridFinishedFuture());
checkpoints--;
if (checkpoints == 0)
// No more writes should be done at this point.
run.set(false);
info("Acquired pages for checkpoint: " + pageIds.size());
} finally {
updLock.writeLock().unlock();
}
boolean ok = false;
try {
ByteBuffer tmpBuf = ByteBuffer.allocate(mem.pageSize());
tmpBuf.order(ByteOrder.nativeOrder());
long begin = System.currentTimeMillis();
long cp = 0;
long write = 0;
for (FullPageId fullId : pageIds) {
long cpStart = System.nanoTime();
Integer tag;
AtomicReference<Integer> tag0 = new AtomicReference<>();
PageStoreWriter pageStoreWriter = (fullPageId, buf, tagx) -> {
tag0.set(tagx);
};
while (true) {
mem.checkpointWritePage(fullId, tmpBuf, pageStoreWriter, null);
tag = tag0.get();
if (tag != null && tag == PageMemoryImpl.TRY_AGAIN_TAG)
continue;
break;
}
if (tag == null)
continue;
long cpEnd = System.nanoTime();
cp += cpEnd - cpStart;
Integer state = snapshot.get(fullId);
if (allocated.contains(fullId) && state != -1) {
tmpBuf.rewind();
Integer first = null;
for (int i = PageIO.COMMON_HEADER_END; i < mem.realPageSize(fullId.groupId()); i++) {
int val = tmpBuf.get(i) & 0xFF;
if (first == null)
first = val;
// Avoid string concat.
if (first != val)
assertEquals("Corrupted buffer at position [pageId=" + fullId + ", pos=" + i + ']', (int) first, val);
// Avoid string concat.
if (state != val)
assertEquals("Invalid value at position [pageId=" + fullId + ", pos=" + i + ']', (int) state, val);
}
}
tmpBuf.rewind();
long writeStart = System.nanoTime();
storeMgr.write(cacheId, fullId.pageId(), tmpBuf, tag, true);
long writeEnd = System.nanoTime();
write += writeEnd - writeStart;
tmpBuf.rewind();
}
long syncStart = System.currentTimeMillis();
storeMgr.sync(cacheId, 0);
long end = System.currentTimeMillis();
info("Written pages in " + (end - begin) + "ms, copy took " + (cp / 1_000_000) + "ms, " + "write took " + (write / 1_000_000) + "ms, sync took " + (end - syncStart) + "ms");
ok = true;
} finally {
info("Finishing checkpoint...");
mem.finishCheckpoint();
info("Finished checkpoint");
if (!ok) {
info("Cancelling updates...");
run.set(false);
updFut.get();
}
}
if (checkpoints != 0)
Thread.sleep(2_000);
}
info("checkpoints=" + checkpoints + ", done=" + updFut.isDone());
updFut.get();
assertEquals(0, mem.activePagesCount());
for (FullPageId fullId : pages) {
long page = mem.acquirePage(fullId.groupId(), fullId.pageId());
try {
assertFalse("Page has a temp heap copy after the last checkpoint: [cacheId=" + fullId.groupId() + ", pageId=" + fullId.pageId() + "]", mem.hasTempCopy(page));
assertFalse("Page is dirty after the last checkpoint: [cacheId=" + fullId.groupId() + ", pageId=" + fullId.pageId() + "]", mem.isDirty(fullId.groupId(), fullId.pageId(), page));
} finally {
mem.releasePage(fullId.groupId(), fullId.pageId(), page);
}
}
return F.t((Map<FullPageId, Integer>) resMap, start);
}
use of org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIO in project ignite by apache.
the class IgnitePdsPageReplacementTest method writeData.
/**
* @param memory Page memory.
* @param cacheId Cache id.
* @throws IgniteCheckedException If failed.
*/
private void writeData(final IgniteEx ignite, final PageMemory memory, final int cacheId) throws Exception {
final int pagesNum = getPagesNum();
final List<FullPageId> pageIds = new ArrayList<>(pagesNum);
IgniteCacheDatabaseSharedManager db = ignite.context().cache().context().database();
PageIO pageIO = new DummyPageIO();
// Allocate.
for (int i = 0; i < pagesNum; i++) {
db.checkpointReadLock();
try {
final FullPageId fullId = new FullPageId(memory.allocatePage(cacheId, i % 256, PageMemory.FLAG_DATA), cacheId);
initPage(memory, pageIO, fullId);
pageIds.add(fullId);
} finally {
db.checkpointReadUnlock();
}
}
System.out.println("Allocated pages: " + pageIds.size());
// Write data. (Causes evictions.)
final int part = pagesNum / NUMBER_OF_SEGMENTS;
final Collection<IgniteInternalFuture> futs = new ArrayList<>();
for (int i = 0; i < pagesNum; i += part) futs.add(runWriteInThread(ignite, i, i + part, memory, pageIds));
for (final IgniteInternalFuture fut : futs) fut.get();
System.out.println("Wrote pages: " + pageIds.size());
// Read data. (Causes evictions.)
futs.clear();
for (int i = 0; i < pagesNum; i += part) futs.add(runReadInThread(ignite, i, i + part, memory, pageIds));
for (final IgniteInternalFuture fut : futs) fut.get();
System.out.println("Read pages: " + pageIds.size());
}
Aggregations