【问题标题】:AWS Step Function - Dynamic parallelism MaxConcurrency FieldAWS Step Function - 动态并行 MaxConcurrency 字段
【发布时间】:2022-01-20 20:59:34
【问题描述】:

我们使用带有 Map 状态的 Step 函数动态并行来实现并发。 是否可以在 map state stepfunctions 中从上游任务(lambda 或从文件读取)将值传递给“MaxConcurrency”字段。

当前代码:

"Type": "Map",
"InputPath": "$.detail",
"ItemsPath": "$.shipped",
"MaxConcurrency": 3,
"ResultPath": "$.detail.shipped",

期望(将输入从 lambda 任务或读取文件任务传递给 MaxConcurrency):

 "Type": "Map",
 "InputPath": "$.detail",
 "ItemsPath": "$.shipped",
 "MaxConcurrency": "$.input",
 "ResultPath": "$.detail.shipped"

因为它只支持整数而出现错误。

【问题讨论】:

    标签: amazon-web-services aws-lambda concurrency aws-step-functions


    【解决方案1】:

    您可以在状态机定义时设置maxConcurrency,但不能在执行时设置。如您所见,Map 的maxConcurrency 需要number,但状态机语言使用strings 动态传递变量。

    (注意:Step Functions 默认是并发的。Docs:maxConcurrency's "默认值为 0,这对并行性没有配额,并且迭代被同时调用尽可能”。)

    选项1:选择+地图(难度:低)

    限制在执行时动态的解决方法是Choice state,它根据输入变量分支到离散映射状态。每个分支的 Map 都有一个不同的maxConcurrency,但在其他方面是相同的。添加您需要的任何离散maxConcurrency 选项。 Choice 还接受默认情况以捕获不匹配的选择输入。

    // execution input
    {
      "concurrency": 5,
      "jobs": [ { "jobId": 1 }, { "jobId": 2 }, { "jobId": 3 }, { "jobId": 4}]
    }
    
    // state machine definition (partial)
    "States": {
      "Max-Concurrency-Choice": {
        "Type": "Choice",
        "Choices": [
          {
            "Variable": "$.concurrency",
            "NumericEquals": 5,
            "Next": "MapState-MaxConcurrency-5"  // maxConcurrency in this branch is set at 5
          },
          {
            "Variable": "$.concurrency",
            "NumericEquals": 10,
            "Next": "MapState-MaxConcurrency-10" // maxConcurrency in this branch is set at 10
          }
        ],
        "Default": "MapState-MaxConcurrency-1" // maxConcurrency in the default branch is set at 1
      },
    

    选项 2:嵌套 Sfn + API 调用(难度:高)

    将您的 Sfn 嵌套在新的 Sfn 中。新的父 Sfn 在输入中采用 maxConcurrency。它有两个任务:

    1. 在 Lambda 任务中,调用 UpdateStateMachine API,并为您当前的子 Sfn 使用新的字符串化 JSON 状态机定义。
    2. 调用您当前的状态机。 Sfn 将拥有新的maxConurrency

    【讨论】:

    • 是的。谢谢回复。仍在寻找解决方案,通过传递来自文件的输入并根据负载数量对其进行更改,从而在单个映射状态下实现并发。这是 StepFunctions 的限制吗?
    • 也许我不明白你的目标。如果您想要尽可能多的并发性,Step Functions 开箱即用。仅当您想限制并发时才使用maxConcurrency。我的回答允许您动态限制它。我已经对答案进行了一些编辑。如果仍然没有帮助,请编辑您的问题以添加细节,也许可以举个例子。
    • 可以说当前将 Maxconcurrency 设置为 10... 3 个月后,我想将 Maxconcurrency 更新为 20。我的目标是从变量中设置此值(存储在文件中的变量的值)并将变量传递给我的状态机定义的 MaxConcurrency 字段。尝试避免更改 StepFunctions 定义的选项,而不是在我想增加或减少并发性时直接将其放入文件中。
    • maxConcurrency 是定义的一部分。我的回答的第一句话说:您可以在状态机定义时设置 maxConcurrency,但不能在执行时设置。。您必须更改定义才能更改maxConcurrency。我添加了一个理论上的第二种解决方法,可以自动执行您的评论要求。不过,这个新选项相当复杂。
    • 谢谢..让我试着在这里发帖..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多