-
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.
- Loading branch information
Mátyás Kuti
committed
Dec 4, 2023
1 parent
83157fa
commit 90714d8
Showing
31 changed files
with
394 additions
and
218 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
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,48 @@ | ||
""" | ||
Copyright (c) 2023 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from confluent_kafka import Consumer | ||
from karapace.kafka.common import _KafkaConfigMixin, KafkaClientParams, raise_from_kafkaexception | ||
from karapace.kafka.types import KafkaException, PartitionMetadata | ||
from typing import Iterable | ||
from typing_extensions import Unpack | ||
|
||
import secrets | ||
|
||
# A constant that corresponds to the default value of request.timeout.ms in | ||
# the librdkafka C library | ||
DEFAULT_REQUEST_TIMEOUT_MS = 30000 | ||
|
||
|
||
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-{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) |
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,49 @@ | ||
""" | ||
Copyright (c) 2023 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from confluent_kafka import ( | ||
Message, | ||
TIMESTAMP_CREATE_TIME, | ||
TIMESTAMP_LOG_APPEND_TIME, | ||
TIMESTAMP_NOT_AVAILABLE, | ||
TopicPartition, | ||
) | ||
from confluent_kafka.admin import ( | ||
BrokerMetadata, | ||
ClusterMetadata, | ||
ConfigResource, | ||
ConfigSource, | ||
NewTopic, | ||
OffsetSpec, | ||
PartitionMetadata, | ||
ResourceType, | ||
TopicMetadata, | ||
) | ||
from confluent_kafka.error import KafkaError, KafkaException | ||
|
||
import enum | ||
|
||
__all__ = ( | ||
"BrokerMetadata", | ||
"ClusterMetadata", | ||
"ConfigResource", | ||
"ConfigSource", | ||
"ConfigSource", | ||
"KafkaError", | ||
"KafkaException", | ||
"Message", | ||
"NewTopic", | ||
"OffsetSpec", | ||
"PartitionMetadata", | ||
"ResourceType", | ||
"TopicMetadata", | ||
"TopicPartition", | ||
) | ||
|
||
|
||
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
Oops, something went wrong.