예제 #1
0
    def test_decode_fetch_response(self):
        t1 = b"topic1"
        t2 = b"topic2"
        msgs = [create_message(msg)
                for msg in [b"message1", b"hi", b"boo", b"foo", b"so fun!"]]
        ms1 = KafkaProtocol._encode_message_set([msgs[0], msgs[1]])
        ms2 = KafkaProtocol._encode_message_set([msgs[2]])
        ms3 = KafkaProtocol._encode_message_set([msgs[3], msgs[4]])

        encoded = struct.pack('>iih%dsiihqi%dsihqi%dsh%dsiihqi%ds' %
                              (len(t1), len(ms1), len(ms2), len(t2), len(ms3)),
                              4, 2, len(t1), t1, 2, 0, 0, 10, len(ms1), ms1, 1,
                              1, 20, len(ms2), ms2, len(t2), t2, 1, 0, 0, 30,
                              len(ms3), ms3)

        responses = list(KafkaProtocol.decode_fetch_response(encoded))
        def expand_messages(response):
            return FetchResponsePayload(response.topic, response.partition,
                                        response.error, response.highwaterMark,
                                        list(response.messages))

        expanded_responses = list(map(expand_messages, responses))
        expect = [FetchResponsePayload(t1, 0, 0, 10, [OffsetAndMessage(0, msgs[0]),
                                                      OffsetAndMessage(0, msgs[1])]),
                  FetchResponsePayload(t1, 1, 1, 20, [OffsetAndMessage(0, msgs[2])]),
                  FetchResponsePayload(t2, 0, 0, 30, [OffsetAndMessage(0, msgs[3]),
                                                      OffsetAndMessage(0, msgs[4])])]
        self.assertEqual(expanded_responses, expect)
예제 #2
0
    def test_decode_fetch_response(self):
        t1 = "topic1"
        t2 = "topic2"
        msgs = map(create_message, ["message1", "hi", "boo", "foo", "so fun!"])
        ms1 = KafkaProtocol._encode_message_set([msgs[0], msgs[1]])
        ms2 = KafkaProtocol._encode_message_set([msgs[2]])
        ms3 = KafkaProtocol._encode_message_set([msgs[3], msgs[4]])

        encoded = struct.pack(
            '>iih%dsiihqi%dsihqi%dsh%dsiihqi%ds' %
            (len(t1), len(ms1), len(ms2), len(t2), len(ms3)), 4, 2,
            len(t1), t1, 2, 0, 0, 10, len(ms1), ms1, 1, 1, 20, len(ms2), ms2,
            len(t2), t2, 1, 0, 0, 30, len(ms3), ms3)

        responses = list(KafkaProtocol.decode_fetch_response(encoded))

        def expand_messages(response):
            return FetchResponse(response.topic, response.partition,
                                 response.error, response.highwaterMark,
                                 list(response.messages))

        expanded_responses = map(expand_messages, responses)
        expect = [
            FetchResponse(
                t1, 0, 0, 10,
                [OffsetAndMessage(0, msgs[0]),
                 OffsetAndMessage(0, msgs[1])]),
            FetchResponse(t1, 1, 1, 20, [OffsetAndMessage(0, msgs[2])]),
            FetchResponse(
                t2, 0, 0, 30,
                [OffsetAndMessage(0, msgs[3]),
                 OffsetAndMessage(0, msgs[4])])
        ]
        self.assertEqual(expanded_responses, expect)
예제 #3
0
    def load_metadata_for_topics(self, *topics):
        """
        Discover brokers and metadata for a set of topics. This function is called
        lazily whenever metadata is unavailable.
        """
        request_id = self._next_id()
        request = KafkaProtocol.encode_metadata_request(self.client_id,
                                                        request_id, topics)

        response = self._send_broker_unaware_request(request_id, request)

        (brokers, topics) = KafkaProtocol.decode_metadata_response(response)

        log.debug("Broker metadata: %s", brokers)
        log.debug("Topic metadata: %s", topics)

        self.brokers = brokers

        for topic, partitions in topics.items():
            self.reset_topic_metadata(topic)

            if not partitions:
                log.warning('No partitions for %s', topic)
                continue

            self.topic_partitions[topic] = []
            for partition, meta in partitions.items():
                self.topic_partitions[topic].append(partition)
                topic_part = TopicAndPartition(topic, partition)
                if meta.leader == -1:
                    log.warning('No leader for topic %s partition %s', topic, partition)
                    self.topics_to_brokers[topic_part] = None
                else:
                    self.topics_to_brokers[topic_part] = brokers[meta.leader]
예제 #4
0
    def load_metadata_for_topics(self, *topics):
        """
        Discover brokers and metadata for a set of topics. This function is called
        lazily whenever metadata is unavailable.
        """
        request_id = self._next_id()
        request = KafkaProtocol.encode_metadata_request(
            self.client_id, request_id, topics)

        response = self._send_broker_unaware_request(request_id, request)

        (brokers, topics) = KafkaProtocol.decode_metadata_response(response)

        log.debug("Broker metadata: %s", brokers)
        log.debug("Topic metadata: %s", topics)

        self.brokers = brokers

        for topic, partitions in topics.items():
            self.reset_topic_metadata(topic)

            if not partitions:
                log.warning('No partitions for %s', topic)
                continue

            self.topic_partitions[topic] = []
            for partition, meta in partitions.items():
                self.topic_partitions[topic].append(partition)
                topic_part = TopicAndPartition(topic, partition)
                if meta.leader == -1:
                    log.warning('No leader for topic %s partition %s', topic,
                                partition)
                    self.topics_to_brokers[topic_part] = None
                else:
                    self.topics_to_brokers[topic_part] = brokers[meta.leader]
