Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-8305: support default partitions & replication factor in AdminClient#createTopic #6728

Merged
merged 1 commit into from
Jun 5, 2019

Conversation

agavra
Copy link
Contributor

@agavra agavra commented May 14, 2019

See: KIP-464 for more information.

Description

This change makes the two required changes to support creating topics using the cluster defaults for replication and partitions:

  1. Adds a NewTopic(String) constructor to the NewTopic API
  2. Changes the AdminManager to accept -1 as valid options for replication factor and partitions. If this is the case, it will resolve it using the default configuration.
  3. This also adds a dependency on scalaJava8Compat library to make it simpler to convert Scala Option to Optional.

Testing

  • Updated unit tests with the new conditions

Committer Checklist (excluded from commit message)

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

Copy link
Contributor

@ryannedolan ryannedolan left a comment

Choose a reason for hiding this comment

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

Looking forward to this!

Copy link
Member

@ijuma ijuma left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, I had a quick look and left some comments.

val replicationFactor: java.lang.Short =
if (topic.replicationFactor == NO_REPLICATION_FACTOR) null else topic.replicationFactor
if (topic.replicationFactor == NO_REPLICATION_FACTOR) null else resolvedReplicationFactor
Copy link
Member

Choose a reason for hiding this comment

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

The documentation for CreateTopicPolicy says:

@param numPartitions the number of partitions to create or null if replicasAssignments is set.
@param replicationFactor the replication factor for the topic or null if replicaAssignments is set.

It seems like we may end up with a non null numPartitions and replicationFactor even if replicaAssignment is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the scenario that causes this? If replicaAssignments is set, then we expect the partitions/replication factor to equal NO_PARTITIONS and NO_REPLICATION_FACTOR respectively, otherwise it fails here:

        if ((topic.numPartitions != NO_NUM_PARTITIONS || topic.replicationFactor != NO_REPLICATION_FACTOR)
            && !topic.assignments().isEmpty) {
          throw new InvalidRequestException("Both numPartitions or replicationFactor and replicasAssignments were set. " +
            "Both cannot be used at the same time.")
        }

Copy link
Member

Choose a reason for hiding this comment

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

You're right. But then why do we need this change at all? It seems like we never end up with a different result than the previous code.

Copy link
Contributor Author

@agavra agavra May 19, 2019

Choose a reason for hiding this comment

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

The difference is that if the value sent was -1 (i.e. NO_REPLICATION_FACTOR) and there are no assignments, it will instead be set the resolvedReplicationFactor, which is the cluster default (i.e. default.replication.factor)

(you can see tests for the changed behavior)

Copy link
Contributor

@hachikuji hachikuji May 31, 2019

Choose a reason for hiding this comment

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

Hmm.. I think Ismael's point is that if topic.replicationFactor != NO_REPLICATION_FACTOR, then resolvedReplicationFactor == topic.replicationFactor, and this is equivalent to what we already had. It seems like this is what we're trying to do:

val replicationFactor: java.lang.Short =
  if (topic.assignments().nonEmpty) null else resolvedReplicationFactor

Copy link
Contributor Author

@agavra agavra May 31, 2019

Choose a reason for hiding this comment

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

🤦‍♂ I see what you're saying now. I think @hachikuji's code snippet is the easiest to read so I'll change it to that. @ijuma my original line of thought was that it's easier to reason about the code if we only ever use resolvedX everywhere, hence the motivation for this change. That way I would never accidentally use the value of -1 in business logic code and it's easy to confirm by just looking at usages of topic.numPartitions and topic.replicationFactor.

@agavra
Copy link
Contributor Author

agavra commented May 21, 2019

Pinging +1'ers @colinhicks @omkreddy @gwenshap @rhauch 😄 thanks in advance! (cc @ijuma)

@hachikuji
Copy link
Contributor

hachikuji commented May 30, 2019

@agavra I may be missing something, but if we are not bumping the CreateTopic protocol version, how can the client know whether the default options are supported by the broker? In other words, if you try to use this API with an old broker, it seems like we'd get an INVALID_REPLICATION_FACTOR error, which is a little surprising. I'd expect to get an UNSUPPORTED_VERSION error instead.

@agavra
Copy link
Contributor Author

agavra commented May 30, 2019

@hachikuji - thanks for the review!

if we are not bumping the CreateTopic protocol version, how can the client know whether the default options are supported by the broker?

You are not missing something, this is something that I overlooked (not all errors are the same)!

Copy link
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

Thanks for the updates. Left a few comments.

core/src/main/scala/kafka/server/AdminManager.scala Outdated Show resolved Hide resolved
val replicationFactor: java.lang.Short =
if (topic.replicationFactor == NO_REPLICATION_FACTOR) null else topic.replicationFactor
if (topic.replicationFactor == NO_REPLICATION_FACTOR) null else resolvedReplicationFactor
Copy link
Contributor

@hachikuji hachikuji May 31, 2019

Choose a reason for hiding this comment

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

Hmm.. I think Ismael's point is that if topic.replicationFactor != NO_REPLICATION_FACTOR, then resolvedReplicationFactor == topic.replicationFactor, and this is equivalent to what we already had. It seems like this is what we're trying to do:

val replicationFactor: java.lang.Short =
  if (topic.assignments().nonEmpty) null else resolvedReplicationFactor

core/src/main/scala/kafka/admin/TopicCommand.scala Outdated Show resolved Hide resolved
core/src/main/scala/kafka/admin/TopicCommand.scala Outdated Show resolved Hide resolved
@@ -289,7 +294,7 @@ object TopicCommand extends Logging {
if (topic.hasReplicaAssignment)
adminZkClient.createTopicWithAssignment(topic.name, topic.configsToAdd, topic.replicaAssignment.get)
else
adminZkClient.createTopic(topic.name, topic.partitions.get, topic.replicationFactor, topic.configsToAdd, topic.rackAwareMode)
adminZkClient.createTopic(topic.name, topic.partitions.get, topic.replicationFactor.get, topic.configsToAdd, topic.rackAwareMode)
Copy link
Contributor

Choose a reason for hiding this comment

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

The error may be a bit obscure if no --partitions or --replication-factor option was provided. Could we detect this case in the argument checks we have below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if it was worth adding bloat for the deprecated code, but I'm happy adding it back (I had it then removed it)

Copy link
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

Thanks, looks good. Just a few more small comments.

Copy link
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

Just one more comment, but LGTM overall.

createAndWaitTopic(new TopicCommandOptions(
Array("--topic", testTopicName)))

adminClient.listTopics().names().get().contains(testTopicName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add default assertions here. Maybe we can just use describeTopic like the other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops i missed this comment! Addressing that now.

@agavra
Copy link
Contributor Author

agavra commented Jun 4, 2019

"retest this please"

…lient#createTopic.

This commit makes three changes:
- Adds a constructor for NewTopic(String, Optional<Integer>, Optional<Short>)
which allows users to specify Optional.empty() for numPartitions or
replicationFactor in order to use the broker default.
- Changes AdminManager to accept -1 as valid options for replication
factor and numPartitions (resolving to broker defaults).
- Adds a dependency on scalaJava8Compat library to make it simpler to
convert Scala Option to Java Optional
Copy link
Contributor

@hachikuji hachikuji left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM. Merging to trunk!

@hachikuji hachikuji merged commit 8e16158 into apache:trunk Jun 5, 2019
pengxiaolong pushed a commit to pengxiaolong/kafka that referenced this pull request Jun 14, 2019
…lient#createTopic (KIP-464) (apache#6728)

This commit makes three changes:
- Adds a constructor for NewTopic(String, Optional<Integer>, Optional<Short>)
which allows users to specify Optional.empty() for numPartitions or
replicationFactor in order to use the broker default.
- Changes AdminManager to accept -1 as valid options for replication
factor and numPartitions (resolving to broker defaults).
- Makes --partitions and --replication-factor optional arguments when creating
topics using kafka-topics.sh.
- Adds a dependency on scalaJava8Compat library to make it simpler to
convert Scala Option to Java Optional

Reviewers: Ismael Juma <[email protected]>, Ryanne Dolan <[email protected]>, Jason Gustafson <[email protected]>
ijuma pushed a commit that referenced this pull request Nov 4, 2021
[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: #6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via #10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
ijuma pushed a commit that referenced this pull request Nov 4, 2021
[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: #6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via #10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
ijuma pushed a commit that referenced this pull request Nov 4, 2021
[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: #6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via #10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
a0x8o added a commit to a0x8o/kafka that referenced this pull request Nov 5, 2021
[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: apache/kafka#6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via apache/kafka#10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
xdgrulez pushed a commit to xdgrulez/kafka that referenced this pull request Dec 22, 2021
…he#11429)

[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: apache#6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via apache#10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
lmr3796 pushed a commit to lmr3796/kafka that referenced this pull request Jun 2, 2022
…he#11429)

[KIP-464](https://cwiki.apache.org/confluence/display/KAFKA/KIP-464%3A+Defaults+for+AdminClient%23createTopic) (PR: apache#6728)
made it possible to create topics without passing partition count and/or replica factor when using
the admin client. We incorrectly disallowed this via apache#10457 while
trying to ensure validation was consistent between ZK and the admin client (in this case the
inconsistency was intentional).

Fix this regression and add tests for the command lines in quick start (i.e. create topic and describe
topic) to make sure it won't be broken in the future.

Reviewers: Lee Dongjin <[email protected]>, Ismael Juma <[email protected]>
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.

4 participants