并行调用数百个 azure 函数 [英] Calling hundreds of azure functions in parallel

查看:22
本文介绍了并行调用数百个 azure 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用某些规则引擎执行规则的应用程序.我有大约 500 多条规则,我们的应用程序将收到大约 10,000 个条目,所有这 10,000 个条目都应分别通过这 500 条规则进行验证.我们目前正计划将所有规则迁移到 Azure 函数中.也就是说,每个 Azure 函数都对应一个规则.

I have an application that executes rules using some rules engine. I have around 500+ rules and our application will receive around 10,000 entries and all of those 10,000 entries should go through those 500 rules for validation individually. We are currently planning to migrate all our rules into Azure functions. That is each Azure function will correspond to a single rule.

所以我尝试了概念验证,并使用 Task.delay 创建了几个 Azure 函数来模拟真实的规则验证.我需要为每个请求执行所有 500 条规则.因此,我创建了一个 Durable 函数作为协调器,使用 Task.WhenAll 调用所有 500 个活动触发器(规则),但这不起作用.Azure 函数被一个请求挂起.因此,我为规则创建了单独的 HttpTrigger 函数.从一个 C# 类库中,我使用 Task.WhenAll 为一个请求调用了所有 500 个函数,它的工作原理非常棒.但是,当我尝试为所有条目(从 50 开始)调用 azure 函数时,Task.WhenAll 开始抛出错误,说任务被取消或 TimeoutException 用于 HTTP 调用.代码如下:

So I was trying a proof of concept and created couple of Azure functions with Task.delay to mock real rule validation. I need to execute all 500 rules for each and every request. So I created a Durable function as orchestrator calling all the 500 activity triggers (rules) using Task.WhenAll, but that didn't work. Azure functions got hung with just one request. So instead I created individual HttpTrigger function for the rules. From a C# class library I called all the 500 functions using Task.WhenAll for one request and it worked like a charm. However, when I tried calling azure functions for all entries (starting with 50) then Task.WhenAll started throwing errors saying a task got cancelled or TimeoutException for HTTP call. Below is the code:

var tasksForCallingAzureFunctions = new List<List<Task<HttpResponseMessage>>>();

Parallel.For(0, 50,  (index) =>
{
  var tasksForUser = new List<Task<HttpResponseMessage>>();

  //call the mocked azure function 500 times 
  for (int j = 0; j < 500; j++)
  {
    tasksForUser.Add(client.SendAsync(CreateRequestObject()));
  }

  //add the tasks for each user into main list to call all the requests in parallel.
  tasksForCallingAzureFunctions.Add(tasksForUser);
});

// call all the requests - 50 * 500 requests in parallel.
Parallel.ForEach(tasksForCallingAzureFunctions,  (tasks) => Task.WhenAll(tasks).GetAwaiter().GetResult());

所以我的问题是,这是推荐的方法吗?我正在进行 50 * 500 个 I/O 调用.要求是所有条目 (10000) 都应通过 500 条规则.我认为的另一个选项是将所有条目发送到每个 Azure 函数,以便 Azure 函数具有循环遍历每个条目并验证它们的逻辑.那样我只需要打 500 个电话?

So my question is, Is this a recommended approach? I'm making 50 * 500 I/O calls. Requirement is all the entries (10000) should go through 500 rules. Other option I thought was send all the entries to each Azure function, so that Azure function will have logic to loop through each entry and validate them. That way I only have to make 500 calls?

请指教.

推荐答案

我会用队列触发的函数来实现这个场景.每个规则每个实体发送一个队列消息,每个函数都会选择一个,完成它的工作并将结果保存到某个存储中.这应该比执行大量同步 HTTP 请求更具弹性.

I would implement this scenario with queue-triggered Functions. Send a queue message per entity per rule, each function will pick one up, do its work and save the result to some storage. This should be more resilient than doing a gazillion sync HTTP requests.

现在,Durable Functions 基本上在幕后做同样的事情,所以你走在正确的道路上.也许为您的悬挂问题添加一个最少重现的新问题.

Now, Durable Functions basically do the same behind the scenes, so you were on the right path. Maybe add a new question with minimal repro for your hanging problem.

这篇关于并行调用数百个 azure 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