Skip to content

MINOR: Simplify ApiKeys by relying on ApiMessageType#9748

Merged
ijuma merged 11 commits intoapache:trunkfrom
ijuma:simplify-api-keys
Dec 16, 2020
Merged

MINOR: Simplify ApiKeys by relying on ApiMessageType#9748
ijuma merged 11 commits intoapache:trunkfrom
ijuma:simplify-api-keys

Conversation

@ijuma
Copy link
Copy Markdown
Member

@ijuma ijuma commented Dec 14, 2020

  • The naming for ListOffsets was inconsistent, in some places it was ListOffset and in others
    it was ListOffsets. Picked the latter since it was used in metrics and the protocol documentation
    and made it consistent.
  • Removed unused methods in ApiKeys.
  • Deleted CommonFields.
  • Added lowestSupportedVersion and highestSupportedVersion to ApiMessageType
  • Removed tests in MessageTest that are no longer relevant.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijuma Thanks for this nice cleanup.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit weird that messageType.name and messageType.name() return different string.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's an existing problem with ApiKeys too. The enum method cannot be overridden. I think it should be tackled separately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is never used.

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me this check should be moved to ApiMessageTypeGenerator as it is a generated code from json.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see what you mean. You're suggesting to validate during generation, not to include the validation in the generated code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That alternative looks good to me :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@ijuma ijuma force-pushed the simplify-api-keys branch from 18eec5b to ddad298 Compare December 15, 2020 06:44
@ijuma
Copy link
Copy Markdown
Member Author

ijuma commented Dec 15, 2020

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.

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

@chia7712
Copy link
Copy Markdown
Member

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)
@ijuma ijuma changed the title MINOR: Simplify ApiKeys by relying on ApiMessageType and removing unused methods MINOR: Simplify ApiKeys by relying on ApiMessageType Dec 15, 2020
@ijuma ijuma marked this pull request as ready for review December 15, 2020 16:15
@ijuma ijuma force-pushed the simplify-api-keys branch from 2afb85d to e8dc547 Compare December 15, 2020 16:19
@ijuma
Copy link
Copy Markdown
Member Author

ijuma commented Dec 15, 2020

Tests passed for JDK 11 and 15. JDK 8 had one flaky failure:

kafka.api.ConsumerBounceTest.testClose failed

@chia7712 Do you have cycles to review this?

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ijuma +1 and thanks for this big cleanup!

idToType[key.id] = key;
ID_TO_TYPE = idToType;
MAX_API_KEY = maxKey;
ID_TO_TYPE.put((int) key.id, key);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to check duplicate id?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the generator checks for this. I will double check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'short' type more suitable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the above comment is valid, it can be addressed in separate PR :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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<>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to latest commit.

@ijuma
Copy link
Copy Markdown
Member Author

ijuma commented Dec 16, 2020

JDK8 and 11 passed, 15 had one flaky unrelated failure:

kafka.server.ClientQuotasRequestTest.testAlterIpQuotasRequest

@ijuma ijuma merged commit 782175d into apache:trunk Dec 16, 2020
@ijuma ijuma deleted the simplify-api-keys branch December 16, 2020 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants