You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "soondenana (via GitHub)" <gi...@apache.org> on 2025/03/07 21:54:33 UTC
[PR] MINOR: Use readable interface to parse requests [kafka]
soondenana opened a new pull request, #19163:
URL: https://github.com/apache/kafka/pull/19163
The generated `*RequestData` java file constructors take `Readable` as an input. However, the `parse` method in the `AbstractRequest` takes a `ByteBuffer` as input. So to create the corresponding `*RequestData` objects, each individual concrete Request classes wraps the `ByteBuffer` into a `ByteBufferAccessor`.
This is a boilerplate code present in all the concrete Request classes. This change pulls it into `AbstractRequest` so that subclasses can simply pass the `Readable` they get from the superclass directly to `*RequestData` classes.
The same change is made to the `serialize` method to maintain symmetry.
A simple mechanical refactoring that touches many files, but reduced a lot of duplicated and boilerplate code.
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r2012740807
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static ByteBufferAccessor toReadable(final Message message, final short version) {
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "artemlivshits (via GitHub)" <gi...@apache.org>.
artemlivshits commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1985871102
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -122,8 +124,8 @@ public final ByteBuffer serializeWithHeader(RequestHeader header) {
}
// Visible for testing
- public final ByteBuffer serialize() {
- return MessageUtil.toByteBuffer(data(), version);
+ public final Readable serialize() {
Review Comment:
Maybe just return `ByteAccessor`?
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static Readable toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ return ((ByteBufferAccessor) toReadable(message, version)).buffer();
Review Comment:
I agree, we could probably just have `ByteAccessor toByteAcessor(final Message message, final short version)` either as a single method to be used for both variants or as a private method with 2 public wrappers `toReadable` and `toByteBuffer`.
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on PR #19163:
URL: https://github.com/apache/kafka/pull/19163#issuecomment-2723117303
> BTW, while we're on it, why don't we do a similar refactoring for the `AbstractResponse.parseResponse` (the second overload)?
Maybe in a separate PR. We should also modify the `serializeWithHeader`, but each new change makes this big PR even bigger. Unless changes cancel out, better to have a follow up PR
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r2012339800
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static ByteBufferAccessor toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
Review Comment:
I agree with @artemlivshits . Why not just remove this method? Any code that wants access to the byte buffer can just call `toReadable(message, version).buffer()`.
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static ByteBufferAccessor toReadable(final Message message, final short version) {
Review Comment:
How about name this `toByteBufferAccessor`?
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1988107211
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -168,190 +175,194 @@ public Map<Errors, Integer> errorCounts(Throwable e) {
* Factory method for getting a request object based on ApiKey ID and a version
*/
public static RequestAndSize parseRequest(ApiKeys apiKey, short apiVersion, ByteBuffer buffer) {
- int bufferSize = buffer.remaining();
- return new RequestAndSize(doParseRequest(apiKey, apiVersion, buffer), bufferSize);
+ return parseRequest(apiKey, apiVersion, new ByteBufferAccessor(buffer));
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1988106394
##########
clients/src/test/java/org/apache/kafka/common/requests/RequestResponseTest.java:
##########
@@ -1923,9 +1922,9 @@ private void checkRequest(AbstractRequest req) {
// Check for equality of the ByteBuffer only if indicated (it is likely to fail if any of the fields
// in the request is a HashMap with multiple elements since ordering of the elements may vary)
try {
- ByteBuffer serializedBytes = req.serialize();
+ ByteBuffer serializedBytes = req.serializeToByteBuffer();
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "artemlivshits (via GitHub)" <gi...@apache.org>.
artemlivshits commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1987985812
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -122,8 +124,8 @@ public final ByteBuffer serializeWithHeader(RequestHeader header) {
}
// Visible for testing
- public final ByteBuffer serialize() {
- return MessageUtil.toByteBuffer(data(), version);
+ public final Readable serialize() {
Review Comment:
Then probably it would make sense to make `toByteBufferAccessor` public, and I'm not sure if we need `toReadable` and `toByteBuffer` overloads as the data can be easily extracted from `ByteBufferAccessor`.
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -168,190 +175,194 @@ public Map<Errors, Integer> errorCounts(Throwable e) {
* Factory method for getting a request object based on ApiKey ID and a version
*/
public static RequestAndSize parseRequest(ApiKeys apiKey, short apiVersion, ByteBuffer buffer) {
- int bufferSize = buffer.remaining();
- return new RequestAndSize(doParseRequest(apiKey, apiVersion, buffer), bufferSize);
+ return parseRequest(apiKey, apiVersion, new ByteBufferAccessor(buffer));
Review Comment:
Do we need an overload that takes a ByteBuffer? I found one case (see my other comment), but it could be easily fixed if `serialize()` returned `ByteBufferAccessor`.
##########
clients/src/test/java/org/apache/kafka/common/requests/RequestResponseTest.java:
##########
@@ -1923,9 +1922,9 @@ private void checkRequest(AbstractRequest req) {
// Check for equality of the ByteBuffer only if indicated (it is likely to fail if any of the fields
// in the request is a HashMap with multiple elements since ordering of the elements may vary)
try {
- ByteBuffer serializedBytes = req.serialize();
+ ByteBuffer serializedBytes = req.serializeToByteBuffer();
Review Comment:
If `req.serialize()` returned `ByteBufferAccessor`, we could use that here. The benefit is that we could just use `parseRequest` that takes `Readable`.
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -122,8 +124,8 @@ public final ByteBuffer serializeWithHeader(RequestHeader header) {
}
// Visible for testing
- public final ByteBuffer serialize() {
- return MessageUtil.toByteBuffer(data(), version);
+ public final Readable serialize() {
Review Comment:
I was kind of thinking maybe it should just return `ByteAccessor` without having another overload? All the code that takes `Readable` would just work, and a small number of places that need to work with the buffer could use the result as the `ByteAccessor`. Pretty much similar to what you had with downcasting only without downcasting :-).
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1985768753
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static Readable toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ return ((ByteBufferAccessor) toReadable(message, version)).buffer();
Review Comment:
I see a few other unsafe casts in the PR. We should devise a solution that doesn't involve that.
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static Readable toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ return ((ByteBufferAccessor) toReadable(message, version)).buffer();
Review Comment:
I see a few other unsafe casts in the PR. We should devise a solution that doesn't require that.
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "artemlivshits (via GitHub)" <gi...@apache.org>.
artemlivshits commented on PR #19163:
URL: https://github.com/apache/kafka/pull/19163#issuecomment-2719177262
BTW, while we're on it, why don't we do a similar refactoring for the `AbstractResponse.parseResponse` (the second overload)?
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "ijuma (via GitHub)" <gi...@apache.org>.
ijuma commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1985765879
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static Readable toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ return ((ByteBufferAccessor) toReadable(message, version)).buffer();
Review Comment:
This is an unsafe cast on the surface. You can avoid it by having a private method that returns `ByteBufferAccessor`.
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1985891433
##########
clients/src/main/java/org/apache/kafka/common/requests/AbstractRequest.java:
##########
@@ -122,8 +124,8 @@ public final ByteBuffer serializeWithHeader(RequestHeader header) {
}
// Visible for testing
- public final ByteBuffer serialize() {
- return MessageUtil.toByteBuffer(data(), version);
+ public final Readable serialize() {
Review Comment:
> Maybe just return `ByteAccessor`?
Added an overloaded method for test cases where they need ByteBuffer to validate properties.
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "artemlivshits (via GitHub)" <gi...@apache.org>.
artemlivshits commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1988203221
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static ByteBufferAccessor toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
Review Comment:
Is this used anywhere?
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "jsancio (via GitHub)" <gi...@apache.org>.
jsancio merged PR #19163:
URL: https://github.com/apache/kafka/pull/19163
--
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] MINOR: Use readable interface to parse requests [kafka]
Posted by "soondenana (via GitHub)" <gi...@apache.org>.
soondenana commented on code in PR #19163:
URL: https://github.com/apache/kafka/pull/19163#discussion_r1989669227
##########
clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:
##########
@@ -204,13 +204,17 @@ public static boolean compareRawTaggedFields(List<RawTaggedField> first,
}
}
- public static ByteBuffer toByteBuffer(final Message message, final short version) {
+ public static ByteBufferAccessor toReadable(final Message message, final short version) {
ObjectSerializationCache cache = new ObjectSerializationCache();
int messageSize = message.size(cache, version);
ByteBufferAccessor bytes = new ByteBufferAccessor(ByteBuffer.allocate(messageSize));
message.write(bytes, cache, version);
bytes.flip();
- return bytes.buffer();
+ return bytes;
+ }
+
+ public static ByteBuffer toByteBuffer(final Message message, final short version) {
Review Comment:
Yes. In a bunch of tests and few other classes.
--
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