Search in sources :

Example 1 with WALPointer

use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.

the class RecordDataV2Serializer method writePlainRecord.

/**
 * {@inheritDoc}
 */
@Override
protected void writePlainRecord(WALRecord rec, ByteBuffer buf) throws IgniteCheckedException {
    if (rec instanceof HeaderRecord)
        throw new UnsupportedOperationException("Writing header records is forbidden since version 2 of serializer");
    switch(rec.type()) {
        case CHECKPOINT_RECORD:
            CheckpointRecord cpRec = (CheckpointRecord) rec;
            WALPointer walPtr = cpRec.checkpointMark();
            UUID cpId = cpRec.checkpointId();
            buf.putLong(cpId.getMostSignificantBits());
            buf.putLong(cpId.getLeastSignificantBits());
            buf.put(walPtr == null ? (byte) 0 : 1);
            if (walPtr != null) {
                buf.putLong(walPtr.index());
                buf.putInt(walPtr.fileOffset());
                buf.putInt(walPtr.length());
            }
            putCacheStates(buf, cpRec.cacheGroupStates());
            buf.put(cpRec.end() ? (byte) 1 : 0);
            break;
        case MVCC_DATA_RECORD:
        case DATA_RECORD_V2:
            DataRecord dataRec = (DataRecord) rec;
            int entryCnt = dataRec.entryCount();
            buf.putInt(entryCnt);
            buf.putLong(dataRec.timestamp());
            boolean encrypted = isDataRecordEncrypted(dataRec);
            for (int i = 0; i < entryCnt; i++) {
                DataEntry dataEntry = dataRec.get(i);
                if (encrypted)
                    putEncryptedDataEntry(buf, dataEntry);
                else
                    putPlainDataEntry(buf, dataEntry);
            }
            break;
        case SNAPSHOT:
            SnapshotRecord snpRec = (SnapshotRecord) rec;
            buf.putLong(snpRec.getSnapshotId());
            buf.put(snpRec.isFull() ? (byte) 1 : 0);
            break;
        case EXCHANGE:
            ExchangeRecord r = (ExchangeRecord) rec;
            buf.putInt(r.getType().ordinal());
            buf.putShort(r.getConstId());
            buf.putLong(r.timestamp());
            break;
        case TX_RECORD:
            txRecordSerializer.write((TxRecord) rec, buf);
            break;
        case MVCC_TX_RECORD:
            txRecordSerializer.write((MvccTxRecord) rec, buf);
            break;
        case ROLLBACK_TX_RECORD:
            RollbackRecord rb = (RollbackRecord) rec;
            buf.putInt(rb.groupId());
            buf.putInt(rb.partitionId());
            buf.putLong(rb.start());
            buf.putLong(rb.range());
            break;
        case TRACKING_PAGE_REPAIR_DELTA:
            TrackingPageRepairDeltaRecord tprDelta = (TrackingPageRepairDeltaRecord) rec;
            buf.putInt(tprDelta.groupId());
            buf.putLong(tprDelta.pageId());
            break;
        default:
            super.writePlainRecord(rec, buf);
    }
}
Also used : CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) SnapshotRecord(org.apache.ignite.internal.pagemem.wal.record.SnapshotRecord) TrackingPageRepairDeltaRecord(org.apache.ignite.internal.pagemem.wal.record.delta.TrackingPageRepairDeltaRecord) MvccDataEntry(org.apache.ignite.internal.pagemem.wal.record.MvccDataEntry) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) LazyMvccDataEntry(org.apache.ignite.internal.pagemem.wal.record.LazyMvccDataEntry) HeaderRecord(org.apache.ignite.internal.processors.cache.persistence.wal.record.HeaderRecord) ExchangeRecord(org.apache.ignite.internal.pagemem.wal.record.ExchangeRecord) RollbackRecord(org.apache.ignite.internal.pagemem.wal.record.RollbackRecord) MvccDataRecord(org.apache.ignite.internal.pagemem.wal.record.MvccDataRecord) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) UUID(java.util.UUID) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 2 with WALPointer

use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.

the class RecordDataV2Serializer method plainSize.

/**
 * {@inheritDoc}
 */
@Override
protected int plainSize(WALRecord rec) throws IgniteCheckedException {
    switch(rec.type()) {
        case HEADER_RECORD:
            return HEADER_RECORD_DATA_SIZE;
        case CHECKPOINT_RECORD:
            CheckpointRecord cpRec = (CheckpointRecord) rec;
            int cacheStatesSize = cacheStatesSize(cpRec.cacheGroupStates());
            WALPointer walPtr = cpRec.checkpointMark();
            return 18 + cacheStatesSize + (walPtr == null ? 0 : 16);
        case MVCC_DATA_RECORD:
            return 4 + /*entry count*/
            8 + /*timestamp*/
            dataSize((DataRecord) rec);
        case DATA_RECORD_V2:
            return super.plainSize(rec) + 8;
        case SNAPSHOT:
            return 8 + 1;
        case EXCHANGE:
            return 4 + /*type*/
            8 + /*timestamp*/
            2;
        case TX_RECORD:
            return txRecordSerializer.size((TxRecord) rec);
        case MVCC_TX_RECORD:
            return txRecordSerializer.size((MvccTxRecord) rec);
        case ROLLBACK_TX_RECORD:
            return 4 + 4 + 8 + 8;
        case TRACKING_PAGE_REPAIR_DELTA:
            return 4 + 8;
        case PARTITION_CLEARING_START_RECORD:
            return 4 + 4 + 8;
        default:
            return super.plainSize(rec);
    }
}
Also used : CheckpointRecord(org.apache.ignite.internal.pagemem.wal.record.CheckpointRecord) MvccDataRecord(org.apache.ignite.internal.pagemem.wal.record.MvccDataRecord) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 3 with WALPointer

use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.

the class RecordV1Serializer method readSegmentHeader.

/**
 * Reads stored record from provided {@code io}.
 * NOTE: Method mutates position of {@code io}.
 *
 * @param io I/O interface for file.
 * @param segmentFileInputFactory File input factory.
 * @return Instance of {@link SegmentHeader} extracted from the file.
 * @throws IgniteCheckedException If failed to read serializer version.
 */
public static SegmentHeader readSegmentHeader(SegmentIO io, SegmentFileInputFactory segmentFileInputFactory) throws IgniteCheckedException, IOException {
    try (ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
        ByteBufferBackedDataInput in = segmentFileInputFactory.createFileInput(io, buf);
        in.ensure(HEADER_RECORD_SIZE);
        int recordType = in.readUnsignedByte();
        if (recordType == WALRecord.RecordType.STOP_ITERATION_RECORD_TYPE)
            throw new SegmentEofException("Reached logical end of the segment", null);
        WALRecord.RecordType type = WALRecord.RecordType.fromIndex(recordType - 1);
        if (type != WALRecord.RecordType.HEADER_RECORD)
            throw new IOException("Can't read serializer version", null);
        // Read file pointer.
        WALPointer ptr = readPosition(in);
        if (io.getSegmentId() != ptr.index())
            throw new SegmentEofException("Reached logical end of the segment by pointer", null);
        assert ptr.fileOffset() == 0 : "Header record should be placed at the beginning of file " + ptr;
        long hdrMagicNum = in.readLong();
        boolean compacted;
        if (hdrMagicNum == HeaderRecord.REGULAR_MAGIC)
            compacted = false;
        else if (hdrMagicNum == HeaderRecord.COMPACTED_MAGIC)
            compacted = true;
        else {
            throw new IOException("Magic is corrupted [exp=" + U.hexLong(HeaderRecord.REGULAR_MAGIC) + ", actual=" + U.hexLong(hdrMagicNum) + ']');
        }
        // Read serializer version.
        int ver = in.readInt();
        // Read and skip CRC.
        in.readInt();
        return new SegmentHeader(ver, compacted);
    }
}
Also used : ByteBufferExpander(org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferExpander) WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) ByteBufferBackedDataInput(org.apache.ignite.internal.processors.cache.persistence.wal.ByteBufferBackedDataInput) RecordType(org.apache.ignite.internal.pagemem.wal.record.WALRecord.RecordType) SegmentEofException(org.apache.ignite.internal.processors.cache.persistence.wal.SegmentEofException) IOException(java.io.IOException) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 4 with WALPointer

use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.

