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;
}
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);
}
Aggregations