Google api google.cloud.pubsub-流式拉占pubsub消息

Google api google.cloud.pubsub-流式拉占pubsub消息,google-api,google-cloud-pubsub,google-cloud-python,Google Api,Google Cloud Pubsub,Google Cloud Python,我目前正在最新的googlecloudpubsub==0.35.4pubsub版本上运行一些测试。我的意图是使用动态数量的订户客户端来处理一个永无止境的流(负载变化) 然而,当我有一队人在排队时,我会说。。600条消息和1个正在运行的客户端,然后添加其他客户端: 预期:所有剩余消息在所有客户端上均匀分布 观察到:只有新消息在客户端之间分发,任何旧消息都会发送到预先存在的客户端 下面是我为我的客户使用的简化版本(仅供参考,我们将只运行低优先级主题)。 我将不包括出版商,因为它没有关系 PRIO

我目前正在最新的
googlecloudpubsub==0.35.4
pubsub版本上运行一些测试。我的意图是使用动态数量的订户客户端来处理一个永无止境的流(负载变化)

然而,当我有一队人在排队时,我会说。。600条消息和1个正在运行的客户端,然后添加其他客户端:

  • 预期:所有剩余消息在所有客户端上均匀分布
  • 观察到:只有新消息在客户端之间分发,任何旧消息都会发送到预先存在的客户端
下面是我为我的客户使用的简化版本(仅供参考,我们将只运行低优先级主题)。 我将不包括出版商,因为它没有关系

PRIORITY_HIGH = 1
PRIORITY_MEDIUM = 2
PRIORITY_LOW = 3

MESSAGE_LIMIT = 10
ACKS_PER_MIN = 100.00
ACKS_RATIO = {
    PRIORITY_LOW: 100,
}

PRIORITY_TOPICS = {
    PRIORITY_LOW: 'test_low',
}

PRIORITY_SEQUENCES = {
    PRIORITY_LOW: [PRIORITY_LOW, PRIORITY_MEDIUM, PRIORITY_HIGH],
}


class Subscriber:
    subscriber_client = None
    subscriptions = {}

    priority_queue = defaultdict(Queue.Queue)
    priorities = []

    def __init__(self):
        logging.basicConfig()
        self.subscriber_client = pubsub_v1.SubscriberClient()

        for option, percentage in ACKS_RATIO.iteritems():
            self.priorities += [option] * percentage

    def subscribe_to_topic(self, topic, max_messages=10):
        self.subscriptions[topic] = self.subscriber_client.subscribe(
            BASE_TOPIC_PATH.format(project=PROJECT, topic=topic,),
            self.process_message,
            flow_control=pubsub_v1.types.FlowControl(
                max_messages=max_messages,
            ),
        )

    def un_subscribe_from_topic(self, topic):
        subscription = self.subscriptions.get(topic)
        if subscription:
            subscription.cancel()
            del self.subscriptions[topic]

    def process_message(self, message):
        json_message = json.loads(message.data.decode('utf8'))
        self.priority_queue[json_message['priority']].put(message)

    def retrieve_message(self):
        message = None
        priority = random.choice(self.priorities)
        ack_priorities = PRIORITY_SEQUENCES[priority]

        for ack_priority in ack_priorities:
            try:
                message = self.priority_queue[ack_priority].get(block=False)
                break
            except Queue.Empty:
                pass

        return message


if __name__ == '__main__':
    messages_acked = 0

    pub_sub = Subscriber()
    pub_sub.subscribe_to_topic(PRIORITY_TOPICS[PRIORITY_LOW], MESSAGE_LIMIT * 3)

    while True:
        msg = pub_sub.retrieve_message()
        if msg:
            json_msg = json.loads(msg.data.decode('utf8'))

            msg.ack()
            print ("%s - Akked Priority %s , High %s, Medium %s, Low %s" % (
                datetime.datetime.now().strftime('%H:%M:%S'),
                json_msg['priority'],
                pub_sub.priority_queue[PRIORITY_HIGH].qsize(),
                pub_sub.priority_queue[PRIORITY_MEDIUM].qsize(),
                pub_sub.priority_queue[PRIORITY_LOW].qsize(),
            ))

        time.sleep(60.0 / ACKS_PER_MIN)
我想知道这种行为是否是流媒体拉取功能固有的,或者是否有配置可以改变这种行为

干杯

考虑到云发布/订阅,对于每个订阅,每个发布的消息至少交付一次,但是这种行为有一些例外:

  • 在7天的最长保留时间内无法传递的邮件将被删除
  • 将不会传递在创建给定订阅之前发布的消息
换句话说,服务将把消息传递给在消息发布之前创建的订阅,因此,旧消息将不可用于新订阅。据我所知,云发布/订阅没有提供改变这种行为的功能