Search in sources :

Example 1 with RawSnapshotWriter

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

the class MockLogTest method testTruncateFullyToLatestSnapshot.

@Test
public void testTruncateFullyToLatestSnapshot() throws IOException {
    int numberOfRecords = 10;
    int epoch = 0;
    OffsetAndEpoch sameEpochSnapshotId = new OffsetAndEpoch(2 * numberOfRecords, epoch);
    appendBatch(numberOfRecords, epoch);
    try (RawSnapshotWriter snapshot = log.storeSnapshot(sameEpochSnapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.truncateToLatestSnapshot());
    assertEquals(sameEpochSnapshotId.offset, log.startOffset());
    assertEquals(sameEpochSnapshotId.epoch, log.lastFetchedEpoch());
    assertEquals(sameEpochSnapshotId.offset, log.endOffset().offset);
    assertEquals(sameEpochSnapshotId.offset, log.highWatermark().offset);
    OffsetAndEpoch greaterEpochSnapshotId = new OffsetAndEpoch(3 * numberOfRecords, epoch + 1);
    appendBatch(numberOfRecords, epoch);
    try (RawSnapshotWriter snapshot = log.storeSnapshot(greaterEpochSnapshotId).get()) {
        snapshot.freeze();
    }
    assertTrue(log.truncateToLatestSnapshot());
    assertEquals(greaterEpochSnapshotId.offset, log.startOffset());
    assertEquals(greaterEpochSnapshotId.epoch, log.lastFetchedEpoch());
    assertEquals(greaterEpochSnapshotId.offset, log.endOffset().offset);
    assertEquals(greaterEpochSnapshotId.offset, log.highWatermark().offset);
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 2 with RawSnapshotWriter

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

the class MockLogTest method testCreateSnapshotBeforeLogStartOffset.

@Test
public void testCreateSnapshotBeforeLogStartOffset() {
    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(new OffsetAndEpoch(numberOfRecords - 1, epoch)));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 3 with RawSnapshotWriter

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

the class MockLogTest method testValidateOffsetEqualToOldestSnapshotOffset.

@Test
public void testValidateOffsetEqualToOldestSnapshotOffset() throws IOException {
    int offset = 2;
    int epoch = 1;
    OffsetAndEpoch olderEpochSnapshotId = new OffsetAndEpoch(offset, epoch);
    try (RawSnapshotWriter snapshot = log.storeSnapshot(olderEpochSnapshotId).get()) {
        snapshot.freeze();
    }
    log.truncateToLatestSnapshot();
    ValidOffsetAndEpoch resultOffsetAndEpoch = log.validateOffsetAndEpoch(offset, epoch);
    assertEquals(ValidOffsetAndEpoch.Kind.VALID, resultOffsetAndEpoch.kind());
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) Test(org.junit.jupiter.api.Test)

Example 4 with RawSnapshotWriter

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

the class MockLogTest method testReadOutOfRangeOffset.

@Test
public void testReadOutOfRangeOffset() throws IOException {
    final long initialOffset = 5L;
    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));
    assertThrows(OffsetOutOfRangeException.class, () -> log.read(log.startOffset() - 1, Isolation.UNCOMMITTED));
    assertThrows(OffsetOutOfRangeException.class, () -> log.read(log.endOffset().offset + 1, Isolation.UNCOMMITTED));
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) SimpleRecord(org.apache.kafka.common.record.SimpleRecord) Test(org.junit.jupiter.api.Test)

Example 5 with RawSnapshotWriter

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

the class MockLogTest method testCreateSnapshot.

@Test
public void testCreateSnapshot() throws IOException {
    int numberOfRecords = 10;
    int epoch = 0;
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(numberOfRecords, epoch);
    appendBatch(numberOfRecords, epoch);
    log.updateHighWatermark(new LogOffsetMetadata(numberOfRecords));
    try (RawSnapshotWriter snapshot = log.createNewSnapshot(snapshotId).get()) {
        snapshot.freeze();
    }
    RawSnapshotReader snapshot = log.readSnapshot(snapshotId).get();
    assertEquals(0, snapshot.sizeInBytes());
}
Also used : RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) RawSnapshotReader(org.apache.kafka.snapshot.RawSnapshotReader) 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