Thanks to visit codestin.com
Credit goes to github.com

Skip to content

KAFKA-2136: support Fetch and Produce v1 (throttle_time_ms) #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions kafka/consumer/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Fetcher(six.Iterator):
'max_partition_fetch_bytes': 1048576,
'check_crcs': True,
'iterator_refetch_records': 1, # undocumented -- interface may change
'api_version': (0, 8, 0),
}

def __init__(self, client, subscriptions, **configs):
Expand Down Expand Up @@ -531,7 +532,7 @@ def _create_fetch_requests(self):
FetchRequests skipped if no leader, or node has requests in flight

Returns:
dict: {node_id: FetchRequest, ...}
dict: {node_id: FetchRequest, ...} (version depends on api_version)
"""
# create the fetch info as a dict of lists of partition info tuples
# which can be passed to FetchRequest() via .items()
Expand Down Expand Up @@ -564,9 +565,10 @@ def _create_fetch_requests(self):
log.debug("Adding fetch request for partition %s at offset %d",
partition, position)

version = 1 if self.config['api_version'] >= (0, 9) else 0
requests = {}
for node_id, partition_data in six.iteritems(fetchable):
requests[node_id] = FetchRequest[0](
requests[node_id] = FetchRequest[version](
-1, # replica_id
self.config['fetch_max_wait_ms'],
self.config['fetch_min_bytes'],
Expand Down
8 changes: 5 additions & 3 deletions kafka/producer/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Sender(threading.Thread):
'retries': 0,
'request_timeout_ms': 30000,
'client_id': 'kafka-python-' + __version__,
'api_version': (0, 8, 0),
}

def __init__(self, client, metadata, accumulator, **configs):
Expand Down Expand Up @@ -232,7 +233,7 @@ def _create_produce_requests(self, collated):
collated: {node_id: [RecordBatch]}

Returns:
dict: {node_id: ProduceRequest}
dict: {node_id: ProduceRequest} (version depends on api_version)
"""
requests = {}
for node_id, batches in six.iteritems(collated):
Expand All @@ -245,7 +246,7 @@ def _produce_request(self, node_id, acks, timeout, batches):
"""Create a produce request from the given record batches.

Returns:
ProduceRequest
ProduceRequest (version depends on api_version)
"""
produce_records_by_partition = collections.defaultdict(dict)
for batch in batches:
Expand All @@ -256,7 +257,8 @@ def _produce_request(self, node_id, acks, timeout, batches):
buf = batch.records.buffer()
produce_records_by_partition[topic][partition] = buf

return ProduceRequest[0](
version = 1 if self.config['api_version'] >= (0, 9) else 0
return ProduceRequest[version](
required_acks=acks,
timeout=timeout,
topics=[(topic, list(partition_info.items()))
Expand Down
26 changes: 24 additions & 2 deletions kafka/protocol/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ class FetchResponse_v0(Struct):
)


class FetchResponse_v1(Struct):
API_KEY = 1
API_VERSION = 1
SCHEMA = Schema(
('throttle_time_ms', Int32),
('topics', Array(
('topics', String('utf-8')),
('partitions', Array(
('partition', Int32),
('error_code', Int16),
('highwater_offset', Int64),
('message_set', MessageSet)))))
)


class FetchRequest_v0(Struct):
API_KEY = 1
API_VERSION = 0
Expand All @@ -34,5 +49,12 @@ class FetchRequest_v0(Struct):
)


FetchRequest = [FetchRequest_v0]
FetchResponse = [FetchResponse_v0]
class FetchRequest_v1(Struct):
API_KEY = 1
API_VERSION = 1
RESPONSE_TYPE = FetchResponse_v1
SCHEMA = FetchRequest_v0.SCHEMA


FetchRequest = [FetchRequest_v0, FetchRequest_v1]
FetchResponse = [FetchResponse_v0, FetchResponse_v1]
6 changes: 1 addition & 5 deletions kafka/protocol/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,7 @@ def encode_offset_fetch_request(cls, group, payloads, from_kafka=False):
payloads: list of OffsetFetchRequestPayload
from_kafka: bool, default False, set True for Kafka-committed offsets
"""
if from_kafka:
version = 1
else:
version = 0

version = 1 if from_kafka else 0
return kafka.protocol.commit.OffsetFetchRequest[version](
consumer_group=group,
topics=[(
Expand Down
25 changes: 23 additions & 2 deletions kafka/protocol/produce.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ class ProduceResponse_v0(Struct):
)


class ProduceResponse_v1(Struct):
API_KEY = 0
API_VERSION = 1
SCHEMA = Schema(
('topics', Array(
('topic', String('utf-8')),
('partitions', Array(
('partition', Int32),
('error_code', Int16),
('offset', Int64))))),
('throttle_time_ms', Int32)
)


class ProduceRequest_v0(Struct):
API_KEY = 0
API_VERSION = 0
Expand All @@ -31,5 +45,12 @@ class ProduceRequest_v0(Struct):
)


ProduceRequest = [ProduceRequest_v0]
ProduceResponse = [ProduceResponse_v0]
class ProduceRequest_v1(Struct):
API_KEY = 0
API_VERSION = 1
RESPONSE_TYPE = ProduceResponse_v1
SCHEMA = ProduceRequest_v0.SCHEMA


ProduceRequest = [ProduceRequest_v0, ProduceRequest_v1]
ProduceResponse = [ProduceResponse_v0, ProduceResponse_v1]