Search in sources :

Example 1 with UnwrappedDataEntry

use of org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry in project ignite by apache.

the class IgniteWalReaderTest method iterateAndCountDataRecord.

/**
 * Iterates over data records, checks each DataRecord and its entries, finds out all transactions in WAL.
 *
 * @param walIter iterator to use.
 * @return count of data records observed for each global TX ID. Contains null for non tx updates.
 * @throws IgniteCheckedException if failure.
 */
private Map<GridCacheVersion, Integer> iterateAndCountDataRecord(WALIterator walIter, @Nullable IgniteBiInClosure<Object, Object> cacheObjHnd, @Nullable IgniteInClosure<DataRecord> dataRecordHnd) throws IgniteCheckedException {
    Map<GridCacheVersion, Integer> entriesUnderTxFound = new HashMap<>();
    try (WALIterator stIt = walIter) {
        while (stIt.hasNextX()) {
            IgniteBiTuple<WALPointer, WALRecord> tup = stIt.nextX();
            WALRecord walRecord = tup.get2();
            WALRecord.RecordType type = walRecord.type();
            // noinspection EnumSwitchStatementWhichMissesCases
            switch(type) {
                case DATA_RECORD_V2:
                // Fallthrough.
                case MVCC_DATA_RECORD:
                    {
                        assert walRecord instanceof DataRecord;
                        DataRecord dataRecord = (DataRecord) walRecord;
                        if (dataRecordHnd != null)
                            dataRecordHnd.apply(dataRecord);
                        for (int i = 0; i < dataRecord.entryCount(); i++) {
                            DataEntry entry = dataRecord.get(i);
                            if (walRecord.type() == DATA_RECORD_V2) {
                                assertEquals(primary, (entry.flags() & DataEntry.PRIMARY_FLAG) != 0);
                                assertEquals(rebalance, (entry.flags() & DataEntry.PRELOAD_FLAG) != 0);
                            }
                            GridCacheVersion globalTxId = entry.nearXidVersion();
                            Object unwrappedKeyObj;
                            Object unwrappedValObj;
                            if (entry instanceof UnwrappedDataEntry) {
                                UnwrappedDataEntry unwrapDataEntry = (UnwrappedDataEntry) entry;
                                unwrappedKeyObj = unwrapDataEntry.unwrappedKey();
                                unwrappedValObj = unwrapDataEntry.unwrappedValue();
                            } else if (entry instanceof MarshalledDataEntry) {
                                unwrappedKeyObj = null;
                                unwrappedValObj = null;
                            // can't check value
                            } else {
                                final CacheObject val = entry.value();
                                unwrappedValObj = val instanceof BinaryObject ? val : val.value(null, false);
                                final CacheObject key = entry.key();
                                unwrappedKeyObj = key instanceof BinaryObject ? key : key.value(null, false);
                            }
                            if (DUMP_RECORDS)
                                log.info("//Entry operation " + entry.op() + "; cache Id" + entry.cacheId() + "; " + "under transaction: " + globalTxId + // ; entry " + entry +
                                "; Key: " + unwrappedKeyObj + "; Value: " + unwrappedValObj);
                            if (cacheObjHnd != null && (unwrappedKeyObj != null || unwrappedValObj != null))
                                cacheObjHnd.apply(unwrappedKeyObj, unwrappedValObj);
                            Integer entriesUnderTx = entriesUnderTxFound.get(globalTxId);
                            entriesUnderTxFound.put(globalTxId, entriesUnderTx == null ? 1 : entriesUnderTx + 1);
                        }
                    }
                    break;
                case TX_RECORD:
                // Fallthrough
                case MVCC_TX_RECORD:
                    {
                        assert walRecord instanceof TxRecord;
                        TxRecord txRecord = (TxRecord) walRecord;
                        GridCacheVersion globalTxId = txRecord.nearXidVersion();
                        if (DUMP_RECORDS)
                            log.info("//Tx Record, state: " + txRecord.state() + "; nearTxVersion" + globalTxId);
                    }
            }
        }
    }
    return entriesUnderTxFound;
}
Also used : WALRecord(org.apache.ignite.internal.pagemem.wal.record.WALRecord) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) HashMap(java.util.HashMap) TxRecord(org.apache.ignite.internal.pagemem.wal.record.TxRecord) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) MarshalledDataEntry(org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrapDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry) GridCacheVersion(org.apache.ignite.internal.processors.cache.version.GridCacheVersion) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) BinaryObject(org.apache.ignite.binary.BinaryObject) WALIterator(org.apache.ignite.internal.pagemem.wal.WALIterator) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) WALPointer(org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)

Example 2 with UnwrappedDataEntry

use of org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry in project ignite by apache.

the class WalRecordsConsumer method onRecords.

/**
 * Handles record from the WAL.
 * If this method return {@code true} then current offset in WAL will be stored and WAL iteration will be
 * started from it on CDC application fail/restart.
 *
 * @param recs WAL records iterator.
 * @return {@code True} if current offset in WAL should be commited.
 */
public boolean onRecords(Iterator<DataRecord> recs) {
    Iterator<CdcEvent> evts = new Iterator<CdcEvent>() {

        /**
         */
        private Iterator<CdcEvent> entries;

        @Override
        public boolean hasNext() {
            advance();
            return hasCurrent();
        }

        @Override
        public CdcEvent next() {
            advance();
            if (!hasCurrent())
                throw new NoSuchElementException();
            evtsCnt.increment();
            lastEvtTs.value(System.currentTimeMillis());
            return entries.next();
        }

        private void advance() {
            if (hasCurrent())
                return;
            while (recs.hasNext()) {
                entries = F.iterator(recs.next().writeEntries().iterator(), this::transform, true, OPERATIONS_FILTER);
                if (entries.hasNext())
                    break;
                entries = null;
            }
        }

        private boolean hasCurrent() {
            return entries != null && entries.hasNext();
        }

        /**
         */
        private CdcEvent transform(DataEntry e) {
            UnwrappedDataEntry ue = (UnwrappedDataEntry) e;
            return new CdcEventImpl(ue.unwrappedKey(), ue.unwrappedValue(), (e.flags() & DataEntry.PRIMARY_FLAG) != 0, e.partitionId(), e.writeVersion(), e.cacheId());
        }
    };
    return consumer.onEvents(evts);
}
Also used : DataEntry(org.apache.ignite.internal.pagemem.wal.record.DataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) UnwrappedDataEntry(org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry) Iterator(java.util.Iterator) CdcEvent(org.apache.ignite.cdc.CdcEvent) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

DataEntry (org.apache.ignite.internal.pagemem.wal.record.DataEntry)2 UnwrappedDataEntry (org.apache.ignite.internal.pagemem.wal.record.UnwrappedDataEntry)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 CdcEvent (org.apache.ignite.cdc.CdcEvent)1 WALIterator (org.apache.ignite.internal.pagemem.wal.WALIterator)1 DataRecord (org.apache.ignite.internal.pagemem.wal.record.DataRecord)1 MarshalledDataEntry (org.apache.ignite.internal.pagemem.wal.record.MarshalledDataEntry)1 TxRecord (org.apache.ignite.internal.pagemem.wal.record.TxRecord)1 UnwrapDataEntry (org.apache.ignite.internal.pagemem.wal.record.UnwrapDataEntry)1 WALRecord (org.apache.ignite.internal.pagemem.wal.record.WALRecord)1 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)1 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)1 WALPointer (org.apache.ignite.internal.processors.cache.persistence.wal.WALPointer)1 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)1