ezhou413 commented on code in PR #23483:
URL: https://github.com/apache/kafka/pull/23483#discussion_r4603103391


##########
clients/src/main/java/org/apache/kafka/clients/producer/internals/ChunkedRecordAccumulator.java:
##########
@@ -284,10 +280,56 @@ public RecordAppendResult append(String topic,
         }
     }
 
+    /**
+     * Bytes of chunk capacity to allocate for a new batch's first record (see 
the new-batch path in
+     * {@link #append}).
+     * <p>
+     * Uncompressed, this is the record's upper bound, which is exact, and the 
first-record capacity check
+     * in {@link ChunkedProducerBatch#tryAppend} relies on it.
+     * <p>
+     * Compressed, per KIP-1332, it is the batch header plus the record's 
upper bound scaled by the topic's
+     * compression ratio estimate and padded by the 1.05 estimation factor: 
the same formula
+     * ({@link MemoryRecordsBuilder#estimatedCompressedSizeInBytes}) the 
batch's builder uses for its
+     * fullness checks, and so {@link 
ChunkedProducerBatch#extensionBytesNeeded} for later records. Sizing
+     * it to the uncompressed upper bound was a rejected alternative, because 
it over-reserves pool memory
+     * for data that compresses. This is only an estimate, so a compressor 
overshoot is absorbed by the
+     * growable stream growing mid-write. The estimate read here can differ 
from the one the batch reads in
+     * its constructor moments later, which {@link 
ChunkedProducerBatch#tryAppend} accepts for the same reason.
+     * <p>
+     * The compressed size is capped at the pool's total memory. {@code 
KafkaProducer.ensureValidRecordSize}
+     * bounds only the uncompressed upper bound by {@code buffer.memory}, so 
an estimate near or above 1.0
+     * could otherwise size a record that passed that check past the whole 
pool, which
+     * {@link BufferPool#allocateChunks} rejects. The stream is growable, so 
the cap is safe.
+     *
+     * @param firstRecordUpperBound the record's uncompressed upper bound, 
batch header included
+     */
+    private int newBatchChunkBytes(String topic, int firstRecordUpperBound) {
+        if (compression.type() == CompressionType.NONE)
+            return firstRecordUpperBound;
+        int batchHeaderSize = 
AbstractRecords.recordBatchHeaderSizeInBytes(RecordBatch.CURRENT_MAGIC_VALUE, 
compression.type());
+        int estimate = 
MemoryRecordsBuilder.estimatedCompressedSizeInBytes(batchHeaderSize,
+                firstRecordUpperBound - batchHeaderSize, 
CompressionRatioEstimator.estimation(topic, compression.type()));
+        return (int) Math.min(estimate, chunkedFree.totalMemory());

Review Comment:
   Agree, added the fix



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to