Search in sources :

Example 1 with UnalignedFileRecords

use of org.apache.kafka.common.record.UnalignedFileRecords in project kafka by apache.

the class FileRawSnapshotTest method testPartialWriteReadSnapshot.

@Test
public void testPartialWriteReadSnapshot() throws IOException {
    Path tempDir = TestUtils.tempDirectory().toPath();
    OffsetAndEpoch offsetAndEpoch = new OffsetAndEpoch(10L, 3);
    ByteBuffer records = buildRecords(ByteBuffer.wrap(Utils.utf8("foo"))).buffer();
    ByteBuffer expectedBuffer = ByteBuffer.wrap(records.array());
    ByteBuffer buffer1 = expectedBuffer.duplicate();
    buffer1.position(0);
    buffer1.limit(expectedBuffer.limit() / 2);
    ByteBuffer buffer2 = expectedBuffer.duplicate();
    buffer2.position(expectedBuffer.limit() / 2);
    buffer2.limit(expectedBuffer.limit());
    try (FileRawSnapshotWriter snapshot = createSnapshotWriter(tempDir, offsetAndEpoch)) {
        snapshot.append(new UnalignedMemoryRecords(buffer1));
        snapshot.append(new UnalignedMemoryRecords(buffer2));
        snapshot.freeze();
    }
    try (FileRawSnapshotReader snapshot = FileRawSnapshotReader.open(tempDir, offsetAndEpoch)) {
        int totalSize = Math.toIntExact(snapshot.sizeInBytes());
        assertEquals(expectedBuffer.remaining(), totalSize);
        UnalignedFileRecords record1 = (UnalignedFileRecords) snapshot.slice(0, totalSize / 2);
        UnalignedFileRecords record2 = (UnalignedFileRecords) snapshot.slice(totalSize / 2, totalSize - totalSize / 2);
        assertEquals(buffer1, TestUtils.toBuffer(record1));
        assertEquals(buffer2, TestUtils.toBuffer(record2));
        ByteBuffer readBuffer = ByteBuffer.allocate(record1.sizeInBytes() + record2.sizeInBytes());
        readBuffer.put(TestUtils.toBuffer(record1));
        readBuffer.put(TestUtils.toBuffer(record2));
        readBuffer.flip();
        assertEquals(expectedBuffer, readBuffer);
    }
}
Also used : Path(java.nio.file.Path) OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) UnalignedFileRecords(org.apache.kafka.common.record.UnalignedFileRecords) UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 UnalignedFileRecords (org.apache.kafka.common.record.UnalignedFileRecords)1 UnalignedMemoryRecords (org.apache.kafka.common.record.UnalignedMemoryRecords)1 OffsetAndEpoch (org.apache.kafka.raft.OffsetAndEpoch)1 Test (org.junit.jupiter.api.Test)1