summaryrefslogtreecommitdiff
path: root/kafka/client08.py
blob: f120f378cea22f447407ac73821711964c67bfc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import base64
from collections import namedtuple, defaultdict
from functools import partial
from itertools import groupby, count
import logging
from operator import attrgetter
import socket
import struct
import time
import zlib

from .codec import gzip_encode, gzip_decode
from .codec import snappy_encode, snappy_decode
from .util import read_short_string, read_int_string
from .util import relative_unpack
from .util import write_short_string, write_int_string

log = logging.getLogger("kafka")

# Request payloads
ProduceRequest = namedtuple("ProduceRequest", ["topic", "partition", "messages"])
FetchRequest = namedtuple("FetchRequest", ["topic", "partition", "offset", "maxBytes"])
OffsetRequest = namedtuple("OffsetRequest", ["topic", "partition", "time", "maxOffsets"])

# Response payloads
ProduceResponse = namedtuple("ProduceResponse", ["topic", "partition", "error", "offset"])
FetchResponse = namedtuple("FetchResponse", ["topic", "partition", "error", "highwaterMark", "messages"])
OffsetResponse = namedtuple("OffsetResponse", ["topic", "partition", "error", "offset"])
BrokerMetadata = namedtuple("BrokerMetadata", ["nodeId", "host", "port"])
PartitionMetadata = namedtuple("PartitionMetadata", ["topic", "partitionId", "leader", "replicas", "isr"])

# Other useful structs 
OffsetAndMessage = namedtuple("OffsetAndMessage", ["offset", "message"])
Message = namedtuple("Message", ["magic", "attributes", "key", "value"])
TopicAndPartition = namedtuple("TopicAndPartition", ["topic", "partitionId"])

class ErrorMapping(object):
    Unknown                 = -1
    NoError                 = 0
    OffsetOutOfRange        = 1
    InvalidMessage          = 2
    UnknownTopicOrPartition = 3
    InvalidFetchSize        = 4
    LeaderNotAvailable      = 5
    NotLeaderForPartition   = 6
    RequestTimedOut         = 7
    BrokerNotAvailable      = 8
    ReplicaNotAvailable     = 9
    MessageSizeTooLarge     = 10
    StaleControllerEpoch    = 11
    OffsetMetadataTooLarge  = 12

