Search in sources :

Example 1 with SnapshotHeaderRecord

use of org.apache.kafka.common.message.SnapshotHeaderRecord in project kafka by apache.

the class SnapshotWriterReaderTest method validateDelimiters.

private int validateDelimiters(RawSnapshotReader snapshot, long lastContainedLogTime) {
    assertNotEquals(0, snapshot.sizeInBytes());
    int countRecords = 0;
    Iterator<RecordBatch> recordBatches = Utils.covariantCast(snapshot.records().batchIterator());
    assertTrue(recordBatches.hasNext());
    RecordBatch batch = recordBatches.next();
    Iterator<Record> records = batch.streamingIterator(new GrowableBufferSupplier());
    // Verify existence of the header record
    assertTrue(batch.isControlBatch());
    assertTrue(records.hasNext());
    Record record = records.next();
    countRecords += 1;
    SnapshotHeaderRecord headerRecord = ControlRecordUtils.deserializedSnapshotHeaderRecord(record);
    assertEquals(headerRecord.version(), ControlRecordUtils.SNAPSHOT_HEADER_HIGHEST_VERSION);
    assertEquals(headerRecord.lastContainedLogTimestamp(), lastContainedLogTime);
    assertFalse(records.hasNext());
    // Loop over remaining records
    while (recordBatches.hasNext()) {
        batch = recordBatches.next();
        records = batch.streamingIterator(new GrowableBufferSupplier());
        while (records.hasNext()) {
            countRecords += 1;
            record = records.next();
        }
    }
    // Verify existence of the footer record in the end
    assertTrue(batch.isControlBatch());
    SnapshotFooterRecord footerRecord = ControlRecordUtils.deserializedSnapshotFooterRecord(record);
    assertEquals(footerRecord.version(), ControlRecordUtils.SNAPSHOT_HEADER_HIGHEST_VERSION);
    return countRecords;
}
Also used : SnapshotHeaderRecord(org.apache.kafka.common.message.SnapshotHeaderRecord) GrowableBufferSupplier(org.apache.kafka.common.utils.BufferSupplier.GrowableBufferSupplier) RecordBatch(org.apache.kafka.common.record.RecordBatch) SnapshotFooterRecord(org.apache.kafka.common.message.SnapshotFooterRecord) Record(org.apache.kafka.common.record.Record) SnapshotHeaderRecord(org.apache.kafka.common.message.SnapshotHeaderRecord) SnapshotFooterRecord(org.apache.kafka.common.message.SnapshotFooterRecord)

Example 2 with SnapshotHeaderRecord

use of org.apache.kafka.common.message.SnapshotHeaderRecord in project kafka by apache.

the class RecordsSnapshotWriter method initializeSnapshotWithHeader.

/**
 * Adds a {@link SnapshotHeaderRecord} to snapshot
 *
 * @throws IllegalStateException if the snapshot is not empty
 */
private void initializeSnapshotWithHeader() {
    if (snapshot.sizeInBytes() != 0) {
        String message = String.format("Initializing writer with a non-empty snapshot: id = '%s'.", snapshot.snapshotId());
        throw new IllegalStateException(message);
    }
    SnapshotHeaderRecord headerRecord = new SnapshotHeaderRecord().setVersion(ControlRecordUtils.SNAPSHOT_HEADER_HIGHEST_VERSION).setLastContainedLogTimestamp(lastContainedLogTimestamp);
    accumulator.appendSnapshotHeaderMessage(headerRecord, time.milliseconds());
    accumulator.forceDrain();
}
Also used : SnapshotHeaderRecord(org.apache.kafka.common.message.SnapshotHeaderRecord)

Aggregations

SnapshotHeaderRecord (org.apache.kafka.common.message.SnapshotHeaderRecord)2 SnapshotFooterRecord (org.apache.kafka.common.message.SnapshotFooterRecord)1 Record (org.apache.kafka.common.record.Record)1 RecordBatch (org.apache.kafka.common.record.RecordBatch)1 GrowableBufferSupplier (org.apache.kafka.common.utils.BufferSupplier.GrowableBufferSupplier)1