예제 #5
0
    def test_encode_produce_request(self):
        requests = [
            ProduceRequestPayload("topic1", 0, [
                kafka.protocol.message.Message(b"a"),
                kafka.protocol.message.Message(b"b")
            ]),
            ProduceRequestPayload("topic2", 1,
                                  [kafka.protocol.message.Message(b"c")])
        ]

        msg_a_binary = KafkaProtocol._encode_message(create_message(b"a"))
        msg_b_binary = KafkaProtocol._encode_message(create_message(b"b"))
        msg_c_binary = KafkaProtocol._encode_message(create_message(b"c"))

        header = b"".join([
            struct.pack('>i', 0x94),  # The length of the message overall
            struct.pack('>h', 0),  # Msg Header, Message type = Produce
            struct.pack('>h', 0),  # Msg Header, API version
            struct.pack('>i', 2),  # Msg Header, Correlation ID
            struct.pack('>h7s', 7, b"client1"),  # Msg Header, The client ID
            struct.pack('>h', 2),  # Num acks required
            struct.pack('>i', 100),  # Request Timeout
            struct.pack('>i', 2),  # The number of requests
        ])

        total_len = len(msg_a_binary) + len(msg_b_binary)
        topic1 = b"".join([
            struct.pack('>h6s', 6, b'topic1'),  # The topic1
            struct.pack('>i', 1),  # One message set
            struct.pack('>i', 0),  # Partition 0
            struct.pack('>i',
                        total_len + 24),  # Size of the incoming message set
            struct.pack('>q', 0),  # No offset specified
            struct.pack('>i', len(msg_a_binary)),  # Length of message
            msg_a_binary,  # Actual message
            struct.pack('>q', 0),  # No offset specified
            struct.pack('>i', len(msg_b_binary)),  # Length of message
            msg_b_binary,  # Actual message
        ])

        topic2 = b"".join([
            struct.pack('>h6s', 6, b'topic2'),  # The topic1
            struct.pack('>i', 1),  # One message set
            struct.pack('>i', 1),  # Partition 1
            struct.pack('>i',
                        len(msg_c_binary) +
                        12),  # Size of the incoming message set
            struct.pack('>q', 0),  # No offset specified
            struct.pack('>i', len(msg_c_binary)),  # Length of message
            msg_c_binary,  # Actual message
        ])

        expected1 = b"".join([header, topic1, topic2])
        expected2 = b"".join([header, topic2, topic1])

        encoded = KafkaProtocol.encode_produce_request(b"client1", 2, requests,
                                                       2, 100)
        self.assertIn(encoded, [expected1, expected2])
예제 #6
0
    def test_encode_produce_request(self):
        requests = [
            ProduceRequestPayload("topic1", 0, [
                kafka.protocol.message.Message(b"a"),
                kafka.protocol.message.Message(b"b")
            ]),
            ProduceRequestPayload("topic2", 1, [
                kafka.protocol.message.Message(b"c")
            ])
        ]

        msg_a_binary = KafkaProtocol._encode_message(create_message(b"a"))
        msg_b_binary = KafkaProtocol._encode_message(create_message(b"b"))
        msg_c_binary = KafkaProtocol._encode_message(create_message(b"c"))

        header = b"".join([
            struct.pack('>i', 0x94),                   # The length of the message overall
            struct.pack('>h', 0),                      # Msg Header, Message type = Produce
            struct.pack('>h', 0),                      # Msg Header, API version
            struct.pack('>i', 2),                      # Msg Header, Correlation ID
            struct.pack('>h7s', 7, b"client1"),        # Msg Header, The client ID
            struct.pack('>h', 2),                      # Num acks required
            struct.pack('>i', 100),                    # Request Timeout
            struct.pack('>i', 2),                      # The number of requests
        ])

        total_len = len(msg_a_binary) + len(msg_b_binary)
        topic1 = b"".join([
            struct.pack('>h6s', 6, b'topic1'),         # The topic1
            struct.pack('>i', 1),                      # One message set
            struct.pack('>i', 0),                      # Partition 0
            struct.pack('>i', total_len + 24),         # Size of the incoming message set
            struct.pack('>q', 0),                      # No offset specified
            struct.pack('>i', len(msg_a_binary)),      # Length of message
            msg_a_binary,                              # Actual message
            struct.pack('>q', 0),                      # No offset specified
            struct.pack('>i', len(msg_b_binary)),      # Length of message
            msg_b_binary,                              # Actual message
        ])

        topic2 = b"".join([
            struct.pack('>h6s', 6, b'topic2'),         # The topic1
            struct.pack('>i', 1),                      # One message set
            struct.pack('>i', 1),                      # Partition 1
            struct.pack('>i', len(msg_c_binary) + 12), # Size of the incoming message set
            struct.pack('>q', 0),                      # No offset specified
            struct.pack('>i', len(msg_c_binary)),      # Length of message
            msg_c_binary,                              # Actual message
        ])

        expected1 = b"".join([ header, topic1, topic2 ])
        expected2 = b"".join([ header, topic2, topic1 ])

        encoded = KafkaProtocol.encode_produce_request(b"client1", 2, requests, 2, 100)
        self.assertIn(encoded, [ expected1, expected2 ])
예제 #7
0
 def test_decode_message(self):
     encoded = "\xaa\xf1\x8f[\x00\x00\x00\x00\x00\x03key\x00\x00\x00\x04test"
     offset = 10
     (returned_offset, decoded_message) = \
         list(KafkaProtocol._decode_message(encoded, offset))[0]
     self.assertEqual(returned_offset, offset)
     self.assertEqual(decoded_message, create_message("test", "key"))
예제 #8
0
    def test_encode_message_set(self):
        message_set = [
            create_message(b"v1", b"k1"),
            create_message(b"v2", b"k2")
        ]

        encoded = KafkaProtocol._encode_message_set(message_set)
        expect = b"".join([
            struct.pack(">q", 0),          # MsgSet Offset
            struct.pack(">i", 18),         # Msg Size
            struct.pack(">i", 1474775406), # CRC
            struct.pack(">bb", 0, 0),      # Magic, flags
            struct.pack(">i", 2),          # Length of key
            b"k1",                          # Key
            struct.pack(">i", 2),          # Length of value
            b"v1",                          # Value

            struct.pack(">q", 0),          # MsgSet Offset
            struct.pack(">i", 18),         # Msg Size
            struct.pack(">i", -16383415),  # CRC
            struct.pack(">bb", 0, 0),      # Magic, flags
            struct.pack(">i", 2),          # Length of key
            b"k2",                          # Key
            struct.pack(">i", 2),          # Length of value
            b"v2",                          # Value
        ])

        self.assertEqual(encoded, expect)
예제 #9
0
    def test_decode_message_set(self):
        encoded = b"".join([
            struct.pack(">q", 0),          # MsgSet Offset
            struct.pack(">i", 18),         # Msg Size
            struct.pack(">i", 1474775406), # CRC
            struct.pack(">bb", 0, 0),      # Magic, flags
            struct.pack(">i", 2),          # Length of key
            b"k1",                         # Key
            struct.pack(">i", 2),          # Length of value
            b"v1",                         # Value

            struct.pack(">q", 1),          # MsgSet Offset
            struct.pack(">i", 18),         # Msg Size
            struct.pack(">i", -16383415),  # CRC
            struct.pack(">bb", 0, 0),      # Magic, flags
            struct.pack(">i", 2),          # Length of key
            b"k2",                         # Key
            struct.pack(">i", 2),          # Length of value
            b"v2",                         # Value
        ])

        msgs = list(KafkaProtocol._decode_message_set_iter(encoded))
        self.assertEqual(len(msgs), 2)
        msg1, msg2 = msgs

        returned_offset1, decoded_message1 = msg1
        returned_offset2, decoded_message2 = msg2

        self.assertEqual(returned_offset1, 0)
        self.assertEqual(decoded_message1, create_message(b"v1", b"k1"))

        self.assertEqual(returned_offset2, 1)
        self.assertEqual(decoded_message2, create_message(b"v2", b"k2"))
