使用队列触发器和消费计划上的 functionAppScaleLimit 进行 Azure Functions 扩展和并发

2023-12-11

我在 Linux 消费计划上有一个 Azure Function 应用程序,它有两个队列触发器。两个队列触发器都有batchSize参数设置为1因为它们每个都可以使用大约 500 MB 的内存,而我不想超过 1.5 GB 内存限制,所以应该只允许它们一次接收一条消息。

如果我想允许这两个队列触发器同时运行,但不希望它们超出范围,则设置functionAppScaleLimit to 2足以实现这一目标吗?

编辑:添加了新的示例,感谢@Hury Shen 为这些示例提供了框架

Please see @Hury Shen's answer below for more details. I've tested three queue trigger scenarios. All use the following legend: enter image description here

QueueTrigger with no functionAppScaleLimit enter image description here

QueueTrigger with functionAppScaleLimit set to 2
enter image description here QueueTrigger with functionAppScaleLimit set to 1
enter image description here

现在,我想我会坚持最后一个例子,但将来我想我可以安全地设置我的functionAppScaleLimit to 2 or 3如果我升级到高级计划。我还将测试两个队列触发器,它们使用以下命令侦听不同的存储队列:functionAppScaleLimit of 2,但我怀疑对我来说最安全的做法是为该场景中的每个队列触发器创建单独的 Azure Function 应用程序。

编辑 2:在一个函数应用程序中添加两个队列触发器的示例

Here are the results when using two queue triggers within one Azure Function that are listening on two different storage queues. This is the legend for both queue triggers: enter image description here

Both queue triggers running concurrently with functionAppScaleLimit set to 2
enter image description here Both queue triggers running concurrently with functionAppScaleLimit set to 1 enter image description here

在两个队列触发器同时运行的示例中functionAppScaleLimit set to 2看起来规模限制不起作用。微软的人可以解释一下吗?官方文档中没有警告(https://learn.microsoft.com/en-us/azure/azure-functions/functions-scale#limit-scale-out)表明此设置处于预览模式,但我们可以清楚地看到,当限制设置为 2 时,Azure Function 正在扩展到 4 个实例。在下面的示例中,看起来限制受到了限制,但功能是这不是我想要的,我们仍然看到 @Hury Shen 的答案中存在等待。

结论
要使用队列触发器限制 Azure Functions 中的并发性并控制缩放,必须将 Azure Function 限制为每个函数应用使用一个队列触发器,并使用batchSize and functionAppScaleLimit设置。如果您使用多个队列触发器,您将遇到竞争条件和等待,这可能会导致超时。


是的,你只需要设置functionAppScaleLimit to 2。但有一些关于消费计划的机制你需要了解。我在我身边测试它batchSize as 1并设置functionAppScaleLimit to 2(I set WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT as 2 in "应用程序设置函数应用程序的“而不是设置functionAppScaleLimit,它们是相同的)。我用下面的代码进行测试:

import logging
import azure.functions as func
import time

def main(msg: func.QueueMessage) -> None:
    logging.info('=========sleep start')
    time.sleep(30)
    logging.info('=========sleep end')
    logging.info('Python queue trigger function processed a queue item: %s',
                 msg.get_body().decode('utf-8'))

Then I add message to the queue, I sent 10 messages: 111, 222, 333, 444, 555, 666, 777, 888, 999, 000, I sent them one by one. The function was triggered success and after a few minutes, we can see the logs in "Monitor". Click one of the log in "Monitor", we can see the logs show as: enter image description here

I use 4 red boxes on the right of the screenshot above, I named each of the four logs as "s1", "s2", "s3", "s4"(step 1-4). And summarize the logs in excel for your reference: enter image description here

我用“s2" to "s4” 为黄色,因为这段时间是指函数任务的执行时间。

根据excel截图,我们可以推断出以下几点:

1.最大实例数只能扩展到2,因为我们可以发现Excel表格的每一行中不存在超过两个黄色单元格。因此,正如您在问题中提到的,该函数不能扩展到超过 2 个实例。

2.你想让这两个队列触发器同时运行,是可以实现的。但实例将通过消费机制进行扩展。简单来说,当一个函数实例被一条消息触发并且还没有完成任务,现在又进来另一条消息时,它不能确保使用另一个实例。第二条消息可能正在等待第一个实例。我们无法控制是否启用另一个实例.

===============================Update================================

由于我对你的描述不太清楚,我不确定你想要监听多少个存储队列以及你在你这边创建了多少个函数应用程序和 QueueTrigger 函数。我把我的测试结果总结如下,供大家参考:

1.对于您的问题would the Maximum Burst you described on the premium plan behave differently than this ?我认为如果我们选择高级计划,实例也会以相同的消费计划机制进行扩展。

2.如果你有两个存储队列需要监听,当然我们应该创建两个QueueTrigger函数来监听每个存储队列。

3.如果您只需要监听一个存储队列,我会用三种情况进行测试(我在这三种情况下将最大规模实例设置为 2):

A)创建oneQueueTrigger函数在one功能应用程序来听one存储队列。这是我在原始答案中测试的内容,Excel表格向我们显示实例将通过消费计划机制进行扩展,我们无法控制它。

B)创建twoQueueTrigger 函数在one功能应用程序来听same存储队列。结果与情况A几乎相同,我们无法控制使用多少个实例来处理消息。

C) 创建two运行应用程序并创建aQueueTrigger函数在each of功能应用程序来听same存储队列。结果也类似于案例 A 和 B,不同的是最大实例可以扩展到 4,因为我创建了两个函数应用程序(它们都可以扩展到 2 个实例)。

总之,我认为这三个案例都是相似的。虽然我们选择情况 3,但创建两个函数应用程序,每个函数应用程序中都有一个 QueueTrigger 函数。我们也不能确保第二个消息立即处理,它仍然可能被处理到第一个实例并等待第一个实例完成处理第一个消息。

所以这篇文章中你当前问题的答案 is setting the functionAppScaleLimit to 2 enough to achieve that? 是:如果您希望两个实例能够同时运行,我们无法确定。如果您只想要两个实例来处理消息,答案是肯定的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用队列触发器和消费计划上的 functionAppScaleLimit 进行 Azure Functions 扩展和并发 的相关文章

随机推荐