Skip to content

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

Merged
hachikuji merged 1 commit into
apache:trunkfrom
agavra:kafka-8305
Jun 5, 2019
Merged

KAFKA-8305: support default partitions & replication factor in AdminClient#createTopic#6728
hachikuji merged 1 commit into
apache:trunkfrom
agavra:kafka-8305

Conversation

@agavra

@agavra agavra commented May 14, 2019

Copy link
Copy Markdown
Contributor

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)

@ryannedolan ryannedolan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking forward to this!

Comment thread clients/src/main/java/org/apache/kafka/clients/admin/NewTopic.java Outdated

@ijuma ijuma left a comment

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.

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

Comment thread clients/src/main/java/org/apache/kafka/clients/admin/NewTopic.java Outdated

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.

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
Copy Markdown
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
Copy Markdown
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.

@agavra agavra May 19, 2019

Copy link
Copy Markdown
Contributor Author

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)

@hachikuji hachikuji May 31, 2019

Copy link
Copy Markdown
Contributor

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

@agavra agavra May 31, 2019

Copy link
Copy Markdown
Contributor Author

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

agavra commented May 21, 2019

Copy link
Copy Markdown
Contributor Author

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

@hachikuji

hachikuji commented May 30, 2019

Copy link
Copy Markdown
Contributor

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

Comment thread core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala Outdated
@agavra

agavra commented May 30, 2019

Copy link
Copy Markdown
Contributor Author

@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)!

@hachikuji hachikuji left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread core/src/main/scala/kafka/server/AdminManager.scala Outdated

@hachikuji hachikuji May 31, 2019

Copy link
Copy Markdown
Contributor

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

Comment thread core/src/main/scala/kafka/admin/TopicCommand.scala Outdated
Comment thread core/src/main/scala/kafka/admin/TopicCommand.scala Outdated

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

@hachikuji hachikuji left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread clients/src/main/java/org/apache/kafka/clients/admin/NewTopic.java Outdated
Comment thread core/src/main/scala/kafka/admin/TopicCommand.scala Outdated
Comment thread core/src/test/scala/unit/kafka/admin/TopicCommandWithAdminClientTest.scala Outdated

@hachikuji hachikuji left a comment

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
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
Copy Markdown
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

agavra commented Jun 4, 2019

Copy link
Copy Markdown
Contributor Author

"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

@hachikuji hachikuji left a comment

Copy link
Copy Markdown
Contributor

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 <ismael@juma.me.uk>, Ryanne Dolan <ryannedolan@gmail.com>, Jason Gustafson <jason@confluent.io>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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 <dongjin@apache.org>, Ismael Juma <ismael@juma.me.uk>
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