首页 > 解决方案 > 为什么我不能在 AWS Batch C++ SDK 中覆盖多节点并行作业的容器变量?

问题描述

我正在使用 AWS Batch。我的目标是通过适用于 C++ 的 AWS 开发工具包创建多节点并行作业。为此,我按照此处的说明创建了一个工作定义。

我正在使用 AWS C++ SDK,我注意到当我尝试覆盖环境变量或命令时,实际上没有任何内容传输到作业中。

有趣的是,对于使用普通作业定义(与多节点作业相反)的作业,相同的代码工作得非常好:

#include <aws/batch/BatchClient.h>
#include <aws/batch/model/ContainerOverrides.h>
#include <aws/batch/model/KeyValuePair.h>
#include <aws/batch/model/SubmitJobRequest.h>
#include <aws/core/Aws.h>
#include <aws/core/utils/Outcome.h>

int main(void)
{
    Aws::SDKOptions options;
    Aws::InitAPI(options);

    Aws::Batch::BatchClient batchClient;
    Aws::Batch::Model::SubmitJobRequest submitJobRequest;
    Aws::Batch::Model::SubmitJobOutcome submitJobOutcome;
    Aws::Batch::Model::ContainerOverrides containerOverrides;
    Aws::Batch::Model::KeyValuePair envVariable;

    envVariable.SetName("foo");
    envVariable.SetValue("bar");

    containerOverrides.AddEnvironment(envVariable); // This does nothing for a multi-node job definition.
    containerOverrides.AddCommand("foobarbaz"); // This does nothing for a multi-node job definition.

    submitJobRequest.SetJobName("myjob");
    submitJobRequest.SetJobDefinition("arn:aws:...."); // This string is an example. I have used the actual job definition ARN.
    submitJobRequest.SetJobQueue("arn:aws:...."); // This string is an exmaple. I have used the actual queue ARN.
    submitJobRequest.SetContainerOverrides(containerOverrides);

    submitJobOutcome = batchClient.SubmitJob(submitJobRequest);

    Aws::ShutdownAPI(options);

    return 0;

}

我应该为多节点并行作业使用不同的 API 吗?

标签: amazon-web-servicesaws-sdkaws-batchaws-sdk-cpp

解决方案


不要在请求中设置顶级容器覆盖,而是尝试在请求的节点覆盖内设置容器覆盖。节点覆盖具有节点属性覆盖向量,其中包含您的容器覆盖,包括环境变量。在节点属性覆盖中,您需要指定目标节点的名称。

我不太擅长 C++,所以我无法为您提供准确的代码示例,但 SDK 调用的 API 的 JSON 看起来像这样:


POST /v1/submitjob HTTP/1.1
Content-type: application/json

{
   "jobDefinition": "string",
   "jobName": "string",
   "jobQueue": "string",
   "nodeOverrides": { 
      "nodePropertyOverrides": [ 
         { 
            "containerOverrides": { 
               "command": [ "string" ],
               "environment": [ 
                  { 
                     "name": "string",
                     "value": "string"
                  }
               ],
               "instanceType": "string",
               "memory": number,
               "resourceRequirements": [ 
                  { 
                     "type": "string",
                     "value": "string"
                  }
               ],
               "vcpus": number
            },
            "targetNodes": "string"
         }
      ],
      "numNodes": number
   },
   # lots of other stuff
}

希望有帮助。


推荐阅读