예제 #10
0
 def test_decode_metadata_response(self):
     node_brokers = {
         0: BrokerMetadata(0, "brokers1.kafka.rdio.com", 1000),
         1: BrokerMetadata(1, "brokers1.kafka.rdio.com", 1001),
         3: BrokerMetadata(3, "brokers2.kafka.rdio.com", 1000)
     }
     topic_partitions = {
         "topic1": {
             0: PartitionMetadata("topic1", 0, 1, (0, 2), (2,)),
             1: PartitionMetadata("topic1", 1, 3, (0, 1), (0, 1))
         },
         "topic2": {
             0: PartitionMetadata("topic2", 0, 0, (), ())
         }
     }
     topic_errors = {"topic1": 0, "topic2": 1}
     partition_errors = {
         ("topic1", 0): 0,
         ("topic1", 1): 1,
         ("topic2", 0): 0
     }
     encoded = self._create_encoded_metadata_response(node_brokers,
                                                      topic_partitions,
                                                      topic_errors,
                                                      partition_errors)
     decoded = KafkaProtocol.decode_metadata_response(encoded)
     self.assertEqual(decoded, (node_brokers, topic_partitions))
예제 #11
0
    def test_encode_offset_request__no_payload(self):
        expected = "".join([
            struct.pack(">i", 65),  # Total length of the request
            struct.pack('>h', 2),  # Message type = offset fetch
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 4),  # Correlation ID
            struct.pack('>h3s', 3, "cid"),  # The client ID
            struct.pack('>i', -1),  # Replica Id
            struct.pack('>i', 1),  # Num topics
            struct.pack(">h6s", 6, "topic1"),  # Topic for the request
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 3),  # Partition 3
            struct.pack(">q", -1),  # No time offset
            struct.pack(">i", 1),  # One offset requested
            struct.pack(">i", 4),  # Partition 3
            struct.pack(">q", -1),  # No time offset
            struct.pack(">i", 1),  # One offset requested
        ])

        encoded = KafkaProtocol.encode_offset_request("cid", 4, [
            OffsetRequest('topic1', 3, -1, 1),
            OffsetRequest('topic1', 4, -1, 1),
        ])

        self.assertEqual(encoded, expected)
예제 #12
0
    def test_encode_offset_fetch_request(self):
        header = "".join([
            struct.pack('>i', 69),  # Total message length
            struct.pack('>h', 9),  # Message type = offset fetch
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 42),  # Correlation ID
            struct.pack('>h9s', 9, "client_id"),  # The client ID
            struct.pack('>h8s', 8, "group_id"),  # The group to commit for
            struct.pack('>i', 2),  # Num topics
        ])

        topic1 = "".join([
            struct.pack(">h6s", 6, "topic1"),  # Topic for the request
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 0),  # Partition 0
            struct.pack(">i", 1),  # Partition 1
        ])

        topic2 = "".join([
            struct.pack(">h6s", 6, "topic2"),  # Topic for the request
            struct.pack(">i", 1),  # One partitions
            struct.pack(">i", 2),  # Partition 2
        ])

        expected1 = "".join([header, topic1, topic2])
        expected2 = "".join([header, topic2, topic1])

        encoded = KafkaProtocol.encode_offset_fetch_request(
            "client_id", 42, "group_id", [
                OffsetFetchRequest("topic1", 0),
                OffsetFetchRequest("topic1", 1),
                OffsetFetchRequest("topic2", 2),
            ])

        self.assertIn(encoded, [expected1, expected2])
예제 #13
0
    def test_decode_offset_fetch_response(self):
        encoded = "".join([
            struct.pack(">i", 42),  # Correlation ID
            struct.pack(">i", 1),  # One topics
            struct.pack(">h6s", 6, "topic1"),  # First topic
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 2),  # Partition 2
            struct.pack(">q", 4),  # Offset 4
            struct.pack(">h4s", 4, "meta"),  # Metadata
            struct.pack(">h", 0),  # No error
            struct.pack(">i", 4),  # Partition 4
            struct.pack(">q", 8),  # Offset 8
            struct.pack(">h4s", 4, "meta"),  # Metadata
            struct.pack(">h", 0),  # No error
        ])

        results = KafkaProtocol.decode_offset_fetch_response(encoded)
        self.assertEqual(
            set(results),
            set([
                OffsetFetchResponse(topic='topic1',
                                    partition=2,
                                    offset=4,
                                    error=0,
                                    metadata="meta"),
                OffsetFetchResponse(topic='topic1',
                                    partition=4,
                                    offset=8,
                                    error=0,
                                    metadata="meta"),
            ]))
예제 #14
0
    def test_decode_metadata_response(self):
        node_brokers = {
            0: BrokerMetadata(0, "brokers1.kafka.rdio.com", 1000),
            1: BrokerMetadata(1, "brokers1.kafka.rdio.com", 1001),
            3: BrokerMetadata(3, "brokers2.kafka.rdio.com", 1000)
        }

        topic_partitions = {
            "topic1": {
                0: PartitionMetadata("topic1", 0, 1, (0, 2), (2, )),
                1: PartitionMetadata("topic1", 1, 3, (0, 1), (0, 1))
            },
            "topic2": {
                0: PartitionMetadata("topic2", 0, 0, (), ())
            }
        }
        topic_errors = {"topic1": 0, "topic2": 1}
        partition_errors = {
            ("topic1", 0): 0,
            ("topic1", 1): 1,
            ("topic2", 0): 0
        }
        encoded = self._create_encoded_metadata_response(
            node_brokers, topic_partitions, topic_errors, partition_errors)
        decoded = KafkaProtocol.decode_metadata_response(encoded)
        self.assertEqual(decoded, (node_brokers, topic_partitions))
예제 #15
0
    def test_encode_offset_request__no_payload(self):
        expected = b"".join([
            struct.pack(">i", 65),            # Total length of the request

            struct.pack('>h', 2),             # Message type = offset fetch
            struct.pack('>h', 0),             # API version
            struct.pack('>i', 4),             # Correlation ID
            struct.pack('>h3s', 3, b"cid"),   # The client ID
            struct.pack('>i', -1),            # Replica Id
            struct.pack('>i', 1),             # Num topics
            struct.pack(">h6s", 6, b"topic1"),# Topic for the request
            struct.pack(">i", 2),             # Two partitions

            struct.pack(">i", 3),             # Partition 3
            struct.pack(">q", -1),            # No time offset
            struct.pack(">i", 1),             # One offset requested

            struct.pack(">i", 4),             # Partition 3
            struct.pack(">q", -1),            # No time offset
            struct.pack(">i", 1),             # One offset requested
        ])

        encoded = KafkaProtocol.encode_offset_request(b"cid", 4, [
            OffsetRequest(b'topic1', 3, -1, 1),
            OffsetRequest(b'topic1', 4, -1, 1),
        ])

        self.assertEqual(encoded, expected)
예제 #16
0
 def test_decode_message(self):
     encoded = "\xaa\xf1\x8f[\x00\x00\x00\x00\x00\x03key\x00\x00\x00\x04test"
     offset = 10
     (returned_offset, decoded_message) = \
         list(KafkaProtocol._decode_message(encoded, offset))[0]
     self.assertEqual(returned_offset, offset)
     self.assertEqual(decoded_message, create_message("test", "key"))
예제 #17
0
    def test_send_broker_unaware_request_fail(self):
        'Tests that call fails when all hosts are unavailable'

        mocked_conns = {
            ('kafka01', 9092): MagicMock(),
            ('kafka02', 9092): MagicMock()
        }

        # inject KafkaConnection side effects
        mocked_conns[('kafka01', 9092)].send.side_effect = RuntimeError("kafka01 went away (unittest)")
        mocked_conns[('kafka02', 9092)].send.side_effect = RuntimeError("Kafka02 went away (unittest)")

        def mock_get_conn(host, port):
            return mocked_conns[(host, port)]

        # patch to avoid making requests before we want it
        with patch.object(KafkaClient, 'load_metadata_for_topics'):
            with patch.object(KafkaClient, '_get_conn', side_effect=mock_get_conn):
                client = KafkaClient(hosts=['kafka01:9092', 'kafka02:9092'])

                req = KafkaProtocol.encode_metadata_request(b'client', 0)
                with self.assertRaises(KafkaUnavailableError):
                    client._send_broker_unaware_request(payloads=['fake request'],
                                                        encoder_fn=MagicMock(return_value='fake encoded message'),
                                                        decoder_fn=lambda x: x)

                for key, conn in six.iteritems(mocked_conns):
                    conn.send.assert_called_with(ANY, 'fake encoded message')
예제 #18
0
    def test_encode_offset_fetch_request(self):
        header = b"".join([
            struct.pack('>i', 69),               # Total message length
            struct.pack('>h', 9),                # Message type = offset fetch
            struct.pack('>h', 0),                # API version
            struct.pack('>i', 42),               # Correlation ID
            struct.pack('>h9s', 9, b"client_id"),# The client ID
            struct.pack('>h8s', 8, b"group_id"), # The group to commit for
            struct.pack('>i', 2),                # Num topics
        ])

        topic1 = b"".join([
            struct.pack(">h6s", 6, b"topic1"),   # Topic for the request
            struct.pack(">i", 2),                # Two partitions
            struct.pack(">i", 0),                # Partition 0
            struct.pack(">i", 1),                # Partition 1
        ])

        topic2 = b"".join([
            struct.pack(">h6s", 6, b"topic2"),   # Topic for the request
            struct.pack(">i", 1),                # One partitions
            struct.pack(">i", 2),                # Partition 2
        ])

        expected1 = b"".join([ header, topic1, topic2 ])
        expected2 = b"".join([ header, topic2, topic1 ])

        encoded = KafkaProtocol.encode_offset_fetch_request(b"client_id", 42, b"group_id", [
            OffsetFetchRequest(b"topic1", 0),
            OffsetFetchRequest(b"topic1", 1),
            OffsetFetchRequest(b"topic2", 2),
        ])

        self.assertIn(encoded, [ expected1, expected2 ])
예제 #19
0
    def test_send_broker_unaware_request_fail(self):
        'Tests that call fails when all hosts are unavailable'

        mocked_conns = {
            ('kafka01', 9092): MagicMock(),
            ('kafka02', 9092): MagicMock()
        }

        # inject KafkaConnection side effects
        mocked_conns[('kafka01', 9092)].send.side_effect = RuntimeError(
            "kafka01 went away (unittest)")
        mocked_conns[('kafka02', 9092)].send.side_effect = RuntimeError(
            "Kafka02 went away (unittest)")

        def mock_get_conn(host, port):
            return mocked_conns[(host, port)]

        # patch to avoid making requests before we want it
        with patch.object(KafkaClient, 'load_metadata_for_topics'):
            with patch.object(KafkaClient,
                              '_get_conn',
                              side_effect=mock_get_conn):
                client = KafkaClient(hosts=['kafka01:9092', 'kafka02:9092'])

                req = KafkaProtocol.encode_metadata_request(b'client', 0)
                with self.assertRaises(KafkaUnavailableError):
                    client._send_broker_unaware_request(
                        payloads=['fake request'],
                        encoder_fn=MagicMock(
                            return_value='fake encoded message'),
                        decoder_fn=lambda x: x)

                for key, conn in six.iteritems(mocked_conns):
                    conn.send.assert_called_with(ANY, 'fake encoded message')
예제 #20
0
    def test_decode_message_set_stop_iteration(self):
        encoded = "".join([
            struct.pack(">q", 0),  # MsgSet Offset
            struct.pack(">i", 18),  # Msg Size
            struct.pack(">i", 1474775406),  # CRC
            struct.pack(">bb", 0, 0),  # Magic, flags
            struct.pack(">i", 2),  # Length of key
            "k1",  # Key
            struct.pack(">i", 2),  # Length of value
            "v1",  # Value
            struct.pack(">q", 1),  # MsgSet Offset
            struct.pack(">i", 18),  # Msg Size
            struct.pack(">i", -16383415),  # CRC
            struct.pack(">bb", 0, 0),  # Magic, flags
            struct.pack(">i", 2),  # Length of key
            "k2",  # Key
            struct.pack(">i", 2),  # Length of value
            "v2",  # Value
            "@1$%(Y!",  # Random padding
        ])

        msgs = list(KafkaProtocol._decode_message_set_iter(encoded))
        self.assertEqual(len(msgs), 2)
        msg1, msg2 = msgs

        returned_offset1, decoded_message1 = msg1
        returned_offset2, decoded_message2 = msg2

        self.assertEqual(returned_offset1, 0)
        self.assertEqual(decoded_message1, create_message("v1", "k1"))

        self.assertEqual(returned_offset2, 1)
        self.assertEqual(decoded_message2, create_message("v2", "k2"))
