You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "Yunyung (via GitHub)" <gi...@apache.org> on 2025/09/11 15:42:03 UTC

[PR] KAFKA-19644: Enhance the documentation for producer headers [kafka]

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

   - Improve the docs for Record Headers
   - Add integration tests to verify that the order of headers in a record is preserved when producing and consuming.
   
   
   Jira: https://issues.apache.org/jira/browse/KAFKA-19644


-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Header.java:
##########
@@ -16,6 +16,9 @@
  */
 package org.apache.kafka.common.header;
 
+/**
+ * The key of a header is guaranteed to be non-null, while the value of a header may be null.

Review Comment:
   Updated. 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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerTest.java:
##########
@@ -217,6 +231,33 @@ var record = new ProducerRecord<>(TP.topic(), TP.partition(), null, "key".getByt
         }
     }
 
+    private void testHeadersOrderPreserved(Map<String, Object> consumerConfig) throws Exception {
+        var numRecords = 1;
+
+        try (Producer<byte[], byte[]> producer = cluster.producer();
+             Consumer<byte[], byte[]> consumer = cluster.consumer(consumerConfig)
+        ) {
+            var record = new ProducerRecord<>(TP.topic(), TP.partition(), null, "key".getBytes(), "value".getBytes());
+            record.headers().add("headerKey", "headerValue".getBytes());
+            record.headers().add("headerKey2", "headerValue2".getBytes());
+            record.headers().add("headerKey3", "headerValue3".getBytes());
+            producer.send(record);
+
+            assertEquals(0, consumer.assignment().size());
+            consumer.assign(List.of(TP));
+            assertEquals(1, consumer.assignment().size());
+
+            consumer.seek(TP, 0);
+            var records = consumeRecords(consumer, numRecords);
+            assertEquals(numRecords, records.size());

Review Comment:
   nit: the current approach works fine since `consumeRecords` waits until the record arrives.
   Just wondering if adding `producer.flush()` might make the intent slightly clearer?



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

Posted by "Yunyung (via GitHub)" <gi...@apache.org>.
Yunyung commented on PR #20524:
URL: https://github.com/apache/kafka/pull/20524#issuecomment-3284791514

   Thanks for the review, all! Updated based on the comments.


-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerTest.java:
##########
@@ -195,6 +195,20 @@ public void testAsyncConsumerHeaders() throws Exception {
         ));
     }
 
+    @ClusterTest
+    public void testClassicConsumerHeadersOrderPreserved() throws Exception {
+        testHeadersOrderPreserved(Map.of(
+            GROUP_PROTOCOL_CONFIG, GroupProtocol.CLASSIC.name().toLowerCase(Locale.ROOT)
+        ));
+    }
+
+    @ClusterTest
+    public void testAsyncConsumerHeadersOrderPreserved() throws Exception {
+        testHeadersOrderPreserved(Map.of(
+            GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name().toLowerCase(Locale.ROOT)
+        ));
+    }

Review Comment:
   Could you move `testClassicConsumerHeadersOrderPreserved` and `testAsyncConsumerHeadersOrderPreserved` methods under the `testHeaders` test and group the related ones together?



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerTest.java:
##########
@@ -217,6 +231,33 @@ var record = new ProducerRecord<>(TP.topic(), TP.partition(), null, "key".getByt
         }
     }
 
+    private void testHeadersOrderPreserved(Map<String, Object> consumerConfig) throws Exception {
+        var numRecords = 1;
+
+        try (Producer<byte[], byte[]> producer = cluster.producer();
+             Consumer<byte[], byte[]> consumer = cluster.consumer(consumerConfig)
+        ) {
+            var record = new ProducerRecord<>(TP.topic(), TP.partition(), null, "key".getBytes(), "value".getBytes());
+            record.headers().add("headerKey", "headerValue".getBytes());
+            record.headers().add("headerKey2", "headerValue2".getBytes());
+            record.headers().add("headerKey3", "headerValue3".getBytes());
+            producer.send(record);
+
+            assertEquals(0, consumer.assignment().size());
+            consumer.assign(List.of(TP));
+            assertEquals(1, consumer.assignment().size());
+
+            consumer.seek(TP, 0);
+            var records = consumeRecords(consumer, numRecords);
+            assertEquals(numRecords, records.size());

Review Comment:
   Done.



##########
docs/implementation.html:
##########
@@ -101,6 +101,7 @@ <h5 class="anchor-heading"><a id="recordheader" class="anchor-link"></a><a href=
 headerKey: String
 headerValueLength: varint
 Value: byte[]</code></pre>
+    <p>The key of a record header is guaranteed to be non-null, while the value of a record header may be null. The order of headers in a record is preserved when producing and consuming.</p>

Review Comment:
   Done.



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/test/java/org/apache/kafka/common/header/internals/RecordHeadersTest.java:
##########
@@ -47,6 +47,21 @@ public void testAdd() {
         assertEquals(2, getCount(headers));
     }
 
+    @Test
+    public void testAddHeadersPreserveOder() {

Review Comment:
   Done.



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Headers.java:
##########
@@ -49,15 +55,15 @@ public interface Headers extends Iterable<Header> {
     /**
      * Returns just one (the very last) header for the given key, if present.
      * 
-     * @param key to get the last header for.
+     * @param key to get the last header for; must not be null.
      * @return this last header matching the given key, returns null if not present.
      */
     Header lastHeader(String key);
 
     /**
      * Returns all headers for the given key, in the order they were added in, if present.
      *
-     * @param key to return the headers for.
+     * @param key to return the headers for; must not be null.
      * @return all headers for the given key, in the order they were added in, if NO headers are present an empty iterable is returned. 
      */
     Iterable<Header> headers(String key);

