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

Use confluent-kafka-python for Kafka admin client instead of kafka-python #757

Merged
merged 2 commits into from
Nov 20, 2023

Conversation

matyaskuti
Copy link
Contributor

@matyaskuti matyaskuti commented Nov 8, 2023

About this change - What it does

Replace Kafka admin clients (including KafkaRestAdminClient and "vanilla" admin clients from kafka-python) with a new confluent_kafka based one.

The main change is:

  • The addition of the KafkaAdminClient in a new module
  • Which is more or less moving the KafkaRestAdminClient from its original location and adapting to the new base class from confluent-kafka-python
  • As well as splitting off the existing test suite and expanding it into its own.

Other changes are mostly a consequence of this, adapting to a slightly different interface, simplifying client code when it was straight forward, etc. Many files have been changed, however, most of these are straightforward renamings or small changes to adapt to the new client.

Why this way

  • Completely removing the dependency on kafka-python is out of scope for this PR, however there is only one admin client now
  • The new KafkaAdminClient aims to keep the same interface as much as possible as it was before (exceptions include many-many NewTopic.name -> NewTopic.topic changes), also in exceptions thrown
  • The underlying client from confluent-kafka-python takes care of API version negotiation (that was previously implemented in the admin client explicitly)
  • When implementing and using the new admin client I aimed to contain all references to confluent_kafka within the new module
  • Throwing meaningful errors still depends on kafka-python's kafka.errors.for_code and its specific exceptions
  • No typing stubs are added for confluent-kafka-python, that's something that could potentially be done after a "full switch" Type stubs are going to be added in a follow-up: Add typing stubs for confluent-kafka #760
  • confluent-kafka-python is version pinned to 2.3.0 to avoid any kind of future incompatibilities without an explicit update

Request from reviewers

Are there any "what if" kind of scenarios that might not be covered by tests or the code at the moment? (Especially backups and schema registry related.)

@matyaskuti matyaskuti force-pushed the matyaskuti/confluent_kafka_for_admin_client branch 6 times, most recently from 82962fe to 8a91b03 Compare November 8, 2023 15:28
@matyaskuti matyaskuti changed the title Use confluent-kafka-python for Kafka admin client Use confluent-kafka-python for Kafka admin client instead of kafka-python Nov 9, 2023
The new method is a confluent-kafka-python compatible callback, which
will be required by the new Kafka admin client.

At the same time, remove the token from the token provider classes'
`repr` - while we don't log these, it's better not to accidentally leak
tokens.
@matyaskuti matyaskuti force-pushed the matyaskuti/confluent_kafka_for_admin_client branch from 8a91b03 to 80bb504 Compare November 9, 2023 14:09
Copy link
Contributor

@eliax1996 eliax1996 left a comment

Choose a reason for hiding this comment

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

Very nice code 😄, I've left some comments, feel free to disagree with all of them.
I've just left what I was feeling while reading the code. The code looks very auto explicative and you've added doc on all the new methods cool!

PPS before merging just make sure to sync the well doc you added to the commits with the pr description.

I need another round of reading but I've left this to flush my mental buffer :)

karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/schema_reader.py Show resolved Hide resolved
@matyaskuti matyaskuti force-pushed the matyaskuti/confluent_kafka_for_admin_client branch 2 times, most recently from 119860b to 485fbb8 Compare November 14, 2023 12:12
Copy link
Contributor

@aiven-anton aiven-anton left a comment

Choose a reason for hiding this comment

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

Leaving a partial review, I haven't read through everything yet. I left a few comments on the design/interface of the new admin client.

karapace/backup/topic_configurations.py Outdated Show resolved Hide resolved
karapace/constants.py Show resolved Hide resolved
tests/unit/test_authentication.py Show resolved Hide resolved
mypy.ini Show resolved Hide resolved
karapace/kafka_admin.py Outdated Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Outdated Show resolved Hide resolved
karapace/kafka_admin.py Show resolved Hide resolved
karapace/kafka_admin.py Outdated Show resolved Hide resolved
@matyaskuti matyaskuti force-pushed the matyaskuti/confluent_kafka_for_admin_client branch 4 times, most recently from 7293345 to cfdc71a Compare November 15, 2023 15:03
This change replaces all Kafka admin client usages with a new
implementation, based on confluent-kafka-python's `AdminClient`, aiming
to keep the same interface as much as possible for the time being.

The goal is not to completely remove kafka-python as a dependency,
rather take a step towards that. The kafka-python library is still widely
used in the codebase, eg. for error handling (even in the new
`KafkaAdminClient`).

The new `KafkaAdminClient` takes the place of all previous admin clients
used, thus not specific to the REST proxy anymore. Hence it's move to a
new module as `karapace.kafka_admin.KafkaAdminClient`, which also
consolidates all references to the `confluent_kafka` lib. The test suite
has also been moved and expanded.

Wherever possible the same interface was kept, only adding new methods
to simplify client code (to a minimal extent). This is most notable in
tests and the backups functionality. The new admin client aims to hide
away confluent-kafka-python's Future-based async client approach by
providing wrapper methods that resolve these futures.

Typing (issues) from `confluent_kafka` are ignored for now, no typing
stubs have been added on purpose.

Resources:
* confluent-kafka-python documentation: https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#
* librdkafka configuration documentation: https://github.com/confluentinc/librdkafka/blob/master/CONFIGURATION.md
@matyaskuti matyaskuti force-pushed the matyaskuti/confluent_kafka_for_admin_client branch 2 times, most recently from 1038d9b to 79c6198 Compare November 15, 2023 16:01
@matyaskuti matyaskuti marked this pull request as ready for review November 20, 2023 08:22
@matyaskuti matyaskuti requested review from a team as code owners November 20, 2023 08:22
@@ -14,6 +14,7 @@ warn_unused_ignores = True
warn_no_return = True
warn_unreachable = True
strict_equality = True
enable_incomplete_feature = Unpack
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed this was just marked stable in 1.7 that was just released, oddly coincidental timing! https://mypy-lang.blogspot.com/2023/11/mypy-17-released.html

But let's keep this as-is and upgrade mypy separately to limit the scope.

@aiven-anton
Copy link
Contributor

This looks good to me 👍

@eliax1996 Do you want to make another pass on this?

@eliax1996 eliax1996 merged commit b0d6b29 into main Nov 20, 2023
8 checks passed
@eliax1996 eliax1996 deleted the matyaskuti/confluent_kafka_for_admin_client branch November 20, 2023 14:21
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.

3 participants