예제 #21
0
 def test_decode_produce_response(self):
     t1 = "topic1"
     t2 = "topic2"
     encoded = struct.pack(
         ">iih%dsiihqihqh%dsiihq" % (len(t1), len(t2)),
         2,
         2,
         len(t1),
         t1,
         2,
         0,
         0,
         10L,
         1,
         1,
         20L,
         len(t2),
         t2,
         1,
         0,
         0,
         30L,
     )
     responses = list(KafkaProtocol.decode_produce_response(encoded))
     self.assertEqual(
         responses, [ProduceResponse(t1, 0, 0, 10L), ProduceResponse(t1, 1, 1, 20L), ProduceResponse(t2, 0, 0, 30L)]
     )
예제 #22
0
    def test_decode_offset_response(self):
        encoded = "".join([
            struct.pack(">i", 42),  # Correlation ID
            struct.pack(">i", 1),  # One topics
            struct.pack(">h6s", 6, "topic1"),  # First topic
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 2),  # Partition 2
            struct.pack(">h", 0),  # No error
            struct.pack(">i", 1),  # One offset
            struct.pack(">q", 4),  # Offset 4
            struct.pack(">i", 4),  # Partition 4
            struct.pack(">h", 0),  # No error
            struct.pack(">i", 1),  # One offset
            struct.pack(">q", 8),  # Offset 8
        ])

        results = KafkaProtocol.decode_offset_response(encoded)
        self.assertEqual(
            set(results),
            set([
                OffsetResponse(topic='topic1',
                               partition=2,
                               error=0,
                               offsets=(4, )),
                OffsetResponse(topic='topic1',
                               partition=4,
                               error=0,
                               offsets=(8, )),
            ]))
예제 #23
0
 def test_encode_message_set(self):
     message_set = [create_message("v1", "k1"), create_message("v2", "k2")]
     encoded = KafkaProtocol._encode_message_set(message_set)
     expect = ("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12W\xe7In\x00"
               "\x00\x00\x00\x00\x02k1\x00\x00\x00\x02v1\x00\x00\x00\x00"
               "\x00\x00\x00\x00\x00\x00\x00\x12\xff\x06\x02I\x00\x00\x00"
               "\x00\x00\x02k2\x00\x00\x00\x02v2")
     self.assertEqual(encoded, expect)
예제 #24
0
 def test_encode_message_set(self):
     message_set = [create_message("v1", "k1"), create_message("v2", "k2")]
     encoded = KafkaProtocol._encode_message_set(message_set)
     expect = ("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12W\xe7In\x00"
               "\x00\x00\x00\x00\x02k1\x00\x00\x00\x02v1\x00\x00\x00\x00"
               "\x00\x00\x00\x00\x00\x00\x00\x12\xff\x06\x02I\x00\x00\x00"
               "\x00\x00\x02k2\x00\x00\x00\x02v2")
     self.assertEqual(encoded, expect)
예제 #25
0
    def _load_metadata_for_topics(self, *topics):
        """
        Discover brokers and metadata for a set of topics. This method will
        recurse in the event of a retry.
        """
        requestId = self._next_id()
        request = KafkaProtocol.encode_metadata_request(
            self.client_id, requestId, topics)

        response = self._send_broker_unaware_request(requestId, request)
        if response is None:
            raise Exception("All servers failed to process request")

        (brokers, topics) = KafkaProtocol.decode_metadata_response(response)

        log.debug("Broker metadata: %s", brokers)
        log.debug("Topic metadata: %s", topics)

        self.brokers = brokers
        self.topics_to_brokers = {}

        for topic, partitions in topics.items():
            # Clear the list once before we add it. This removes stale entries
            # and avoids duplicates
            self.topic_partitions.pop(topic, None)

            if not partitions:
                log.warn(
                    "Partition is unassigned, delay for 1s and retry. Have you created {} on zookeeper?"
                    .format(topic))
                time.sleep(1)
                self._load_metadata_for_topics(topic)
                break

            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)
                else:
                    topic_part = TopicAndPartition(topic, partition)
                    self.topics_to_brokers[topic_part] = brokers[meta.leader]
                    self.topic_partitions[topic].append(partition)
예제 #26
0
    def test_encode_message_header(self):
        expect = "".join([
            struct.pack(">h", 10),  # API Key
            struct.pack(">h", 0),  # API Version
            struct.pack(">i", 4),  # Correlation Id
            struct.pack(">h", len("client3")),  # Length of clientId
            "client3",  # ClientId
        ])

        encoded = KafkaProtocol._encode_message_header("client3", 4, 10)
        self.assertEqual(encoded, expect)
예제 #27
0
 def test_decode_produce_response(self):
     t1 = "topic1"
     t2 = "topic2"
     encoded = struct.pack('>iih%dsiihqihqh%dsiihq' % (len(t1), len(t2)),
                           2, 2, len(t1), compat.bytes(t1), 2, 0, 0, compat.long(10), 1, 1, compat.long(20),
                           len(t2), compat.bytes(t2), 1, 0, 0, compat.long(30))
     responses = list(KafkaProtocol.decode_produce_response(encoded))
     self.assertEqual(responses,
                      [ProduceResponse(t1, 0, 0, compat.long(10)),
                       ProduceResponse(t1, 1, 1, compat.long(20)),
                       ProduceResponse(t2, 0, 0, compat.long(30))])
예제 #28
0
    def test_encode_message_header(self):
        expect = b"".join([
            struct.pack(">h", 10),             # API Key
            struct.pack(">h", 0),              # API Version
            struct.pack(">i", 4),              # Correlation Id
            struct.pack(">h", len("client3")), # Length of clientId
            b"client3",                         # ClientId
        ])

        encoded = KafkaProtocol._encode_message_header(b"client3", 4, 10)
        self.assertEqual(encoded, expect)
예제 #29
0
파일: client.py 프로젝트: duanhongyi/kakfa
    def _load_metadata_for_topics(self, *topics):
        """
        Discover brokers and metadata for a set of topics. This method will
        recurse in the event of a retry.
        """
        request_id = self._next_id()
        request = KafkaProtocol.encode_metadata_request(self.client_id,
                                                        request_id, topics)

        response = self._send_broker_unaware_request(request_id, request)
        if response is None:
            raise Exception("All servers failed to process request")

        (brokers, topics) = KafkaProtocol.decode_metadata_response(response)

        log.debug("Broker metadata: %s", brokers)
        log.debug("Topic metadata: %s", topics)

        self.brokers = brokers
        self.topics_to_brokers = {}

        for topic, partitions in topics.items():
            # Clear the list once before we add it. This removes stale entries
            # and avoids duplicates
            self.topic_partitions.pop(topic, None)

            if not partitions:
                log.info("Partition is unassigned, delay for 1s and retry")
                time.sleep(1)
                self._load_metadata_for_topics(topic)
                break

            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)
                else:
                    topic_part = TopicAndPartition(topic, partition)
                    self.topics_to_brokers[topic_part] = brokers[meta.leader]
                    self.topic_partitions[topic].append(partition)
