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


Python common.KafkaUnavailableError方法代碼示例

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


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

示例1: get_watermark_for_topic

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_watermark_for_topic(
    kafka_client,
    topic,
):
    """This method:
        * refreshes metadata for the kafka client
        * fetches watermarks

    :param kafka_client: KafkaToolClient instance
    :param topic: the topic
    :returns: dict <topic>: [ConsumerPartitionOffsets]
    """
    # Refresh client metadata. We do not use the topic list, because we
    # don't want to accidentally create the topic if it does not exist.
    # If Kafka is unavailable, let's retry loading client metadata
    try:
        kafka_client.load_metadata_for_topics()
    except KafkaUnavailableError:
        kafka_client.load_metadata_for_topics()

    watermarks = get_topics_watermarks(
        kafka_client, [topic]
    )
    return watermarks 
開發者ID:Yelp,項目名稱:kafka-utils,代碼行數:26,代碼來源:monitoring.py

示例2: get_kafka_connection

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_kafka_connection(cls, timeout_seconds=15):
        """Returns a kafka connection, waiting timeout_seconds for the container
        to come up.

        Args:
            timeout_seconds: Retry time (seconds) to get a kafka connection
        """
        end_time = time.time() + timeout_seconds
        logger.info("Getting connection to Kafka container on yocalhost")
        while end_time > time.time():
            try:
                return KafkaClient(get_config().cluster_config.broker_list)
            except KafkaUnavailableError:
                logger.info("Kafka not yet available, waiting...")
                time.sleep(0.1)
        raise KafkaUnavailableError() 
開發者ID:Yelp,項目名稱:data_pipeline,代碼行數:18,代碼來源:containers.py

示例3: get_watermark_for_regex

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_watermark_for_regex(
    kafka_client,
    topic_regex,
):
    """This method:
        * refreshes metadata for the kafka client
        * fetches watermarks

    :param kafka_client: KafkaToolClient instance
    :param topic: the topic regex
    :returns: dict <topic>: [ConsumerPartitionOffsets]
    """
    # Refresh client metadata. We do not use the topic list, because we
    # don't want to accidentally create the topic if it does not exist.
    # If Kafka is unavailable, let's retry loading client metadata
    try:
        kafka_client.load_metadata_for_topics()
    except KafkaUnavailableError:
        kafka_client.load_metadata_for_topics()

    topics_to_be_considered = []

    for topic in kafka_client.topic_partitions:
        if re.search(topic_regex, topic):
            topics_to_be_considered.append(topic)

    watermarks = get_topics_watermarks(
        kafka_client, topics_to_be_considered
    )
    return watermarks 
開發者ID:Yelp,項目名稱:kafka-utils,代碼行數:32,代碼來源:monitoring.py

示例4: test_get_metadata_kafka_error

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def test_get_metadata_kafka_error(self, kafka_client_mock):
        with mock.patch.object(
            MyKafkaToolClient,
            'load_metadata_for_topics',
            side_effect=KafkaUnavailableError("Boom!"),
            autospec=True
        ) as mock_func:
            with pytest.raises(KafkaUnavailableError):
                get_consumer_offsets_metadata(
                    kafka_client_mock,
                    self.group,
                    {'topic1': [99]},
                )
            assert mock_func.call_count == 2 
開發者ID:Yelp,項目名稱:kafka-utils,代碼行數:16,代碼來源:test_monitoring.py

示例5: test_get_metadata_kafka_error

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def test_get_metadata_kafka_error(self, kafka_client_mock):
        with mock.patch.object(
            MyKafkaClient,
            'load_metadata_for_topics',
            side_effect=KafkaUnavailableError("Boom!"),
            autospec=True
        ) as mock_func:
            with pytest.raises(KafkaUnavailableError):
                get_consumer_offsets_metadata(
                    kafka_client_mock,
                    self.group,
                    {'topic1': [99]},
                )
            assert mock_func.call_count == 2 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:16,代碼來源:test_monitoring.py

示例6: test__release_retry

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def test__release_retry(self, cluster):
        config = KafkaConsumerConfig(
            self.group,
            cluster,
            auto_commit_enable=True
        )
        consumer = KafkaConsumerGroup([], config)

        mock_consumer = mock.Mock()
        mock_consumer.set_topic_partitions.side_effect = KafkaUnavailableError
        consumer.consumer = mock_consumer

        with pytest.raises(KafkaUnavailableError):
            consumer._release({})
        assert mock_consumer.set_topic_partitions.call_count == 2 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:17,代碼來源:test_consumer_group.py

示例7: test_get_kafka_topics_recover_from_error

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def test_get_kafka_topics_recover_from_error():
    expected = {
        'topic1': [0, 1, 2, 3],
        'topic2': [0, 1]
    }
    mock_client = mock.Mock()
    mock_client.topic_partitions = expected
    mock_client.load_metadata_for_topics.side_effect = [KafkaUnavailableError(), None]
    actual = utils.get_kafka_topics(mock_client)
    assert expected == actual 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:12,代碼來源:test_utils.py

示例8: test_get_kafka_topics_error

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def test_get_kafka_topics_error():
    expected = {
        'topic1': [0, 1, 2, 3],
        'topic2': [0, 1]
    }
    mock_client = mock.Mock()
    mock_client.topic_partitions = expected
    mock_client.load_metadata_for_topics.side_effect = KafkaUnavailableError('Boom!')
    with pytest.raises(KafkaUnavailableError):
        utils.get_kafka_topics(mock_client) 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:12,代碼來源:test_utils.py