class KafkaProtocol(object):
    PRODUCE_KEY      = 0
    FETCH_KEY        = 1
    OFFSET_KEY       = 2
    METADATA_KEY     = 3

    ATTRIBUTE_CODEC_MASK = 0x03

    @classmethod
    def encode_message_header(cls, clientId, correlationId, requestKey):
        return struct.pack('>HHiH%ds' % len(clientId), 
                           requestKey,          # ApiKey
                           0,                   # ApiVersion
                           correlationId,       # CorrelationId
                           len(clientId),       #
                           clientId)            # ClientId

    @classmethod
    def encode_message_set(cls, messages):
        message_set = ""
        for message in messages:
            encoded_message = KafkaProtocol.encode_message(message)
            message_set += struct.pack('>qi%ds' % len(encoded_message), 0, len(encoded_message), encoded_message)
        return message_set

    @classmethod
    def encode_message(cls, message):
        if message.magic == 0:
            msg = struct.pack('>BB', message.magic, message.attributes)
            msg += write_int_string(message.key)
            msg += write_int_string(message.value)
            crc = zlib.crc32(msg)
            msg = struct.pack('>i%ds' % len(msg), crc, msg)
        else:
            raise Exception("Unexpected magic number: %d" % message.magic)
        return msg

    @classmethod
    def create_message(cls, value):
        return Message(0, 0, "foo", value)

    @classmethod
    def create_gzip_message(cls, value):
        message_set = KafkaProtocol.encode_message_set([KafkaProtocol.create_message(value)])
        gzipped = gzip_encode(message_set) 
        return Message(0, 0x00 | (KafkaProtocol.ATTRIBUTE_CODEC_MASK & 0x01), "foo", gzipped)

    @classmethod
    def decode_message_set_iter(cls, data):
        """
        Decode a MessageSet, iteratively

        Reads repeated elements of (offset, message), calling decode_message to decode a
        single message. Since compressed messages contain futher MessageSets, these two methods
        have been decoupled so that they may recurse easily.
        
        Format
        ======
        MessageSet => [Offset MessageSize Message]
          Offset => int64
          MessageSize => int32

        N.B., the repeating element of the MessageSet is not preceded by an int32 like other
        repeating elements in this protocol
        """
        cur = 0
        while cur < len(data):
            ((offset, ), cur) = relative_unpack('>q', data, cur)
            (msg, cur) = read_int_string(data, cur)
            for (offset, message) in KafkaProtocol.decode_message(msg, offset):
                yield OffsetAndMessage(offset, message)

    @classmethod
    def decode_message(cls, data, offset):
        """
        Decode a single Message

        The only caller of this method is decode_message_set_iter. They are decoupled to 
        support nested messages (compressed MessageSets). The offset is actually read from
        decode_message_set_iter (it is part of the MessageSet payload).

        Format 
        ========
        Message => Crc MagicByte Attributes Key Value
          Crc => int32
          MagicByte => int8
          Attributes => int8
          Key => bytes
          Value => bytes
        """
        ((crc, magic, att), cur) = relative_unpack('>iBB', data, 0)
        assert crc == zlib.crc32(data[4:])
        (key, cur) = read_int_string(data, cur)
        (value, cur) = read_int_string(data, cur)
        if att & KafkaProtocol.ATTRIBUTE_CODEC_MASK == 0:
            yield (offset, Message(magic, att, key, value))
        elif att & KafkaProtocol.ATTRIBUTE_CODEC_MASK == 1:
            gz = gzip_decode(value)
            for (offset, message) in KafkaProtocol.decode_message_set_iter(gz):
                yield (offset, message)
        elif att & KafkaProtocol.ATTRIBUTE_CODEC_MASK == 2:
            snp = snappy_decode(value)
            for (offset, message) in KafkaProtocol.decode_message_set_iter(snp):
                yield (offset, message)

    @classmethod
    def encode_metadata_request(cls, clientId, correlationId, *topics):
        # Header
        message = cls.encode_message_header(clientId, correlationId, KafkaProtocol.METADATA_KEY)

        # TopicMetadataRequest
        message += struct.pack('>i', len(topics))
        for topic in topics:
            message += struct.pack('>H%ds' % len(topic), len(topic), topic)

        # Length-prefix the whole thing
        return write_int_string(message)

    @classmethod
    def decode_metadata_response(cls, data):
        # TopicMetadataResponse
        cur = 0
        ((correlationId, numBrokers), cur) = relative_unpack('>ii', data, cur)
        brokers = {}
        for i in range(numBrokers):
            ((nodeId, ), cur) = relative_unpack('>i', data, cur)
            (host, cur) = read_short_string(data, cur)
            ((port,), cur) = relative_unpack('>i', data, cur)
            brokers[nodeId] = BrokerMetadata(nodeId, host, port)

        ((numTopics,), cur) = relative_unpack('>i', data, cur)
        topicMetadata = {}
        for i in range(numTopics):
            ((topicError,), cur) = relative_unpack('>H', data, cur)
            (topicName, cur) = read_short_string(data, cur)
            ((numPartitions,), cur) = relative_unpack('>i', data, cur)
            partitionMetadata = {}
            for j in range(numPartitions):
                ((partitionErrorCode, partitionId, leader, numReplicas), cur) = relative_unpack('>Hiii', data, cur)
                (replicas, cur) = relative_unpack('>%di' % numReplicas, data, cur)
                ((numIsr,), cur) = relative_unpack('>i', data, cur)
                (isr, cur) = relative_unpack('>%di' % numIsr, data, cur)
                partitionMetadata[partitionId] = PartitionMetadata(topicName, partitionId, leader, replicas, isr)
            topicMetadata[topicName] = partitionMetadata
        return (brokers, topicMetadata)

    @classmethod
    def encode_produce_request(self, clientId, correlationId, payloads=[], acks=1, timeout=1000):
        # Group the payloads by topic
        sorted_payloads = sorted(payloads, key=attrgetter("topic"))
        grouped_payloads = list(groupby(sorted_payloads, key=attrgetter("topic")))

        # Pack the message header
        message = struct.pack('>HHiH%ds' % len(clientId), 
                              KafkaProtocol.PRODUCE_KEY,    # ApiKey
                              0,                            # ApiVersion
                              correlationId,                # CorrelationId
                              len(clientId),                #
                              clientId)                     # ClientId

        # Pack the message sets
        message += struct.pack('>Hii', acks, timeout, len(grouped_payloads))
        for topic, payload in grouped_payloads:
            payloads = list(payloads)
            message += struct.pack('>H%dsi' % len(topic), len(topic), topic, len(payloads))
            for payload in payloads:
                message_set = KafkaProtocol.encode_message_set(payload.messages)
                message += struct.pack('>ii%ds' % len(message_set), payload.partition, len(message_set), message_set)

        # Length-prefix the whole thing
        return struct.pack('>i%ds' % len(message), len(message), message)

    @classmethod
    def decode_produce_response(cls, data):
        ((correlationId, numTopics), cur) = relative_unpack('>ii', data, 0)
        for i in range(numTopics):
            ((strlen,), cur) = relative_unpack('>H', data, cur)
            topic = data[cur:cur+strlen]
            cur += strlen
            ((numPartitions,), cur) = relative_unpack('>i', data, cur)
            for i in range(numPartitions):
                ((partition, error, offset), cur) = relative_unpack('>iHq', data, cur)
                yield ProduceResponse(topic, partition, error, offset)

    @classmethod
    def encode_fetch_request(cls, clientId, correlationId, payloads=[], replicaId=-1, maxWaitTime=100, minBytes=1024):
        # Group the payloads by topic
        sorted_payloads = sorted(payloads, key=attrgetter("topic"))
        grouped_payloads = list(groupby(sorted_payloads, key=attrgetter("topic")))

        # Pack the message header
        message = struct.pack('>HHiH%ds' % len(clientId), 
                              KafkaProtocol.FETCH_KEY,  # ApiKey
                              0,                        # ApiVersion
                              correlationId,            # CorrelationId
                              len(clientId),            #
                              clientId)                 # ClientId

        # Pack the FetchRequest
        message += struct.pack('>iiii',
                               replicaId,   # ReplicaId
                               maxWaitTime, # MaxWaitTime
                               minBytes,    # MinBytes
                               len(grouped_payloads))
        for topic, payload in grouped_payloads:
            payloads = list(payloads)
            message += write_short_string(topic)
            message += struct.pack('>i', len(payloads))
            for payload in payloads:
                message += struct.pack('>iqi', payload.partition, payload.offset, payload.maxBytes)

        # Length-prefix the whole thing
        return struct.pack('>i%ds' % len(message), len(message), message)

    @classmethod
    def decode_fetch_response_iter(cls, data):
        ((correlationId, numTopics), cur) = relative_unpack('>ii', data, 0)
        for i in range(numTopics):
            (topic, cur) = read_short_string(data, cur)
            ((numPartitions,), cur) = relative_unpack('>i', data, cur)
            for i in range(numPartitions):
                ((partition, error, highwaterMarkOffset), cur) = relative_unpack('>iHq', data, cur)
                (messageSet, cur) = read_int_string(data, cur)
                yield FetchResponse(topic, partition, error, highwaterMarkOffset, KafkaProtocol.decode_message_set_iter(messageSet))

    @classmethod
    def encode_offset_request(cls, clientId, correlationId, payloads=[], replicaId=-1):
        # Group the payloads by topic
        sorted_payloads = sorted(payloads, key=attrgetter("topic"))
        grouped_payloads = list(groupby(sorted_payloads, key=attrgetter("topic")))

        # Pack the message header
        message = struct.pack('>HHiH%ds' % len(clientId), 
                              KafkaProtocol.OFFSET_KEY, # ApiKey
                              0,                        # ApiVersion
                              correlationId,            # CorrelationId
                              len(clientId),            #
                              clientId)                 # ClientId

        message += struct.pack('>ii', replicaId, len(grouped_payloads))

        # Pack the OffsetRequest
        for topic, payload in grouped_payloads:
            payloads = list(payloads)
            message += write_short_string(topic)
            message += struct.pack('>i', len(payloads))
            for payload in payloads:
                message += struct.pack('>iqi', payload.partition, payload.time, payload.maxOffsets)

        # Length-prefix the whole thing
        return struct.pack('>i%ds' % len(message), len(message), message)

    @classmethod
    def decode_offset_response(cls, data):
        ((correlationId, numTopics), cur) = relative_unpack('>ii', data, 0)
        for i in range(numTopics):
            (topic, cur) = read_short_string(data, cur)
            ((numPartitions,), cur) = relative_unpack('>i', data, cur)
            for i in range(numPartitions):
                ((partition, error, offset), cur) = relative_unpack('>iHq', data, cur)
                yield OffsetResponse(topic, partition, error, offset)