예제 #30
0
 def test_encode_fetch_request(self):
     requests = [FetchRequest("topic1", 0, 10, 1024),
                 FetchRequest("topic2", 1, 20, 100)]
     expect = ('\x00\x00\x00Y\x00\x01\x00\x00\x00\x00\x00\x03\x00\x07'
               'client1\xff\xff\xff\xff\x00\x00\x00\x02\x00\x00\x00d\x00'
               '\x00\x00\x02\x00\x06topic1\x00\x00\x00\x01\x00\x00\x00\x00'
               '\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x04\x00\x00\x06'
               'topic2\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00'
               '\x00\x00\x14\x00\x00\x00d')
     encoded = KafkaProtocol.encode_fetch_request("client1", 3, requests, 2,
                                                  100)
     self.assertEqual(encoded, expect)
예제 #31
0
 def test_decode_produce_response(self):
     t1 = "topic1"
     t2 = "topic2"
     encoded = struct.pack('>iih%dsiihqihqh%dsiihq' % (len(t1), len(t2)),
                           2, 2, len(t1), t1, 2, 0, 0, 10L, 1, 1, 20L,
                           len(t2), t2, 1, 0, 0, 30L)
     responses = list(KafkaProtocol.decode_produce_response(encoded))
     self.assertEqual(responses, [
         ProduceResponse(t1, 0, 0, 10L),
         ProduceResponse(t1, 1, 1, 20L),
         ProduceResponse(t2, 0, 0, 30L)
     ])
예제 #32
0
    def test_encode_message(self):
        message = create_message(b"test", b"key")
        encoded = KafkaProtocol._encode_message(message)
        expect = b"".join([
            struct.pack(">i", -1427009701), # CRC
            struct.pack(">bb", 0, 0),       # Magic, flags
            struct.pack(">i", 3),           # Length of key
            b"key",                          # key
            struct.pack(">i", 4),           # Length of value
            b"test",                         # value
        ])

        self.assertEqual(encoded, expect)
예제 #33
0
    def test_encode_metadata_request_no_topics(self):
        expected = b"".join([
            struct.pack(">i", 17),         # Total length of the request
            struct.pack('>h', 3),          # API key metadata fetch
            struct.pack('>h', 0),          # API version
            struct.pack('>i', 4),          # Correlation ID
            struct.pack('>h3s', 3, b"cid"),# The client ID
            struct.pack('>i', 0),          # No topics, give all the data!
        ])

        encoded = KafkaProtocol.encode_metadata_request(b"cid", 4)

        self.assertEqual(encoded, expected)
예제 #34
0
    def test_encode_metadata_request_no_topics(self):
        expected = "".join([
            struct.pack(">i", 17),  # Total length of the request
            struct.pack('>h', 3),  # API key metadata fetch
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 4),  # Correlation ID
            struct.pack('>h3s', 3, "cid"),  # The client ID
            struct.pack('>i', 0),  # No topics, give all the data!
        ])

        encoded = KafkaProtocol.encode_metadata_request("cid", 4)

        self.assertEqual(encoded, expected)
예제 #35
0
    def test_decode_consumer_metadata_response(self):
        encoded = b"".join([
            struct.pack(">i", 42),                                 # Correlation ID
            struct.pack(">h", 0),                                  # No Error
            struct.pack(">i", 1),                                  # Broker ID
            struct.pack(">h23s", 23, b"brokers1.kafka.rdio.com"),  # Broker Host
            struct.pack(">i", 1000),                               # Broker Port
        ])

        results = KafkaProtocol.decode_consumer_metadata_response(encoded)
        self.assertEqual(results,
            ConsumerMetadataResponse(error = 0, nodeId = 1, host = b'brokers1.kafka.rdio.com', port = 1000)
        )
예제 #36
0
 def test_decode_produce_response(self):
     t1 = "topic1"
     t2 = "topic2"
     encoded = struct.pack('>iih%dsiihqihqh%dsiihq' % (len(t1), len(t2)), 2,
                           2, len(t1), compat.bytes(t1), 2, 0, 0,
                           compat.long(10), 1, 1, compat.long(20), len(t2),
                           compat.bytes(t2), 1, 0, 0, compat.long(30))
     responses = list(KafkaProtocol.decode_produce_response(encoded))
     self.assertEqual(responses, [
         ProduceResponse(t1, 0, 0, compat.long(10)),
         ProduceResponse(t1, 1, 1, compat.long(20)),
         ProduceResponse(t2, 0, 0, compat.long(30))
     ])
예제 #37
0
    def test_encode_consumer_metadata_request(self):
        expected = b"".join([
            struct.pack(">i", 17),         # Total length of the request
            struct.pack('>h', 10),         # API key consumer metadata
            struct.pack('>h', 0),          # API version
            struct.pack('>i', 4),          # Correlation ID
            struct.pack('>h3s', 3, b"cid"),# The client ID
            struct.pack('>h2s', 2, b"g1"), # Group "g1"
        ])

        encoded = KafkaProtocol.encode_consumer_metadata_request(b"cid", 4, b"g1")

        self.assertEqual(encoded, expected)
예제 #38
0
    def test_encode_message(self):
        message = create_message("test", "key")
        encoded = KafkaProtocol._encode_message(message)
        expect = "".join([
            struct.pack(">i", -1427009701),  # CRC
            struct.pack(">bb", 0, 0),  # Magic, flags
            struct.pack(">i", 3),  # Length of key
            "key",  # key
            struct.pack(">i", 4),  # Length of value
            "test",  # value
        ])

        self.assertEqual(encoded, expect)
예제 #39
0
 def test_decode_message_snappy(self):
     snappy_encoded = ('\xec\x80\xa1\x95\x00\x02\xff\xff\xff\xff\x00\x00'
                       '\x00,8\x00\x00\x19\x01@\x10L\x9f[\xc2\x00\x00\xff'
                       '\xff\xff\xff\x00\x00\x00\x02v1\x19\x1bD\x00\x10\xd5'
                       '\x96\nx\x00\x00\xff\xff\xff\xff\x00\x00\x00\x02v2')
     offset = 11
     decoded = list(KafkaProtocol._decode_message(snappy_encoded, offset))
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #40
0
 def test_decode_message_set_stop_iteration(self):
     encoded = ('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10L\x9f[\xc2'
                '\x00\x00\xff\xff\xff\xff\x00\x00\x00\x02v1\x00\x00\x00\x00'
                '\x00\x00\x00\x00\x00\x00\x00\x10\xd5\x96\nx\x00\x00\xff'
                '\xff\xff\xff\x00\x00\x00\x02v2')
     iter = KafkaProtocol._decode_message_set_iter(encoded + "@#$%(Y!")
     decoded = list(iter)
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #41
0
 def test_decode_produce_response(self):
     t1 = b"topic1"
     t2 = b"topic2"
     _long = int
     if six.PY2:
         _long = long
     encoded = struct.pack('>iih%dsiihqihqh%dsiihq' % (len(t1), len(t2)),
                           2, 2, len(t1), t1, 2, 0, 0, _long(10), 1, 1, _long(20),
                           len(t2), t2, 1, 0, 0, _long(30))
     responses = list(KafkaProtocol.decode_produce_response(encoded))
     self.assertEqual(responses,
                      [ProduceResponse(t1, 0, 0, _long(10)),
                       ProduceResponse(t1, 1, 1, _long(20)),
                       ProduceResponse(t2, 0, 0, _long(30))])
