Search in sources :

Example 11 with OffsetAndEpoch

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

the class FileRawSnapshotTest method testWritingSnapshot.

@Test
public void testWritingSnapshot() throws IOException {
    OffsetAndEpoch offsetAndEpoch = new OffsetAndEpoch(10L, 3);
    int bufferSize = 256;
    int numberOfBatches = 10;
    int expectedSize = 0;
    try (FileRawSnapshotWriter snapshot = createSnapshotWriter(tempDir, offsetAndEpoch)) {
        assertEquals(0, snapshot.sizeInBytes());
        UnalignedMemoryRecords records = buildRecords(ByteBuffer.wrap(randomBytes(bufferSize)));
        for (int i = 0; i < numberOfBatches; i++) {
            snapshot.append(records);
            expectedSize += records.sizeInBytes();
        }
        assertEquals(expectedSize, snapshot.sizeInBytes());
        snapshot.freeze();
    }
    // File should exist and the size should be the sum of all the buffers
    assertTrue(Files.exists(Snapshots.snapshotPath(tempDir, offsetAndEpoch)));
    assertEquals(expectedSize, Files.size(Snapshots.snapshotPath(tempDir, offsetAndEpoch)));
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords) Test(org.junit.jupiter.api.Test)

Example 12 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 13 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 14 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)

Example 15 with OffsetAndEpoch

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

the class Snapshots method parse.

public static Optional<SnapshotPath> parse(Path path) {
    Path filename = path.getFileName();
    if (filename == null) {
        return Optional.empty();
    }
    String name = filename.toString();
    boolean partial = false;
    boolean deleted = false;
    if (name.endsWith(PARTIAL_SUFFIX)) {
        partial = true;
    } else if (name.endsWith(DELETE_SUFFIX)) {
        deleted = true;
    } else if (!name.endsWith(SUFFIX)) {
        return Optional.empty();
    }
    long endOffset = Long.parseLong(name.substring(0, OFFSET_WIDTH));
    int epoch = Integer.parseInt(name.substring(OFFSET_WIDTH + 1, OFFSET_WIDTH + EPOCH_WIDTH + 1));
    return Optional.of(new SnapshotPath(path, new OffsetAndEpoch(endOffset, epoch), partial, deleted));
}
Also used : Path(java.nio.file.Path) OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch)

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