Пример #1
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, )),
            ]))
Пример #2
0
    def send_offset_request(
        self,
        payloads=None,
        fail_on_error=True,
        callback=None
    ):
        if payloads is None:
            payloads = []

        resps = []
        for req in payloads:
            if req.time == -1:
                offset = self.high_offsets[req.topic.decode()].get(req.partition, -1)
            else:
                offset = self.low_offsets[req.topic.decode()].get(req.partition, -1)
            if self.offset_request_error:
                error_code = NotLeaderForPartitionError.errno
            elif req.partition not in self.topics[req.topic.decode()]:
                error_code = UnknownTopicOrPartitionError.errno
            else:
                error_code = 0
            resps.append(OffsetResponse(
                req.topic.decode(),
                req.partition,
                error_code,
                (offset,)
            ))

        return [resp if not callback else callback(resp) for resp in resps]
Пример #3
0
def _check_fetch_response_error(resp):
    try:
        check_error(resp)
    except BrokerResponseError:
        # In case of error we set the offset to (-1,)
        return OffsetResponse(
            resp.topic,
            resp.partition,
            resp.error,
            (-1, ),
        )
    return resp
Пример #4
0
    def decode_offset_response(cls, data):
        """
        Decode bytes to an OffsetResponse

        Arguments:
            data: bytes to decode
        """
        ((correlation_id, num_topics), cur) = relative_unpack('>ii', data, 0)

        for i in range(num_topics):
            (topic, cur) = read_short_string(data, cur)
            ((num_partitions,), cur) = relative_unpack('>i', data, cur)

            for i in range(num_partitions):
                ((partition, error, num_offsets,), cur) = \
                    relative_unpack('>ihi', data, cur)

                offsets = []
                for j in range(num_offsets):
                    ((offset,), cur) = relative_unpack('>q', data, cur)
                    offsets.append(offset)

                yield OffsetResponse(topic, partition, error, tuple(offsets))