kafka-python 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kafka/__init__.py +34 -0
- kafka/__main__.py +5 -0
- kafka/admin/__init__.py +29 -0
- kafka/admin/__main__.py +5 -0
- kafka/admin/_acls.py +355 -0
- kafka/admin/_cluster.py +359 -0
- kafka/admin/_configs.py +479 -0
- kafka/admin/_groups.py +754 -0
- kafka/admin/_partitions.py +595 -0
- kafka/admin/_topics.py +281 -0
- kafka/admin/_transactions.py +450 -0
- kafka/admin/_users.py +194 -0
- kafka/admin/client.py +373 -0
- kafka/benchmarks/__init__.py +0 -0
- kafka/benchmarks/consumer_performance.py +138 -0
- kafka/benchmarks/load_example.py +109 -0
- kafka/benchmarks/producer_encode_path.py +201 -0
- kafka/benchmarks/producer_performance.py +161 -0
- kafka/benchmarks/profile_protocol.py +138 -0
- kafka/benchmarks/protocol_old_vs_new.py +447 -0
- kafka/benchmarks/record_batch_compose.py +77 -0
- kafka/benchmarks/record_batch_read.py +82 -0
- kafka/benchmarks/varint_speed.py +426 -0
- kafka/cli/__init__.py +36 -0
- kafka/cli/admin/__init__.py +117 -0
- kafka/cli/admin/acls/__init__.py +9 -0
- kafka/cli/admin/acls/common.py +76 -0
- kafka/cli/admin/acls/create.py +19 -0
- kafka/cli/admin/acls/delete.py +23 -0
- kafka/cli/admin/acls/describe.py +16 -0
- kafka/cli/admin/cluster/__init__.py +14 -0
- kafka/cli/admin/cluster/describe.py +11 -0
- kafka/cli/admin/cluster/describe_quorum.py +11 -0
- kafka/cli/admin/cluster/features.py +52 -0
- kafka/cli/admin/cluster/log_dirs.py +43 -0
- kafka/cli/admin/cluster/versions.py +33 -0
- kafka/cli/admin/configs/__init__.py +10 -0
- kafka/cli/admin/configs/alter.py +43 -0
- kafka/cli/admin/configs/common.py +17 -0
- kafka/cli/admin/configs/describe.py +30 -0
- kafka/cli/admin/configs/list.py +16 -0
- kafka/cli/admin/configs/reset.py +20 -0
- kafka/cli/admin/groups/__init__.py +16 -0
- kafka/cli/admin/groups/alter_offsets.py +30 -0
- kafka/cli/admin/groups/delete.py +11 -0
- kafka/cli/admin/groups/delete_offsets.py +29 -0
- kafka/cli/admin/groups/describe.py +11 -0
- kafka/cli/admin/groups/list.py +28 -0
- kafka/cli/admin/groups/list_offsets.py +29 -0
- kafka/cli/admin/groups/remove_members.py +40 -0
- kafka/cli/admin/groups/reset_offsets.py +139 -0
- kafka/cli/admin/partitions/__init__.py +21 -0
- kafka/cli/admin/partitions/alter_reassignments.py +37 -0
- kafka/cli/admin/partitions/create.py +27 -0
- kafka/cli/admin/partitions/delete_records.py +31 -0
- kafka/cli/admin/partitions/describe.py +36 -0
- kafka/cli/admin/partitions/elect_leaders.py +53 -0
- kafka/cli/admin/partitions/list_offsets.py +88 -0
- kafka/cli/admin/partitions/list_reassignments.py +35 -0
- kafka/cli/admin/topics/__init__.py +10 -0
- kafka/cli/admin/topics/create.py +13 -0
- kafka/cli/admin/topics/delete.py +19 -0
- kafka/cli/admin/topics/describe.py +18 -0
- kafka/cli/admin/topics/list.py +11 -0
- kafka/cli/admin/transactions/__init__.py +17 -0
- kafka/cli/admin/transactions/abort.py +38 -0
- kafka/cli/admin/transactions/describe.py +24 -0
- kafka/cli/admin/transactions/describe_producers.py +29 -0
- kafka/cli/admin/transactions/find_hanging.py +26 -0
- kafka/cli/admin/transactions/list.py +37 -0
- kafka/cli/admin/users/__init__.py +8 -0
- kafka/cli/admin/users/alter_user_scram_credentials.py +34 -0
- kafka/cli/admin/users/describe_user_scram_credentials.py +15 -0
- kafka/cli/common.py +95 -0
- kafka/cli/consumer/__init__.py +63 -0
- kafka/cli/producer/__init__.py +57 -0
- kafka/cluster.py +824 -0
- kafka/codec.py +325 -0
- kafka/consumer/__init__.py +5 -0
- kafka/consumer/__main__.py +5 -0
- kafka/consumer/fetcher.py +2012 -0
- kafka/consumer/group.py +1347 -0
- kafka/consumer/subscription_state.py +897 -0
- kafka/coordinator/__init__.py +0 -0
- kafka/coordinator/assignors/__init__.py +0 -0
- kafka/coordinator/assignors/abstract.py +90 -0
- kafka/coordinator/assignors/cooperative_sticky.py +167 -0
- kafka/coordinator/assignors/range.py +81 -0
- kafka/coordinator/assignors/roundrobin.py +101 -0
- kafka/coordinator/assignors/sticky/StickyAssignorUserData.json +37 -0
- kafka/coordinator/assignors/sticky/__init__.py +0 -0
- kafka/coordinator/assignors/sticky/partition_movements.py +149 -0
- kafka/coordinator/assignors/sticky/sorted_set.py +63 -0
- kafka/coordinator/assignors/sticky/sticky_assignor.py +665 -0
- kafka/coordinator/assignors/sticky/user_data.py +8 -0
- kafka/coordinator/base.py +1215 -0
- kafka/coordinator/consumer.py +1224 -0
- kafka/coordinator/heartbeat.py +82 -0
- kafka/coordinator/subscription.py +34 -0
- kafka/errors.py +1004 -0
- kafka/future.py +166 -0
- kafka/metrics/__init__.py +13 -0
- kafka/metrics/compound_stat.py +33 -0
- kafka/metrics/dict_reporter.py +81 -0
- kafka/metrics/kafka_metric.py +36 -0
- kafka/metrics/measurable.py +27 -0
- kafka/metrics/measurable_stat.py +13 -0
- kafka/metrics/metric_config.py +33 -0
- kafka/metrics/metric_name.py +105 -0
- kafka/metrics/metrics.py +261 -0
- kafka/metrics/metrics_reporter.py +53 -0
- kafka/metrics/quota.py +41 -0
- kafka/metrics/stat.py +19 -0
- kafka/metrics/stats/__init__.py +15 -0
- kafka/metrics/stats/avg.py +24 -0
- kafka/metrics/stats/count.py +17 -0
- kafka/metrics/stats/histogram.py +99 -0
- kafka/metrics/stats/max_stat.py +17 -0
- kafka/metrics/stats/min_stat.py +19 -0
- kafka/metrics/stats/percentile.py +14 -0
- kafka/metrics/stats/percentiles.py +75 -0
- kafka/metrics/stats/rate.py +118 -0
- kafka/metrics/stats/sampled_stat.py +99 -0
- kafka/metrics/stats/sensor.py +136 -0
- kafka/metrics/stats/total.py +15 -0
- kafka/net/__init__.py +19 -0
- kafka/net/compat.py +165 -0
- kafka/net/connection.py +593 -0
- kafka/net/http_connect.py +144 -0
- kafka/net/inet.py +122 -0
- kafka/net/manager.py +451 -0
- kafka/net/metrics.py +149 -0
- kafka/net/sasl/__init__.py +32 -0
- kafka/net/sasl/abc.py +28 -0
- kafka/net/sasl/gssapi.py +95 -0
- kafka/net/sasl/msk.py +245 -0
- kafka/net/sasl/oauth.py +98 -0
- kafka/net/sasl/plain.py +42 -0
- kafka/net/sasl/scram.py +135 -0
- kafka/net/sasl/sspi.py +111 -0
- kafka/net/selector.py +644 -0
- kafka/net/socks5.py +262 -0
- kafka/net/transport.py +415 -0
- kafka/net/wakeup_notifier.py +72 -0
- kafka/partitioner/__init__.py +8 -0
- kafka/partitioner/abc.py +8 -0
- kafka/partitioner/default.py +89 -0
- kafka/partitioner/sticky.py +109 -0
- kafka/producer/__init__.py +5 -0
- kafka/producer/__main__.py +5 -0
- kafka/producer/future.py +101 -0
- kafka/producer/kafka.py +1123 -0
- kafka/producer/producer_batch.py +192 -0
- kafka/producer/record_accumulator.py +647 -0
- kafka/producer/sender.py +884 -0
- kafka/producer/transaction_manager.py +1326 -0
- kafka/protocol/__init__.py +0 -0
- kafka/protocol/admin/__init__.py +29 -0
- kafka/protocol/admin/acl.py +83 -0
- kafka/protocol/admin/acl.pyi +375 -0
- kafka/protocol/admin/client_quotas.py +14 -0
- kafka/protocol/admin/client_quotas.pyi +265 -0
- kafka/protocol/admin/cluster.py +31 -0
- kafka/protocol/admin/cluster.pyi +620 -0
- kafka/protocol/admin/configs.py +22 -0
- kafka/protocol/admin/configs.pyi +437 -0
- kafka/protocol/admin/groups.py +24 -0
- kafka/protocol/admin/groups.pyi +261 -0
- kafka/protocol/admin/topics.py +53 -0
- kafka/protocol/admin/topics.pyi +982 -0
- kafka/protocol/admin/transactions.py +18 -0
- kafka/protocol/admin/transactions.pyi +311 -0
- kafka/protocol/admin/users.py +14 -0
- kafka/protocol/admin/users.pyi +223 -0
- kafka/protocol/api_data.py +125 -0
- kafka/protocol/api_header.py +55 -0
- kafka/protocol/api_key.py +97 -0
- kafka/protocol/api_message.py +277 -0
- kafka/protocol/broker_version_data.py +246 -0
- kafka/protocol/consumer/__init__.py +13 -0
- kafka/protocol/consumer/fetch.py +16 -0
- kafka/protocol/consumer/fetch.pyi +298 -0
- kafka/protocol/consumer/group.py +38 -0
- kafka/protocol/consumer/group.pyi +824 -0
- kafka/protocol/consumer/metadata.py +30 -0
- kafka/protocol/consumer/metadata.pyi +89 -0
- kafka/protocol/consumer/offsets.py +75 -0
- kafka/protocol/consumer/offsets.pyi +288 -0
- kafka/protocol/data_container.py +166 -0
- kafka/protocol/frame.py +30 -0
- kafka/protocol/generate_stubs.py +468 -0
- kafka/protocol/metadata/__init__.py +10 -0
- kafka/protocol/metadata/api_versions.py +41 -0
- kafka/protocol/metadata/api_versions.pyi +128 -0
- kafka/protocol/metadata/find_coordinator.py +19 -0
- kafka/protocol/metadata/find_coordinator.pyi +105 -0
- kafka/protocol/metadata/metadata.py +34 -0
- kafka/protocol/metadata/metadata.pyi +160 -0
- kafka/protocol/old/__init__.py +0 -0
- kafka/protocol/old/abstract.py +17 -0
- kafka/protocol/old/add_offsets_to_txn.py +54 -0
- kafka/protocol/old/add_partitions_to_txn.py +71 -0
- kafka/protocol/old/admin.py +1086 -0
- kafka/protocol/old/api.py +205 -0
- kafka/protocol/old/api_versions.py +133 -0
- kafka/protocol/old/commit.py +355 -0
- kafka/protocol/old/consumer_protocol.py +36 -0
- kafka/protocol/old/end_txn.py +53 -0
- kafka/protocol/old/fetch.py +408 -0
- kafka/protocol/old/find_coordinator.py +72 -0
- kafka/protocol/old/group.py +451 -0
- kafka/protocol/old/init_producer_id.py +42 -0
- kafka/protocol/old/list_offsets.py +186 -0
- kafka/protocol/old/metadata.py +290 -0
- kafka/protocol/old/offset_for_leader_epoch.py +133 -0
- kafka/protocol/old/produce.py +247 -0
- kafka/protocol/old/sasl_authenticate.py +38 -0
- kafka/protocol/old/sasl_handshake.py +39 -0
- kafka/protocol/old/struct.py +87 -0
- kafka/protocol/old/txn_offset_commit.py +73 -0
- kafka/protocol/old/types.py +440 -0
- kafka/protocol/parser.py +191 -0
- kafka/protocol/producer/__init__.py +7 -0
- kafka/protocol/producer/produce.py +17 -0
- kafka/protocol/producer/produce.pyi +197 -0
- kafka/protocol/producer/transaction.py +30 -0
- kafka/protocol/producer/transaction.pyi +663 -0
- kafka/protocol/sasl.py +52 -0
- kafka/protocol/sasl.pyi +126 -0
- kafka/protocol/schemas/__init__.py +7 -0
- kafka/protocol/schemas/fields/__init__.py +7 -0
- kafka/protocol/schemas/fields/array.py +127 -0
- kafka/protocol/schemas/fields/base.py +156 -0
- kafka/protocol/schemas/fields/codecs/__init__.py +12 -0
- kafka/protocol/schemas/fields/codecs/encode_buffer.py +82 -0
- kafka/protocol/schemas/fields/codecs/tagged_fields.py +109 -0
- kafka/protocol/schemas/fields/codecs/types.py +505 -0
- kafka/protocol/schemas/fields/codegen.py +40 -0
- kafka/protocol/schemas/fields/simple.py +127 -0
- kafka/protocol/schemas/fields/struct.py +357 -0
- kafka/protocol/schemas/fields/struct_array.py +142 -0
- kafka/protocol/schemas/load_json.py +42 -0
- kafka/protocol/schemas/resources/AddOffsetsToTxnRequest.json +40 -0
- kafka/protocol/schemas/resources/AddOffsetsToTxnResponse.json +35 -0
- kafka/protocol/schemas/resources/AddPartitionsToTxnRequest.json +65 -0
- kafka/protocol/schemas/resources/AddPartitionsToTxnResponse.json +60 -0
- kafka/protocol/schemas/resources/AlterClientQuotasRequest.json +47 -0
- kafka/protocol/schemas/resources/AlterClientQuotasResponse.json +41 -0
- kafka/protocol/schemas/resources/AlterConfigsRequest.json +43 -0
- kafka/protocol/schemas/resources/AlterConfigsResponse.json +39 -0
- kafka/protocol/schemas/resources/AlterPartitionReassignmentsRequest.json +42 -0
- kafka/protocol/schemas/resources/AlterPartitionReassignmentsResponse.json +47 -0
- kafka/protocol/schemas/resources/AlterReplicaLogDirsRequest.json +41 -0
- kafka/protocol/schemas/resources/AlterReplicaLogDirsResponse.json +41 -0
- kafka/protocol/schemas/resources/AlterUserScramCredentialsRequest.json +45 -0
- kafka/protocol/schemas/resources/AlterUserScramCredentialsResponse.json +35 -0
- kafka/protocol/schemas/resources/ApiVersionsRequest.json +34 -0
- kafka/protocol/schemas/resources/ApiVersionsResponse.json +79 -0
- kafka/protocol/schemas/resources/ConsumerProtocolAssignment.json +42 -0
- kafka/protocol/schemas/resources/ConsumerProtocolSubscription.json +49 -0
- kafka/protocol/schemas/resources/CreateAclsRequest.json +46 -0
- kafka/protocol/schemas/resources/CreateAclsResponse.json +37 -0
- kafka/protocol/schemas/resources/CreatePartitionsRequest.json +47 -0
- kafka/protocol/schemas/resources/CreatePartitionsResponse.json +41 -0
- kafka/protocol/schemas/resources/CreateTopicsRequest.json +65 -0
- kafka/protocol/schemas/resources/CreateTopicsResponse.json +72 -0
- kafka/protocol/schemas/resources/DeleteAclsRequest.json +46 -0
- kafka/protocol/schemas/resources/DeleteAclsResponse.json +59 -0
- kafka/protocol/schemas/resources/DeleteGroupsRequest.json +30 -0
- kafka/protocol/schemas/resources/DeleteGroupsResponse.json +36 -0
- kafka/protocol/schemas/resources/DeleteRecordsRequest.json +42 -0
- kafka/protocol/schemas/resources/DeleteRecordsResponse.json +43 -0
- kafka/protocol/schemas/resources/DeleteTopicsRequest.json +43 -0
- kafka/protocol/schemas/resources/DeleteTopicsResponse.json +52 -0
- kafka/protocol/schemas/resources/DescribeAclsRequest.json +43 -0
- kafka/protocol/schemas/resources/DescribeAclsResponse.json +55 -0
- kafka/protocol/schemas/resources/DescribeClientQuotasRequest.json +37 -0
- kafka/protocol/schemas/resources/DescribeClientQuotasResponse.json +47 -0
- kafka/protocol/schemas/resources/DescribeClusterRequest.json +35 -0
- kafka/protocol/schemas/resources/DescribeClusterResponse.json +56 -0
- kafka/protocol/schemas/resources/DescribeConfigsRequest.json +42 -0
- kafka/protocol/schemas/resources/DescribeConfigsResponse.json +69 -0
- kafka/protocol/schemas/resources/DescribeGroupsRequest.json +38 -0
- kafka/protocol/schemas/resources/DescribeGroupsResponse.json +74 -0
- kafka/protocol/schemas/resources/DescribeLogDirsRequest.json +38 -0
- kafka/protocol/schemas/resources/DescribeLogDirsResponse.json +65 -0
- kafka/protocol/schemas/resources/DescribeProducersRequest.json +32 -0
- kafka/protocol/schemas/resources/DescribeProducersResponse.json +55 -0
- kafka/protocol/schemas/resources/DescribeQuorumRequest.json +39 -0
- kafka/protocol/schemas/resources/DescribeQuorumResponse.json +82 -0
- kafka/protocol/schemas/resources/DescribeTopicPartitionsRequest.json +40 -0
- kafka/protocol/schemas/resources/DescribeTopicPartitionsResponse.json +66 -0
- kafka/protocol/schemas/resources/DescribeTransactionsRequest.json +27 -0
- kafka/protocol/schemas/resources/DescribeTransactionsResponse.json +52 -0
- kafka/protocol/schemas/resources/DescribeUserScramCredentialsRequest.json +30 -0
- kafka/protocol/schemas/resources/DescribeUserScramCredentialsResponse.json +45 -0
- kafka/protocol/schemas/resources/ElectLeadersRequest.json +41 -0
- kafka/protocol/schemas/resources/ElectLeadersResponse.json +45 -0
- kafka/protocol/schemas/resources/EndTxnRequest.json +43 -0
- kafka/protocol/schemas/resources/EndTxnResponse.json +41 -0
- kafka/protocol/schemas/resources/FetchRequest.json +125 -0
- kafka/protocol/schemas/resources/FetchResponse.json +124 -0
- kafka/protocol/schemas/resources/FindCoordinatorRequest.json +43 -0
- kafka/protocol/schemas/resources/FindCoordinatorResponse.json +58 -0
- kafka/protocol/schemas/resources/HeartbeatRequest.json +39 -0
- kafka/protocol/schemas/resources/HeartbeatResponse.json +35 -0
- kafka/protocol/schemas/resources/IncrementalAlterConfigsRequest.json +44 -0
- kafka/protocol/schemas/resources/IncrementalAlterConfigsResponse.json +38 -0
- kafka/protocol/schemas/resources/InitProducerIdRequest.json +50 -0
- kafka/protocol/schemas/resources/InitProducerIdResponse.json +47 -0
- kafka/protocol/schemas/resources/JoinGroupRequest.json +63 -0
- kafka/protocol/schemas/resources/JoinGroupResponse.json +69 -0
- kafka/protocol/schemas/resources/LeaveGroupRequest.json +47 -0
- kafka/protocol/schemas/resources/LeaveGroupResponse.json +47 -0
- kafka/protocol/schemas/resources/ListConfigResourcesRequest.json +31 -0
- kafka/protocol/schemas/resources/ListConfigResourcesResponse.json +37 -0
- kafka/protocol/schemas/resources/ListGroupsRequest.json +36 -0
- kafka/protocol/schemas/resources/ListGroupsResponse.json +49 -0
- kafka/protocol/schemas/resources/ListOffsetsRequest.json +72 -0
- kafka/protocol/schemas/resources/ListOffsetsResponse.json +71 -0
- kafka/protocol/schemas/resources/ListPartitionReassignmentsRequest.json +34 -0
- kafka/protocol/schemas/resources/ListPartitionReassignmentsResponse.json +46 -0
- kafka/protocol/schemas/resources/ListTransactionsRequest.json +40 -0
- kafka/protocol/schemas/resources/ListTransactionsResponse.json +42 -0
- kafka/protocol/schemas/resources/MetadataRequest.json +56 -0
- kafka/protocol/schemas/resources/MetadataResponse.json +101 -0
- kafka/protocol/schemas/resources/OffsetCommitRequest.json +76 -0
- kafka/protocol/schemas/resources/OffsetCommitResponse.json +71 -0
- kafka/protocol/schemas/resources/OffsetDeleteRequest.json +39 -0
- kafka/protocol/schemas/resources/OffsetDeleteResponse.json +42 -0
- kafka/protocol/schemas/resources/OffsetFetchRequest.json +76 -0
- kafka/protocol/schemas/resources/OffsetFetchResponse.json +107 -0
- kafka/protocol/schemas/resources/OffsetForLeaderEpochRequest.json +52 -0
- kafka/protocol/schemas/resources/OffsetForLeaderEpochResponse.json +51 -0
- kafka/protocol/schemas/resources/ProduceRequest.json +73 -0
- kafka/protocol/schemas/resources/ProduceResponse.json +96 -0
- kafka/protocol/schemas/resources/RequestHeader.json +44 -0
- kafka/protocol/schemas/resources/ResponseHeader.json +26 -0
- kafka/protocol/schemas/resources/SaslAuthenticateRequest.json +29 -0
- kafka/protocol/schemas/resources/SaslAuthenticateResponse.json +34 -0
- kafka/protocol/schemas/resources/SaslHandshakeRequest.json +31 -0
- kafka/protocol/schemas/resources/SaslHandshakeResponse.json +32 -0
- kafka/protocol/schemas/resources/SyncGroupRequest.json +56 -0
- kafka/protocol/schemas/resources/SyncGroupResponse.json +46 -0
- kafka/protocol/schemas/resources/TxnOffsetCommitRequest.json +68 -0
- kafka/protocol/schemas/resources/TxnOffsetCommitResponse.json +47 -0
- kafka/protocol/schemas/resources/UpdateFeaturesRequest.json +43 -0
- kafka/protocol/schemas/resources/UpdateFeaturesResponse.json +39 -0
- kafka/protocol/schemas/resources/WriteTxnMarkersRequest.json +49 -0
- kafka/protocol/schemas/resources/WriteTxnMarkersResponse.json +45 -0
- kafka/protocol/schemas/resources/__init__.py +0 -0
- kafka/record/__init__.py +3 -0
- kafka/record/_crc32c.py +161 -0
- kafka/record/abc.py +144 -0
- kafka/record/default_records.py +782 -0
- kafka/record/legacy_records.py +587 -0
- kafka/record/memory_records.py +255 -0
- kafka/record/util.py +135 -0
- kafka/serializer/__init__.py +4 -0
- kafka/serializer/abstract.py +20 -0
- kafka/serializer/default.py +16 -0
- kafka/serializer/json.py +17 -0
- kafka/serializer/wrapper.py +21 -0
- kafka/structs.py +69 -0
- kafka/util.py +159 -0
- kafka/vendor/__init__.py +0 -0
- kafka/version.py +1 -0
- kafka_python-3.0.0.dist-info/METADATA +319 -0
- kafka_python-3.0.0.dist-info/RECORD +373 -0
- kafka_python-3.0.0.dist-info/WHEEL +5 -0
- kafka_python-3.0.0.dist-info/entry_points.txt +2 -0
- kafka_python-3.0.0.dist-info/licenses/LICENSE +202 -0
- kafka_python-3.0.0.dist-info/top_level.txt +1 -0
kafka/admin/client.py
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"""KafkaAdminClient - high-level Kafka cluster administration."""
|
|
2
|
+
|
|
3
|
+
import copy
|
|
4
|
+
import logging
|
|
5
|
+
import selectors
|
|
6
|
+
import socket
|
|
7
|
+
import time
|
|
8
|
+
|
|
9
|
+
import kafka.errors as Errors
|
|
10
|
+
from kafka.errors import KafkaConfigurationError, UnrecognizedBrokerVersion
|
|
11
|
+
from kafka.metrics import MetricConfig, Metrics
|
|
12
|
+
from kafka.net.compat import KafkaNetClient
|
|
13
|
+
from kafka.protocol.metadata import MetadataRequest, FindCoordinatorRequest, CoordinatorType
|
|
14
|
+
from kafka.version import __version__
|
|
15
|
+
|
|
16
|
+
from kafka.admin._acls import ACLAdminMixin
|
|
17
|
+
from kafka.admin._cluster import ClusterAdminMixin
|
|
18
|
+
from kafka.admin._configs import ConfigAdminMixin
|
|
19
|
+
from kafka.admin._groups import GroupAdminMixin
|
|
20
|
+
from kafka.admin._partitions import PartitionAdminMixin
|
|
21
|
+
from kafka.admin._topics import TopicAdminMixin
|
|
22
|
+
from kafka.admin._transactions import TransactionsAdminMixin
|
|
23
|
+
from kafka.admin._users import UserAdminMixin
|
|
24
|
+
|
|
25
|
+
log = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class KafkaAdminClient(
|
|
29
|
+
ACLAdminMixin,
|
|
30
|
+
ClusterAdminMixin,
|
|
31
|
+
ConfigAdminMixin,
|
|
32
|
+
GroupAdminMixin,
|
|
33
|
+
PartitionAdminMixin,
|
|
34
|
+
TopicAdminMixin,
|
|
35
|
+
TransactionsAdminMixin,
|
|
36
|
+
UserAdminMixin,
|
|
37
|
+
):
|
|
38
|
+
"""A class for administering the Kafka cluster.
|
|
39
|
+
|
|
40
|
+
Keyword Arguments:
|
|
41
|
+
bootstrap_servers: 'host[:port]' string (or list of 'host[:port]'
|
|
42
|
+
strings) that the consumer should contact to bootstrap initial
|
|
43
|
+
cluster metadata. This does not have to be the full node list.
|
|
44
|
+
It just needs to have at least one broker that will respond to a
|
|
45
|
+
Metadata API Request. Default port is 9092. If no servers are
|
|
46
|
+
specified, will default to localhost:9092.
|
|
47
|
+
client_id (str): a name for this client. This string is passed in
|
|
48
|
+
each request to servers and can be used to identify specific
|
|
49
|
+
server-side log entries that correspond to this client. Also
|
|
50
|
+
submitted to GroupCoordinator for logging with respect to
|
|
51
|
+
consumer group administration. Default: 'kafka-python-{version}'
|
|
52
|
+
reconnect_backoff_ms (int): The amount of time in milliseconds to
|
|
53
|
+
wait before attempting to reconnect to a given host.
|
|
54
|
+
Default: 50.
|
|
55
|
+
reconnect_backoff_max_ms (int): The maximum amount of time in
|
|
56
|
+
milliseconds to backoff/wait when reconnecting to a broker that has
|
|
57
|
+
repeatedly failed to connect. If provided, the backoff per host
|
|
58
|
+
will increase exponentially for each consecutive connection
|
|
59
|
+
failure, up to this maximum. Once the maximum is reached,
|
|
60
|
+
reconnection attempts will continue periodically with this fixed
|
|
61
|
+
rate. To avoid connection storms, a randomization factor of 0.2
|
|
62
|
+
will be applied to the backoff resulting in a random range between
|
|
63
|
+
20% below and 20% above the computed value. Default: 30000.
|
|
64
|
+
request_timeout_ms (int): Client request timeout in milliseconds.
|
|
65
|
+
Default: 30000.
|
|
66
|
+
connections_max_idle_ms: Close idle connections after the number of
|
|
67
|
+
milliseconds specified by this config. The broker closes idle
|
|
68
|
+
connections after connections.max.idle.ms, so this avoids hitting
|
|
69
|
+
unexpected socket disconnected errors on the client.
|
|
70
|
+
Default: 540000
|
|
71
|
+
retry_backoff_ms (int): Milliseconds to backoff when retrying on
|
|
72
|
+
errors. Default: 100.
|
|
73
|
+
max_in_flight_requests_per_connection (int): Requests are pipelined
|
|
74
|
+
to kafka brokers up to this number of maximum requests per
|
|
75
|
+
broker connection. Default: 5.
|
|
76
|
+
receive_message_max_bytes (int): Maximum allowed network frame size.
|
|
77
|
+
Used to avoid OOM when decoding malformed network message header.
|
|
78
|
+
Default: 1000000.
|
|
79
|
+
receive_buffer_bytes (int): The size of the TCP receive buffer
|
|
80
|
+
(SO_RCVBUF) to use when reading data. Default: None (relies on
|
|
81
|
+
system defaults). Java client defaults to 32768.
|
|
82
|
+
send_buffer_bytes (int): The size of the TCP send buffer
|
|
83
|
+
(SO_SNDBUF) to use when sending data. Default: None (relies on
|
|
84
|
+
system defaults). Java client defaults to 131072.
|
|
85
|
+
socket_options (list): List of tuple-arguments to socket.setsockopt
|
|
86
|
+
to apply to broker connection sockets. Default:
|
|
87
|
+
[(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
|
|
88
|
+
metadata_max_age_ms (int): The period of time in milliseconds after
|
|
89
|
+
which we force a refresh of metadata even if we haven't seen any
|
|
90
|
+
partition leadership changes to proactively discover any new
|
|
91
|
+
brokers or partitions. Default: 300000
|
|
92
|
+
security_protocol (str): Protocol used to communicate with brokers.
|
|
93
|
+
Valid values are: PLAINTEXT, SSL, SASL_PLAINTEXT, SASL_SSL.
|
|
94
|
+
Default: PLAINTEXT.
|
|
95
|
+
ssl_context (ssl.SSLContext): Pre-configured SSLContext for wrapping
|
|
96
|
+
socket connections. If provided, all other ssl_* configurations
|
|
97
|
+
will be ignored. Default: None.
|
|
98
|
+
ssl_check_hostname (bool): Flag to configure whether SSL handshake
|
|
99
|
+
should verify that the certificate matches the broker's hostname.
|
|
100
|
+
Default: True.
|
|
101
|
+
ssl_cafile (str): Optional filename of CA file to use in certificate
|
|
102
|
+
verification. Default: None.
|
|
103
|
+
ssl_certfile (str): Optional filename of file in PEM format containing
|
|
104
|
+
the client certificate, as well as any CA certificates needed to
|
|
105
|
+
establish the certificate's authenticity. Default: None.
|
|
106
|
+
ssl_keyfile (str): Optional filename containing the client private key.
|
|
107
|
+
Default: None.
|
|
108
|
+
ssl_password (str): Optional password to be used when loading the
|
|
109
|
+
certificate chain. Default: None.
|
|
110
|
+
ssl_crlfile (str): Optional filename containing the CRL to check for
|
|
111
|
+
certificate expiration. By default, no CRL check is done. When
|
|
112
|
+
providing a file, only the leaf certificate will be checked against
|
|
113
|
+
this CRL. Default: None.
|
|
114
|
+
api_version (tuple): Specify which Kafka API version to use. If set to
|
|
115
|
+
None, the client will infer the broker version from the results of
|
|
116
|
+
ApiVersionsRequest API. For brokers earlier than 0.10, which do not
|
|
117
|
+
support the ApiVersionsRequest API, api_version is required.
|
|
118
|
+
Note: Dynamic version checking is performed eagerly during __init__
|
|
119
|
+
and can raise KafkaTimeoutError if no connection can be made before
|
|
120
|
+
timeout (see bootstrap_timeout_ms below).
|
|
121
|
+
Different versions enable different functionality.
|
|
122
|
+
|
|
123
|
+
Examples::
|
|
124
|
+
|
|
125
|
+
(4, 3) most recent broker release, enable all supported features
|
|
126
|
+
(2, 7) support SCRAM user credential apis
|
|
127
|
+
(0, 11) enables message format v2 (internal)
|
|
128
|
+
(0, 10, 0) enables sasl authentication and message format v1
|
|
129
|
+
(0, 9) enables full group coordination features with automatic
|
|
130
|
+
partition assignment and rebalancing,
|
|
131
|
+
(0, 8, 2) enables kafka-storage offset commits with manual
|
|
132
|
+
partition assignment only,
|
|
133
|
+
(0, 8, 1) enables zookeeper-storage offset commits with manual
|
|
134
|
+
partition assignment only,
|
|
135
|
+
(0, 8, 0) enables basic functionality but requires manual
|
|
136
|
+
partition assignment and offset management.
|
|
137
|
+
|
|
138
|
+
Default: None
|
|
139
|
+
bootstrap_timeout_ms (int): number of milliseconds to wait for first
|
|
140
|
+
successful cluster bootstrap. If provided, an attempt to bootstrap
|
|
141
|
+
will raise KafkaTimeoutError if it is unable to fetch cluster
|
|
142
|
+
metadata before the configured timeout. Note that bootstrap is
|
|
143
|
+
called eagerly from __init__().
|
|
144
|
+
Default: 30000
|
|
145
|
+
selector (selectors.BaseSelector): Provide a specific selector
|
|
146
|
+
implementation to use for I/O multiplexing.
|
|
147
|
+
Default: selectors.DefaultSelector
|
|
148
|
+
metrics (kafka.metrics.Metrics): Optionally provide a metrics
|
|
149
|
+
instance for capturing network IO stats. Default: None.
|
|
150
|
+
metric_group_prefix (str): Prefix for metric names. Default: ''
|
|
151
|
+
sasl_mechanism (str): Authentication mechanism when security_protocol
|
|
152
|
+
is configured for SASL_PLAINTEXT or SASL_SSL. Valid values are:
|
|
153
|
+
PLAIN, GSSAPI, OAUTHBEARER, SCRAM-SHA-256, SCRAM-SHA-512.
|
|
154
|
+
sasl_plain_username (str): username for sasl PLAIN and SCRAM authentication.
|
|
155
|
+
Required if sasl_mechanism is PLAIN or one of the SCRAM mechanisms.
|
|
156
|
+
sasl_plain_password (str): password for sasl PLAIN and SCRAM authentication.
|
|
157
|
+
Required if sasl_mechanism is PLAIN or one of the SCRAM mechanisms.
|
|
158
|
+
sasl_kerberos_name (str or gssapi.Name): Constructed gssapi.Name for use with
|
|
159
|
+
sasl mechanism handshake. If provided, sasl_kerberos_service_name and
|
|
160
|
+
sasl_kerberos_domain name are ignored. Default: None.
|
|
161
|
+
sasl_kerberos_service_name (str): Service name to include in GSSAPI
|
|
162
|
+
sasl mechanism handshake. Default: 'kafka'
|
|
163
|
+
sasl_kerberos_domain_name (str): kerberos domain name to use in GSSAPI
|
|
164
|
+
sasl mechanism handshake. Default: one of bootstrap servers
|
|
165
|
+
sasl_oauth_token_provider (kafka.net.sasl.oauth.AbstractTokenProvider): OAuthBearer
|
|
166
|
+
token provider instance. Default: None
|
|
167
|
+
proxy_url (str): URL to proxy socket connections through. Supports SOCKS5 only.
|
|
168
|
+
Requires scheme:// (e.g., socks5://foo.bar/). Default: None
|
|
169
|
+
kafka_client (callable): Custom class / callable for creating KafkaNetClient instances
|
|
170
|
+
"""
|
|
171
|
+
DEFAULT_CONFIG = {
|
|
172
|
+
# client configs
|
|
173
|
+
'bootstrap_servers': 'localhost',
|
|
174
|
+
'client_id': 'kafka-python-' + __version__,
|
|
175
|
+
'request_timeout_ms': 30000,
|
|
176
|
+
'connections_max_idle_ms': 9 * 60 * 1000,
|
|
177
|
+
'reconnect_backoff_ms': 50,
|
|
178
|
+
'reconnect_backoff_max_ms': 30000,
|
|
179
|
+
'max_in_flight_requests_per_connection': 5,
|
|
180
|
+
'receive_message_max_bytes': 1000000,
|
|
181
|
+
'receive_buffer_bytes': None,
|
|
182
|
+
'send_buffer_bytes': None,
|
|
183
|
+
'socket_options': [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)],
|
|
184
|
+
'retry_backoff_ms': 100,
|
|
185
|
+
'metadata_max_age_ms': 300000,
|
|
186
|
+
'client_dns_lookup': 'use_all_dns_ips',
|
|
187
|
+
'security_protocol': 'PLAINTEXT',
|
|
188
|
+
'ssl_context': None,
|
|
189
|
+
'ssl_check_hostname': True,
|
|
190
|
+
'ssl_cafile': None,
|
|
191
|
+
'ssl_certfile': None,
|
|
192
|
+
'ssl_keyfile': None,
|
|
193
|
+
'ssl_password': None,
|
|
194
|
+
'ssl_crlfile': None,
|
|
195
|
+
'api_version': None,
|
|
196
|
+
'bootstrap_timeout_ms': 30000,
|
|
197
|
+
'selector': selectors.DefaultSelector,
|
|
198
|
+
'sasl_mechanism': None,
|
|
199
|
+
'sasl_plain_username': None,
|
|
200
|
+
'sasl_plain_password': None,
|
|
201
|
+
'sasl_kerberos_name': None,
|
|
202
|
+
'sasl_kerberos_service_name': 'kafka',
|
|
203
|
+
'sasl_kerberos_domain_name': None,
|
|
204
|
+
'sasl_oauth_token_provider': None,
|
|
205
|
+
'proxy_url': None,
|
|
206
|
+
'socks5_proxy': None, # deprecated
|
|
207
|
+
|
|
208
|
+
# metrics configs
|
|
209
|
+
'metric_reporters': [],
|
|
210
|
+
'metrics_num_samples': 2,
|
|
211
|
+
'metrics_sample_window_ms': 30000,
|
|
212
|
+
'kafka_client': KafkaNetClient,
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
def __init__(self, **configs):
|
|
216
|
+
log.debug("Starting KafkaAdminClient with configuration: %s", configs)
|
|
217
|
+
extra_configs = set(configs).difference(self.DEFAULT_CONFIG)
|
|
218
|
+
if extra_configs:
|
|
219
|
+
raise KafkaConfigurationError("Unrecognized configs: {}".format(extra_configs))
|
|
220
|
+
|
|
221
|
+
self.config = copy.copy(self.DEFAULT_CONFIG)
|
|
222
|
+
self.config.update(configs)
|
|
223
|
+
|
|
224
|
+
# Configure metrics
|
|
225
|
+
metrics_tags = {'client-id': self.config['client_id']}
|
|
226
|
+
metric_config = MetricConfig(samples=self.config['metrics_num_samples'],
|
|
227
|
+
time_window_ms=self.config['metrics_sample_window_ms'],
|
|
228
|
+
tags=metrics_tags)
|
|
229
|
+
reporters = [reporter() for reporter in self.config['metric_reporters']]
|
|
230
|
+
self._metrics = Metrics(metric_config, reporters)
|
|
231
|
+
|
|
232
|
+
self._client = self.config['kafka_client'](
|
|
233
|
+
metrics=self._metrics,
|
|
234
|
+
metric_group_prefix='admin',
|
|
235
|
+
**self.config
|
|
236
|
+
)
|
|
237
|
+
# Goal: migrate all self._client calls -> self._manager (skipping compat layer)
|
|
238
|
+
self._manager = self._client._manager
|
|
239
|
+
self._net = self._manager._net
|
|
240
|
+
|
|
241
|
+
# Run all IO on a dedicated background thread; public admin methods
|
|
242
|
+
# block on cross-thread Events via self._manager.run(...).
|
|
243
|
+
self._net.start()
|
|
244
|
+
|
|
245
|
+
# Bootstrap on __init__
|
|
246
|
+
self._manager.bootstrap(self.config['bootstrap_timeout_ms'])
|
|
247
|
+
self._closed = False
|
|
248
|
+
self._controller_id = None
|
|
249
|
+
log.debug("KafkaAdminClient started.")
|
|
250
|
+
|
|
251
|
+
def __enter__(self):
|
|
252
|
+
return self
|
|
253
|
+
|
|
254
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
255
|
+
self.close()
|
|
256
|
+
|
|
257
|
+
def close(self):
|
|
258
|
+
"""Close the KafkaAdminClient connection to the Kafka broker."""
|
|
259
|
+
if not hasattr(self, '_closed') or self._closed:
|
|
260
|
+
log.info("KafkaAdminClient already closed.")
|
|
261
|
+
return
|
|
262
|
+
|
|
263
|
+
self._closed = True
|
|
264
|
+
self._metrics.close()
|
|
265
|
+
self._client.close()
|
|
266
|
+
self._net.stop()
|
|
267
|
+
log.debug("KafkaAdminClient is now closed.")
|
|
268
|
+
|
|
269
|
+
def _validate_timeout(self, timeout_ms):
|
|
270
|
+
"""Validate the timeout is set or use the configuration default."""
|
|
271
|
+
return timeout_ms or self.config['request_timeout_ms']
|
|
272
|
+
|
|
273
|
+
# -- Routing primitives (used by mixins) ----------------------------------
|
|
274
|
+
|
|
275
|
+
async def _refresh_controller_id(self, timeout_ms=30000):
|
|
276
|
+
"""Determine the Kafka cluster controller."""
|
|
277
|
+
if self._manager.broker_version < (0, 10):
|
|
278
|
+
raise UnrecognizedBrokerVersion(
|
|
279
|
+
"Kafka Admin Client controller requests requires broker version >= (0, 10)")
|
|
280
|
+
|
|
281
|
+
request = MetadataRequest()
|
|
282
|
+
timeout_at = time.monotonic() + timeout_ms / 1000
|
|
283
|
+
while time.monotonic() < timeout_at:
|
|
284
|
+
response = await self._manager.send(request)
|
|
285
|
+
controller_id = response.controller_id
|
|
286
|
+
if controller_id == -1:
|
|
287
|
+
log.warning("Controller ID not available, got -1")
|
|
288
|
+
await self._net.sleep(1)
|
|
289
|
+
continue
|
|
290
|
+
return controller_id
|
|
291
|
+
else:
|
|
292
|
+
raise Errors.NodeNotReadyError('controller')
|
|
293
|
+
|
|
294
|
+
async def _find_coordinator_ids(self, keys, key_type=CoordinatorType.GROUP):
|
|
295
|
+
"""Find broker node_ids of the coordinators for a set of keys.
|
|
296
|
+
|
|
297
|
+
``key_type`` is the CoordinatorType enum (GROUP=0, TRANSACTION=1,
|
|
298
|
+
SHARE=2). Results are cached in the shared
|
|
299
|
+
``ClusterMetadata._coordinators`` map; only keys not already in
|
|
300
|
+
the cache hit the network. On brokers supporting FindCoordinator
|
|
301
|
+
v4+ (KIP-699, Apache Kafka 3.0+), all unknown keys are resolved
|
|
302
|
+
in a single batched request; older brokers fall back to one
|
|
303
|
+
request per key.
|
|
304
|
+
|
|
305
|
+
Returns a dict mapping key -> node_id.
|
|
306
|
+
"""
|
|
307
|
+
key_type = CoordinatorType.build_from(key_type)
|
|
308
|
+
cluster = self._manager.cluster
|
|
309
|
+
result = {}
|
|
310
|
+
unknown = []
|
|
311
|
+
for key in keys:
|
|
312
|
+
cached = cluster.get_coordinator(key, key_type)
|
|
313
|
+
if cached is not None:
|
|
314
|
+
result[key] = cached
|
|
315
|
+
else:
|
|
316
|
+
unknown.append(key)
|
|
317
|
+
if not unknown:
|
|
318
|
+
return result
|
|
319
|
+
|
|
320
|
+
if self._manager.broker_version_data.api_version(FindCoordinatorRequest) >= 4:
|
|
321
|
+
request = FindCoordinatorRequest(key_type=key_type.value,
|
|
322
|
+
coordinator_keys=unknown,
|
|
323
|
+
min_version=4)
|
|
324
|
+
response = await self._manager.send(request)
|
|
325
|
+
# v4+ Coordinator rows are duck-compatible with v0-v3 response
|
|
326
|
+
# shape: both have error_code/error_message/node_id/host/port.
|
|
327
|
+
for coordinator in response.coordinators:
|
|
328
|
+
node_id = cluster.add_coordinator(
|
|
329
|
+
coordinator, key_type, coordinator.key,
|
|
330
|
+
synthesize_node_id=False)
|
|
331
|
+
result[coordinator.key] = node_id
|
|
332
|
+
else:
|
|
333
|
+
# Broker does not support batch api; fan-out request per-key
|
|
334
|
+
for key in unknown:
|
|
335
|
+
request = FindCoordinatorRequest(key=key,
|
|
336
|
+
key_type=key_type.value,
|
|
337
|
+
max_version=3)
|
|
338
|
+
response = await self._manager.send(request)
|
|
339
|
+
node_id = cluster.add_coordinator(
|
|
340
|
+
response, key_type, key, synthesize_node_id=False)
|
|
341
|
+
result[key] = node_id
|
|
342
|
+
return result
|
|
343
|
+
|
|
344
|
+
async def _find_coordinator_id(self, key, key_type=CoordinatorType.GROUP):
|
|
345
|
+
"""Single-key wrapper for _find_coordinator_ids()"""
|
|
346
|
+
ids = await self._find_coordinator_ids([key], key_type=key_type)
|
|
347
|
+
return ids[key]
|
|
348
|
+
|
|
349
|
+
async def _send_request_to_controller(self, request, get_errors_fn=lambda r: (), raise_errors=True, ignore_errors=()):
|
|
350
|
+
"""Send a Kafka protocol message to the cluster controller.
|
|
351
|
+
|
|
352
|
+
Retries once on NotControllerError after refreshing the controller id.
|
|
353
|
+
"""
|
|
354
|
+
if self._controller_id is None or self._controller_id == -1:
|
|
355
|
+
self._controller_id = await self._refresh_controller_id()
|
|
356
|
+
|
|
357
|
+
response = await self._manager.send(request, node_id=self._controller_id)
|
|
358
|
+
|
|
359
|
+
# Refresh controller and retry on NotControllerError
|
|
360
|
+
if Errors.NotControllerError in get_errors_fn(response):
|
|
361
|
+
self._controller_id = await self._refresh_controller_id()
|
|
362
|
+
response = await self._manager.send(request, node_id=self._controller_id)
|
|
363
|
+
|
|
364
|
+
for error_type in get_errors_fn(response):
|
|
365
|
+
if error_type is Errors.NoError:
|
|
366
|
+
continue
|
|
367
|
+
elif error_type is Errors.NotControllerError:
|
|
368
|
+
raise RuntimeError("Failed to find active controller id!")
|
|
369
|
+
elif raise_errors and error_type not in ignore_errors:
|
|
370
|
+
raise error_type(
|
|
371
|
+
"Request '{}' failed with response '{}'."
|
|
372
|
+
.format(request, response))
|
|
373
|
+
return response
|
|
File without changes
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# Adapted from https://github.com/mrafayaleem/kafka-jython
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import pprint
|
|
6
|
+
import sys
|
|
7
|
+
import threading
|
|
8
|
+
import time
|
|
9
|
+
import traceback
|
|
10
|
+
|
|
11
|
+
from kafka import KafkaConsumer
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ConsumerPerformance:
|
|
15
|
+
@staticmethod
|
|
16
|
+
def run(args):
|
|
17
|
+
try:
|
|
18
|
+
props = {}
|
|
19
|
+
for prop in args.consumer_config:
|
|
20
|
+
k, v = prop.split('=')
|
|
21
|
+
try:
|
|
22
|
+
v = int(v)
|
|
23
|
+
except ValueError:
|
|
24
|
+
pass
|
|
25
|
+
if v == 'None':
|
|
26
|
+
v = None
|
|
27
|
+
elif v == 'False':
|
|
28
|
+
v = False
|
|
29
|
+
elif v == 'True':
|
|
30
|
+
v = True
|
|
31
|
+
props[k] = v
|
|
32
|
+
|
|
33
|
+
print('Initializing Consumer...')
|
|
34
|
+
props['bootstrap_servers'] = args.bootstrap_servers
|
|
35
|
+
props['auto_offset_reset'] = 'earliest'
|
|
36
|
+
if 'consumer_timeout_ms' not in props:
|
|
37
|
+
props['consumer_timeout_ms'] = 10000
|
|
38
|
+
props['metrics_sample_window_ms'] = args.stats_interval * 1000
|
|
39
|
+
for k, v in props.items():
|
|
40
|
+
print('---> {0}={1}'.format(k, v))
|
|
41
|
+
consumer = KafkaConsumer(args.topic, **props)
|
|
42
|
+
print('---> topic={0}'.format(args.topic))
|
|
43
|
+
print('---> report stats every {0} secs'.format(args.stats_interval))
|
|
44
|
+
print('---> raw metrics? {0}'.format(args.raw_metrics))
|
|
45
|
+
timer_stop = threading.Event()
|
|
46
|
+
timer = StatsReporter(args.stats_interval, consumer,
|
|
47
|
+
event=timer_stop,
|
|
48
|
+
raw_metrics=args.raw_metrics)
|
|
49
|
+
timer.start()
|
|
50
|
+
print('-> OK!')
|
|
51
|
+
print()
|
|
52
|
+
|
|
53
|
+
start_time = time.monotonic()
|
|
54
|
+
records = 0
|
|
55
|
+
for msg in consumer:
|
|
56
|
+
records += 1
|
|
57
|
+
if records >= args.num_records:
|
|
58
|
+
break
|
|
59
|
+
|
|
60
|
+
end_time = time.monotonic()
|
|
61
|
+
timer_stop.set()
|
|
62
|
+
timer.join()
|
|
63
|
+
print('Consumed {0} records'.format(records))
|
|
64
|
+
print('Execution time:', end_time - start_time, 'secs')
|
|
65
|
+
|
|
66
|
+
except Exception:
|
|
67
|
+
exc_info = sys.exc_info()
|
|
68
|
+
traceback.print_exception(*exc_info)
|
|
69
|
+
sys.exit(1)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class StatsReporter(threading.Thread):
|
|
73
|
+
def __init__(self, interval, consumer, event=None, raw_metrics=False):
|
|
74
|
+
super().__init__()
|
|
75
|
+
self.interval = interval
|
|
76
|
+
self.consumer = consumer
|
|
77
|
+
self.event = event
|
|
78
|
+
self.raw_metrics = raw_metrics
|
|
79
|
+
|
|
80
|
+
def print_stats(self):
|
|
81
|
+
metrics = self.consumer.metrics()
|
|
82
|
+
if self.raw_metrics:
|
|
83
|
+
pprint.pprint(metrics)
|
|
84
|
+
else:
|
|
85
|
+
print('{records-consumed-rate:.0f} records/sec ({bytes-consumed-rate:.0f} B/sec),'
|
|
86
|
+
' {fetch-latency-avg:.0f}ms avg latency,'
|
|
87
|
+
' {fetch-rate:.0f} avg fetch requests/sec,'
|
|
88
|
+
' {fetch-size-avg:.0f} avg fetch size,'
|
|
89
|
+
' {records-lag-max:.0f} max record lag,'
|
|
90
|
+
' {records-per-request-avg:.0f} avg records/req'
|
|
91
|
+
.format(**metrics['consumer-fetch-manager-metrics']))
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def print_final(self):
|
|
95
|
+
self.print_stats()
|
|
96
|
+
|
|
97
|
+
def run(self):
|
|
98
|
+
while self.event and not self.event.wait(self.interval):
|
|
99
|
+
self.print_stats()
|
|
100
|
+
else:
|
|
101
|
+
self.print_final()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_args_parser():
|
|
105
|
+
parser = argparse.ArgumentParser(
|
|
106
|
+
description='This tool is used to verify the consumer performance.')
|
|
107
|
+
|
|
108
|
+
parser.add_argument(
|
|
109
|
+
'-b', '--bootstrap-servers', type=str, nargs='+', default=(),
|
|
110
|
+
help='host:port for cluster bootstrap servers')
|
|
111
|
+
parser.add_argument(
|
|
112
|
+
'-t', '--topic', type=str,
|
|
113
|
+
help='Topic for consumer test (default: kafka-python-benchmark-test)',
|
|
114
|
+
default='kafka-python-benchmark-test')
|
|
115
|
+
parser.add_argument(
|
|
116
|
+
'--num-records', type=int,
|
|
117
|
+
help='number of messages to consume (default: 1000000)',
|
|
118
|
+
default=1000000)
|
|
119
|
+
parser.add_argument(
|
|
120
|
+
'-c', '--consumer-config', type=str, nargs='+', default=(),
|
|
121
|
+
help='kafka consumer related configuration properties like '
|
|
122
|
+
'bootstrap_servers,client_id etc..')
|
|
123
|
+
parser.add_argument(
|
|
124
|
+
'--fixture-compression', type=str,
|
|
125
|
+
help='specify a compression type for use with broker fixtures / producer')
|
|
126
|
+
parser.add_argument(
|
|
127
|
+
'--stats-interval', type=int,
|
|
128
|
+
help='Interval in seconds for stats reporting to console (default: 5)',
|
|
129
|
+
default=5)
|
|
130
|
+
parser.add_argument(
|
|
131
|
+
'--raw-metrics', action='store_true',
|
|
132
|
+
help='Enable this flag to print full metrics dict on each interval')
|
|
133
|
+
return parser
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if __name__ == '__main__':
|
|
137
|
+
args = get_args_parser().parse_args()
|
|
138
|
+
ConsumerPerformance.run(args)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import logging
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from kafka import KafkaConsumer, KafkaProducer
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Producer(threading.Thread):
|
|
12
|
+
|
|
13
|
+
def __init__(self, bootstrap_servers, topic, stop_event, msg_size):
|
|
14
|
+
super().__init__()
|
|
15
|
+
self.bootstrap_servers = bootstrap_servers
|
|
16
|
+
self.topic = topic
|
|
17
|
+
self.stop_event = stop_event
|
|
18
|
+
self.big_msg = b'1' * msg_size
|
|
19
|
+
|
|
20
|
+
def run(self):
|
|
21
|
+
producer = KafkaProducer(bootstrap_servers=self.bootstrap_servers)
|
|
22
|
+
self.sent = 0
|
|
23
|
+
|
|
24
|
+
while not self.stop_event.is_set():
|
|
25
|
+
producer.send(self.topic, self.big_msg)
|
|
26
|
+
self.sent += 1
|
|
27
|
+
producer.flush()
|
|
28
|
+
producer.close()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Consumer(threading.Thread):
|
|
32
|
+
def __init__(self, bootstrap_servers, topic, stop_event, msg_size):
|
|
33
|
+
super().__init__()
|
|
34
|
+
self.bootstrap_servers = bootstrap_servers
|
|
35
|
+
self.topic = topic
|
|
36
|
+
self.stop_event = stop_event
|
|
37
|
+
self.msg_size = msg_size
|
|
38
|
+
|
|
39
|
+
def run(self):
|
|
40
|
+
consumer = KafkaConsumer(bootstrap_servers=self.bootstrap_servers,
|
|
41
|
+
auto_offset_reset='earliest')
|
|
42
|
+
consumer.subscribe([self.topic])
|
|
43
|
+
self.valid = 0
|
|
44
|
+
self.invalid = 0
|
|
45
|
+
|
|
46
|
+
for message in consumer:
|
|
47
|
+
if len(message.value) == self.msg_size:
|
|
48
|
+
self.valid += 1
|
|
49
|
+
else:
|
|
50
|
+
print('Invalid message:', len(message.value), self.msg_size)
|
|
51
|
+
self.invalid += 1
|
|
52
|
+
|
|
53
|
+
if self.stop_event.is_set():
|
|
54
|
+
break
|
|
55
|
+
consumer.close()
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def get_args_parser():
|
|
59
|
+
parser = argparse.ArgumentParser(
|
|
60
|
+
description='This tool is used to demonstrate consumer and producer load.')
|
|
61
|
+
|
|
62
|
+
parser.add_argument(
|
|
63
|
+
'--bootstrap-servers', type=str, nargs='+', default=('localhost:9092'),
|
|
64
|
+
help='host:port for cluster bootstrap servers (default: localhost:9092)')
|
|
65
|
+
parser.add_argument(
|
|
66
|
+
'--topic', type=str,
|
|
67
|
+
help='Topic for load test (default: kafka-python-benchmark-load-example)',
|
|
68
|
+
default='kafka-python-benchmark-load-example')
|
|
69
|
+
parser.add_argument(
|
|
70
|
+
'--msg-size', type=int,
|
|
71
|
+
help='Message size, in bytes, for load test (default: 524288)',
|
|
72
|
+
default=524288)
|
|
73
|
+
parser.add_argument(
|
|
74
|
+
'--load-time', type=int,
|
|
75
|
+
help='number of seconds to run load test (default: 10)',
|
|
76
|
+
default=10)
|
|
77
|
+
parser.add_argument(
|
|
78
|
+
'--log-level', type=str,
|
|
79
|
+
help='Optional logging level for load test: ERROR|INFO|DEBUG etc',
|
|
80
|
+
default=None)
|
|
81
|
+
return parser
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def main(args):
|
|
85
|
+
if args.log_level:
|
|
86
|
+
logging.basicConfig(
|
|
87
|
+
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
|
|
88
|
+
level=getattr(logging, args.log_level))
|
|
89
|
+
producer_stop = threading.Event()
|
|
90
|
+
consumer_stop = threading.Event()
|
|
91
|
+
threads = [
|
|
92
|
+
Producer(args.bootstrap_servers, args.topic, producer_stop, args.msg_size),
|
|
93
|
+
Consumer(args.bootstrap_servers, args.topic, consumer_stop, args.msg_size)
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
for t in threads:
|
|
97
|
+
t.start()
|
|
98
|
+
|
|
99
|
+
time.sleep(args.load_time)
|
|
100
|
+
producer_stop.set()
|
|
101
|
+
consumer_stop.set()
|
|
102
|
+
print('Messages sent: %d' % threads[0].sent)
|
|
103
|
+
print('Messages recvd: %d' % threads[1].valid)
|
|
104
|
+
print('Messages invalid: %d' % threads[1].invalid)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
if __name__ == "__main__":
|
|
108
|
+
args = get_args_parser().parse_args()
|
|
109
|
+
main(args)
|