Search in sources :

Example 11 with UnalignedMemoryRecords

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

the class SendBuilderTest method testZeroCopyUnalignedRecords.

@Test
public void testZeroCopyUnalignedRecords() {
    ByteBuffer buffer = ByteBuffer.allocate(128);
    MemoryRecords records = createRecords(buffer, "foo");
    ByteBuffer buffer1 = records.buffer().duplicate();
    buffer1.limit(buffer1.limit() / 2);
    ByteBuffer buffer2 = records.buffer().duplicate();
    buffer2.position(buffer2.limit() / 2);
    UnalignedMemoryRecords records1 = new UnalignedMemoryRecords(buffer1);
    UnalignedMemoryRecords records2 = new UnalignedMemoryRecords(buffer2);
    SendBuilder builder = new SendBuilder(8);
    builder.writeInt(5);
    builder.writeRecords(records1);
    builder.writeRecords(records2);
    builder.writeInt(15);
    Send send = builder.build();
    // Overwrite the original buffer in order to prove the data was not copied
    buffer.rewind();
    MemoryRecords overwrittenRecords = createRecords(buffer, "bar");
    ByteBuffer readBuffer = TestUtils.toBuffer(send);
    assertEquals(5, readBuffer.getInt());
    assertEquals(overwrittenRecords, getRecords(readBuffer, records.sizeInBytes()));
    assertEquals(15, readBuffer.getInt());
}
Also used : UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords) ByteBuffer(java.nio.ByteBuffer) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords) Send(org.apache.kafka.common.network.Send) Test(org.junit.jupiter.api.Test)

Example 12 with UnalignedMemoryRecords

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

the class KafkaRaftClient method handleFetchSnapshotResponse.

