-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace sync Kafka consumers with confluent_kafka one
This change replaces all synchronous Kafka consumers (from the kafka-python library) with a new implementation based on confluent-kafka-python's `Consumer`, keeping the same interface as much as possible. The PyTest timeout is raised from 60s to 90s to accomodate for the default poll timeout for backups consumers (otherwise the tests would time out while still waiting for messages to arrive).o Since the `conluent_kafka.Consumer` implementation does not allow for consumers to be without a group ID, if the new `KafkaConsumer` client is not given one, we'll generate one on the fly to mimic a groupless behaviour. 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
- Loading branch information
Mátyás Kuti
committed
Dec 13, 2023
1 parent
da85083
commit 640a9d0
Showing
29 changed files
with
459 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Copyright (c) 2023 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from confluent_kafka import Consumer, TopicPartition | ||
from confluent_kafka.admin import PartitionMetadata | ||
from confluent_kafka.error import KafkaException | ||
from kafka.errors import KafkaTimeoutError | ||
from karapace.kafka.common import _KafkaConfigMixin, KafkaClientParams, raise_from_kafkaexception | ||
from typing import Iterable | ||
from typing_extensions import Unpack | ||
|
||
import secrets | ||
|
||
|
||
class KafkaConsumer(_KafkaConfigMixin, Consumer): | ||
def __init__( | ||
self, | ||
topic: str, | ||
bootstrap_servers: Iterable[str] | str, | ||
verify_connection: bool = True, | ||
**params: Unpack[KafkaClientParams], | ||
) -> None: | ||
# The `confluent_kafka.Consumer` does not allow for a missing group id | ||
# if the client of this class does not provide one, we'll generate a | ||
# unique group id to achieve the groupless behaviour | ||
if "group_id" not in params: | ||
params["group_id"] = self._create_group_id() | ||
|
||
super().__init__(bootstrap_servers, verify_connection, **params) | ||
|
||
self.subscribe([topic]) | ||
|
||
@staticmethod | ||
def _create_group_id() -> str: | ||
return f"karapace-autogenerated-{secrets.token_hex(6)}" | ||
|
||
def partitions_for_topic(self, topic: str) -> dict[int, PartitionMetadata]: | ||
"""Returns all partition metadata for the given topic.""" | ||
try: | ||
return self.list_topics(topic).topics[topic].partitions | ||
except KafkaException as exc: | ||
raise_from_kafkaexception(exc) | ||
|
||
def get_watermark_offsets( | ||
self, partition: TopicPartition, timeout: float | None = None, cached: bool = False | ||
) -> tuple[int, int]: | ||
"""Wrapper around `Consumer.get_watermark_offsets` to handle error cases and exceptions. | ||
confluent-kafka is somewhat inconsistent with error-related behaviours, | ||
`get_watermark_offsets` returns `None` on timeouts, so we are translating it to an | ||
exception. | ||
""" | ||
try: | ||
if timeout is not None: | ||
result = super().get_watermark_offsets(partition, timeout, cached) | ||
else: | ||
result = super().get_watermark_offsets(partition, cached=cached) | ||
|
||
if result is None: | ||
raise KafkaTimeoutError() | ||
|
||
return result | ||
except KafkaException as exc: | ||
raise_from_kafkaexception(exc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
Copyright (c) 2023 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from confluent_kafka import TIMESTAMP_CREATE_TIME, TIMESTAMP_LOG_APPEND_TIME, TIMESTAMP_NOT_AVAILABLE | ||
from typing import Final | ||
|
||
import enum | ||
|
||
# A constant that corresponds to the default value of request.timeout.ms in | ||
# the librdkafka C library | ||
DEFAULT_REQUEST_TIMEOUT_MS: Final = 30000 | ||
|
||
|
||
class Timestamp(enum.IntEnum): | ||
NOT_AVAILABLE = TIMESTAMP_NOT_AVAILABLE | ||
CREATE_TIME = TIMESTAMP_CREATE_TIME | ||
LOG_APPEND_TIME = TIMESTAMP_LOG_APPEND_TIME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.