Search in sources :

Example 1 with OffsetAndEpoch

use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.

the class SnapshotWriterReaderTest method testWritingSnapshot.

@Test
public void testWritingSnapshot() throws Exception {
    int recordsPerBatch = 3;
    int batches = 3;
    int delimiterCount = 2;
    long magicTimestamp = 0xDEADBEEF;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    List<List<String>> expected = buildRecords(recordsPerBatch, batches);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    for (List<String> batch : expected) {
        contextBuilder.appendToLog(id.epoch, batch);
    }
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    int epoch = context.currentEpoch();
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, magicTimestamp).get()) {
        assertEquals(id, snapshot.snapshotId());
        expected.forEach(batch -> assertDoesNotThrow(() -> snapshot.append(batch)));
        snapshot.freeze();
    }
    try (SnapshotReader<String> reader = readSnapshot(context, id, Integer.MAX_VALUE)) {
        RawSnapshotReader snapshot = context.log.readSnapshot(id).get();
        int recordCount = validateDelimiters(snapshot, magicTimestamp);
        assertEquals((recordsPerBatch * batches) + delimiterCount, recordCount);
        assertSnapshot(expected, reader);
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with OffsetAndEpoch

use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.

the class SnapshotWriterReaderTest method testAbortedSnapshot.

@Test
public void testAbortedSnapshot() throws Exception {
    int recordsPerBatch = 3;
    int batches = 3;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    List<List<String>> expected = buildRecords(recordsPerBatch, batches);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    for (List<String> batch : expected) {
        contextBuilder.appendToLog(id.epoch, batch);
    }
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    int epoch = context.currentEpoch();
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, 0).get()) {
        assertEquals(id, snapshot.snapshotId());
        expected.forEach(batch -> {
            assertDoesNotThrow(() -> snapshot.append(batch));
        });
    }
    assertEquals(Optional.empty(), context.log.readSnapshot(id));
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with OffsetAndEpoch

use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.

the class SnapshotsTest method testValidSnapshotFilename.

@Test
public void testValidSnapshotFilename() {
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
    Path path = Snapshots.snapshotPath(TestUtils.tempDirectory().toPath(), snapshotId);
    SnapshotPath snapshotPath = Snapshots.parse(path).get();
    assertEquals(path, snapshotPath.path);
    assertEquals(snapshotId, snapshotPath.snapshotId);
    assertFalse(snapshotPath.partial);
    assertFalse(snapshotPath.deleted);
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with OffsetAndEpoch

use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.

the class SnapshotsTest method testValidPartialSnapshotFilename.

@Test
public void testValidPartialSnapshotFilename() throws IOException {
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
    Path path = Snapshots.createTempFile(TestUtils.tempDirectory().toPath(), snapshotId);
    // Delete it as we only need the path for testing
    Files.delete(path);
    SnapshotPath snapshotPath = Snapshots.parse(path).get();
    assertEquals(path, snapshotPath.path);
    assertEquals(snapshotId, snapshotPath.snapshotId);
    assertTrue(snapshotPath.partial);
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) Path(java.nio.file.Path) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with OffsetAndEpoch

use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.

the class SnapshotsTest method testDeleteSnapshot.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testDeleteSnapshot(boolean renameBeforeDeleting) throws IOException {
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
    Path logDirPath = TestUtils.tempDirectory().toPath();
    try (FileRawSnapshotWriter snapshot = FileRawSnapshotWriter.create(logDirPath, snapshotId, Optional.empty())) {
        snapshot.freeze();
        Path snapshotPath = Snapshots.snapshotPath(logDirPath, snapshotId);
        assertTrue(Files.exists(snapshotPath));
        if (renameBeforeDeleting)
            // rename snapshot before deleting
            Utils.atomicMoveWithFallback(snapshotPath, Snapshots.deleteRename(snapshotPath, snapshotId), false);
        assertTrue(Snapshots.deleteIfExists(logDirPath, snapshot.snapshotId()));
        assertFalse(Files.exists(snapshotPath));
        assertFalse(Files.exists(Snapshots.deleteRename(snapshotPath, snapshotId)));
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) Path(java.nio.file.Path) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

OffsetAndEpoch (org.apache.kafka.raft.OffsetAndEpoch)25 Test (org.junit.jupiter.api.Test)22 Path (java.nio.file.Path)8 UnalignedMemoryRecords (org.apache.kafka.common.record.UnalignedMemoryRecords)8 QuorumState (org.apache.kafka.raft.QuorumState)7 ByteBuffer (java.nio.ByteBuffer)4 RaftClientTestContext (org.apache.kafka.raft.RaftClientTestContext)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Record (org.apache.kafka.common.record.Record)3 RecordBatch (org.apache.kafka.common.record.RecordBatch)3 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)3 UnalignedFileRecords (org.apache.kafka.common.record.UnalignedFileRecords)3 GrowableBufferSupplier (org.apache.kafka.common.utils.BufferSupplier.GrowableBufferSupplier)3 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Arrays (java.util.Arrays)2 Iterator (java.util.Iterator)2 Optional (java.util.Optional)2