You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "dajac (via GitHub)" <gi...@apache.org> on 2023/11/30 20:16:39 UTC

[PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

dajac opened a new pull request, #14885:
URL: https://github.com/apache/kafka/pull/14885

   This patch adds a ThreadLocal with a GrowableBufferSupplier so that each writing thread can reuse the same buffer instead of allocating a new one for each write. The patch relies on existing tests.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414500105


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   For more context on 4. There are assumptions in the code that a request is handled on the same thread so ByteBuffers are not thread safe.
   
   There was a bug in KIP-890 (see [here](https://issues.apache.org/jira/browse/KAFKA-15653)) where we used the wrong byte buffer and it caused NPEs and other exceptions.
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414500105


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   For more context on 4. There are assumptions in the code that a request is handled on the same thread so ByteBuffers are not thread safe.
   
   There was a bug in KIP-890 (see [here](https://issues.apache.org/jira/browse/KAFKA-15653)) where we used the wrong byte buffer and it caused NPEs and other exceptions.
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414519712


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   Just for my understanding though -- in the past we relied on RequestLocal for this sort of thing. Is the idea to move to ThreadLocal here so it is safer and we don't run into things like https://issues.apache.org/jira/browse/KAFKA-15653 ?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414500105


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   For more context on 4. There are assumptions in the code that a request is handled on the same thread. 
   There was a bug in KIP-890 (see [here](https://issues.apache.org/jira/browse/KAFKA-15653)) where we used the wrong byte buffer and it caused NPEs and other exceptions.
   
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jeffkbkim (via GitHub)" <gi...@apache.org>.
jeffkbkim commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1412575325


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   some general questions for my understanding:
   
   1. We are introducing a buffer supplier so that we can reuse the same buffer and not reallocate a new buffer each time.
   2. It's a growable buffer supplier since maxBatchSize may change on subsequent append()
   3. we're adding a try-finally block to always clear the buffer in the case of exceptions. this was not needed previously since we were not using a buffer supplier
   4. it's a thread local supplier because we don't want the buffer to be reused across different threads - i don't think i fully understand the repercussions
   
   is 1-3 correct? 4 is the actual question. thanks!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414553902


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -126,57 +129,63 @@ class CoordinatorPartitionWriter[T](
         val magic = logConfig.recordVersion.value
         val maxBatchSize = logConfig.maxMessageSize
         val currentTimeMs = time.milliseconds()
-
-        val recordsBuilder = MemoryRecords.builder(
-          ByteBuffer.allocate(math.min(16384, maxBatchSize)),
-          magic,
-          compressionType,
-          TimestampType.CREATE_TIME,
-          0L,
-          time.milliseconds(),
-          producerId,
-          producerEpoch,
-          0,
-          producerId != RecordBatch.NO_PRODUCER_ID,
-          RecordBatch.NO_PARTITION_LEADER_EPOCH
-        )
-
-        records.forEach { record =>
-          val keyBytes = serializer.serializeKey(record)
-          val valueBytes = serializer.serializeValue(record)
-
-          if (recordsBuilder.hasRoomFor(currentTimeMs, keyBytes, valueBytes, EMPTY_HEADERS)) recordsBuilder.append(
-            currentTimeMs,
-            keyBytes,
-            valueBytes,
-            EMPTY_HEADERS
-          ) else throw new RecordTooLargeException(s"Message batch size is ${recordsBuilder.estimatedSizeInBytes()} bytes " +
-            s"in append to partition $tp which exceeds the maximum configured size of $maxBatchSize.")
+        val bufferSupplier = threadLocalBufferSupplier.get()
+        val buffer = bufferSupplier.get(math.min(16384, maxBatchSize))

Review Comment:
   That’s right. I actually took it from here: https://github.com/apache/kafka/blob/a83bc2d977d2af85d4edfc8096854137481001e9/core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala#L177



##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -126,57 +129,63 @@ class CoordinatorPartitionWriter[T](
         val magic = logConfig.recordVersion.value
         val maxBatchSize = logConfig.maxMessageSize
         val currentTimeMs = time.milliseconds()
-
-        val recordsBuilder = MemoryRecords.builder(
-          ByteBuffer.allocate(math.min(16384, maxBatchSize)),
-          magic,
-          compressionType,
-          TimestampType.CREATE_TIME,
-          0L,
-          time.milliseconds(),
-          producerId,
-          producerEpoch,
-          0,
-          producerId != RecordBatch.NO_PRODUCER_ID,
-          RecordBatch.NO_PARTITION_LEADER_EPOCH
-        )
-
-        records.forEach { record =>
-          val keyBytes = serializer.serializeKey(record)
-          val valueBytes = serializer.serializeValue(record)
-
-          if (recordsBuilder.hasRoomFor(currentTimeMs, keyBytes, valueBytes, EMPTY_HEADERS)) recordsBuilder.append(
-            currentTimeMs,
-            keyBytes,
-            valueBytes,
-            EMPTY_HEADERS
-          ) else throw new RecordTooLargeException(s"Message batch size is ${recordsBuilder.estimatedSizeInBytes()} bytes " +
-            s"in append to partition $tp which exceeds the maximum configured size of $maxBatchSize.")
+        val bufferSupplier = threadLocalBufferSupplier.get()
+        val buffer = bufferSupplier.get(math.min(16384, maxBatchSize))

Review Comment:
   That’s right. I actually took it from here: https://github.com/apache/kafka/blob/a83bc2d977d2af85d4edfc8096854137481001e9/core/src/main/scala/kafka/coordinator/transaction/TransactionStateManager.scala#L177



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jeffkbkim (via GitHub)" <gi...@apache.org>.
jeffkbkim commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414507880


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   makes sense. thanks!



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414550451


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   That’s right.



##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   That’s right.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1414508061


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -126,57 +129,63 @@ class CoordinatorPartitionWriter[T](
         val magic = logConfig.recordVersion.value
         val maxBatchSize = logConfig.maxMessageSize
         val currentTimeMs = time.milliseconds()
-
-        val recordsBuilder = MemoryRecords.builder(
-          ByteBuffer.allocate(math.min(16384, maxBatchSize)),
-          magic,
-          compressionType,
-          TimestampType.CREATE_TIME,
-          0L,
-          time.milliseconds(),
-          producerId,
-          producerEpoch,
-          0,
-          producerId != RecordBatch.NO_PRODUCER_ID,
-          RecordBatch.NO_PARTITION_LEADER_EPOCH
-        )
-
-        records.forEach { record =>
-          val keyBytes = serializer.serializeKey(record)
-          val valueBytes = serializer.serializeValue(record)
-
-          if (recordsBuilder.hasRoomFor(currentTimeMs, keyBytes, valueBytes, EMPTY_HEADERS)) recordsBuilder.append(
-            currentTimeMs,
-            keyBytes,
-            valueBytes,
-            EMPTY_HEADERS
-          ) else throw new RecordTooLargeException(s"Message batch size is ${recordsBuilder.estimatedSizeInBytes()} bytes " +
-            s"in append to partition $tp which exceeds the maximum configured size of $maxBatchSize.")
+        val bufferSupplier = threadLocalBufferSupplier.get()
+        val buffer = bufferSupplier.get(math.min(16384, maxBatchSize))

Review Comment:
   Is this 16384 just the default batch size?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac commented on code in PR #14885:
URL: https://github.com/apache/kafka/pull/14885#discussion_r1413633150


##########
core/src/main/scala/kafka/coordinator/group/CoordinatorPartitionWriter.scala:
##########
@@ -66,6 +65,10 @@ class CoordinatorPartitionWriter[T](
   compressionType: CompressionType,
   time: Time
 ) extends PartitionWriter[T] {
+  private val threadLocalBufferSupplier = ThreadLocal.withInitial(
+    () => new BufferSupplier.GrowableBufferSupplier()
+  )

Review Comment:
   1. Yes.
   2. Yes.
   3. Yes.
   4. Each thread must have its own buffer as a given buffer cannot be used by two threads at the same time.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] KAFKA-15061; CoordinatorPartitionWriter should reuse buffer [kafka]

Posted by "dajac (via GitHub)" <gi...@apache.org>.
dajac merged PR #14885:
URL: https://github.com/apache/kafka/pull/14885


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org