the class ReleaseSegmentOnHistoricalRebalanceTest method testReleaseSegmentAfterSearchAndReserveCheckpoints.

/**
 * Checks that if release the segment after {@link CheckpointHistory#searchAndReserveCheckpoints},
 * there will be no errors and the rebalance will be completed.
 *
 * @throws Exception If failed.
 */
@Test
public void testReleaseSegmentAfterSearchAndReserveCheckpoints() throws Exception {
    checkHistoricalRebalance(n -> {
        CheckpointHistory spy = spy(dbMgr(n).checkpointHistory());
        when(spy.searchAndReserveCheckpoints(any())).thenAnswer(m -> {
            CheckpointHistoryResult res = (CheckpointHistoryResult) m.callRealMethod();
            WALPointer reserved = res.reservedCheckpointMark();
            assertNotNull(reserved);
            release(n, reserved);
            return res;
        });
        checkpointHistory(n, spy);
    });
}
Also used : CheckpointHistoryResult(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointHistoryResult) CheckpointHistory(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointHistory) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) Test(org.junit.Test)

Example 5 with WALPointer

use of org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer in project ignite by apache.

the class GridCacheDatabaseSharedManager method reserveHistoryForExchange.

/**
 * {@inheritDoc}
 */
@Override
public synchronized Map<Integer, Map<Integer, Long>> reserveHistoryForExchange() {
    assert reservedForExchange == null : reservedForExchange;
    Map<Integer, Set<Integer>> /*partId*/
    applicableGroupsAndPartitions = partitionsApplicableForWalRebalance();
    Map<Integer, T2<ReservationReason, Map<Integer, CheckpointEntry>>> /*partId*/
    earliestValidCheckpoints;
    checkpointReadLock();
    WALPointer reservedCheckpointMark;
    try {
        CheckpointHistoryResult checkpointHistoryResult = checkpointHistory().searchAndReserveCheckpoints(applicableGroupsAndPartitions);
        earliestValidCheckpoints = checkpointHistoryResult.earliestValidCheckpoints();
        reservedForExchange = reservedCheckpointMark = checkpointHistoryResult.reservedCheckpointMark();
    } finally {
        checkpointReadUnlock();
    }
    Map<Integer, Map<Integer, Long>> /*updCntr*/
    grpPartsWithCnts = new HashMap<>();
    for (Map.Entry<Integer, T2<ReservationReason, Map<Integer, CheckpointEntry>>> /*partId*/
    e : earliestValidCheckpoints.entrySet()) {
        int grpId = e.getKey();
        if (e.getValue().get2() == null)
            continue;
        for (Map.Entry<Integer, CheckpointEntry> e0 : e.getValue().get2().entrySet()) {
            CheckpointEntry cpEntry = e0.getValue();
            int partId = e0.getKey();
            if (reservedCheckpointMark != null && !cctx.wal().reserved(reservedCheckpointMark)) {
                log.warning("Reservation failed because the segment was released: " + reservedCheckpointMark);
                reservedForExchange = null;
                grpPartsWithCnts.clear();
                return grpPartsWithCnts;
            }
            try {
                Long updCntr = cpEntry.partitionCounter(cctx.wal(), grpId, partId);
                if (updCntr != null)
                    grpPartsWithCnts.computeIfAbsent(grpId, k -> new HashMap<>()).put(partId, updCntr);
            } catch (IgniteCheckedException ex) {
                log.warning("Reservation failed because counters are not available [grpId=" + grpId + ", part=" + partId + ", cp=(" + cpEntry.checkpointId() + ", " + U.format(cpEntry.timestamp()) + ")]", ex);
            }
        }
    }
    if (log.isInfoEnabled() && !F.isEmpty(earliestValidCheckpoints))
        printReservationToLog(earliestValidCheckpoints);
    return grpPartsWithCnts;
}
Also used : CheckpointEntry(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointEntry) Set(java.util.Set) Collectors.toSet(java.util.stream.Collectors.toSet) HashSet(java.util.HashSet) GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteSystemProperties.getInteger(org.apache.ignite.IgniteSystemProperties.getInteger) CheckpointHistoryResult(org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointHistoryResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicLong(java.util.concurrent.atomic.AtomicLong) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) T2(org.apache.ignite.internal.util.typedef.T2)

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