private boolean handleFetchSnapshotResponse(RaftResponse.Inbound responseMetadata, long currentTimeMs) {
    FetchSnapshotResponseData data = (FetchSnapshotResponseData) responseMetadata.data;
    Errors topLevelError = Errors.forCode(data.errorCode());
    if (topLevelError != Errors.NONE) {
        return handleTopLevelError(topLevelError, responseMetadata);
    }
    if (data.topics().size() != 1 && data.topics().get(0).partitions().size() != 1) {
        return false;
    }
    Optional<FetchSnapshotResponseData.PartitionSnapshot> partitionSnapshotOpt = FetchSnapshotResponse.forTopicPartition(data, log.topicPartition());
    if (!partitionSnapshotOpt.isPresent()) {
        return false;
    }
    FetchSnapshotResponseData.PartitionSnapshot partitionSnapshot = partitionSnapshotOpt.get();
    FetchSnapshotResponseData.LeaderIdAndEpoch currentLeaderIdAndEpoch = partitionSnapshot.currentLeader();
    OptionalInt responseLeaderId = optionalLeaderId(currentLeaderIdAndEpoch.leaderId());
    int responseEpoch = currentLeaderIdAndEpoch.leaderEpoch();
    Errors error = Errors.forCode(partitionSnapshot.errorCode());
    Optional<Boolean> handled = maybeHandleCommonResponse(error, responseLeaderId, responseEpoch, currentTimeMs);
    if (handled.isPresent()) {
        return handled.get();
    }
    FollowerState state = quorum.followerStateOrThrow();
    if (Errors.forCode(partitionSnapshot.errorCode()) == Errors.SNAPSHOT_NOT_FOUND || partitionSnapshot.snapshotId().endOffset() < 0 || partitionSnapshot.snapshotId().epoch() < 0) {
        /* The leader deleted the snapshot before the follower could download it. Start over by
             * reseting the fetching snapshot state and sending another fetch request.
             */
        logger.trace("Leader doesn't know about snapshot id {}, returned error {} and snapshot id {}", state.fetchingSnapshot(), partitionSnapshot.errorCode(), partitionSnapshot.snapshotId());
        state.setFetchingSnapshot(Optional.empty());
        state.resetFetchTimeout(currentTimeMs);
        return true;
    }
    OffsetAndEpoch snapshotId = new OffsetAndEpoch(partitionSnapshot.snapshotId().endOffset(), partitionSnapshot.snapshotId().epoch());
    RawSnapshotWriter snapshot;
    if (state.fetchingSnapshot().isPresent()) {
        snapshot = state.fetchingSnapshot().get();
    } else {
        throw new IllegalStateException(String.format("Received unexpected fetch snapshot response: %s", partitionSnapshot));
    }
    if (!snapshot.snapshotId().equals(snapshotId)) {
        throw new IllegalStateException(String.format("Received fetch snapshot response with an invalid id. Expected %s; Received %s", snapshot.snapshotId(), snapshotId));
    }
    if (snapshot.sizeInBytes() != partitionSnapshot.position()) {
        throw new IllegalStateException(String.format("Received fetch snapshot response with an invalid position. Expected %s; Received %s", snapshot.sizeInBytes(), partitionSnapshot.position()));
    }
    final UnalignedMemoryRecords records;
    if (partitionSnapshot.unalignedRecords() instanceof MemoryRecords) {
        records = new UnalignedMemoryRecords(((MemoryRecords) partitionSnapshot.unalignedRecords()).buffer());
    } else if (partitionSnapshot.unalignedRecords() instanceof UnalignedMemoryRecords) {
        records = (UnalignedMemoryRecords) partitionSnapshot.unalignedRecords();
    } else {
        throw new IllegalStateException(String.format("Received unexpected fetch snapshot response: %s", partitionSnapshot));
    }
    snapshot.append(records);
    if (snapshot.sizeInBytes() == partitionSnapshot.size()) {
        // Finished fetching the snapshot.
        snapshot.freeze();
        state.setFetchingSnapshot(Optional.empty());
        if (log.truncateToLatestSnapshot()) {
            updateFollowerHighWatermark(state, OptionalLong.of(log.highWatermark().offset));
        } else {
            throw new IllegalStateException(String.format("Full log truncation expected but didn't happen. Snapshot of %s, log end offset %s, last fetched %s", snapshot.snapshotId(), log.endOffset(), log.lastFetchedEpoch()));
        }
    }
    state.resetFetchTimeout(currentTimeMs);
    return true;
}
Also used : FetchSnapshotResponseData(org.apache.kafka.common.message.FetchSnapshotResponseData) OptionalInt(java.util.OptionalInt) Errors(org.apache.kafka.common.protocol.Errors) RawSnapshotWriter(org.apache.kafka.snapshot.RawSnapshotWriter) UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords) MemoryRecords(org.apache.kafka.common.record.MemoryRecords) UnalignedMemoryRecords(org.apache.kafka.common.record.UnalignedMemoryRecords)

Aggregations

UnalignedMemoryRecords (org.apache.kafka.common.record.UnalignedMemoryRecords)12 Test (org.junit.jupiter.api.Test)10 OffsetAndEpoch (org.apache.kafka.raft.OffsetAndEpoch)7 ByteBuffer (java.nio.ByteBuffer)6 FetchSnapshotResponseData (org.apache.kafka.common.message.FetchSnapshotResponseData)3 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)3 Path (java.nio.file.Path)2 Record (org.apache.kafka.common.record.Record)2 RecordBatch (org.apache.kafka.common.record.RecordBatch)2 SimpleRecord (org.apache.kafka.common.record.SimpleRecord)2 UnalignedFileRecords (org.apache.kafka.common.record.UnalignedFileRecords)2 GrowableBufferSupplier (org.apache.kafka.common.utils.BufferSupplier.GrowableBufferSupplier)2 RawSnapshotReader (org.apache.kafka.snapshot.RawSnapshotReader)2 SnapshotWriterReaderTest (org.apache.kafka.snapshot.SnapshotWriterReaderTest)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1