lianetm commented on code in PR #23483:
URL: https://github.com/apache/kafka/pull/23483#discussion_r4566210643
##########
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:
for the totalMemory cap, shouldn't we think in chunks? (meaning, the pool
totalMemory may be enough for the record size, but once we convert that to full
chunks, it may be more memory than what the pool can give out, right?)
This relates to the gap @Nikita-Shupletsov filed here
https://issues.apache.org/jira/browse/KAFKA-21459 , but still seems we should
fix this compression-related one here (and still have that issue to review the
uncompressed path as it mentions).
As for the fix here, I guess we can just use totalMem / chunkSize *
chunkSize, wdyt?
Also let's add a test to cover this case (compressed record estimate fits in
the available pool total mem, but then needs more whole chunks than the pool
can give out). It should still be accepted I expect, (growing from the heap as
needed)
--
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]