예제 #42
0
 def test_encode_fetch_request(self):
     requests = [
         FetchRequest("topic1", 0, 10, 1024),
         FetchRequest("topic2", 1, 20, 100)
     ]
     expect = ('\x00\x00\x00Y\x00\x01\x00\x00\x00\x00\x00\x03\x00\x07'
               'client1\xff\xff\xff\xff\x00\x00\x00\x02\x00\x00\x00d\x00'
               '\x00\x00\x02\x00\x06topic1\x00\x00\x00\x01\x00\x00\x00\x00'
               '\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x04\x00\x00\x06'
               'topic2\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00'
               '\x00\x00\x14\x00\x00\x00d')
     encoded = KafkaProtocol.encode_fetch_request("client1", 3, requests, 2,
                                                  100)
     self.assertEqual(encoded, expect)
예제 #43
0
 def test_decode_message_snappy(self):
     snappy_encoded = ('\xec\x80\xa1\x95\x00\x02\xff\xff\xff\xff\x00\x00'
                       '\x00,8\x00\x00\x19\x01@\x10L\x9f[\xc2\x00\x00\xff'
                       '\xff\xff\xff\x00\x00\x00\x02v1\x19\x1bD\x00\x10\xd5'
                       '\x96\nx\x00\x00\xff\xff\xff\xff\x00\x00\x00\x02v2')
     offset = 11
     decoded = list(KafkaProtocol._decode_message(snappy_encoded, offset))
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #44
0
    def test_encode_offset_request(self):
        expected = b"".join([
            struct.pack(">i", 21),         # Total length of the request
            struct.pack('>h', 2),          # Message type = offset fetch
            struct.pack('>h', 0),          # API version
            struct.pack('>i', 4),          # Correlation ID
            struct.pack('>h3s', 3, b"cid"), # The client ID
            struct.pack('>i', -1),         # Replica Id
            struct.pack('>i', 0),          # No topic/partitions
        ])

        encoded = KafkaProtocol.encode_offset_request(b"cid", 4)

        self.assertEqual(encoded, expected)
예제 #45
0
 def test_decode_message_set_stop_iteration(self):
     encoded = ('\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10L\x9f[\xc2'
                '\x00\x00\xff\xff\xff\xff\x00\x00\x00\x02v1\x00\x00\x00\x00'
                '\x00\x00\x00\x00\x00\x00\x00\x10\xd5\x96\nx\x00\x00\xff'
                '\xff\xff\xff\x00\x00\x00\x02v2')
     iter = KafkaProtocol._decode_message_set_iter(encoded + "@#$%(Y!")
     decoded = list(iter)
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #46
0
    def test_encode_offset_request(self):
        expected = "".join([
            struct.pack(">i", 21),  # Total length of the request
            struct.pack('>h', 2),  # Message type = offset fetch
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 4),  # Correlation ID
            struct.pack('>h3s', 3, "cid"),  # The client ID
            struct.pack('>i', -1),  # Replica Id
            struct.pack('>i', 0),  # No topic/partitions
        ])

        encoded = KafkaProtocol.encode_offset_request("cid", 4)

        self.assertEqual(encoded, expected)
예제 #47
0
 def test_decode_message_gzip(self):
     gzip_encoded = ('\xc0\x11\xb2\xf0\x00\x01\xff\xff\xff\xff\x00\x00\x000'
                     '\x1f\x8b\x08\x00\xa1\xc1\xc5R\x02\xffc`\x80\x03\x01'
                     '\x9f\xf9\xd1\x87\x18\x18\xfe\x03\x01\x90\xc7Tf\xc8'
                     '\x80$wu\x1aW\x05\x92\x9c\x11\x00z\xc0h\x888\x00\x00'
                     '\x00')
     offset = 11
     decoded = list(KafkaProtocol._decode_message(gzip_encoded, offset))
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #48
0
    def test_decode_message(self):
        encoded = b"".join([
            struct.pack(">i", -1427009701), # CRC
            struct.pack(">bb", 0, 0),       # Magic, flags
            struct.pack(">i", 3),           # Length of key
            b"key",                         # key
            struct.pack(">i", 4),           # Length of value
            b"test",                        # value
        ])

        offset = 10
        (returned_offset, decoded_message) = list(KafkaProtocol._decode_message(encoded, offset))[0]

        self.assertEqual(returned_offset, offset)
        self.assertEqual(decoded_message, create_message(b"test", b"key"))
예제 #49
0
 def test_decode_message_gzip(self):
     gzip_encoded = ('\xc0\x11\xb2\xf0\x00\x01\xff\xff\xff\xff\x00\x00\x000'
                     '\x1f\x8b\x08\x00\xa1\xc1\xc5R\x02\xffc`\x80\x03\x01'
                     '\x9f\xf9\xd1\x87\x18\x18\xfe\x03\x01\x90\xc7Tf\xc8'
                     '\x80$wu\x1aW\x05\x92\x9c\x11\x00z\xc0h\x888\x00\x00'
                     '\x00')
     offset = 11
     decoded = list(KafkaProtocol._decode_message(gzip_encoded, offset))
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #50
0
    def test_encode_metadata_request_with_topics(self):
        expected = b"".join([
            struct.pack(">i", 25),         # Total length of the request
            struct.pack('>h', 3),          # API key metadata fetch
            struct.pack('>h', 0),          # API version
            struct.pack('>i', 4),          # Correlation ID
            struct.pack('>h3s', 3, b"cid"),# The client ID
            struct.pack('>i', 2),          # Number of topics in the request
            struct.pack('>h2s', 2, b"t1"), # Topic "t1"
            struct.pack('>h2s', 2, b"t2"), # Topic "t2"
        ])

        encoded = KafkaProtocol.encode_metadata_request(b"cid", 4, [b"t1", b"t2"])

        self.assertEqual(encoded, expected)
