use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.
the class PageMemoryImpl method tryToRestorePage.
/**
* Restores page from WAL page snapshot & delta records.
*
* @param fullId Full page ID.
* @param buf Destination byte buffer. Note: synchronization to provide ByteBuffer safety should be done outside
* this method.
*
* @throws IgniteCheckedException If failed to start WAL iteration, if incorrect page type observed in data, etc.
* @throws StorageException If it was not possible to restore page, page not found in WAL.
*/
private void tryToRestorePage(FullPageId fullId, ByteBuffer buf) throws IgniteCheckedException {
Long tmpAddr = null;
try {
ByteBuffer curPage = null;
ByteBuffer lastValidPage = null;
try (WALIterator it = walMgr.replay(null)) {
for (IgniteBiTuple<WALPointer, WALRecord> tuple : it) {
switch(tuple.getValue().type()) {
case PAGE_RECORD:
PageSnapshot snapshot = (PageSnapshot) tuple.getValue();
if (snapshot.fullPageId().equals(fullId)) {
if (tmpAddr == null) {
assert snapshot.pageDataSize() <= pageSize() : snapshot.pageDataSize();
tmpAddr = GridUnsafe.allocateMemory(pageSize());
}
if (curPage == null)
curPage = wrapPointer(tmpAddr, pageSize());
PageUtils.putBytes(tmpAddr, 0, snapshot.pageData());
if (PageIO.getCompressionType(tmpAddr) != CompressionProcessor.UNCOMPRESSED_PAGE) {
int realPageSize = realPageSize(snapshot.groupId());
assert snapshot.pageDataSize() < realPageSize : snapshot.pageDataSize();
ctx.kernalContext().compress().decompressPage(curPage, realPageSize);
}
}
break;
case CHECKPOINT_RECORD:
CheckpointRecord rec = (CheckpointRecord) tuple.getValue();
assert !rec.end();
if (curPage != null) {
lastValidPage = curPage;
curPage = null;
}
break;
case // It means that previous checkpoint was broken.
MEMORY_RECOVERY:
curPage = null;
break;
default:
if (tuple.getValue() instanceof PageDeltaRecord) {
PageDeltaRecord deltaRecord = (PageDeltaRecord) tuple.getValue();
if (curPage != null && deltaRecord.pageId() == fullId.pageId() && deltaRecord.groupId() == fullId.groupId()) {
assert tmpAddr != null;
deltaRecord.applyDelta(this, tmpAddr);
}
}
}
}
}
ByteBuffer restored = curPage == null ? lastValidPage : curPage;
if (restored == null)
throw new StorageException(String.format("Page is broken. Can't restore it from WAL. (grpId = %d, pageId = %X).", fullId.groupId(), fullId.pageId()));
buf.put(restored);
} finally {
if (tmpAddr != null)
GridUnsafe.freeMemory(tmpAddr);
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.
the class GridEncryptionManager method writeKeysToWal.
/**
* Writes the record with the master key name and all keys to WAL.
*/
private void writeKeysToWal() throws IgniteCheckedException {
List<T2<Integer, GroupKeyEncrypted>> reencryptedKeys = new ArrayList<>();
for (int grpId : grpKeys.groupIds()) {
for (GroupKeyEncrypted grpKey : grpKeys.getAll(grpId)) reencryptedKeys.add(new T2<>(grpId, grpKey));
}
MasterKeyChangeRecordV2 rec = new MasterKeyChangeRecordV2(getSpi().getMasterKeyName(), reencryptedKeys);
WALPointer ptr = ctx.cache().context().wal().log(rec);
assert ptr != null;
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.
the class GridEncryptionManager method changeCacheGroupKeyLocal.
/**
* @param grpIds Cache group IDs.
* @param keyIds Encryption key IDs.
* @param keys Encryption keys.
* @throws IgniteCheckedException If failed.
*/
protected void changeCacheGroupKeyLocal(int[] grpIds, byte[] keyIds, byte[][] keys) throws IgniteCheckedException {
Map<Integer, Byte> encryptionStatus = U.newHashMap(grpIds.length);
for (int i = 0; i < grpIds.length; i++) encryptionStatus.put(grpIds[i], keyIds[i]);
WALPointer ptr = ctx.cache().context().wal().log(new ReencryptionStartRecord(encryptionStatus));
if (ptr != null)
ctx.cache().context().wal().flush(ptr, false);
for (int i = 0; i < grpIds.length; i++) {
int grpId = grpIds[i];
int newKeyId = keyIds[i] & 0xff;
withMasterKeyChangeReadLock(() -> {
synchronized (metaStorageMux) {
// Set new key as key for writing. Note that we cannot pass the encrypted key here because the master
// key may have changed in which case we will not be able to decrypt the cache encryption key.
GroupKey prevGrpKey = grpKeys.changeActiveKey(grpId, newKeyId);
writeGroupKeysToMetaStore(grpId, grpKeys.getAll(grpId));
if (ptr == null)
return null;
grpKeys.reserveWalKey(grpId, prevGrpKey.unsignedId(), ctx.cache().context().wal().currentSegment());
writeTrackedWalIdxsToMetaStore();
}
return null;
});
CacheGroupContext grp = ctx.cache().cacheGroup(grpId);
if (grp != null && grp.affinityNode())
reencryptGroups.put(grpId, pageScanner.pagesCount(grp));
if (log.isInfoEnabled())
log.info("New encryption key for group was added [grpId=" + grpId + ", keyId=" + newKeyId + ']');
}
startReencryption(encryptionStatus.keySet());
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.
the class CheckpointMarkersStorage method readPointer.
/**
* Loads WAL pointer from CP file
*
* @param cpMarkerFile Checkpoint mark file.
* @return WAL pointer.
* @throws IgniteCheckedException If failed to read mignite-put-get-exampleark file.
*/
private WALPointer readPointer(File cpMarkerFile, ByteBuffer buf) throws IgniteCheckedException {
buf.position(0);
try (FileIO io = ioFactory.create(cpMarkerFile, READ)) {
io.readFully(buf);
buf.flip();
return new WALPointer(buf.getLong(), buf.getInt(), buf.getInt());
} catch (Exception e) {
throw new IgniteCheckedException("Failed to read checkpoint pointer from marker file: " + cpMarkerFile.getAbsolutePath(), e);
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.
the class CheckpointMarkersStorage method parseFromFile.
/**
* Parses checkpoint entry from given file.
*
* @param buf Temporary byte buffer.
* @param file Checkpoint file.
*/
@Nullable
private CheckpointEntry parseFromFile(ByteBuffer buf, File file) throws IgniteCheckedException {
Matcher matcher = CP_FILE_NAME_PATTERN.matcher(file.getName());
if (!matcher.matches())
return null;
CheckpointEntryType type = CheckpointEntryType.valueOf(matcher.group(3));
if (type != CheckpointEntryType.START)
return null;
long cpTs = Long.parseLong(matcher.group(1));
UUID cpId = UUID.fromString(matcher.group(2));
WALPointer ptr = readPointer(file, buf);
return createCheckPointEntry(cpTs, ptr, cpId, null, CheckpointEntryType.START);
}
Aggregations