當前位置: 首頁>>代碼示例>>Python>>正文


Python AIOKafkaClient.load_metadata_for_topics方法代碼示例

本文整理匯總了Python中aiokafka.client.AIOKafkaClient.load_metadata_for_topics方法的典型用法代碼示例。如果您正苦於以下問題:Python AIOKafkaClient.load_metadata_for_topics方法的具體用法?Python AIOKafkaClient.load_metadata_for_topics怎麽用?Python AIOKafkaClient.load_metadata_for_topics使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在aiokafka.client.AIOKafkaClient的用法示例。


在下文中一共展示了AIOKafkaClient.load_metadata_for_topics方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_get_leader_for_unassigned_partitions

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_get_leader_for_unassigned_partitions(self, protocol):
        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata("topic_no_partitions", NO_LEADER, []),
            TopicMetadata("topic_unknown", UNKNOWN_TOPIC_OR_PARTITION, []),
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        self.assertDictEqual({}, client._topics_to_brokers)

        with self.assertRaises(LeaderNotAvailableError):
            self.loop.run_until_complete(client._get_leader_for_partition("topic_no_partitions", 0))

        with self.assertRaises(UnknownTopicOrPartitionError):
            self.loop.run_until_complete(client._get_leader_for_partition("topic_unknown", 0))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:29,代碼來源:test_client.py

示例2: test_send_produce_request_raises_when_noleader

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_send_produce_request_raises_when_noleader(self, protocol):
        """Send producer request raises LeaderNotAvailableError
           if leader is not available"""

        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata(
                "topic_noleader",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleader", 0, -1, [], [], NO_LEADER),
                    PartitionMetadata("topic_noleader", 1, -1, [], [], NO_LEADER),
                ],
            )
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        requests = [ProduceRequest("topic_noleader", 0, [create_message("a"), create_message("b")])]

        with self.assertRaises(LeaderNotAvailableError):
            self.loop.run_until_complete(client.send_produce_request(requests))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:35,代碼來源:test_client.py

示例3: test_ensure_topic_exists

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_ensure_topic_exists(self, protocol):

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        fut = asyncio.Future(loop=self.loop)
        fut.set_result(b"response")
        mocked_conns[("broker_1", 4567)].send.return_value = fut
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata("topic_still_creating", NO_LEADER, []),
            TopicMetadata("topic_doesnt_exist", UNKNOWN_TOPIC_OR_PARTITION, []),
            TopicMetadata(
                "topic_noleaders",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleaders", 0, -1, [], [], NO_LEADER),
                    PartitionMetadata("topic_noleaders", 1, -1, [], [], NO_LEADER),
                ],
            ),
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        with self.assertRaises(UnknownTopicOrPartitionError):
            self.loop.run_until_complete(client.ensure_topic_exists("topic_doesnt_exist", timeout=1))

        with self.assertRaises(KafkaTimeoutError):
            self.loop.run_until_complete(client.ensure_topic_exists("topic_still_creating", timeout=1))

        # This should not raise
        self.loop.run_until_complete(client.ensure_topic_exists("topic_noleaders", timeout=1))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:37,代碼來源:test_client.py

示例4: test_has_metadata_for_topic

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_has_metadata_for_topic(self, protocol):
        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata("topic_still_creating", NO_LEADER, []),
            TopicMetadata("topic_doesnt_exist", UNKNOWN_TOPIC_OR_PARTITION, []),
            TopicMetadata(
                "topic_noleaders",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleaders", 0, -1, [], [], NO_LEADER),
                    PartitionMetadata("topic_noleaders", 1, -1, [], [], NO_LEADER),
                ],
            ),
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        # Topics with no partitions return False
        self.assertFalse(client.has_metadata_for_topic("topic_still_creating"))
        self.assertFalse(client.has_metadata_for_topic("topic_doesnt_exist"))

        # Topic with partition metadata, but no leaders return True
        self.assertTrue(client.has_metadata_for_topic("topic_noleaders"))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:36,代碼來源:test_client.py

示例5: test_get_leader_exceptions_when_noleader

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_get_leader_exceptions_when_noleader(self, protocol):
        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata(
                "topic_noleader",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleader", 0, -1, [], [], NO_LEADER),
                    PartitionMetadata("topic_noleader", 1, -1, [], [], NO_LEADER),
                ],
            )
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())
        self.assertDictEqual(
            {TopicAndPartition("topic_noleader", 0): None, TopicAndPartition("topic_noleader", 1): None},
            client._topics_to_brokers,
        )

        # No leader partitions -- raise LeaderNotAvailableError
        with self.assertRaises(LeaderNotAvailableError):
            self.assertIsNone(self.loop.run_until_complete(client._get_leader_for_partition("topic_noleader", 0)))
        with self.assertRaises(LeaderNotAvailableError):
            self.assertIsNone(self.loop.run_until_complete(client._get_leader_for_partition("topic_noleader", 1)))

        # Unknown partitions -- raise UnknownTopicOrPartitionError
        with self.assertRaises(UnknownTopicOrPartitionError):
            self.assertIsNone(self.loop.run_until_complete(client._get_leader_for_partition("topic_noleader", 2)))

        topics = [
            TopicMetadata(
                "topic_noleader",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleader", 0, 0, [0, 1], [0, 1], NO_ERROR),
                    PartitionMetadata("topic_noleader", 1, 1, [1, 0], [1, 0], NO_ERROR),
                ],
            )
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)
        self.assertEqual(
            brokers[0], self.loop.run_until_complete(client._get_leader_for_partition("topic_noleader", 0))
        )
        self.assertEqual(
            brokers[1], self.loop.run_until_complete(client._get_leader_for_partition("topic_noleader", 1))
        )
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:59,代碼來源:test_client.py