Review Comment:
   Could you please add documents and ut for the `Iterable#remove`?



##########
clients/src/test/java/org/apache/kafka/common/header/internals/RecordHeadersTest.java:
##########
@@ -47,6 +47,21 @@ public void testAdd() {
         assertEquals(2, getCount(headers));
     }
 
+    @Test
+    public void testAddHeadersPreserveOder() {

Review Comment:
   nit: `Oder` -> `Order`



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Headers.java:
##########
@@ -49,16 +56,17 @@ public interface Headers extends Iterable<Header> {
     /**
      * Returns just one (the very last) header for the given key, if present.
      * 
-     * @param key to get the last header for.
+     * @param key to get the last header for; must not be null.
      * @return this last header matching the given key, returns null if not present.
      */
     Header lastHeader(String key);
 
     /**
      * Returns all headers for the given key, in the order they were added in, if present.
+     * The returned iterators {@link org.apache.kafka.common.utils.AbstractIterator#remove()} always throws {@link UnsupportedOperationException}.

Review Comment:
   Good point. Updated.



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Headers.java:
##########
@@ -49,16 +56,17 @@ public interface Headers extends Iterable<Header> {
     /**
      * Returns just one (the very last) header for the given key, if present.
      * 
-     * @param key to get the last header for.
+     * @param key to get the last header for; must not be null.
      * @return this last header matching the given key, returns null if not present.
      */
     Header lastHeader(String key);
 
     /**
      * Returns all headers for the given key, in the order they were added in, if present.
+     * The returned iterators {@link org.apache.kafka.common.utils.AbstractIterator#remove()} always throws {@link UnsupportedOperationException}.

Review Comment:
   `AbstractIterator` is not a public API. Perhaps we could just state the `Iterable#remove` is unsupported?



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
docs/implementation.html:
##########
@@ -101,6 +101,7 @@ <h5 class="anchor-heading"><a id="recordheader" class="anchor-link"></a><a href=
 headerKey: String
 headerValueLength: varint
 Value: byte[]</code></pre>
+    <p>The key of a record header is guaranteed to be non-null, while the value of a record header may be null. The order of headers in a record is preserved when producing and consuming.</p>

Review Comment:
   Could you please add comments to the public API `Header`? 



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/PlaintextConsumerTest.java:
##########
@@ -195,6 +195,20 @@ public void testAsyncConsumerHeaders() throws Exception {
         ));
     }
 
+    @ClusterTest
+    public void testClassicConsumerHeadersOrderPreserved() throws Exception {
+        testHeadersOrderPreserved(Map.of(
+            GROUP_PROTOCOL_CONFIG, GroupProtocol.CLASSIC.name().toLowerCase(Locale.ROOT)
+        ));
+    }
+
+    @ClusterTest
+    public void testAsyncConsumerHeadersOrderPreserved() throws Exception {
+        testHeadersOrderPreserved(Map.of(
+            GROUP_PROTOCOL_CONFIG, GroupProtocol.CONSUMER.name().toLowerCase(Locale.ROOT)
+        ));
+    }

Review Comment:
   Done.



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Headers.java:
##########
@@ -49,15 +55,15 @@ public interface Headers extends Iterable<Header> {
     /**
      * Returns just one (the very last) header for the given key, if present.
      * 
-     * @param key to get the last header for.
+     * @param key to get the last header for; must not be null.
      * @return this last header matching the given key, returns null if not present.
      */
     Header lastHeader(String key);
 
     /**
      * Returns all headers for the given key, in the order they were added in, if present.
      *
-     * @param key to return the headers for.
+     * @param key to return the headers for; must not be null.
      * @return all headers for the given key, in the order they were added in, if NO headers are present an empty iterable is returned. 
      */
     Iterable<Header> headers(String key);

Review Comment:
   Updated. 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-19644: Enhance the documentation for producer headers and integration tests [kafka]

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


##########
clients/src/main/java/org/apache/kafka/common/header/Header.java:
##########
@@ -16,6 +16,9 @@
  */
 package org.apache.kafka.common.header;
 
+/**
+ * The key of a header is guaranteed to be non-null, while the value of a header may be null.

Review Comment:
   Please add the comments to each method



-- 
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-19644: Enhance the documentation for producer headers and integration tests [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on PR #20524:
URL: https://github.com/apache/kafka/pull/20524#issuecomment-3294172701

   the flaky is  traced by KAFKA-7542


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