Search in sources :

Example 6 with RawSnapshotWriter

use of org.apache.kafka.snapshot.RawSnapshotWriter in project kafka by apache.

the class MockLogTest method testUpdateLogStartOffsetWillRemoveOlderSnapshot.

@Test
public void testUpdateLogStartOffsetWillRemoveOlderSnapshot() throws IOException {
    int numberOfRecords = 10;
    int epoch = 1;
    OffsetAndEpoch sameEpochSnapshotId = new OffsetAndEpoch(numberOfRecords, epoch);
    appendBatch(numberOfRecords, epoch);
    log.updateHighWatermark(new LogOffsetMetadata(sameEpochSnapshotId.offset));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(sameEpochSnapshotId).get()) {
        snapshot.freeze();
    }
    OffsetAndEpoch greaterEpochSnapshotId = new OffsetAndEpoch(2 * numberOfRecords, epoch + 1);
    appendBatch(numberOfRecords, greaterEpochSnapshotId.epoch);
    log.updateHighWatermark(new LogOffsetMetadata(greaterEpochSnapshotId.offset));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(greaterEpochSnapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.deleteBeforeSnapshot(greaterEpochSnapshotId));
    assertEquals(Optional.empty(), log.readSnapshot(sameEpochSnapshotId));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 7 with RawSnapshotWriter

use of org.apache.kafka.snapshot.RawSnapshotWriter in project kafka by apache.

the class MockLogTest method testUpdateLogStartOffset.

@Test
public void testUpdateLogStartOffset() throws IOException {
    int offset = 10;
    int epoch = 0;
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(offset, epoch);
    appendBatch(offset, epoch);
    log.updateHighWatermark(new LogOffsetMetadata(offset));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(snapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.deleteBeforeSnapshot(snapshotId));
    assertEquals(offset, log.startOffset());
    assertEquals(epoch, log.lastFetchedEpoch());
    assertEquals(offset, log.endOffset().offset);
    int newRecords = 10;
    appendBatch(newRecords, epoch + 1);
    log.updateHighWatermark(new LogOffsetMetadata(offset + newRecords));
    // Start offset should not change since a new snapshot was not generated
    assertFalse(log.deleteBeforeSnapshot(new OffsetAndEpoch(offset + newRecords, epoch)));
    assertEquals(offset, log.startOffset());
    assertEquals(epoch + 1, log.lastFetchedEpoch());
    assertEquals(offset + newRecords, log.endOffset().offset);
    assertEquals(offset + newRecords, log.highWatermark().offset);
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 8 with RawSnapshotWriter

use of org.apache.kafka.snapshot.RawSnapshotWriter in project kafka by apache.

the class MockLogTest method testCreateSnapshotMuchEalierEpoch.

@Test
public void testCreateSnapshotMuchEalierEpoch() {
    int numberOfRecords = 10;
    int epoch = 2;
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(numberOfRecords, epoch);
    appendBatch(numberOfRecords, epoch);
    log.updateHighWatermark(new LogOffsetMetadata(numberOfRecords));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(snapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.deleteBeforeSnapshot(snapshotId));
    assertEquals(snapshotId.offset, log.startOffset());
    assertThrows(IllegalArgumentException.class, () -> log.createNewSnapshot(new OffsetAndEpoch(numberOfRecords, epoch - 1)));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 9 with RawSnapshotWriter

use of org.apache.kafka.snapshot.RawSnapshotWriter in project kafka by apache.

the class MockLogTest method testAppendAsFollower.

@Test
public void testAppendAsFollower() throws IOException {
    final long initialOffset = 5;
    final int epoch = 3;
    SimpleRecord recordFoo = new SimpleRecord("foo".getBytes());
    try (RawSnapshotWriter snapshot = log.storeSnapshot(new OffsetAndEpoch(initialOffset, 0)).get()) {
        snapshot.freeze();
    }
    log.truncateToLatestSnapshot();
    log.appendAsFollower(MemoryRecords.withRecords(initialOffset, CompressionType.NONE, epoch, recordFoo));
    assertEquals(initialOffset, log.startOffset());
    assertEquals(initialOffset + 1, log.endOffset().offset);
    assertEquals(3, log.lastFetchedEpoch());
    Records records = log.read(5L, Isolation.UNCOMMITTED).records;
    List<ByteBuffer> extractRecords = new ArrayList<>();
    for (Record record : records.records()) {
        extractRecords.add(record.value());
    }
    assertEquals(1, extractRecords.size());
    assertEquals(recordFoo.value(), extractRecords.get(0));
    assertEquals(new OffsetAndEpoch(5, 0), log.endOffsetForEpoch(0));
    assertEquals(new OffsetAndEpoch(log.endOffset().offset, epoch), log.endOffsetForEpoch(epoch));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) ArrayList(java.util.ArrayList) Record(org.apache.kafka.common.record.Record) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) Records(org.apache.kafka.common.record.Records) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with RawSnapshotWriter

use of org.apache.kafka.snapshot.RawSnapshotWriter in project kafka by apache.

the class MockLogTest method testCreateExistingSnapshot.

@Test
public void testCreateExistingSnapshot() {
    int numberOfRecords = 10;
    int epoch = 1;
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(numberOfRecords, epoch);
    appendBatch(numberOfRecords, epoch);
    log.updateHighWatermark(new LogOffsetMetadata(numberOfRecords));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(snapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.deleteBeforeSnapshot(snapshotId));
    assertEquals(snapshotId.offset, log.startOffset());
    assertEquals(Optional.empty(), log.createNewSnapshot(snapshotId));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Aggregations

RawSnapshotWriter (org.apache.kafka.snapshot.RawSnapshotWriter)19 Test (org.junit.jupiter.api.Test)17 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)2 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 OptionalInt (java.util.OptionalInt)1 FetchSnapshotResponseData (org.apache.kafka.common.message.FetchSnapshotResponseData)1 ApiMessage (org.apache.kafka.common.protocol.ApiMessage)1 Errors (org.apache.kafka.common.protocol.Errors)1 Record (org.apache.kafka.common.record.Record)1 Records (org.apache.kafka.common.record.Records)1 UnalignedMemoryRecords (org.apache.kafka.common.record.UnalignedMemoryRecords)1 RawSnapshotReader (org.apache.kafka.snapshot.RawSnapshotReader)1