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
|
@@ -0,0 +1,1224 @@
|
|
|
1
|
+
import collections
|
|
2
|
+
import copy
|
|
3
|
+
import functools
|
|
4
|
+
import inspect
|
|
5
|
+
import logging
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
from kafka.coordinator.base import BaseCoordinator, Generation
|
|
9
|
+
from kafka.coordinator.assignors.abstract import RebalanceProtocol, AbstractPartitionAssignor
|
|
10
|
+
from kafka.coordinator.assignors.range import RangePartitionAssignor
|
|
11
|
+
from kafka.coordinator.assignors.roundrobin import RoundRobinPartitionAssignor
|
|
12
|
+
from kafka.coordinator.assignors.sticky.sticky_assignor import StickyPartitionAssignor
|
|
13
|
+
from kafka.protocol.consumer.metadata import (
|
|
14
|
+
ConsumerProtocolType, ConsumerProtocolSubscription, ConsumerProtocolAssignment,
|
|
15
|
+
)
|
|
16
|
+
import kafka.errors as Errors
|
|
17
|
+
from kafka.future import Future
|
|
18
|
+
from kafka.metrics import AnonMeasurable
|
|
19
|
+
from kafka.metrics.stats import Avg, Count, Max, Rate
|
|
20
|
+
from kafka.protocol.consumer import OffsetCommitRequest, OffsetFetchRequest, IsolationLevel
|
|
21
|
+
from kafka.structs import OffsetAndMetadata, TopicPartition
|
|
22
|
+
from kafka.util import Timer, WeakMethod
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
log = logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ConsumerCoordinator(BaseCoordinator):
|
|
29
|
+
"""This class manages the coordination process with the consumer coordinator."""
|
|
30
|
+
DEFAULT_CONFIG = BaseCoordinator.DEFAULT_CONFIG.copy()
|
|
31
|
+
DEFAULT_CONFIG.update({
|
|
32
|
+
'enable_auto_commit': True,
|
|
33
|
+
'auto_commit_interval_ms': 5000,
|
|
34
|
+
'default_offset_commit_callback': None,
|
|
35
|
+
'assignors': (RangePartitionAssignor, RoundRobinPartitionAssignor, StickyPartitionAssignor),
|
|
36
|
+
'exclude_internal_topics': True,
|
|
37
|
+
'isolation_level': 'read_uncommitted',
|
|
38
|
+
'metric_group_prefix': 'consumer'
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
def __init__(self, client, subscription, **configs):
|
|
42
|
+
"""Initialize the coordination manager.
|
|
43
|
+
|
|
44
|
+
Keyword Arguments:
|
|
45
|
+
group_id (str): name of the consumer group to join for dynamic
|
|
46
|
+
partition assignment (if enabled), and to use for fetching and
|
|
47
|
+
committing offsets. Default: 'kafka-python-default-group'
|
|
48
|
+
enable_auto_commit (bool): If true the consumer's offset will be
|
|
49
|
+
periodically committed in the background. Default: True.
|
|
50
|
+
auto_commit_interval_ms (int): milliseconds between automatic
|
|
51
|
+
offset commits, if enable_auto_commit is True. Default: 5000.
|
|
52
|
+
default_offset_commit_callback (callable): called as
|
|
53
|
+
callback(offsets, response) response will be either an Exception
|
|
54
|
+
or None. This callback can be used to trigger custom actions when
|
|
55
|
+
a commit request completes.
|
|
56
|
+
assignors (list): List of objects to use to distribute partition
|
|
57
|
+
ownership amongst consumer instances when group management is
|
|
58
|
+
used. Default: [RangePartitionAssignor, RoundRobinPartitionAssignor, StickyPartitionAssignor]
|
|
59
|
+
retry_backoff_ms (int): Milliseconds to backoff when retrying on
|
|
60
|
+
errors. Default: 100.
|
|
61
|
+
exclude_internal_topics (bool): Whether records from internal topics
|
|
62
|
+
(such as offsets) should be exposed to the consumer. If set to
|
|
63
|
+
True the only way to receive records from an internal topic is
|
|
64
|
+
subscribing to it. Requires 0.10+. Default: True
|
|
65
|
+
"""
|
|
66
|
+
super().__init__(client, **configs)
|
|
67
|
+
|
|
68
|
+
self.config = copy.copy(self.DEFAULT_CONFIG)
|
|
69
|
+
for key in self.config:
|
|
70
|
+
if key in configs:
|
|
71
|
+
self.config[key] = configs[key]
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
self._isolation_level = IsolationLevel.build_from(self.config['isolation_level'])
|
|
75
|
+
except ValueError:
|
|
76
|
+
raise Errors.KafkaConfigurationError('Unrecognized isolation_level') from None
|
|
77
|
+
|
|
78
|
+
self._subscription = subscription
|
|
79
|
+
self._is_leader = False
|
|
80
|
+
self._rebalance_protocol = None
|
|
81
|
+
self._joined_subscription = set()
|
|
82
|
+
self._metadata_snapshot = self._build_metadata_snapshot(subscription, self._cluster)
|
|
83
|
+
self._assignment_snapshot = None
|
|
84
|
+
self.auto_commit_interval = self.config['auto_commit_interval_ms'] / 1000
|
|
85
|
+
self.next_auto_commit_deadline = None
|
|
86
|
+
self.completed_offset_commits = collections.deque()
|
|
87
|
+
self._offset_fetch_futures = dict()
|
|
88
|
+
self._async_commit_fenced = False
|
|
89
|
+
|
|
90
|
+
if self.config['default_offset_commit_callback'] is None:
|
|
91
|
+
self.config['default_offset_commit_callback'] = self._default_offset_commit_callback
|
|
92
|
+
|
|
93
|
+
if self.config['group_id'] is not None:
|
|
94
|
+
if self._use_group_apis:
|
|
95
|
+
if not self.config['assignors']:
|
|
96
|
+
raise Errors.KafkaConfigurationError('Coordinator requires assignors')
|
|
97
|
+
|
|
98
|
+
if self.config['enable_auto_commit']:
|
|
99
|
+
if not self._use_offset_apis:
|
|
100
|
+
log.warning('Broker version (%s) does not support offset'
|
|
101
|
+
' commits; disabling auto-commit.',
|
|
102
|
+
self.config['api_version'])
|
|
103
|
+
self.config['enable_auto_commit'] = False
|
|
104
|
+
elif self.config['group_id'] is None:
|
|
105
|
+
log.warning('group_id is None: disabling auto-commit.')
|
|
106
|
+
self.config['enable_auto_commit'] = False
|
|
107
|
+
else:
|
|
108
|
+
self.next_auto_commit_deadline = time.monotonic() + self.auto_commit_interval
|
|
109
|
+
|
|
110
|
+
if self.config['metrics']:
|
|
111
|
+
self._consumer_sensors = ConsumerCoordinatorMetrics(
|
|
112
|
+
self.config['metrics'], self.config['metric_group_prefix'], self._subscription)
|
|
113
|
+
else:
|
|
114
|
+
self._consumer_sensors = None
|
|
115
|
+
|
|
116
|
+
self._assignors = {}
|
|
117
|
+
for klass in self.config['assignors']:
|
|
118
|
+
if isinstance(klass, AbstractPartitionAssignor):
|
|
119
|
+
assignor = klass
|
|
120
|
+
else:
|
|
121
|
+
assignor = klass()
|
|
122
|
+
self._assignors[assignor.name] = assignor
|
|
123
|
+
# KIP-429: all configured assignors must agree on a single
|
|
124
|
+
# RebalanceProtocol mode. Mixing EAGER and COOPERATIVE
|
|
125
|
+
# assignors in the same consumer is unsafe - at JoinGroup time
|
|
126
|
+
# the broker picks one assignor, and the consumer needs to
|
|
127
|
+
# know up front whether to do eager (full) or cooperative
|
|
128
|
+
# (incremental) revocation.
|
|
129
|
+
self._rebalance_protocol = self._validate_rebalance_protocol()
|
|
130
|
+
self._cluster.request_update()
|
|
131
|
+
self._cluster.add_listener(WeakMethod(self._handle_metadata_update))
|
|
132
|
+
|
|
133
|
+
def _validate_rebalance_protocol(self):
|
|
134
|
+
"""Return the single :class:`RebalanceProtocol` mode that all
|
|
135
|
+
configured assignors support; raise
|
|
136
|
+
:class:`KafkaConfigurationError` if they don't agree.
|
|
137
|
+
"""
|
|
138
|
+
if not self._assignors:
|
|
139
|
+
return RebalanceProtocol.EAGER
|
|
140
|
+
common = None
|
|
141
|
+
for assignor in self._assignors.values():
|
|
142
|
+
supported = set(assignor.supported_protocols())
|
|
143
|
+
common = supported if common is None else common & supported
|
|
144
|
+
if not common:
|
|
145
|
+
names = [a.name for a in self._assignors.values()]
|
|
146
|
+
raise Errors.KafkaConfigurationError(
|
|
147
|
+
"Specified partition_assignment_strategy assignors %s do not"
|
|
148
|
+
" support a common RebalanceProtocol. Mixing EAGER and"
|
|
149
|
+
" COOPERATIVE assignors in a single consumer is not"
|
|
150
|
+
" supported." % (names,))
|
|
151
|
+
# Pick the highest mode they all agree on (EAGER < COOPERATIVE).
|
|
152
|
+
return max(common)
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def _use_offset_apis(self):
|
|
156
|
+
return self.config['api_version'] >= (0, 8, 1)
|
|
157
|
+
|
|
158
|
+
def protocol_type(self):
|
|
159
|
+
return ConsumerProtocolType
|
|
160
|
+
|
|
161
|
+
def group_protocols(self):
|
|
162
|
+
"""Returns list of preferred (protocols, metadata)"""
|
|
163
|
+
if self._subscription.subscription is None:
|
|
164
|
+
raise Errors.IllegalStateError('Consumer has not subscribed to topics')
|
|
165
|
+
# dpkp note: I really dislike this.
|
|
166
|
+
# why? because we are using this strange method group_protocols,
|
|
167
|
+
# which is seemingly innocuous, to set internal state (_joined_subscription)
|
|
168
|
+
# that is later used to check whether metadata has changed since we joined a group
|
|
169
|
+
# but there is no guarantee that this method, group_protocols, will get called
|
|
170
|
+
# in the correct sequence or that it will only be called when we want it to be.
|
|
171
|
+
# So this really should be moved elsewhere, but I don't have the energy to
|
|
172
|
+
# work that out right now. If you read this at some later date after the mutable
|
|
173
|
+
# state has bitten you... I'm sorry! It mimics the java client, and that's the
|
|
174
|
+
# best I've got for now.
|
|
175
|
+
self._joined_subscription = set(self._subscription.subscription)
|
|
176
|
+
metadata_list = []
|
|
177
|
+
for assignor in self._assignors:
|
|
178
|
+
metadata = self._assignors[assignor].metadata(self._joined_subscription)
|
|
179
|
+
group_protocol = (assignor, metadata)
|
|
180
|
+
metadata_list.append(group_protocol)
|
|
181
|
+
return metadata_list
|
|
182
|
+
|
|
183
|
+
def _handle_metadata_update(self, cluster):
|
|
184
|
+
# if we encounter any unauthorized topics, raise an exception
|
|
185
|
+
if cluster.unauthorized_topics:
|
|
186
|
+
raise Errors.TopicAuthorizationFailedError(cluster.unauthorized_topics)
|
|
187
|
+
|
|
188
|
+
if self._subscription.subscribed_pattern:
|
|
189
|
+
topics = []
|
|
190
|
+
for topic in cluster.topics(self.config['exclude_internal_topics']):
|
|
191
|
+
if self._subscription.subscribed_pattern.match(topic):
|
|
192
|
+
topics.append(topic)
|
|
193
|
+
|
|
194
|
+
if set(topics) != self._subscription.subscription:
|
|
195
|
+
self._subscription.change_subscription(topics)
|
|
196
|
+
self._cluster.set_topics(self._subscription.group_subscription())
|
|
197
|
+
|
|
198
|
+
# check if there are any changes to the metadata which should trigger
|
|
199
|
+
# a rebalance
|
|
200
|
+
if self._subscription.partitions_auto_assigned():
|
|
201
|
+
metadata_snapshot = self._build_metadata_snapshot(self._subscription, cluster)
|
|
202
|
+
if self._metadata_snapshot != metadata_snapshot:
|
|
203
|
+
self._metadata_snapshot = metadata_snapshot
|
|
204
|
+
|
|
205
|
+
# If we haven't got group coordinator support,
|
|
206
|
+
# just assign all partitions locally
|
|
207
|
+
if self._auto_assign_all_partitions():
|
|
208
|
+
self._subscription.assign_from_subscribed([
|
|
209
|
+
TopicPartition(topic, partition)
|
|
210
|
+
for topic in self._subscription.subscription
|
|
211
|
+
for partition in self._metadata_snapshot[topic]
|
|
212
|
+
])
|
|
213
|
+
|
|
214
|
+
def _auto_assign_all_partitions(self):
|
|
215
|
+
# For users that use "subscribe" without group support,
|
|
216
|
+
# we will simply assign all partitions to this consumer
|
|
217
|
+
if not self._use_group_apis:
|
|
218
|
+
return True
|
|
219
|
+
elif self.config['group_id'] is None:
|
|
220
|
+
return True
|
|
221
|
+
else:
|
|
222
|
+
return False
|
|
223
|
+
|
|
224
|
+
def _build_metadata_snapshot(self, subscription, cluster):
|
|
225
|
+
metadata_snapshot = {}
|
|
226
|
+
for topic in subscription.group_subscription():
|
|
227
|
+
partitions = cluster.partitions_for_topic(topic)
|
|
228
|
+
metadata_snapshot[topic] = partitions or set()
|
|
229
|
+
return metadata_snapshot
|
|
230
|
+
|
|
231
|
+
def _lookup_assignor(self, name):
|
|
232
|
+
return self._assignors.get(name, None)
|
|
233
|
+
|
|
234
|
+
# Threshold above which a rebalance-listener invocation is logged as a
|
|
235
|
+
# warning. Sync listeners on the IO loop will block heartbeats while
|
|
236
|
+
# they run; even async ones delay rebalance progress. 1s is a soft
|
|
237
|
+
# ceiling: well below default heartbeat_interval_ms (3s) and
|
|
238
|
+
# session_timeout_ms (45s).
|
|
239
|
+
_REBALANCE_LISTENER_WARN_SECS = 1.0
|
|
240
|
+
|
|
241
|
+
async def _invoke_rebalance_listener_async(self, method_name, arg):
|
|
242
|
+
"""Invoke a rebalance-listener method (sync or async), timing the call.
|
|
243
|
+
|
|
244
|
+
Awaits if the method is a coroutine function; otherwise calls inline.
|
|
245
|
+
Logs a warning if the call exceeds
|
|
246
|
+
:data:`_REBALANCE_LISTENER_WARN_SECS`. Caller wraps in try/except.
|
|
247
|
+
"""
|
|
248
|
+
cb = getattr(self._subscription.rebalance_listener, method_name)
|
|
249
|
+
start = time.monotonic()
|
|
250
|
+
if inspect.iscoroutinefunction(cb):
|
|
251
|
+
await cb(arg)
|
|
252
|
+
else:
|
|
253
|
+
cb(arg)
|
|
254
|
+
elapsed = time.monotonic() - start
|
|
255
|
+
if elapsed > self._REBALANCE_LISTENER_WARN_SECS:
|
|
256
|
+
log.warning(
|
|
257
|
+
"Rebalance listener %s.%s for group %s took %.3fs."
|
|
258
|
+
" Sync listeners block the consumer event loop (including"
|
|
259
|
+
" heartbeats) -- consider AsyncConsumerRebalanceListener or"
|
|
260
|
+
" wrap blocking work in a worker thread.",
|
|
261
|
+
type(self._subscription.rebalance_listener).__name__,
|
|
262
|
+
method_name, self.group_id, elapsed)
|
|
263
|
+
|
|
264
|
+
async def _on_join_complete_async(self, generation, member_id, protocol,
|
|
265
|
+
member_assignment_bytes):
|
|
266
|
+
# only the leader is responsible for monitoring for metadata changes
|
|
267
|
+
# (i.e. partition changes)
|
|
268
|
+
if not self._is_leader:
|
|
269
|
+
self._assignment_snapshot = None
|
|
270
|
+
|
|
271
|
+
assignor = self._lookup_assignor(protocol)
|
|
272
|
+
if not assignor:
|
|
273
|
+
raise ValueError('Coordinator selected invalid assignment protocol: %s' % (protocol,))
|
|
274
|
+
|
|
275
|
+
assignment = ConsumerProtocolAssignment.decode(member_assignment_bytes)
|
|
276
|
+
new_assigned = set(assignment.partitions())
|
|
277
|
+
|
|
278
|
+
# KIP-429: under COOPERATIVE, compute the diff between what we
|
|
279
|
+
# currently own and what the leader just assigned. Revoke the
|
|
280
|
+
# partitions we lost; only invoke on_partitions_assigned for
|
|
281
|
+
# the newly-added ones. If we lost any partitions, request a
|
|
282
|
+
# follow-up rebalance so the revoked partitions can land on
|
|
283
|
+
# their intended new owner.
|
|
284
|
+
# Listener hook exceptions are captured, cleanup completes,
|
|
285
|
+
# and we re-raise at the end as a KafkaError.
|
|
286
|
+
# In the cooperative branch both listeners (revoked + assigned)
|
|
287
|
+
# are invoked even if the first throws - matches Java's
|
|
288
|
+
# invokePartitionsRevoked / invokePartitionsAssigned pattern
|
|
289
|
+
# where the later call overwrites the captured exception.
|
|
290
|
+
listener_exc = None
|
|
291
|
+
|
|
292
|
+
if self._rebalance_protocol == RebalanceProtocol.COOPERATIVE:
|
|
293
|
+
currently_owned = set(self._subscription.assigned_partitions())
|
|
294
|
+
revoked = currently_owned - new_assigned
|
|
295
|
+
added = new_assigned - currently_owned
|
|
296
|
+
|
|
297
|
+
try:
|
|
298
|
+
self._subscription.assign_from_subscribed(sorted(new_assigned))
|
|
299
|
+
except ValueError as e:
|
|
300
|
+
log.warning("Cooperative assignment rejected: %s."
|
|
301
|
+
" Probably due to a deleted topic."
|
|
302
|
+
" Requesting re-join.", e)
|
|
303
|
+
self.request_rejoin()
|
|
304
|
+
return
|
|
305
|
+
|
|
306
|
+
assignor.on_assignment(assignment, generation)
|
|
307
|
+
self.next_auto_commit_deadline = time.monotonic() + self.auto_commit_interval
|
|
308
|
+
|
|
309
|
+
log.info("Cooperative rebalance complete for group %s:"
|
|
310
|
+
" owned=%s, assigned=%s, revoked=%s, added=%s",
|
|
311
|
+
self.group_id, currently_owned, new_assigned, revoked, added)
|
|
312
|
+
|
|
313
|
+
if self._subscription.rebalance_listener:
|
|
314
|
+
if revoked:
|
|
315
|
+
try:
|
|
316
|
+
await self._invoke_rebalance_listener_async(
|
|
317
|
+
'on_partitions_revoked', revoked)
|
|
318
|
+
except Exception as exc:
|
|
319
|
+
log.exception(
|
|
320
|
+
"User provided rebalance listener %s for group %s"
|
|
321
|
+
" failed on_partitions_revoked: %s",
|
|
322
|
+
self._subscription.rebalance_listener,
|
|
323
|
+
self.group_id, revoked)
|
|
324
|
+
listener_exc = exc
|
|
325
|
+
if added:
|
|
326
|
+
try:
|
|
327
|
+
await self._invoke_rebalance_listener_async(
|
|
328
|
+
'on_partitions_assigned', added)
|
|
329
|
+
except Exception as exc:
|
|
330
|
+
log.exception(
|
|
331
|
+
"User provided rebalance listener %s for group %s"
|
|
332
|
+
" failed on_partitions_assigned: %s",
|
|
333
|
+
self._subscription.rebalance_listener,
|
|
334
|
+
self.group_id, added)
|
|
335
|
+
listener_exc = exc
|
|
336
|
+
|
|
337
|
+
if revoked:
|
|
338
|
+
# Round 2: the partitions we just revoked should now
|
|
339
|
+
# be unowned cluster-wide and can be assigned to
|
|
340
|
+
# their intended new owners. Trigger a follow-up
|
|
341
|
+
# rebalance to surface those assignments.
|
|
342
|
+
log.info("Triggering follow-up rebalance for group %s to"
|
|
343
|
+
" complete cooperative move of %d partition(s)",
|
|
344
|
+
self.group_id, len(revoked))
|
|
345
|
+
self.request_rejoin()
|
|
346
|
+
|
|
347
|
+
if listener_exc is not None:
|
|
348
|
+
raise Errors.KafkaError(
|
|
349
|
+
"User rebalance callback throws an error") from listener_exc
|
|
350
|
+
return
|
|
351
|
+
|
|
352
|
+
# EAGER mode (legacy): replace the full assignment and invoke
|
|
353
|
+
# on_partitions_assigned with the entire new set.
|
|
354
|
+
try:
|
|
355
|
+
self._subscription.assign_from_subscribed(assignment.partitions())
|
|
356
|
+
except ValueError as e:
|
|
357
|
+
log.warning("Assignment rejected: %s. Probably due to a"
|
|
358
|
+
" deleted topic. Requesting re-join.", e)
|
|
359
|
+
self.request_rejoin()
|
|
360
|
+
return
|
|
361
|
+
|
|
362
|
+
# give the assignor a chance to update internal state
|
|
363
|
+
# based on the received assignment
|
|
364
|
+
assignor.on_assignment(assignment, generation)
|
|
365
|
+
|
|
366
|
+
# reschedule the auto commit starting from now
|
|
367
|
+
self.next_auto_commit_deadline = time.monotonic() + self.auto_commit_interval
|
|
368
|
+
|
|
369
|
+
assigned = set(self._subscription.assigned_partitions())
|
|
370
|
+
log.info("Setting newly assigned partitions %s for group %s",
|
|
371
|
+
assigned, self.group_id)
|
|
372
|
+
|
|
373
|
+
# execute the user's callback after rebalance
|
|
374
|
+
if self._subscription.rebalance_listener:
|
|
375
|
+
try:
|
|
376
|
+
await self._invoke_rebalance_listener_async(
|
|
377
|
+
'on_partitions_assigned', assigned)
|
|
378
|
+
except Exception as exc:
|
|
379
|
+
log.exception("User provided rebalance listener %s for group %s"
|
|
380
|
+
" failed on partition assignment: %s",
|
|
381
|
+
self._subscription.rebalance_listener, self.group_id,
|
|
382
|
+
assigned)
|
|
383
|
+
listener_exc = exc
|
|
384
|
+
|
|
385
|
+
if listener_exc is not None:
|
|
386
|
+
raise Errors.KafkaError(
|
|
387
|
+
"User rebalance callback throws an error") from listener_exc
|
|
388
|
+
|
|
389
|
+
def poll(self, timeout_ms=None):
|
|
390
|
+
"""
|
|
391
|
+
Poll for coordinator events. Only applicable if group_id is set, and
|
|
392
|
+
broker version supports GroupCoordinators. This ensures that the
|
|
393
|
+
coordinator is known, and if using automatic partition assignment,
|
|
394
|
+
ensures that the consumer has joined the group. This also handles
|
|
395
|
+
periodic offset commits if they are enabled.
|
|
396
|
+
"""
|
|
397
|
+
if self.group_id is None:
|
|
398
|
+
return True
|
|
399
|
+
|
|
400
|
+
timer = Timer(timeout_ms)
|
|
401
|
+
try:
|
|
402
|
+
self._invoke_completed_offset_commit_callbacks()
|
|
403
|
+
if not self.ensure_coordinator_ready(timeout_ms=timer.timeout_ms):
|
|
404
|
+
log.debug('coordinator.poll: timeout in ensure_coordinator_ready; returning early')
|
|
405
|
+
return False
|
|
406
|
+
|
|
407
|
+
if self._use_group_apis and self._subscription.partitions_auto_assigned():
|
|
408
|
+
if self.need_rejoin():
|
|
409
|
+
# due to a race condition between the initial metadata fetch and the
|
|
410
|
+
# initial rebalance, we need to ensure that the metadata is fresh
|
|
411
|
+
# before joining initially, and then request the metadata update. If
|
|
412
|
+
# metadata update arrives while the rebalance is still pending (for
|
|
413
|
+
# example, when the join group is still inflight), then we will lose
|
|
414
|
+
# track of the fact that we need to rebalance again to reflect the
|
|
415
|
+
# change to the topic subscription. Without ensuring that the
|
|
416
|
+
# metadata is fresh, any metadata update that changes the topic
|
|
417
|
+
# subscriptions and arrives while a rebalance is in progress will
|
|
418
|
+
# essentially be ignored. See KAFKA-3949 for the complete
|
|
419
|
+
# description of the problem.
|
|
420
|
+
if self._subscription.subscribed_pattern:
|
|
421
|
+
metadata_update = self._cluster.request_update()
|
|
422
|
+
try:
|
|
423
|
+
self._net.run(
|
|
424
|
+
self._manager.wait_for, metadata_update, timer.timeout_ms)
|
|
425
|
+
except Errors.KafkaTimeoutError:
|
|
426
|
+
log.debug('coordinator.poll: timeout updating metadata; returning early')
|
|
427
|
+
return False
|
|
428
|
+
|
|
429
|
+
if not self.ensure_active_group(timeout_ms=timer.timeout_ms):
|
|
430
|
+
log.debug('coordinator.poll: timeout in ensure_active_group; returning early')
|
|
431
|
+
return False
|
|
432
|
+
|
|
433
|
+
self.poll_heartbeat()
|
|
434
|
+
|
|
435
|
+
self._maybe_auto_commit_offsets_async()
|
|
436
|
+
return True
|
|
437
|
+
|
|
438
|
+
except Errors.KafkaTimeoutError:
|
|
439
|
+
return False
|
|
440
|
+
|
|
441
|
+
def time_to_next_poll(self):
|
|
442
|
+
"""Return seconds (float) remaining until :meth:`.poll` should be called again"""
|
|
443
|
+
if not self.config['enable_auto_commit']:
|
|
444
|
+
return self.time_to_next_heartbeat()
|
|
445
|
+
|
|
446
|
+
if time.monotonic() > self.next_auto_commit_deadline:
|
|
447
|
+
return 0
|
|
448
|
+
|
|
449
|
+
return min(self.next_auto_commit_deadline - time.monotonic(),
|
|
450
|
+
self.time_to_next_heartbeat())
|
|
451
|
+
|
|
452
|
+
def _perform_assignment(self, leader_id, protocol_name, members):
|
|
453
|
+
assignor = self._lookup_assignor(protocol_name)
|
|
454
|
+
if not assignor:
|
|
455
|
+
raise ValueError('Invalid assignment protocol: %s' % (protocol_name,))
|
|
456
|
+
all_subscribed_topics = set()
|
|
457
|
+
for member in members:
|
|
458
|
+
member.metadata = ConsumerProtocolSubscription.decode(member.metadata)
|
|
459
|
+
all_subscribed_topics.update(member.metadata.topics)
|
|
460
|
+
|
|
461
|
+
# the leader will begin watching for changes to any of the topics
|
|
462
|
+
# the group is interested in, which ensures that all metadata changes
|
|
463
|
+
# will eventually be seen
|
|
464
|
+
# Because assignment typically happens within response callbacks,
|
|
465
|
+
# we cannot block on metadata updates here (no recursion into poll())
|
|
466
|
+
self._subscription.group_subscribe(all_subscribed_topics)
|
|
467
|
+
self._cluster.set_topics(self._subscription.group_subscription())
|
|
468
|
+
|
|
469
|
+
# keep track of the metadata used for assignment so that we can check
|
|
470
|
+
# after rebalance completion whether anything has changed
|
|
471
|
+
self._cluster.request_update()
|
|
472
|
+
self._is_leader = True
|
|
473
|
+
self._assignment_snapshot = self._metadata_snapshot
|
|
474
|
+
|
|
475
|
+
log.debug("Performing assignment for group %s using strategy %s"
|
|
476
|
+
" with subscriptions %s", self.group_id, assignor.name,
|
|
477
|
+
members)
|
|
478
|
+
|
|
479
|
+
assignments = assignor.assign(self._cluster, members)
|
|
480
|
+
|
|
481
|
+
log.debug("Finished assignment for group %s: %s", self.group_id, assignments)
|
|
482
|
+
|
|
483
|
+
group_assignment = {}
|
|
484
|
+
for member_id, assignment in assignments.items():
|
|
485
|
+
group_assignment[member_id] = assignment
|
|
486
|
+
return group_assignment
|
|
487
|
+
|
|
488
|
+
async def _on_join_prepare_async(self, generation, member_id, timeout_ms=None):
|
|
489
|
+
# Exceptions raised by user rebalance-listener callbacks are captured
|
|
490
|
+
# here, do not abort the cleanup, and are re-raised at the end as a
|
|
491
|
+
# KafkaError so the caller (consumer.poll()) sees them. Matches Java.
|
|
492
|
+
listener_exc = None
|
|
493
|
+
|
|
494
|
+
if self._generation.is_lost():
|
|
495
|
+
lost = set(self._subscription.assigned_partitions())
|
|
496
|
+
if lost:
|
|
497
|
+
log.info("Group %s lost membership; forcibly revoking %s",
|
|
498
|
+
self.group_id, lost)
|
|
499
|
+
self._subscription.mark_pending_revocation(lost)
|
|
500
|
+
if self._subscription.rebalance_listener:
|
|
501
|
+
try:
|
|
502
|
+
await self._invoke_rebalance_listener_async(
|
|
503
|
+
'on_partitions_lost', lost)
|
|
504
|
+
except Exception as exc:
|
|
505
|
+
log.exception("User provided subscription rebalance listener %s"
|
|
506
|
+
" for group %s failed on_partitions_lost",
|
|
507
|
+
self._subscription.rebalance_listener, self.group_id)
|
|
508
|
+
listener_exc = exc
|
|
509
|
+
self._subscription.assign_from_subscribed([])
|
|
510
|
+
self._is_leader = False
|
|
511
|
+
self._subscription.reset_group_subscription()
|
|
512
|
+
if listener_exc is not None:
|
|
513
|
+
raise Errors.KafkaError(
|
|
514
|
+
"User rebalance callback throws an error") from listener_exc
|
|
515
|
+
return
|
|
516
|
+
# else: generation is lost but we have no partitions to
|
|
517
|
+
# lose - this is the initial-join case. Fall through to the
|
|
518
|
+
# normal auto-commit + EAGER/COOPERATIVE path.
|
|
519
|
+
|
|
520
|
+
# commit offsets prior to rebalance if auto-commit enabled
|
|
521
|
+
if self.config['enable_auto_commit']:
|
|
522
|
+
try:
|
|
523
|
+
await self._commit_offsets_sync_async(
|
|
524
|
+
self._subscription.all_consumed_offsets(),
|
|
525
|
+
timeout_ms=timeout_ms)
|
|
526
|
+
except (Errors.UnknownMemberIdError,
|
|
527
|
+
Errors.IllegalGenerationError,
|
|
528
|
+
Errors.RebalanceInProgressError):
|
|
529
|
+
log.warning("Pre-rebalance offset commit failed: group membership"
|
|
530
|
+
" out of date. This is likely to cause duplicate"
|
|
531
|
+
" message delivery.")
|
|
532
|
+
except Exception:
|
|
533
|
+
log.exception("Pre-rebalance offset commit failed: This is likely"
|
|
534
|
+
" to cause duplicate message delivery")
|
|
535
|
+
|
|
536
|
+
# Under EAGER, notify the user that the full current
|
|
537
|
+
# assignment is about to be revoked so they can flush state /
|
|
538
|
+
# commit offsets before the rebalance. The partitions remain
|
|
539
|
+
# in self._subscription.assignment until _on_join_complete
|
|
540
|
+
# replaces it via assign_from_subscribed - this listener call
|
|
541
|
+
# is a *notification*, not the actual state mutation.
|
|
542
|
+
#
|
|
543
|
+
# Under COOPERATIVE we keep most of the assignment across
|
|
544
|
+
# JoinGroup, but partitions whose topic is no longer in the
|
|
545
|
+
# subscription (e.g. the user just unsubscribed from a topic)
|
|
546
|
+
# are revoked here, before the JoinGroup, so the listener
|
|
547
|
+
# gets to commit those offsets while we're still the
|
|
548
|
+
# recognised owner.
|
|
549
|
+
if self._rebalance_protocol == RebalanceProtocol.EAGER:
|
|
550
|
+
log.info("Revoking previously assigned partitions %s for group %s",
|
|
551
|
+
self._subscription.assigned_partitions(), self.group_id)
|
|
552
|
+
if self._subscription.rebalance_listener:
|
|
553
|
+
try:
|
|
554
|
+
revoked = set(self._subscription.assigned_partitions())
|
|
555
|
+
self._subscription.mark_pending_revocation(revoked)
|
|
556
|
+
await self._invoke_rebalance_listener_async(
|
|
557
|
+
'on_partitions_revoked', revoked)
|
|
558
|
+
except Exception as exc:
|
|
559
|
+
log.exception("User provided subscription rebalance listener %s"
|
|
560
|
+
" for group %s failed on_partitions_revoked",
|
|
561
|
+
self._subscription.rebalance_listener, self.group_id)
|
|
562
|
+
listener_exc = exc
|
|
563
|
+
|
|
564
|
+
elif self._rebalance_protocol == RebalanceProtocol.COOPERATIVE:
|
|
565
|
+
owned = set(self._subscription.assigned_partitions())
|
|
566
|
+
subscribed_topics = self._subscription.subscription or set()
|
|
567
|
+
revoked = {tp for tp in owned if tp.topic not in subscribed_topics}
|
|
568
|
+
if revoked:
|
|
569
|
+
log.info("Cooperative pre-rebalance for group %s: revoking %s"
|
|
570
|
+
" (no longer in subscription)", self.group_id, revoked)
|
|
571
|
+
self._subscription.mark_pending_revocation(revoked)
|
|
572
|
+
if self._subscription.rebalance_listener:
|
|
573
|
+
try:
|
|
574
|
+
await self._invoke_rebalance_listener_async(
|
|
575
|
+
'on_partitions_revoked', revoked)
|
|
576
|
+
except Exception as exc:
|
|
577
|
+
log.exception("User provided subscription rebalance listener %s"
|
|
578
|
+
" for group %s failed on_partitions_revoked",
|
|
579
|
+
self._subscription.rebalance_listener, self.group_id)
|
|
580
|
+
listener_exc = exc
|
|
581
|
+
self._subscription.assign_from_subscribed(sorted(owned - revoked))
|
|
582
|
+
|
|
583
|
+
self._is_leader = False
|
|
584
|
+
self._subscription.reset_group_subscription()
|
|
585
|
+
|
|
586
|
+
if listener_exc is not None:
|
|
587
|
+
raise Errors.KafkaError(
|
|
588
|
+
"User rebalance callback throws an error") from listener_exc
|
|
589
|
+
|
|
590
|
+
def need_rejoin(self):
|
|
591
|
+
"""Check whether the group should be rejoined
|
|
592
|
+
|
|
593
|
+
Returns:
|
|
594
|
+
bool: True if consumer should rejoin group, False otherwise
|
|
595
|
+
"""
|
|
596
|
+
if not self._subscription.partitions_auto_assigned():
|
|
597
|
+
log.debug("need_rejoin: False (partitions not auto-assigned)")
|
|
598
|
+
return False
|
|
599
|
+
|
|
600
|
+
if self._auto_assign_all_partitions():
|
|
601
|
+
log.debug("need_rejoin: False (auto-assign all partitions)")
|
|
602
|
+
return False
|
|
603
|
+
|
|
604
|
+
# we need to rejoin if we performed the assignment and metadata has changed
|
|
605
|
+
if (self._assignment_snapshot is not None
|
|
606
|
+
and self._assignment_snapshot != self._metadata_snapshot):
|
|
607
|
+
log.debug("need_rejoin: True (assignment_snapshot != metadata_snapshot: %s != %s)",
|
|
608
|
+
self._assignment_snapshot, self._metadata_snapshot)
|
|
609
|
+
return True
|
|
610
|
+
|
|
611
|
+
# we need to join if our subscription has changed since the last join
|
|
612
|
+
if (self._joined_subscription is not None
|
|
613
|
+
and self._joined_subscription != self._subscription.subscription):
|
|
614
|
+
log.debug("need_rejoin: True (joined_subscription != subscription: %s != %s)",
|
|
615
|
+
self._joined_subscription, self._subscription.subscription)
|
|
616
|
+
return True
|
|
617
|
+
|
|
618
|
+
parent = super().need_rejoin()
|
|
619
|
+
log.debug("need_rejoin: %s (from base.rejoin_needed; assignment_snapshot=%s metadata_snapshot=%s joined_subscription=%s)",
|
|
620
|
+
parent, self._assignment_snapshot, self._metadata_snapshot, self._joined_subscription)
|
|
621
|
+
return parent
|
|
622
|
+
|
|
623
|
+
def refresh_committed_offsets_if_needed(self, timeout_ms=None):
|
|
624
|
+
"""Fetch committed offsets for assigned partitions."""
|
|
625
|
+
return self._net.run(self.refresh_committed_offsets_if_needed_async, timeout_ms)
|
|
626
|
+
|
|
627
|
+
async def refresh_committed_offsets_if_needed_async(self, timeout_ms=None):
|
|
628
|
+
missing_fetch_positions = set(self._subscription.missing_fetch_positions())
|
|
629
|
+
try:
|
|
630
|
+
offsets = await self.fetch_committed_offsets_async(missing_fetch_positions, timeout_ms=timeout_ms)
|
|
631
|
+
except Errors.KafkaTimeoutError:
|
|
632
|
+
return False
|
|
633
|
+
for partition, offset in offsets.items():
|
|
634
|
+
log.debug("Setting offset for partition %s to the committed offset %s", partition, offset.offset)
|
|
635
|
+
self._subscription.seek(partition, offset.offset)
|
|
636
|
+
return True
|
|
637
|
+
|
|
638
|
+
def fetch_committed_offsets(self, partitions, timeout_ms=None):
|
|
639
|
+
"""Fetch the current committed offsets for specified partitions
|
|
640
|
+
|
|
641
|
+
Arguments:
|
|
642
|
+
partitions (list of TopicPartition): partitions to fetch
|
|
643
|
+
|
|
644
|
+
Returns:
|
|
645
|
+
dict: {TopicPartition: OffsetAndMetadata}
|
|
646
|
+
|
|
647
|
+
Raises:
|
|
648
|
+
KafkaTimeoutError if timeout_ms provided
|
|
649
|
+
"""
|
|
650
|
+
if not partitions:
|
|
651
|
+
return {}
|
|
652
|
+
return self._net.run(self.fetch_committed_offsets_async, partitions, timeout_ms)
|
|
653
|
+
|
|
654
|
+
async def fetch_committed_offsets_async(self, partitions, timeout_ms=None):
|
|
655
|
+
"""Async variant of :meth:`fetch_committed_offsets`."""
|
|
656
|
+
if not partitions:
|
|
657
|
+
return {}
|
|
658
|
+
|
|
659
|
+
future_key = frozenset(partitions)
|
|
660
|
+
timer = Timer(timeout_ms)
|
|
661
|
+
while True:
|
|
662
|
+
if not await self.ensure_coordinator_ready_async(timeout_ms=timer.timeout_ms):
|
|
663
|
+
timer.maybe_raise()
|
|
664
|
+
|
|
665
|
+
# contact coordinator to fetch committed offsets
|
|
666
|
+
if future_key in self._offset_fetch_futures:
|
|
667
|
+
future = self._offset_fetch_futures[future_key]
|
|
668
|
+
else:
|
|
669
|
+
future = self._manager.call_soon(self._send_offset_fetch_request, partitions)
|
|
670
|
+
self._offset_fetch_futures[future_key] = future
|
|
671
|
+
|
|
672
|
+
try:
|
|
673
|
+
await self._manager.wait_for(future, timer.timeout_ms)
|
|
674
|
+
except Errors.KafkaTimeoutError:
|
|
675
|
+
pass
|
|
676
|
+
except BaseException:
|
|
677
|
+
# handled below via future.is_done / retriable; cleanup happens too
|
|
678
|
+
pass
|
|
679
|
+
|
|
680
|
+
if future.is_done:
|
|
681
|
+
if future_key in self._offset_fetch_futures:
|
|
682
|
+
del self._offset_fetch_futures[future_key]
|
|
683
|
+
|
|
684
|
+
if future.succeeded():
|
|
685
|
+
return future.value
|
|
686
|
+
|
|
687
|
+
elif not future.retriable():
|
|
688
|
+
raise future.exception # pylint: disable-msg=raising-bad-type
|
|
689
|
+
|
|
690
|
+
# future failed but is retriable, or is not done yet
|
|
691
|
+
delay_ms = self.config['retry_backoff_ms']
|
|
692
|
+
if timer.timeout_ms is not None:
|
|
693
|
+
delay_ms = min(delay_ms, timer.timeout_ms)
|
|
694
|
+
if delay_ms > 0:
|
|
695
|
+
await self._net.sleep(delay_ms / 1000)
|
|
696
|
+
timer.maybe_raise()
|
|
697
|
+
|
|
698
|
+
async def _on_close_prepare_async(self):
|
|
699
|
+
"""Notify the rebalance listener that our partitions are being
|
|
700
|
+
revoked because the consumer is closing. Mirrors Java's
|
|
701
|
+
ConsumerCoordinator.onLeavePrepare.
|
|
702
|
+
|
|
703
|
+
Only fires for auto-assigned (group) subscriptions that currently
|
|
704
|
+
own partitions. If the group membership has already been lost
|
|
705
|
+
(forced eviction), on_partitions_lost is invoked instead of
|
|
706
|
+
on_partitions_revoked, since the user cannot commit offsets for
|
|
707
|
+
partitions the broker has already reassigned.
|
|
708
|
+
|
|
709
|
+
Runs before we leave the group so a sync listener can still commit
|
|
710
|
+
offsets while we are the recognised owner. Listener exceptions are
|
|
711
|
+
captured, the local assignment is cleared regardless, and the
|
|
712
|
+
exception is re-raised as a KafkaError after cleanup.
|
|
713
|
+
"""
|
|
714
|
+
if not self._subscription.partitions_auto_assigned():
|
|
715
|
+
return
|
|
716
|
+
revoked = set(self._subscription.assigned_partitions())
|
|
717
|
+
if not revoked:
|
|
718
|
+
return
|
|
719
|
+
|
|
720
|
+
if self._generation.is_lost():
|
|
721
|
+
method = 'on_partitions_lost'
|
|
722
|
+
else:
|
|
723
|
+
method = 'on_partitions_revoked'
|
|
724
|
+
|
|
725
|
+
log.info("Revoking previously assigned partitions %s for group %s"
|
|
726
|
+
" on close", revoked, self.group_id)
|
|
727
|
+
self._subscription.mark_pending_revocation(revoked)
|
|
728
|
+
listener_exc = None
|
|
729
|
+
if self._subscription.rebalance_listener:
|
|
730
|
+
try:
|
|
731
|
+
await self._invoke_rebalance_listener_async(method, revoked)
|
|
732
|
+
except Exception as exc:
|
|
733
|
+
log.exception("User provided subscription rebalance listener %s"
|
|
734
|
+
" for group %s failed %s on close",
|
|
735
|
+
self._subscription.rebalance_listener,
|
|
736
|
+
self.group_id, method)
|
|
737
|
+
listener_exc = exc
|
|
738
|
+
self._subscription.assign_from_subscribed([])
|
|
739
|
+
self._is_leader = False
|
|
740
|
+
self._subscription.reset_group_subscription()
|
|
741
|
+
if listener_exc is not None:
|
|
742
|
+
raise Errors.KafkaError(
|
|
743
|
+
"User rebalance callback throws an error") from listener_exc
|
|
744
|
+
|
|
745
|
+
def close(self, autocommit=True, timeout_ms=None):
|
|
746
|
+
"""Close the coordinator, leave the current group,
|
|
747
|
+
and reset local generation / member_id.
|
|
748
|
+
|
|
749
|
+
Keyword Arguments:
|
|
750
|
+
autocommit (bool): If auto-commit is configured for this consumer,
|
|
751
|
+
this optional flag causes the consumer to attempt to commit any
|
|
752
|
+
pending consumed offsets prior to close. Default: True
|
|
753
|
+
"""
|
|
754
|
+
try:
|
|
755
|
+
if autocommit:
|
|
756
|
+
self._maybe_auto_commit_offsets_sync(timeout_ms=timeout_ms)
|
|
757
|
+
self._net.run(self._on_close_prepare_async)
|
|
758
|
+
finally:
|
|
759
|
+
self._cluster.remove_listener(WeakMethod(self._handle_metadata_update))
|
|
760
|
+
super().close(timeout_ms=timeout_ms)
|
|
761
|
+
|
|
762
|
+
def _invoke_completed_offset_commit_callbacks(self):
|
|
763
|
+
if self._async_commit_fenced:
|
|
764
|
+
raise Errors.FencedInstanceIdError("Got fenced exception for group_instance_id %s" % (self.group_instance_id,))
|
|
765
|
+
while self.completed_offset_commits:
|
|
766
|
+
callback, offsets, res_or_exc = self.completed_offset_commits.popleft()
|
|
767
|
+
callback(offsets, res_or_exc)
|
|
768
|
+
|
|
769
|
+
def commit_offsets_async(self, offsets, callback=None):
|
|
770
|
+
"""Commit specific offsets asynchronously.
|
|
771
|
+
|
|
772
|
+
Arguments:
|
|
773
|
+
offsets (dict {TopicPartition: OffsetAndMetadata}): what to commit
|
|
774
|
+
callback (callable, optional): called as callback(offsets, response)
|
|
775
|
+
response will be either an Exception or a OffsetCommitResponse
|
|
776
|
+
struct. This callback can be used to trigger custom actions when
|
|
777
|
+
a commit request completes.
|
|
778
|
+
|
|
779
|
+
Returns:
|
|
780
|
+
kafka.future.Future
|
|
781
|
+
"""
|
|
782
|
+
self._invoke_completed_offset_commit_callbacks()
|
|
783
|
+
if not self.coordinator_unknown():
|
|
784
|
+
future = self._do_commit_offsets_async(offsets, callback)
|
|
785
|
+
else:
|
|
786
|
+
# we don't know the current coordinator, so try to find it and then
|
|
787
|
+
# send the commit or fail (we don't want recursive retries which can
|
|
788
|
+
# cause offset commits to arrive out of order). Note that there may
|
|
789
|
+
# be multiple offset commits chained to the same coordinator lookup
|
|
790
|
+
# request. This is fine because the listeners will be invoked in the
|
|
791
|
+
# same order that they were added. Note also that BaseCoordinator
|
|
792
|
+
# prevents multiple concurrent coordinator lookup requests.
|
|
793
|
+
future = self.lookup_coordinator()
|
|
794
|
+
future.add_callback(lambda r: functools.partial(self._do_commit_offsets_async, offsets, callback)())
|
|
795
|
+
if callback:
|
|
796
|
+
future.add_errback(lambda e: self.completed_offset_commits.appendleft((callback, offsets, e)))
|
|
797
|
+
return future
|
|
798
|
+
|
|
799
|
+
def _do_commit_offsets_async(self, offsets, callback=None):
|
|
800
|
+
if not self._use_offset_apis:
|
|
801
|
+
raise Errors.UnsupportedVersionError('OffsetCommitRequest requires 0.8.1+ broker')
|
|
802
|
+
if not all(map(lambda k: isinstance(k, TopicPartition), offsets)) or \
|
|
803
|
+
not all(map(lambda v: isinstance(v, OffsetAndMetadata), offsets.values())):
|
|
804
|
+
raise TypeError('offsets must be dict[TopicPartition, OffsetAndMetadata]')
|
|
805
|
+
if callback is None:
|
|
806
|
+
callback = self.config['default_offset_commit_callback']
|
|
807
|
+
future = self._manager.call_soon(self._send_offset_commit_request, offsets)
|
|
808
|
+
future.add_both(lambda res: self.completed_offset_commits.appendleft((callback, offsets, res)))
|
|
809
|
+
def _maybe_set_async_commit_fenced(exc):
|
|
810
|
+
if isinstance(exc, Errors.FencedInstanceIdError):
|
|
811
|
+
self._async_commit_fenced = True
|
|
812
|
+
future.add_errback(_maybe_set_async_commit_fenced)
|
|
813
|
+
return future
|
|
814
|
+
|
|
815
|
+
def commit_offsets_sync(self, offsets, timeout_ms=None):
|
|
816
|
+
"""Commit specific offsets synchronously.
|
|
817
|
+
|
|
818
|
+
This method will retry until the commit completes successfully or an
|
|
819
|
+
unrecoverable error is encountered.
|
|
820
|
+
|
|
821
|
+
Arguments:
|
|
822
|
+
offsets (dict {TopicPartition: OffsetAndMetadata}): what to commit
|
|
823
|
+
|
|
824
|
+
Raises error on failure
|
|
825
|
+
"""
|
|
826
|
+
if not self._use_offset_apis:
|
|
827
|
+
raise Errors.UnsupportedVersionError('OffsetCommitRequest requires 0.8.1+ broker')
|
|
828
|
+
if not all(map(lambda k: isinstance(k, TopicPartition), offsets)) or \
|
|
829
|
+
not all(map(lambda v: isinstance(v, OffsetAndMetadata), offsets.values())):
|
|
830
|
+
raise TypeError('offsets must be dict[TopicPartition, OffsetAndMetadata]')
|
|
831
|
+
self._invoke_completed_offset_commit_callbacks()
|
|
832
|
+
return self._net.run(self._commit_offsets_sync_async, offsets, timeout_ms)
|
|
833
|
+
|
|
834
|
+
async def _commit_offsets_sync_async(self, offsets, timeout_ms=None):
|
|
835
|
+
if not offsets:
|
|
836
|
+
return
|
|
837
|
+
# Default to request_timeout_ms, matching offsets_by_times / _reset_offsets_async
|
|
838
|
+
if timeout_ms is None:
|
|
839
|
+
timeout_ms = self.config['request_timeout_ms']
|
|
840
|
+
timer = Timer(timeout_ms)
|
|
841
|
+
while True:
|
|
842
|
+
await self.ensure_coordinator_ready_async(timeout_ms=timer.timeout_ms)
|
|
843
|
+
|
|
844
|
+
future = self._manager.call_soon(self._send_offset_commit_request, offsets)
|
|
845
|
+
try:
|
|
846
|
+
await self._manager.wait_for(future, timer.timeout_ms)
|
|
847
|
+
except Errors.KafkaTimeoutError:
|
|
848
|
+
pass
|
|
849
|
+
except BaseException:
|
|
850
|
+
# handled below via future.is_done / retriable
|
|
851
|
+
pass
|
|
852
|
+
|
|
853
|
+
if future.is_done:
|
|
854
|
+
if future.succeeded():
|
|
855
|
+
return future.value
|
|
856
|
+
|
|
857
|
+
elif not future.retriable():
|
|
858
|
+
raise future.exception # pylint: disable-msg=raising-bad-type
|
|
859
|
+
|
|
860
|
+
# future failed but is retriable, or it is still pending
|
|
861
|
+
delay_ms = self.config['retry_backoff_ms']
|
|
862
|
+
if timer.timeout_ms is not None:
|
|
863
|
+
delay_ms = min(delay_ms, timer.timeout_ms)
|
|
864
|
+
if delay_ms > 0:
|
|
865
|
+
await self._net.sleep(delay_ms / 1000)
|
|
866
|
+
timer.maybe_raise()
|
|
867
|
+
|
|
868
|
+
def _maybe_auto_commit_offsets_sync(self, timeout_ms=None):
|
|
869
|
+
if self.config['enable_auto_commit']:
|
|
870
|
+
try:
|
|
871
|
+
self.commit_offsets_sync(self._subscription.all_consumed_offsets(), timeout_ms=timeout_ms)
|
|
872
|
+
|
|
873
|
+
# The three main group membership errors are known and should not
|
|
874
|
+
# require a stacktrace -- just a warning
|
|
875
|
+
except (Errors.UnknownMemberIdError,
|
|
876
|
+
Errors.IllegalGenerationError,
|
|
877
|
+
Errors.RebalanceInProgressError):
|
|
878
|
+
log.warning("Offset commit failed: group membership out of date"
|
|
879
|
+
" This is likely to cause duplicate message"
|
|
880
|
+
" delivery.")
|
|
881
|
+
except Exception:
|
|
882
|
+
log.exception("Offset commit failed: This is likely to cause"
|
|
883
|
+
" duplicate message delivery")
|
|
884
|
+
|
|
885
|
+
async def _send_offset_commit_request(self, offsets):
|
|
886
|
+
"""Commit offsets for the specified list of topics and partitions.
|
|
887
|
+
|
|
888
|
+
Arguments:
|
|
889
|
+
offsets (dict of {TopicPartition: OffsetAndMetadata}): what should
|
|
890
|
+
be committed.
|
|
891
|
+
|
|
892
|
+
Returns: None on success.
|
|
893
|
+
Raises:
|
|
894
|
+
UnsupportedVersionError if broker is too old.
|
|
895
|
+
CoordinatorNotAvailableError if the coordinator is unknown.
|
|
896
|
+
RebalanceInProgressError / CommitFailedError if generation is
|
|
897
|
+
not stable.
|
|
898
|
+
Other broker-side OffsetCommit errors propagated via
|
|
899
|
+
_handle_offset_commit_response.
|
|
900
|
+
"""
|
|
901
|
+
if not self._use_offset_apis:
|
|
902
|
+
raise Errors.UnsupportedVersionError('OffsetCommitRequest requires 0.8.1+ broker')
|
|
903
|
+
if not all(map(lambda k: isinstance(k, TopicPartition), offsets)) or \
|
|
904
|
+
not all(map(lambda v: isinstance(v, OffsetAndMetadata), offsets.values())):
|
|
905
|
+
raise TypeError('offsets must be dict[TopicPartition, OffsetAndMetadata]')
|
|
906
|
+
if not offsets:
|
|
907
|
+
log.debug('No offsets to commit')
|
|
908
|
+
return None
|
|
909
|
+
|
|
910
|
+
node_id = self.coordinator()
|
|
911
|
+
if node_id is None:
|
|
912
|
+
raise Errors.CoordinatorNotAvailableError()
|
|
913
|
+
|
|
914
|
+
# create the offset commit request
|
|
915
|
+
offset_data = collections.defaultdict(dict)
|
|
916
|
+
for tp, offset in offsets.items():
|
|
917
|
+
offset_data[tp.topic][tp.partition] = offset
|
|
918
|
+
|
|
919
|
+
if self._use_group_apis and self._subscription.partitions_auto_assigned():
|
|
920
|
+
generation = self.generation_if_stable()
|
|
921
|
+
else:
|
|
922
|
+
generation = Generation.NO_GENERATION
|
|
923
|
+
|
|
924
|
+
# if the generation is None, we are not part of an active group
|
|
925
|
+
# (and we expect to be). The only thing we can do is fail the commit
|
|
926
|
+
# and let the user rejoin the group in poll()
|
|
927
|
+
if generation is None:
|
|
928
|
+
log.info("Failing OffsetCommit request since the consumer is not part of an active group")
|
|
929
|
+
if self.rebalance_in_progress():
|
|
930
|
+
# if the client knows it is already rebalancing, we can use RebalanceInProgressError instead of
|
|
931
|
+
# CommitFailedError to indicate this is not a fatal error
|
|
932
|
+
raise Errors.RebalanceInProgressError(
|
|
933
|
+
"Offset commit cannot be completed since the"
|
|
934
|
+
" consumer is undergoing a rebalance for auto partition assignment. You can try completing the rebalance"
|
|
935
|
+
" by calling poll() and then retry the operation.")
|
|
936
|
+
else:
|
|
937
|
+
raise Errors.CommitFailedError(
|
|
938
|
+
"Offset commit cannot be completed since the"
|
|
939
|
+
" consumer is not part of an active group for auto partition assignment; it is likely that the consumer"
|
|
940
|
+
" was kicked out of the group.")
|
|
941
|
+
|
|
942
|
+
_Topic = OffsetCommitRequest.OffsetCommitRequestTopic
|
|
943
|
+
_Partition = _Topic.OffsetCommitRequestPartition
|
|
944
|
+
request = OffsetCommitRequest(
|
|
945
|
+
max_version=8,
|
|
946
|
+
group_id=self.group_id,
|
|
947
|
+
generation_id_or_member_epoch=generation.generation_id,
|
|
948
|
+
member_id=generation.member_id,
|
|
949
|
+
group_instance_id=self.group_instance_id,
|
|
950
|
+
topics=[_Topic(
|
|
951
|
+
name=topic, partitions=[_Partition(
|
|
952
|
+
partition_index=partition,
|
|
953
|
+
committed_offset=offset.offset,
|
|
954
|
+
committed_leader_epoch=offset.leader_epoch,
|
|
955
|
+
committed_metadata=offset.metadata
|
|
956
|
+
) for partition, offset in partitions.items()]
|
|
957
|
+
) for topic, partitions in offset_data.items()]
|
|
958
|
+
)
|
|
959
|
+
|
|
960
|
+
log.debug("Sending offset-commit request with %s for group %s to %s",
|
|
961
|
+
offsets, self.group_id, node_id)
|
|
962
|
+
|
|
963
|
+
send_time = time.monotonic()
|
|
964
|
+
try:
|
|
965
|
+
response = await self._manager.send(request, node_id=node_id)
|
|
966
|
+
except Exception as exc:
|
|
967
|
+
self._failed_request(node_id, request, exc)
|
|
968
|
+
raise
|
|
969
|
+
self._handle_offset_commit_response(offsets, send_time, response)
|
|
970
|
+
|
|
971
|
+
def _handle_offset_commit_response(self, offsets, send_time, response):
|
|
972
|
+
log.debug("Received OffsetCommitResponse: %s", response)
|
|
973
|
+
# TODO look at adding request_latency_ms to response (like java kafka)
|
|
974
|
+
if self._consumer_sensors:
|
|
975
|
+
self._consumer_sensors.commit_latency.record((time.monotonic() - send_time) * 1000)
|
|
976
|
+
unauthorized_topics = set()
|
|
977
|
+
|
|
978
|
+
for topic, partitions in response.topics:
|
|
979
|
+
for partition, error_code in partitions:
|
|
980
|
+
tp = TopicPartition(topic, partition)
|
|
981
|
+
offset = offsets[tp]
|
|
982
|
+
|
|
983
|
+
error_type = Errors.for_code(error_code)
|
|
984
|
+
if error_type is Errors.NoError:
|
|
985
|
+
log.debug("Group %s committed offset %s for partition %s",
|
|
986
|
+
self.group_id, offset, tp)
|
|
987
|
+
elif error_type is Errors.GroupAuthorizationFailedError:
|
|
988
|
+
log.error("Not authorized to commit offsets for group %s",
|
|
989
|
+
self.group_id)
|
|
990
|
+
raise error_type(self.group_id)
|
|
991
|
+
elif error_type is Errors.TopicAuthorizationFailedError:
|
|
992
|
+
unauthorized_topics.add(topic)
|
|
993
|
+
elif error_type in (Errors.OffsetMetadataTooLargeError,
|
|
994
|
+
Errors.InvalidCommitOffsetSizeError):
|
|
995
|
+
# raise the error to the user
|
|
996
|
+
log.debug("OffsetCommit for group %s failed on partition %s"
|
|
997
|
+
" %s", self.group_id, tp, error_type.__name__)
|
|
998
|
+
raise error_type()
|
|
999
|
+
elif error_type is Errors.CoordinatorLoadInProgressError:
|
|
1000
|
+
# just retry
|
|
1001
|
+
log.debug("OffsetCommit for group %s failed: %s",
|
|
1002
|
+
self.group_id, error_type.__name__)
|
|
1003
|
+
raise error_type(self.group_id)
|
|
1004
|
+
elif error_type in (Errors.CoordinatorNotAvailableError,
|
|
1005
|
+
Errors.NotCoordinatorError,
|
|
1006
|
+
Errors.RequestTimedOutError):
|
|
1007
|
+
log.debug("OffsetCommit for group %s failed: %s",
|
|
1008
|
+
self.group_id, error_type.__name__)
|
|
1009
|
+
self.coordinator_dead(error_type())
|
|
1010
|
+
raise error_type(self.group_id)
|
|
1011
|
+
elif error_type is Errors.RebalanceInProgressError:
|
|
1012
|
+
# Consumer never tries to commit offset in between join-group and sync-group,
|
|
1013
|
+
# and hence on broker-side it is not expected to see a commit offset request
|
|
1014
|
+
# during CompletingRebalance phase; if it ever happens then broker would return
|
|
1015
|
+
# this error. In this case we should just treat as a fatal CommitFailed exception.
|
|
1016
|
+
# However, we do not need to reset generations and just request re-join, such that
|
|
1017
|
+
# if the caller decides to proceed and poll, it would still try to proceed and re-join normally.
|
|
1018
|
+
self.request_rejoin()
|
|
1019
|
+
raise Errors.CommitFailedError(error_type())
|
|
1020
|
+
elif error_type is Errors.FencedInstanceIdError:
|
|
1021
|
+
log.error("OffsetCommit for group %s failed due to fenced id error: %s",
|
|
1022
|
+
self.group_id, self.group_instance_id)
|
|
1023
|
+
raise error_type()
|
|
1024
|
+
elif error_type in (Errors.UnknownMemberIdError,
|
|
1025
|
+
Errors.IllegalGenerationError):
|
|
1026
|
+
# need reset generation and re-join group
|
|
1027
|
+
error = error_type(self.group_id)
|
|
1028
|
+
log.warning("OffsetCommit for group %s failed: %s",
|
|
1029
|
+
self.group_id, error)
|
|
1030
|
+
if error_type is Errors.IllegalGenerationError:
|
|
1031
|
+
self.reset_generation(member_id=self._generation.member_id)
|
|
1032
|
+
else:
|
|
1033
|
+
self.reset_generation()
|
|
1034
|
+
raise Errors.CommitFailedError(error_type())
|
|
1035
|
+
else:
|
|
1036
|
+
log.error("Group %s failed to commit partition %s at offset"
|
|
1037
|
+
" %s: %s", self.group_id, tp, offset,
|
|
1038
|
+
error_type.__name__)
|
|
1039
|
+
raise error_type()
|
|
1040
|
+
|
|
1041
|
+
if unauthorized_topics:
|
|
1042
|
+
log.error("Not authorized to commit to topics %s for group %s",
|
|
1043
|
+
unauthorized_topics, self.group_id)
|
|
1044
|
+
raise Errors.TopicAuthorizationFailedError(unauthorized_topics)
|
|
1045
|
+
|
|
1046
|
+
async def _send_offset_fetch_request(self, partitions):
|
|
1047
|
+
"""Fetch the committed offsets for a set of partitions.
|
|
1048
|
+
|
|
1049
|
+
Arguments:
|
|
1050
|
+
partitions (list[TopicPartition]): the partitions to fetch.
|
|
1051
|
+
|
|
1052
|
+
Returns:
|
|
1053
|
+
dict[TopicPartition, OffsetAndMetadata] on success.
|
|
1054
|
+
|
|
1055
|
+
Raises:
|
|
1056
|
+
UnsupportedVersionError if broker is too old.
|
|
1057
|
+
CoordinatorNotAvailableError if the coordinator is unknown.
|
|
1058
|
+
Other broker-side OffsetFetch errors propagated via
|
|
1059
|
+
_handle_offset_fetch_response.
|
|
1060
|
+
"""
|
|
1061
|
+
if not self._use_offset_apis:
|
|
1062
|
+
raise Errors.UnsupportedVersionError('OffsetFetchRequest requires 0.8.1+ broker')
|
|
1063
|
+
if not all(map(lambda k: isinstance(k, TopicPartition), partitions)):
|
|
1064
|
+
raise TypeError("partitions must be list[TopicPartition]")
|
|
1065
|
+
if not partitions and partitions is not None:
|
|
1066
|
+
return {}
|
|
1067
|
+
|
|
1068
|
+
node_id = self.coordinator()
|
|
1069
|
+
if node_id is None:
|
|
1070
|
+
raise Errors.CoordinatorNotAvailableError()
|
|
1071
|
+
|
|
1072
|
+
log.debug("Group %s fetching committed offsets for partitions: %s",
|
|
1073
|
+
self.group_id, '(all)' if partitions is None else partitions)
|
|
1074
|
+
# construct the request
|
|
1075
|
+
_Topic = OffsetFetchRequest.OffsetFetchRequestTopic
|
|
1076
|
+
_Group = OffsetFetchRequest.OffsetFetchRequestGroup
|
|
1077
|
+
_GroupTopic = _Group.OffsetFetchRequestTopics
|
|
1078
|
+
if partitions is not None:
|
|
1079
|
+
topic_partitions = collections.defaultdict(set)
|
|
1080
|
+
for tp in partitions:
|
|
1081
|
+
topic_partitions[tp.topic].add(tp.partition)
|
|
1082
|
+
topics = [_Topic(name=t, partition_indexes=list(p))
|
|
1083
|
+
for t, p in topic_partitions.items()]
|
|
1084
|
+
group_topics = [_GroupTopic(name=t, partition_indexes=list(p))
|
|
1085
|
+
for t, p in topic_partitions.items()]
|
|
1086
|
+
min_version = 0
|
|
1087
|
+
else:
|
|
1088
|
+
topics = None
|
|
1089
|
+
group_topics = None
|
|
1090
|
+
min_version = 2
|
|
1091
|
+
|
|
1092
|
+
groups = [_Group(group_id=self.group_id, topics=group_topics)]
|
|
1093
|
+
require_stable = self._isolation_level == IsolationLevel.READ_COMMITTED
|
|
1094
|
+
request = OffsetFetchRequest(
|
|
1095
|
+
group_id=self.group_id,
|
|
1096
|
+
topics=topics,
|
|
1097
|
+
groups=groups,
|
|
1098
|
+
require_stable=require_stable,
|
|
1099
|
+
min_version=min_version,
|
|
1100
|
+
max_version=8,
|
|
1101
|
+
)
|
|
1102
|
+
|
|
1103
|
+
try:
|
|
1104
|
+
response = await self._manager.send(request, node_id=node_id)
|
|
1105
|
+
except Exception as exc:
|
|
1106
|
+
self._failed_request(node_id, request, exc)
|
|
1107
|
+
raise
|
|
1108
|
+
return self._handle_offset_fetch_response(response)
|
|
1109
|
+
|
|
1110
|
+
def _handle_offset_fetch_response(self, response):
|
|
1111
|
+
log.debug("Received OffsetFetchResponse: %s", response)
|
|
1112
|
+
if response.API_VERSION >= 8:
|
|
1113
|
+
group = response.groups[0]
|
|
1114
|
+
top_level_error_code = group.error_code
|
|
1115
|
+
topics = group.topics
|
|
1116
|
+
else:
|
|
1117
|
+
top_level_error_code = response.error_code if response.API_VERSION >= 2 else Errors.NoError.errno
|
|
1118
|
+
topics = response.topics
|
|
1119
|
+
|
|
1120
|
+
if top_level_error_code != Errors.NoError.errno:
|
|
1121
|
+
error_type = Errors.for_code(top_level_error_code)
|
|
1122
|
+
log.debug("Offset fetch failed: %s", error_type.__name__)
|
|
1123
|
+
error = error_type()
|
|
1124
|
+
if error_type is Errors.NotCoordinatorError:
|
|
1125
|
+
# re-discover the coordinator and retry
|
|
1126
|
+
self.coordinator_dead(error)
|
|
1127
|
+
raise error
|
|
1128
|
+
elif error_type in (Errors.CoordinatorLoadInProgressError,
|
|
1129
|
+
Errors.GroupAuthorizationFailedError):
|
|
1130
|
+
raise error
|
|
1131
|
+
else:
|
|
1132
|
+
log.error("Unknown error fetching offsets: %s", error)
|
|
1133
|
+
raise error
|
|
1134
|
+
|
|
1135
|
+
offsets = {}
|
|
1136
|
+
for topic, partitions in ((t.name, t.partitions) for t in topics):
|
|
1137
|
+
for partition_data in partitions:
|
|
1138
|
+
partition = partition_data.partition_index
|
|
1139
|
+
offset = partition_data.committed_offset
|
|
1140
|
+
leader_epoch = partition_data.committed_leader_epoch
|
|
1141
|
+
metadata = partition_data.metadata
|
|
1142
|
+
error_code = partition_data.error_code
|
|
1143
|
+
tp = TopicPartition(topic, partition)
|
|
1144
|
+
error_type = Errors.for_code(error_code)
|
|
1145
|
+
if error_type is not Errors.NoError:
|
|
1146
|
+
error = error_type()
|
|
1147
|
+
log.debug("Group %s failed to fetch offset for partition"
|
|
1148
|
+
" %s: %s", self.group_id, tp, error)
|
|
1149
|
+
if error_type is Errors.NotCoordinatorError:
|
|
1150
|
+
# re-discover the coordinator and retry
|
|
1151
|
+
self.coordinator_dead(error)
|
|
1152
|
+
raise error
|
|
1153
|
+
elif error_type is Errors.CoordinatorLoadInProgressError:
|
|
1154
|
+
raise error
|
|
1155
|
+
elif error_type is Errors.UnknownTopicOrPartitionError:
|
|
1156
|
+
log.warning("OffsetFetchRequest -- unknown topic %s"
|
|
1157
|
+
" (have you committed any offsets yet?)",
|
|
1158
|
+
topic)
|
|
1159
|
+
continue
|
|
1160
|
+
else:
|
|
1161
|
+
log.error("Unknown error fetching offsets for %s: %s",
|
|
1162
|
+
tp, error)
|
|
1163
|
+
raise error
|
|
1164
|
+
elif offset >= 0:
|
|
1165
|
+
# record the position with the offset
|
|
1166
|
+
# (-1 indicates no committed offset to fetch)
|
|
1167
|
+
offsets[tp] = OffsetAndMetadata(offset, metadata, leader_epoch)
|
|
1168
|
+
else:
|
|
1169
|
+
log.debug("Group %s has no committed offset for partition"
|
|
1170
|
+
" %s", self.group_id, tp)
|
|
1171
|
+
return offsets
|
|
1172
|
+
|
|
1173
|
+
def _default_offset_commit_callback(self, offsets, res_or_exc):
|
|
1174
|
+
if isinstance(res_or_exc, Exception):
|
|
1175
|
+
log.warning("Auto offset commit failed for group %s: %s",
|
|
1176
|
+
self.group_id, res_or_exc)
|
|
1177
|
+
else:
|
|
1178
|
+
log.debug("Completed autocommit of offsets %s for group %s",
|
|
1179
|
+
offsets, self.group_id)
|
|
1180
|
+
|
|
1181
|
+
def _commit_offsets_async_on_complete(self, offsets, res_or_exc):
|
|
1182
|
+
if isinstance(res_or_exc, Errors.RetriableError):
|
|
1183
|
+
self.next_auto_commit_deadline = min(time.monotonic() + self.config['retry_backoff_ms'] / 1000, self.next_auto_commit_deadline)
|
|
1184
|
+
self.config['default_offset_commit_callback'](offsets, res_or_exc)
|
|
1185
|
+
|
|
1186
|
+
def _maybe_auto_commit_offsets_async(self):
|
|
1187
|
+
if self.config['enable_auto_commit']:
|
|
1188
|
+
if self.coordinator_unknown():
|
|
1189
|
+
self.next_auto_commit_deadline = time.monotonic() + self.config['retry_backoff_ms'] / 1000
|
|
1190
|
+
elif time.monotonic() > self.next_auto_commit_deadline:
|
|
1191
|
+
self.next_auto_commit_deadline = time.monotonic() + self.auto_commit_interval
|
|
1192
|
+
self._do_auto_commit_offsets_async()
|
|
1193
|
+
|
|
1194
|
+
def maybe_auto_commit_offsets_now(self):
|
|
1195
|
+
if self.config['enable_auto_commit'] and not self.coordinator_unknown():
|
|
1196
|
+
self._do_auto_commit_offsets_async()
|
|
1197
|
+
|
|
1198
|
+
def _do_auto_commit_offsets_async(self):
|
|
1199
|
+
self.commit_offsets_async(self._subscription.all_consumed_offsets(),
|
|
1200
|
+
self._commit_offsets_async_on_complete)
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
class ConsumerCoordinatorMetrics:
|
|
1204
|
+
def __init__(self, metrics, metric_group_prefix, subscription):
|
|
1205
|
+
self.metrics = metrics
|
|
1206
|
+
self.metric_group_name = '%s-coordinator-metrics' % (metric_group_prefix,)
|
|
1207
|
+
|
|
1208
|
+
self.commit_latency = metrics.sensor('commit-latency')
|
|
1209
|
+
self.commit_latency.add(metrics.metric_name(
|
|
1210
|
+
'commit-latency-avg', self.metric_group_name,
|
|
1211
|
+
'The average time taken for a commit request'), Avg())
|
|
1212
|
+
self.commit_latency.add(metrics.metric_name(
|
|
1213
|
+
'commit-latency-max', self.metric_group_name,
|
|
1214
|
+
'The max time taken for a commit request'), Max())
|
|
1215
|
+
self.commit_latency.add(metrics.metric_name(
|
|
1216
|
+
'commit-rate', self.metric_group_name,
|
|
1217
|
+
'The number of commit calls per second'), Rate(sampled_stat=Count()))
|
|
1218
|
+
|
|
1219
|
+
num_parts = AnonMeasurable(lambda config, now:
|
|
1220
|
+
len(subscription.assigned_partitions()))
|
|
1221
|
+
metrics.add_metric(metrics.metric_name(
|
|
1222
|
+
'assigned-partitions', self.metric_group_name,
|
|
1223
|
+
'The number of partitions currently assigned to this consumer'),
|
|
1224
|
+
num_parts)
|