示例9: get_kafka_topics

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_kafka_topics(kafkaclient):
    """Connect to kafka and fetch all the topics/partitions."""
    try:
        kafkaclient.load_metadata_for_topics()
    except KafkaUnavailableError:
        # Sometimes the kafka server closes the connection for inactivity
        # in this case the second call should succeed otherwise the kafka
        # server is down and we should fail
        log.debug("First call to kafka for loading metadata failed."
                  " Trying again.")
        kafkaclient.load_metadata_for_topics()
    return kafkaclient.topic_partitions 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:14,代碼來源:utils.py

示例10: retry_if_kafka_unavailable_error

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def retry_if_kafka_unavailable_error(exception):
    """Returns true if the exception is of type KafkaUnavailableError

    :param: exception: the exception to be checked
    :returns boolean
    """
    return isinstance(exception, KafkaUnavailableError) 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:9,代碼來源:utils.py

示例11: _connect

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def _connect(self):
        connection = "{0}:{1}".format(self.host, self.port)
        logging.info("Connecting to Kafka at %s...", connection)
        try:
            self.consumer = KafkaConsumer(self.topic,
                                          group_id=self.group,
                                          bootstrap_servers=[connection]
                                          )
        except KafkaUnavailableError as e:
            raise EncoderError(e) 
開發者ID:mre,項目名稱:kafka-influxdb,代碼行數:12,代碼來源:kafka_python.py

示例12: run

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def run(cls, args, cluster_config):
        # Setup the Kafka client
        client = KafkaToolClient(cluster_config.broker_list)
        client.load_metadata_for_topics()

        topics_dict = cls.preprocess_args(
            groupid=args.groupid,
            topic=args.topic,
            partitions=args.partitions,
            cluster_config=cluster_config,
            client=client,
            use_admin_client=args.use_admin_client,
        )
        try:
            consumer_offsets_metadata = get_consumer_offsets_metadata(
                client,
                args.groupid,
                topics_dict,
            )
        except KafkaUnavailableError:
            print(
                "Error: Encountered error with Kafka, please try again later.",
                file=sys.stderr,
            )
            raise

        # Warn the user if a topic being subscribed to does not exist in Kafka.
        for topic in topics_dict:
            if topic not in consumer_offsets_metadata:
                print(
                    "Warning: Topic {topic} does not exist in Kafka"
                    .format(topic=topic),
                    file=sys.stderr,
                )

        cls.save_offsets(
            consumer_offsets_metadata,
            topics_dict,
            args.json_file,
            args.groupid,
        )
        client.close() 
開發者ID:Yelp,項目名稱:kafka-utils,代碼行數:44,代碼來源:offset_save.py

示例13: get_consumer_offsets_metadata

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_consumer_offsets_metadata(
    kafka_client,
    group,
    topics,
    raise_on_error=True,
):
    """This method:
        * refreshes metadata for the kafka client
        * fetches group offsets
        * fetches watermarks

    :param kafka_client: KafkaToolClient instance
    :param group: group id
    :param topics: list of topics
    :param raise_on_error: if False the method ignores missing topics and
      missing partitions. It still may fail on the request send.
    :returns: dict <topic>: [ConsumerPartitionOffsets]
    """
    # Refresh client metadata. We do not use the topic list, because we
    # don't want to accidentally create the topic if it does not exist.
    # If Kafka is unavailable, let's retry loading client metadata
    try:
        kafka_client.load_metadata_for_topics()
    except KafkaUnavailableError:
        kafka_client.load_metadata_for_topics()

    group_offsets = get_current_consumer_offsets(
        kafka_client, group, topics, raise_on_error
    )

    watermarks = get_topics_watermarks(
        kafka_client, topics, raise_on_error
    )

    result = {}
    for topic, partitions in six.iteritems(group_offsets):
        result[topic] = [
            ConsumerPartitionOffsets(
                topic=topic,
                partition=partition,
                current=group_offsets[topic][partition],
                highmark=watermarks[topic][partition].highmark,
                lowmark=watermarks[topic][partition].lowmark,
            ) for partition in partitions
        ]
    return result 
開發者ID:Yelp,項目名稱:kafka-utils,代碼行數:48,代碼來源:monitoring.py

示例14: get_consumer_offsets_metadata

# 需要導入模塊: from kafka import common [as 別名]
# 或者: from kafka.common import KafkaUnavailableError [as 別名]
def get_consumer_offsets_metadata(
    kafka_client,
    group,
    topics,
    raise_on_error=True,
    offset_storage='zookeeper',
):
    """This method:
        * refreshes metadata for the kafka client
        * fetches group offsets
        * fetches watermarks

    :param kafka_client: KafkaClient instance
    :param group: group id
    :param topics: list of topics
    :param raise_on_error: if False the method ignores missing topics and
      missing partitions. It still may fail on the request send.
    :param offset_storage: String, one of {zookeeper, kafka}.
    :returns: dict <topic>: [ConsumerPartitionOffsets]
    """

    # Refresh client metadata. We do now use the topic list, because we
    # don't want to accidentally create the topic if it does not exist.
    # If Kafka is unavailable, let's retry loading client metadata (YELPKAFKA-30)
    try:
        kafka_client.load_metadata_for_topics()
    except KafkaUnavailableError:
        kafka_client.load_metadata_for_topics()

    group_offsets = get_current_consumer_offsets(
        kafka_client, group, topics, raise_on_error, offset_storage
    )

    watermarks = get_topics_watermarks(
        kafka_client, topics, raise_on_error
    )

    result = {}
    for topic, partitions in six.iteritems(group_offsets):
        result[topic] = [
            ConsumerPartitionOffsets(
                topic=topic,
                partition=partition,
                current=group_offsets[topic][partition],
                highmark=watermarks[topic][partition].highmark,
                lowmark=watermarks[topic][partition].lowmark,
            ) for partition in partitions
        ]
    return result 
開發者ID:Yelp,項目名稱:yelp_kafka,代碼行數:51,代碼來源:monitoring.py


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