jsancio commented on a change in pull request #9819:
URL: https://github.com/apache/kafka/pull/9819#discussion_r560420440



##########
File path: raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java
##########
@@ -1220,34 +1216,35 @@ private FetchSnapshotResponseData 
handleFetchSnapshotRequest(
         if (!snapshotOpt.isPresent()) {
             return FetchSnapshotResponse.singleton(
                 log.topicPartition(),
-                responsePartitionSnapshot -> {
-                    return addQuorumLeader(responsePartitionSnapshot)
-                        .setErrorCode(Errors.SNAPSHOT_NOT_FOUND.code());
-                }
+                responsePartitionSnapshot -> 
addQuorumLeader(responsePartitionSnapshot)
+                    .setErrorCode(Errors.SNAPSHOT_NOT_FOUND.code())
             );
         }
 
         try (RawSnapshotReader snapshot = snapshotOpt.get()) {
             if (partitionSnapshot.position() < 0 || 
partitionSnapshot.position() >= snapshot.sizeInBytes()) {
                 return FetchSnapshotResponse.singleton(
                     log.topicPartition(),
-                    responsePartitionSnapshot -> {
-                        return addQuorumLeader(responsePartitionSnapshot)
-                            .setErrorCode(Errors.POSITION_OUT_OF_RANGE.code());
-                    }
+                    responsePartitionSnapshot -> 
addQuorumLeader(responsePartitionSnapshot)
+                        .setErrorCode(Errors.POSITION_OUT_OF_RANGE.code())
                 );
             }
 
             int maxSnapshotSize;
+            int maxSnapshotPosition;
             try {
                 maxSnapshotSize = Math.toIntExact(snapshot.sizeInBytes());
             } catch (ArithmeticException e) {
                 maxSnapshotSize = Integer.MAX_VALUE;
             }
 
-            ByteBuffer buffer = ByteBuffer.allocate(Math.min(data.maxBytes(), 
maxSnapshotSize));
-            snapshot.read(buffer, partitionSnapshot.position());
-            buffer.flip();
+            try {
+                maxSnapshotPosition = 
Math.toIntExact(partitionSnapshot.position());
+            } catch (ArithmeticException e) {
+                maxSnapshotPosition = Integer.MAX_VALUE;

Review comment:
       Resetting the position to `Integer.MAX_VALUE` doesn't seem safe. I think 
the best we can do is let the exception bubble up.
   
   Related to this, shouldn't the position be a long in 
`RawSnapshotReader::read`?

##########
File path: clients/src/main/java/org/apache/kafka/common/record/BaseRegion.java
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.common.record;
+
+import org.apache.kafka.common.network.TransferableChannel;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+
+/**
+ * Base interface for accessing raft snapshot which could be file region or an 
in-memory buffers.
+ */
+public interface BaseRegion {

Review comment:
       I don't think this interface is used or needed. Are you planning to 
remove it?

##########
File path: 
raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java
##########
@@ -53,13 +56,20 @@ public long sizeInBytes() throws IOException {
     }
 
     @Override
-    public void append(ByteBuffer buffer) throws IOException {
+    public void append(BaseRecords records) throws IOException {
         if (frozen) {
             throw new IllegalStateException(
-                String.format("Append is not supported. Snapshot is already 
frozen: id = %s; temp path = %s", snapshotId, tempSnapshotPath)
+                    String.format("Append is not supported. Snapshot is 
already frozen: id = %s; temp path = %s", snapshotId, tempSnapshotPath)
             );
         }
-
+        ByteBuffer buffer;
+        if (records instanceof MemoryRecords) {
+            buffer = ((MemoryRecords) records).buffer();
+        } else {
+            buffer = ByteBuffer.allocate(records.sizeInBytes());
+            ((FileRecords) records).channel().read(buffer);
+            buffer.flip();
+        }

Review comment:
       What do you think about keeping this signature as `void 
append(ByterBuffer buffer)`, having a static function that knows how to convert 
a `BaseRecords` to `ByteBuffer` and moving this responsibility to the 
`KafkaRaftClient`?
   
   If I am not mistake, as of right now it is only the `KafkaRaftClient` that 
needs to deal with this mismatch.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to