MINOR: Simplify ApiKeys by relying on ApiMessageType#9748
MINOR: Simplify ApiKeys by relying on ApiMessageType#9748ijuma merged 11 commits intoapache:trunkfrom
Conversation
There was a problem hiding this comment.
It is a bit weird that messageType.name and messageType.name() return different string.
There was a problem hiding this comment.
Yeah, that's an existing problem with ApiKeys too. The enum method cannot be overridden. I think it should be tackled separately.
There was a problem hiding this comment.
Could we fix the comment about "name" as it is used to be a part of metrics name (see https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/network/RequestChannel.scala#L239). The origin comment is stale.
chia7712
left a comment
There was a problem hiding this comment.
As the "id" is removed from the construction, is it possible to add new ApiKeys instances with incorrect order in the future? the static field ID_TO_TYPE assume all ApiKeys instances are created by id order.
There was a problem hiding this comment.
It seems to me this check should be moved to ApiMessageTypeGenerator as it is a generated code from json.
There was a problem hiding this comment.
We can, but it doesn't seem particularly beneficial. It's important to generate code that is specific to each data class. Generic code is best written without generation.
We can move this code to a base class for ApiMessageType potentially, particularly if we decide to remove ApiKeys.
There was a problem hiding this comment.
it doesn't seem particularly beneficial
It seems to me the benefit is the error happens from runtime/test_runtime to build time (generate message code).
There was a problem hiding this comment.
Oh, I see what you mean. You're suggesting to validate during generation, not to include the validation in the generated code.
There was a problem hiding this comment.
I took a look at this and not clear how to validate this in the generator. I added unit tests to ApiMessageTypeTest instead and I think that's good enough for now. What do you think?
There was a problem hiding this comment.
That alternative looks good to me :)
18eec5b to
ddad298
Compare
@chia7712 Can you please elaborate? I don't see the assumption you mention. The main assumption is that api keys are dense. I changed the code not to rely on the latter although it doesn't change the behavior. |
I assumed the order of instancing ApiKeys is strict so removing the id may makes it hard to "see" where to put the new ApiKeys in the future. However, it seems the order of instancing ApiKeys is free so the worry is invalid. |
…keys * apache-github/trunk: KAFKA-10776: Add version attribute in RequestsPerSec metrics documentation (apache#9661) KAFKA-10854: fix flaky testConnectionRatePerIp test (apache#9752) KAFKA-10525: Emit JSONs with new auto-generated schema (KIP-673) (apache#9526)
2afb85d to
e8dc547
Compare
|
Tests passed for JDK 11 and 15. JDK 8 had one flaky failure:
@chia7712 Do you have cycles to review this? |
| idToType[key.id] = key; | ||
| ID_TO_TYPE = idToType; | ||
| MAX_API_KEY = maxKey; | ||
| ID_TO_TYPE.put((int) key.id, key); |
There was a problem hiding this comment.
Does it need to check duplicate id?
There was a problem hiding this comment.
I think the generator checks for this. I will double check.
There was a problem hiding this comment.
Yes, the generator checks it:
Caused by: java.lang.RuntimeException: Found more than one response with API key 3
There's also ApiMessageTypeTest.testUniqueness that verifies it after the generation.
I added a comment explaining that uniqueness is ensured.
| @@ -361,69 +172,35 @@ private static boolean shouldRetainsBufferReference(Schema[] requestSchemas) { | |||
| } | |||
|
|
|||
| public static ApiKeys forId(int id) { | |||
There was a problem hiding this comment.
Is 'short' type more suitable?
There was a problem hiding this comment.
If the above comment is valid, it can be addressed in separate PR :)
There was a problem hiding this comment.
We used int here because short in Java is a second class citizen and casting is required in some cases. If we decide to change this, a separate PR (as you suggested) would be better.
| UPDATE_FEATURES(ApiMessageType.UPDATE_FEATURES, false, true), | ||
| ENVELOPE(ApiMessageType.ENVELOPE, true, RecordBatch.MAGIC_VALUE_V0, false, false); | ||
|
|
||
| private static final Map<Integer, ApiKeys> ID_TO_TYPE = new HashMap<>(); |
There was a problem hiding this comment.
It seems to me this initialization can be rewrite by following code.
private static final Map<Integer, ApiKeys> ID_TO_TYPE = Arrays.stream(ApiKeys.values())
.collect(Collectors.toMap(key -> (int) key.id, Function.identity()));
The benefit is that he static block can be removed.
|
JDK8 and 11 passed, 15 had one flaky unrelated failure:
|
ListOffsetswas inconsistent, in some places it wasListOffsetand in othersit was
ListOffsets. Picked the latter since it was used in metrics and the protocol documentationand made it consistent.
CommonFields.lowestSupportedVersionandhighestSupportedVersiontoApiMessageTypeMessageTestthat are no longer relevant.Committer Checklist (excluded from commit message)