Search in sources :

Example 11 with WALPointer

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);
    }
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) AtomicLong(java.util.concurrent.atomic.AtomicLong) CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) PageDeltaRecord(org.apache.ignite.internal.pagemem.wal.record.delta.PageDeltaRecord) ByteBuffer(java.nio.ByteBuffer) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) PageSnapshot(org.apache.ignite.internal.pagemem.wal.record.PageSnapshot)

Example 12 with WALPointer

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;
}
Also used : ArrayList(java.util.ArrayList) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) T2(org.apache.ignite.internal.util.typedef.T2) MasterKeyChangeRecordV2(org.apache.ignite.internal.pagemem.wal.record.MasterKeyChangeRecordV2)

Example 13 with WALPointer

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());
}
Also used : ReencryptionStartRecord(org.apache.ignite.internal.pagemem.wal.record.ReencryptionStartRecord) CacheGroupContext(org.apache.ignite.internal.processors.cache.CacheGroupContext) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 14 with WALPointer

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);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) StorageException(org.apache.ignite.internal.processors.cache.persistence.StorageException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) FileIO(org.apache.ignite.internal.processors.cache.persistence.file.FileIO)

Example 15 with WALPointer

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);
}
Also used : Matcher(java.util.regex.Matcher) UUID(java.util.UUID) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)122 WALRecord (org.apache.ignite.internal.pagemem.wal.record.WALRecord)44 Test (org.junit.Test)41 IgniteEx (org.apache.ignite.internal.IgniteEx)38 WALIterator (org.apache.ignite.internal.pagemem.wal.WALIterator)36 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)30 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)27 CheckpointRecord (org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord)24 IgniteWriteAheadLogManager (org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager)23 DataRecord (org.apache.ignite.internal.pagemem.wal.record.DataRecord)23 ArrayList (java.util.ArrayList)20 IgniteWalIteratorFactory (org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory)20 FullPageId (org.apache.ignite.internal.pagemem.FullPageId)19 DataEntry (org.apache.ignite.internal.pagemem.wal.record.DataEntry)19 PageSnapshot (org.apache.ignite.internal.pagemem.wal.record.PageSnapshot)19 File (java.io.File)18 T2 (org.apache.ignite.internal.util.typedef.T2)16 ByteBuffer (java.nio.ByteBuffer)15 UUID (java.util.UUID)15 IOException (java.io.IOException)14