示例6: test_send_produce_request_raises_when_topic_unknown

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_send_produce_request_raises_when_topic_unknown(self, protocol):
        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [TopicMetadata("topic_doesnt_exist", UNKNOWN_TOPIC_OR_PARTITION, [])]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        requests = [ProduceRequest("topic_doesnt_exist", 0, [create_message("a"), create_message("b")])]

        with self.assertRaises(UnknownTopicOrPartitionError):
            self.loop.run_until_complete(client.send_produce_request(requests))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:23,代碼來源:test_client.py

示例7: test_get_leader_for_partitions_reloads_metadata

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_get_leader_for_partitions_reloads_metadata(self, protocol):
        "Get leader for partitions reload metadata if it is not available"

        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [TopicMetadata("topic_no_partitions", NO_LEADER, [])]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())

        # topic metadata is loaded but empty
        self.assertDictEqual({}, client._topics_to_brokers)

        topics = [
            TopicMetadata(
                "topic_one_partition",
                NO_ERROR,
                [PartitionMetadata("topic_no_partition", 0, 0, [0, 1], [0, 1], NO_ERROR)],
            )
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        # calling _get_leader_for_partition (from any broker aware request)
        # will try loading metadata again for the same topic
        leader = self.loop.run_until_complete(client._get_leader_for_partition("topic_one_partition", 0))

        self.assertEqual(brokers[0], leader)
        self.assertDictEqual({TopicAndPartition("topic_one_partition", 0): brokers[0]}, client._topics_to_brokers)
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:39,代碼來源:test_client.py

示例8: test_load_metadata

# 需要導入模塊: from aiokafka.client import AIOKafkaClient [as 別名]
# 或者: from aiokafka.client.AIOKafkaClient import load_metadata_for_topics [as 別名]
    def test_load_metadata(self, protocol):
        @asyncio.coroutine
        def recv(request_id):
            return b"response"

        mocked_conns = {("broker_1", 4567): mock.MagicMock()}
        mocked_conns[("broker_1", 4567)].recv.side_effect = recv
        client = AIOKafkaClient(["broker_1:4567"], loop=self.loop)
        client._conns = mocked_conns

        brokers = [BrokerMetadata(0, "broker_1", 4567), BrokerMetadata(1, "broker_2", 5678)]

        topics = [
            TopicMetadata("topic_1", NO_ERROR, [PartitionMetadata("topic_1", 0, 1, [1, 2], [1, 2], NO_ERROR)]),
            TopicMetadata(
                "topic_noleader",
                NO_ERROR,
                [
                    PartitionMetadata("topic_noleader", 0, -1, [], [], NO_LEADER),
                    PartitionMetadata("topic_noleader", 1, -1, [], [], NO_LEADER),
                ],
            ),
            TopicMetadata("topic_no_partitions", NO_LEADER, []),
            TopicMetadata("topic_unknown", UNKNOWN_TOPIC_OR_PARTITION, []),
            TopicMetadata(
                "topic_3",
                NO_ERROR,
                [
                    PartitionMetadata("topic_3", 0, 0, [0, 1], [0, 1], NO_ERROR),
                    PartitionMetadata("topic_3", 1, 1, [1, 0], [1, 0], NO_ERROR),
                    PartitionMetadata("topic_3", 2, 0, [0, 1], [0, 1], NO_ERROR),
                ],
            ),
            TopicMetadata(
                "topic_4",
                NO_ERROR,
                [
                    PartitionMetadata("topic_4", 0, 0, [0, 1], [0, 1], NO_ERROR),
                    PartitionMetadata("topic_4", 1, 1, [1, 0], [1, 0], REPLICA_NOT_AVAILABLE),
                ],
            ),
        ]
        protocol.decode_metadata_response.return_value = MetadataResponse(brokers, topics)

        self.loop.run_until_complete(client.load_metadata_for_topics())
        self.assertDictEqual(
            {
                TopicAndPartition("topic_1", 0): brokers[1],
                TopicAndPartition("topic_noleader", 0): None,
                TopicAndPartition("topic_noleader", 1): None,
                TopicAndPartition("topic_3", 0): brokers[0],
                TopicAndPartition("topic_3", 1): brokers[1],
                TopicAndPartition("topic_3", 2): brokers[0],
                TopicAndPartition("topic_4", 0): brokers[0],
                TopicAndPartition("topic_4", 1): brokers[1],
            },
            client._topics_to_brokers,
        )

        # if we ask for metadata explicitly, it should raise errors
        with self.assertRaises(LeaderNotAvailableError):
            self.loop.run_until_complete(client.load_metadata_for_topics("topic_no_partitions"))

        with self.assertRaises(UnknownTopicOrPartitionError):
            self.loop.run_until_complete(client.load_metadata_for_topics("topic_unknown"))

        # This should not raise
        self.loop.run_until_complete(client.load_metadata_for_topics("topic_no_leader"))

        # This should not raise ReplicaNotAvailableError
        self.loop.run_until_complete(client.load_metadata_for_topics("topic_4"))
開發者ID:thedrow,項目名稱:aiokafka,代碼行數:73,代碼來源:test_client.py


注:本文中的aiokafka.client.AIOKafkaClient.load_metadata_for_topics方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。