class Conn(object):
    """
    A socket connection to a single Kafka broker
    """
    def __init__(self, host, port, bufsize=1024):
        self.host = host
        self.port = port
        self.bufsize = bufsize
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._sock.connect((host, port))
        self._sock.settimeout(10)

    def close(self):
        self._sock.close()

    def _consume_response(self):
        """
        Fully consumer the response iterator
        """
        data = ""
        for chunk in self._consume_response_iter():
            data += chunk
        return data

    def _consume_response_iter(self):
        """
        This method handles the response header and error messages. It 
        then returns an iterator for the chunks of the response
        """
        log.debug("Handling response from Kafka")

        # Header
        resp = self._sock.recv(4)
        if resp == "":
            raise Exception("Got no response from Kafka")
        (size,) = struct.unpack('>i', resp)

        messageSize = size - 4
        log.debug("About to read %d bytes from Kafka", messageSize)

        # Response iterator
        total = 0
        while total < messageSize:
            resp = self._sock.recv(self.bufsize)
            log.debug("Read %d bytes from Kafka", len(resp))
            if resp == "":
                raise Exception("Underflow")
            total += len(resp)
            yield resp

    def send(self, requestId, payload):
        #print(repr(payload))
        sent = self._sock.sendall(payload)
        if sent == 0:
            raise RuntimeError("Kafka went away")
        self.data = self._consume_response()
        #print(repr(self.data))

    def recv(self, requestId):
        return self.data

class KafkaConnection(object):
    """
    Low-level API for Kafka 0.8
    """

    # ClientId for Kafka
    CLIENT_ID = "kafka-python"

    # Global correlation ids
    ID_GEN = count() 

    def __init__(self, host, port, bufsize=1024):
        # We need one connection to bootstrap 
        self.bufsize = bufsize
        self.conns = {(host, port): Conn(host, port, bufsize)}
        self.brokers = {}           # broker Id -> BrokerMetadata
        self.topics_to_brokers = {} # topic Id -> broker Id
        self.load_metadata_for_topics()

    def get_conn_for_broker(self, broker):
        "Get or create a connection to a broker"
        if (broker.host, broker.port) not in self.conns:
            self.conns[(broker.host, broker.port)] = Conn(broker.host, broker.port, self.bufsize)
        return self.conns[(broker.host, broker.port)]

    def next_id(self):
        return KafkaConnection.ID_GEN.next()

    def load_metadata_for_topics(self, *topics):
        """
        Discover brokers and metadata for a set of topics
        """
        requestId = self.next_id()
        request = KafkaProtocol.encode_metadata_request(KafkaConnection.CLIENT_ID, requestId, *topics) 
        conn = self.conns.values()[0] # Just get the first one in the list
        conn.send(requestId, request)
        response = conn.recv(requestId)
        (brokers, topics) = KafkaProtocol.decode_metadata_response(response)
        log.debug("Broker metadata: %s", brokers)
        log.debug("Topic metadata: %s", topics)
        self.brokers.update(brokers)
        self.topics_to_brokers = {}
        for topic, partitions in topics.items():
            for partition, meta in partitions.items():
                if meta.leader == -1:
                    log.info("Partition is unassigned, delay for 1s and retry")
                    time.sleep(1)
                    self.load_metadata_for_topics(topic)
                    return 
                else:
                    self.topics_to_brokers[TopicAndPartition(topic, partition)] = brokers[meta.leader]

    def get_leader_for_partition(self, topic, partition):
        key = TopicAndPartition(topic, partition)
        if key not in self.topics_to_brokers:
            self.load_metadata_for_topics(topic)
        return self.topics_to_brokers[key]

    def send_produce_request(self, payloads=[], fail_on_error=True, callback=None):
        # Group the produce requests by topic+partition
        sorted_payloads = sorted(payloads, key=lambda x: (x.topic, x.partition))
        grouped_payloads = groupby(sorted_payloads, key=lambda x: (x.topic, x.partition))

        # Group the produce requests by which broker they go to
        payloads_by_broker = defaultdict(list)
        for (topic, partition), payload in grouped_payloads:
            payloads_by_broker[self.get_leader_for_partition(topic, partition)] += list(payload)

        out = []
        # For each broker, send the list of request payloads
        for broker, payloads in payloads_by_broker.items():
            conn = self.get_conn_for_broker(broker)
            requestId = self.next_id()
            request = KafkaProtocol.encode_produce_request(KafkaConnection.CLIENT_ID, requestId, payloads)
            # Send the request
            conn.send(requestId, request)
            response = conn.recv(requestId)
            for produce_response in KafkaProtocol.decode_produce_response(response):
                # Check for errors
                if fail_on_error == True and produce_response.error != 0:
                    raise Exception("ProduceRequest for %s failed with errorcode=%d", 
                            (TopicAndPartition(produce_response.topic, produce_response.partition), produce_response.error))
                # Run the callback
                if callback is not None:
                    out.append(callback(produce_response))
                else:
                    out.append(produce_response)
        return out

    def send_fetch_request(self, payloads=[], fail_on_error=True, callback=None):
        """
        Encode and send a FetchRequest
        
        Payloads are grouped by topic and partition so they can be pipelined to the same
        brokers.
        """
        # Group the produce requests by topic+partition
        sorted_payloads = sorted(payloads, key=lambda x: (x.topic, x.partition))
        grouped_payloads = groupby(sorted_payloads, key=lambda x: (x.topic, x.partition))

        # Group the produce requests by which broker they go to
        payloads_by_broker = defaultdict(list)
        for (topic, partition), payload in grouped_payloads:
            payloads_by_broker[self.get_leader_for_partition(topic, partition)] += list(payload)

        out = []
        # For each broker, send the list of request payloads
        for broker, payloads in payloads_by_broker.items():
            conn = self.get_conn_for_broker(broker)
            requestId = self.next_id()
            request = KafkaProtocol.encode_fetch_request(KafkaConnection.CLIENT_ID, requestId, payloads)
            # Send the request
            conn.send(requestId, request)
            response = conn.recv(requestId)
            for fetch_response in KafkaProtocol.decode_fetch_response_iter(response):
                # Check for errors
                if fail_on_error == True and fetch_response.error != 0:
                    raise Exception("FetchRequest %s failed with errorcode=%d",
                            (TopicAndPartition(fetch_response.topic, fetch_response.partition), fetch_response.error))
                # Run the callback
                if callback is not None:
                    out.append(callback(fetch_response))
                else:
                    out.append(fetch_response)
        return out

if __name__ == "__main__":
    # Bootstrap connection
    conn = KafkaConnection("localhost", 9092)

    # Create some Messages
    messages = (KafkaProtocol.create_gzip_message("GZIPPed"),
                KafkaProtocol.create_message("not-gzipped"))

    # Create a ProduceRequest
    produce = ProduceRequest("foo5", 0, messages)

    # Send the ProduceRequest
    produce_resp = conn.send_produce_request([produce])

    # Check for errors
    for resp in produce_resp:
        if resp.error != 0:
            raise Exception("ProduceRequest failed with errorcode=%d", resp.error)
        print resp