예제 #51
0
    def test_encode_metadata_request_with_topics(self):
        expected = "".join([
            struct.pack(">i", 25),  # Total length of the request
            struct.pack('>h', 3),  # API key metadata fetch
            struct.pack('>h', 0),  # API version
            struct.pack('>i', 4),  # Correlation ID
            struct.pack('>h3s', 3, "cid"),  # The client ID
            struct.pack('>i', 2),  # Number of topics in the request
            struct.pack('>h2s', 2, "t1"),  # Topic "t1"
            struct.pack('>h2s', 2, "t2"),  # Topic "t2"
        ])

        encoded = KafkaProtocol.encode_metadata_request("cid", 4, ["t1", "t2"])

        self.assertEqual(encoded, expected)
예제 #52
0
 def test_decode_message_set(self):
     encoded = (
         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10L\x9f[\xc2"
         "\x00\x00\xff\xff\xff\xff\x00\x00\x00\x02v1\x00\x00\x00\x00"
         "\x00\x00\x00\x00\x00\x00\x00\x10\xd5\x96\nx\x00\x00\xff"
         "\xff\xff\xff\x00\x00\x00\x02v2"
     )
     iter = KafkaProtocol._decode_message_set_iter(encoded)
     decoded = list(iter)
     self.assertEqual(len(decoded), 2)
     (returned_offset1, decoded_message1) = decoded[0]
     self.assertEqual(returned_offset1, 0)
     self.assertEqual(decoded_message1, create_message("v1"))
     (returned_offset2, decoded_message2) = decoded[1]
     self.assertEqual(returned_offset2, 0)
     self.assertEqual(decoded_message2, create_message("v2"))
예제 #53
0
 def test_encode_produce_request(self):
     requests = [ProduceRequest("topic1", 0, [create_message("a"),
                                              create_message("b")]),
                 ProduceRequest("topic2", 1, [create_message("c")])]
     expect = ('\x00\x00\x00\x94\x00\x00\x00\x00\x00\x00\x00\x02\x00\x07'
               'client1\x00\x02\x00\x00\x00d\x00\x00\x00\x02\x00\x06topic1'
               '\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x006\x00\x00\x00'
               '\x00\x00\x00\x00\x00\x00\x00\x00\x0fQ\xdf:2\x00\x00\xff\xff'
               '\xff\xff\x00\x00\x00\x01a\x00\x00\x00\x00\x00\x00\x00\x00'
               '\x00\x00\x00\x0f\xc8\xd6k\x88\x00\x00\xff\xff\xff\xff\x00'
               '\x00\x00\x01b\x00\x06topic2\x00\x00\x00\x01\x00\x00\x00\x01'
               '\x00\x00\x00\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
               '\x00\x0f\xbf\xd1[\x1e\x00\x00\xff\xff\xff\xff\x00\x00\x00'
               '\x01c')
     encoded = KafkaProtocol.encode_produce_request("client1", 2, requests,
                                                    2, 100)
     self.assertEqual(encoded, expect)
예제 #54
0
    def test_encode_offset_commit_request_kafka(self):

        header = b"".join([
            struct.pack('>i', 113),              # Total message length

            struct.pack('>h', 8),                # Message type = offset commit
            struct.pack('>h', 2),                # API version
            struct.pack('>i', 42),               # Correlation ID
            struct.pack('>h9s', 9, b"client_id"),# The client ID
            struct.pack('>h8s', 8, b"group_id"), # The group to commit for
            struct.pack('>i', -1),               # Consumer group generation id
            struct.pack(">h0s", 0, b""),         # Consumer id
            struct.pack('>q', -1),               # Retention time
            struct.pack('>i', 2),                # Num topics
        ])

        topic1 = b"".join([
            struct.pack(">h6s", 6, b"topic1"),   # Topic for the request
            struct.pack(">i", 2),                # Two partitions
            struct.pack(">i", 0),                # Partition 0
            struct.pack(">q", 123),              # Offset 123
            struct.pack(">h", -1),               # Null metadata
            struct.pack(">i", 1),                # Partition 1
            struct.pack(">q", 234),              # Offset 234
            struct.pack(">h", -1),               # Null metadata
        ])

        topic2 = b"".join([
            struct.pack(">h6s", 6, b"topic2"),   # Topic for the request
            struct.pack(">i", 1),                # One partition
            struct.pack(">i", 2),                # Partition 2
            struct.pack(">q", 345),              # Offset 345
            struct.pack(">h", -1),               # Null metadata
        ])

        expected1 = b"".join([ header, topic1, topic2 ])
        expected2 = b"".join([ header, topic2, topic1 ])

        encoded = KafkaProtocol.encode_offset_commit_request_kafka(b"client_id", 42, b"group_id", [
            OffsetCommitRequest(b"topic1", 0, 123, None),
            OffsetCommitRequest(b"topic1", 1, 234, None),
            OffsetCommitRequest(b"topic2", 2, 345, None),
        ])

        self.assertIn(encoded, [ expected1, expected2 ])
예제 #55
0
    def test_encode_offset_commit_request_kafka(self):

        header = b"".join([
            struct.pack('>i', 113),  # Total message length
            struct.pack('>h', 8),  # Message type = offset commit
            struct.pack('>h', 2),  # API version
            struct.pack('>i', 42),  # Correlation ID
            struct.pack('>h9s', 9, b"client_id"),  # The client ID
            struct.pack('>h8s', 8, b"group_id"),  # The group to commit for
            struct.pack('>i', -1),  # Consumer group generation id
            struct.pack(">h0s", 0, b""),  # Consumer id
            struct.pack('>q', -1),  # Retention time
            struct.pack('>i', 2),  # Num topics
        ])

        topic1 = b"".join([
            struct.pack(">h6s", 6, b"topic1"),  # Topic for the request
            struct.pack(">i", 2),  # Two partitions
            struct.pack(">i", 0),  # Partition 0
            struct.pack(">q", 123),  # Offset 123
            struct.pack(">h", -1),  # Null metadata
            struct.pack(">i", 1),  # Partition 1
            struct.pack(">q", 234),  # Offset 234
            struct.pack(">h", -1),  # Null metadata
        ])

        topic2 = b"".join([
            struct.pack(">h6s", 6, b"topic2"),  # Topic for the request
            struct.pack(">i", 1),  # One partition
            struct.pack(">i", 2),  # Partition 2
            struct.pack(">q", 345),  # Offset 345
            struct.pack(">h", -1),  # Null metadata
        ])

        expected1 = b"".join([header, topic1, topic2])
        expected2 = b"".join([header, topic2, topic1])

        encoded = KafkaProtocol.encode_offset_commit_request_kafka(
            b"client_id", 42, b"group_id", [
                OffsetCommitRequest(b"topic1", 0, 123, None),
                OffsetCommitRequest(b"topic1", 1, 234, None),
                OffsetCommitRequest(b"topic2", 2, 345, None),
            ])

        self.assertIn(encoded, [expected1, expected2])