简体   繁体   English

即使有 5 个实例,也仅在 1 个 EC2 上运行操作

[英]Run an operation only on 1 EC2 even if there are 5 instances

Use-case:用例:

I have to scan a DynamoDB table periodically in 30 secs.我必须在 30 秒内定期扫描 DynamoDB 表。 I have five EC2 instances running on a fleet, but I want when the 30secs interval is over, only one EC2 instance out of the five should execute the scan in DynamoDB not all.我有五个 EC2 实例在一个队列上运行,但我希望在 30 秒间隔结束时,五个 EC2 实例中只有一个应该在 DynamoDB 中执行扫描,而不是全部。

How can I implement this mechanism where if an operation is about to start only 1 EC2 instance should pick up the operation not all.如果一个操作即将启动,我该如何实现这种机制,只有 1 个 EC2 实例应该选择该操作而不是全部。

To accomplish your goal you will have to come up with a strategy for your EC2 hosts to agree on which one will do the scan.为了实现您的目标,您必须制定一个策略,让您的 EC2 主机同意由哪个主机进行扫描。

There are multiple possible solutions but one that is probably fairly simple to implement would be to use optimistic concurrency in DynamoDB to select the EC2 host for each scan.有多种可能的解决方案,但一个可能相当容易实现的解决方案是在 DynamoDB 中使用乐观并发到 select EC2 主机进行每次扫描。

Either in the same table (if schema permits) or in a separate table create a scan-schedule item with the following attributes:在同一个表中(如果架构允许)或在单独的表中创建具有以下属性的扫描计划项:

  • key - a primary key (can also be composite if you need to retrofit into your existing table schema) set to some static value that you can use to get and put the item key - 一个主键(如果您需要将 retrofit 放入现有表模式,也可以是复合键)设置为一些 static 值,您可以使用它来获取和放置项目
  • host - a string attribute that will be updated to reflect the name of the host that is doing the scan, each time a scan is successfully started host - 一个字符串属性,将更新以反映正在执行扫描的主机的名称,每次扫描成功启动
  • lastScanTime - a numeric attribute that will be updated to the epoch timestamp when the last scan was started, each time a scan is successfully started lastScanTime - 一个数字属性,每次扫描成功启动时,都会更新为上次扫描开始时的纪元时间戳
  • version - a numeric attribute that will be used as a monotonically increasing numeric value for the purpose of optimistic concurrency (more on this below) version - 一个数字属性,将用作单调递增的数值,以实现乐观并发(更多内容见下文)

Now, on each EC2 host, set up an operation to run once every 30 seconds (can be a local cron set up to run every 30 seconds).现在,在每个 EC2 主机上,设置每 30 秒运行一次的操作(可以是设置为每 30 秒运行一次的本地 cron)。

When the scheduled operation runs, do the following:当计划的操作运行时,请执行以下操作:

  1. GetItem to read the current value of the scan-schedule item we just discussed above GetItem读取我们上面刚刚讨论的扫描计划项的当前值
  2. If the lastScanTimestamp was more than 25 seconds ago, attempt to update the item with this host's info and set lastScanTimestamp to the current timestamp, incrementing the version attribute as well, using a conditional expression that checks that the version == the same value that was read at step 1如果lastScanTimestamp早于 25 秒,尝试使用此主机的信息更新项目并将lastScanTimestamp设置为当前时间戳,同时增加version属性,使用条件表达式检查version == 与之前的值相同在第 1 步阅读
  3. If the conditional update succeeds, the the scan operation can commence;如果条件更新成功,则可以开始扫描操作; however, if the conditional update fails, then it means another host got to it first and this host should not continue to scan但是,如果条件更新失败,则意味着另一台主机先到了它,并且该主机不应继续扫描

Note that the key to the algorithm above is the conditional expression which allows you to condition a read-modify-write sequence such that you can detect if somebody else happened to change the item in the time since you read the status and made your attempt to update it.请注意,上述算法的关键是条件表达式,它允许您设置读取-修改-写入序列的条件,以便您可以检测到自从您读取状态并尝试更改项目后是否有其他人碰巧更改了项目。更新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM