简体   繁体   English

AWS ECS Fargate 和多线程

[英]AWS ECS Fargate and multi threading

Context: I am new to the "serverless" concept.背景:我是“无服务器”概念的新手。 I am creating a pick and place application.我正在创建一个拾取和放置应用程序。 Basically the application will consume/pick messages from 40 queues and send/place these messages in a single outgoing FIFO queue (to maintain the sequence).基本上,应用程序将从 40 个队列中消费/挑选消息,并将这些消息发送/放置在单个传出 FIFO 队列中(以保持序列)。 The logic requires about 10 workers/threads to run in parallel working on different queues.该逻辑需要大约 10 个工作线程/线程在不同队列上并行运行。

Pls dont suggest lambdas.请不要建议 lambdas。 It doesn't fit my use case.它不适合我的用例。

I am planning to create this application in AWS ECS + fargate.我计划在 AWS ECS + fargate 中创建这个应用程序。 I wanted to know will there be any problem when I use fargate for my java application which will create those 10 threads.我想知道当我将 fargate 用于我的 java 应用程序时会不会有任何问题,它将创建这 10 个线程。

Is there any problem with multithreading when using fargate (which is a serverless concept)??使用fargate(这是一个无服务器概念)时,多线程有什么问题吗??

With fargate you can only create the following vCPU and memory configurations per task:使用 fargate,您只能为每个任务创建以下 vCPU 和 memory 配置

512 (0.5 GB), 1024 (1 GB), 2048 (2 GB) - Available cpu values: 256 (.25 vCPU)

1024 (1 GB), 2048 (2 GB), 3072 (3 GB), 4096 (4 GB) - Available cpu values: 512 (.5 vCPU)

2048 (2 GB), 3072 (3 GB), 4096 (4 GB), 5120 (5 GB), 6144 (6 GB), 7168 (7 GB), 8192 (8 GB) - Available cpu values: 1024 (1 vCPU)

Between 4096 (4 GB) and 16384 (16 GB) in increments of 1024 (1 GB) - Available cpu values: 2048 (2 vCPU)

Between 8192 (8 GB) and 30720 (30 GB) in increments of 1024 (1 GB) - Available cpu values: 4096 (4 vCPU)

As you can see, max vCPU count is 4 .如您所见,最大 vCPU 计数为 4

Assuming 1 vCPU is 1 hyper-thread on a physical CPU core (based on this answer ) you could comfortably run 4 threads of your application.假设 1 个 vCPU 是物理 CPU 核心上的 1 个超线程(基于此答案),您可以轻松地运行应用程序的 4 个线程。

With 10 threads, it can be difficult (assuming log-running, very cpu intensive threads), even on the most powerful Farget setup.使用 10 个线程,即使在最强大的 Farget 设置上,也可能很困难(假设日志运行、CPU 密集型线程)。

Nevertheless, this is something that can be tested relatively easy once you get your application running.尽管如此,一旦您的应用程序运行起来,就可以相对容易地测试这一点。

On a physical machine, you have a certain number of在物理机上,你有一定数量的

  • CPUs (examples: 1 CPU on your laptop or 4 CPUs on a server), CPU(例如:笔记本电脑上的 1 个 CPU 或服务器上的 4 个 CPU),
  • each CPU has Y cores (example: 6 cores),每个 CPU 有 Y 个核心(例如:6 个核心),
  • each core can potentially do hyperthreading (usually 2 threads per core).每个核心都可能执行超线程(通常每个核心 2 个线程)。 Think of CPU core threads as conveyer belts leading to the core: when one conveyer belt is empty, the core can work on things coming on the other conveyer belt to process.将 CPU 核心线程想象成通向核心的传送带:当一条传送带为空时,核心可以处理另一条传送带上要处理的东西。 In most architectures (like Intel), there are two threads (conveyor belts) for each CPU core.在大多数架构(如 Intel)中,每个 CPU 内核都有两个线程(传送带)。 Of course, if your conveyer belts are fully loaded (if you are running a very intensive task), then there will be switching costs.当然,如果您的传送带满载(如果您正在运行一项非常密集的任务),那么就会产生转换成本。 I believe that Amazon's new CPU, the Graviton, has 1 thread per core (no Hyperthreading).我相信亚马逊的新 CPU Graviton 每个核心有 1 个线程(没有超线程)。 So, you need to look at each server instance specifically to know how many threads each core has.因此,您需要专门查看每个服务器实例,以了解每个核心有多少线程。

Now, do not confuse CPU threads (ex: 2 threads per CPU core) and application threads!现在,不要混淆 CPU 线程(例如:每个 CPU 核心 2 个线程)和应用程序线程! Those are two totally different things!这是两个完全不同的东西!

You then need to understand that each OS uses the above CPU/core/threads differently.然后您需要了解每个操作系统以不同的方式使用上述 CPU/核心/线程。 It creates processes, threads, and uses time slices on those CPUs/cores/threads.它在这些 CPU/内核/线程上创建进程、线程并使用时间片。 For example, on your laptop, you likely have only one CPU with 2 to 6 core (for Intel processors, depending on i3, i5, i7), or a little bit more on the latest Apple M1.例如,在您的笔记本电脑上,您可能只有一个具有 2 到 6 核的 CPU(对于 Intel 处理器,取决于 i3、i5、i7),或者在最新的 Apple M1 上多一点。 In reality, on your laptop, you run your browser, you might run an IDE, you might run a web server, an application server, docker, Excel, whatever else. In reality, on your laptop, you run your browser, you might run an IDE, you might run a web server, an application server, docker, Excel, whatever else. These are LOTS of processes and application threads.这些是大量的进程和应用程序线程。 Way more than there are CPUs/cores/threads.远远超过 CPU/内核/线程。 It's the Operating System (OS) that splices the work and puts it on the conveyor belt.是操作系统 (OS) 拼接工作并将其放在传送带上。 In Linux, you can make some processes "nice" to yield to other processes, or you can make them "take all they can" from the processor.在 Linux 中,您可以使某些进程“很好地”让给其他进程,或者您可以让它们从处理器中“尽其所能”。 There are many ways to slice up the work.有很多方法可以分割工作。 So, you need to look at the OS as well.因此,您还需要查看操作系统。

Another example: when I install Apache Tomcat on my development laptop, Tomcat runs on a JVM and starts thread pools which might contain dozens of threads. Another example: when I install Apache Tomcat on my development laptop, Tomcat runs on a JVM and starts thread pools which might contain dozens of threads. And then I'd install an application on that Tomcat server which might have a database connection pool containing 20 threads.然后我会在 Tomcat 服务器上安装一个应用程序,该服务器可能有一个包含 20 个线程的数据库连接池。 As you can see, just my Tomcat server is probably running 30-40 Java threads while my Intel i7 laptop only has 1 CPU, 6 cores, and supports HyperThreading = 1x6x2 = 12 threads.如您所见,仅我的 Tomcat 服务器可能运行 30-40 个 Java 线程,而我的 Intel i7 笔记本电脑只有 1 个 CPU,6 个内核,并支持超线程 = 1x6x2 = 12 个线程。

In AWS, everything is virtualized, so 1 vCPU does NOT map to 1 CPU.在 AWS 中,一切都是虚拟化的,因此 1 个 vCPU 不会 map 到 1 个 CPU。 A vCPU actually maps to one core thread. vCPU 实际上映射到一个核心线程。 And that gets confusing because AWS doesn't use the same CPU on all servers, You need to look at the documentation to see which server class maps to which number of threads.这让人感到困惑,因为 AWS 并非在所有服务器上都使用相同的 CPU,您需要查看文档以查看哪个服务器 class 映射到哪个线程数。 etc, For example, I believe on Intel Xeon processors, 1vCPU=1 HyperThread (so. one of the two conveyor belts leading to one core), But for servers that use the new Graviton CPU (which, I think, has a single thread per CPU core).等等,例如,我相信在 Intel Xeon 处理器上,1vCPU=1 HyperThread(所以。两条传送带之一通向一个核心),但是对于使用新 Graviton CPU 的服务器(我认为它有一个线程每个 CPU 内核)。 you get one core.你得到一个核心。

Finally, in AWS Fargate, you specify CPU units.最后,在 AWS Fargate 中,您指定 CPU 单元。 Where 1024 units = 1 vCPU.其中 1024 个单元 = 1 个 vCPU。 This is hard to mentally process but think of how the OS time slices processes and how your laptop is currently running a lot (maybe hundreds) of processes and threads yet only has 1 CPU and a few cores.这很难在心理上处理,但想想操作系统时间片如何处理以及您的笔记本电脑当前如何运行很多(可能是数百个)进程和线程,但只有 1 个 CPU 和几个内核。 Think of it the same way with CPU units: you get a slice of the CPU.以与 CPU 单元相同的方式思考它:您获得了 CPU 的一部分。 Or think of it as if you get access to one of those conveyor belts leading to the core: if you set cpu units to 1024, you get the equivalent of "1 core".或者把它想象成你可以访问通往核心的传送带之一:如果你将 cpu 单位设置为 1024,你会得到相当于“1 个核心”的值。 Note that, in reality, it's actually better than that because AWS is packing those conveyor belts so my example is a little "flaky" (but I guess you get the idea).请注意,实际上,它实际上比这更好,因为 AWS 正在打包这些传送带,所以我的示例有点“不稳定”(但我想你明白了)。

Now, how many threads can you run on an ECS container in fargate if, for example, you set your docker container/task to use only 256, or 512 cpu units (a quarter or half of one conveyor belt leading to a core)?现在,如果您将 docker 容器/任务设置为仅使用 256 或 512 个 cpu 单元(通向核心的一条传送带的四分之一或一半),那么您可以在 fargate 的 ECS 容器上运行多少线程? That is hard to say because it depends on what you're doing.这很难说,因为这取决于你在做什么。 If you're solving math intensive problems that use the cpu threads to the fullest, you probably can't run too many application threads.如果您正在解决使用 cpu 线程的数学密集型问题,您可能无法运行太多的应用程序线程。 But if you're running an application server that waits around a lot (waiting for responses from the database, waiting for requests from the users, etc), then you can crank up the number of threads.但是,如果您正在运行一个等待很多时间的应用程序服务器(等待来自数据库的响应、等待来自用户的请求等),那么您可以增加线程数。

In the end, you likely want to load test your application.最后,您可能希望对应用程序进行负载测试。 If you put too many threads, your application will spend a lot of time, switching from one thread to the other (so that it is fair to all threads) and your app will crawl.如果你放置了太多线程,你的应用程序将花费大量时间,从一个线程切换到另一个线程(这样对所有线程都是公平的)并且你的应用程序会爬网。 If you set it too low, you are leaving capacity on the table.如果你把它设置得太低,你就会把容量留在桌子上。 The only way to know for sure, is to test it and find the sweet spot.唯一确定的方法是测试它并找到最佳位置。

Mistakes you should not make:你不应该犯的错误:

  • think that 1 physical CPU = 1 vCPU (totally not the case, more likely = 1 cpu thread),认为 1 个物理 CPU = 1 个 vCPU(完全不是这种情况,更有可能 = 1 个 cpu 线程),
  • think that 1 core = 2 threads (not always, depends on cpu architecture and other things, but it will likely be 1, or 2 threads, need to look in the AWS documentation to look up exact values),认为 1 个核心 = 2 个线程(并非总是如此,取决于 cpu 架构和其他东西,但可能是 1 个或 2 个线程,需要查看 AWS 文档以查找确切值),
  • think that 1 application thread = 1 CPU thread (these are totally different things),认为 1 个应用程序线程 = 1 个 CPU 线程(这些是完全不同的东西),
  • think that if you have 1 CPU thread, that you can only run single-threaded applications (that is totally not the case).认为如果您有 1 个 CPU 线程,那么您只能运行单线程应用程序(完全不是这种情况)。

Remember, 1 vCPU (~ 1-2 cpu threads) can run MANY application threads.请记住,1 个 vCPU(约 1-2 个 cpu 线程)可以运行许多应用程序线程。 Only you can figure out what is too low, what is too high, and where the sweet spot is.只有你才能弄清楚什么太低,什么太高,以及甜蜜点在哪里。 I hope this helps.我希望这有帮助。 Feel free to correct this post if/where I made mistakes and/or if I made too big logical shortcuts (I also struggled with this so I'm happy to be corrected).如果/我犯了错误和/或如果我犯了太大的逻辑捷径,请随时纠正这篇文章(我也为此苦苦挣扎,所以我很高兴得到纠正)。

Fargate will support multi threading, as long as the task definition and container definition has the resources for the addition threads Fargate 将支持多线程,只要任务定义和容器定义有用于添加线程的资源

AWS Fargate is container orchestration system. AWS Fargate 是容器编排系统。

Say you have containerized your java application.假设您已将 java 应用程序容器化。 You could do the following您可以执行以下操作

  • Define a fargate cluster定义一个 Fargate 集群
  • Define a fargate service定义 Fargate 服务
  • Define a fargate task with your java docker container使用您的 java docker 容器定义 Fargate 任务
  • Configure your service to launch 3 fargate tasks in your cluster配置您的服务以在您的集群中启动 3 个 Fargate 任务

Reasoning推理

Since you need 10 threads, each task(container) can have a max of 4 hyperthreads as @marcin mentioned.由于您需要 10 个线程,因此每个任务(容器)最多可以有4 hyperthreads ,正如@marcin 提到的那样。 So 3 tasks should do the trick.所以3个任务应该可以解决问题。

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

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