简体   繁体   English

什么是 Vert.x verticle“实例”,它们与线程的关系是什么?

[英]What are Vert.x verticle "instances", and what is their relation to threads?

When deploying a verticle in a Vert.x application, the documentation appears to be painstakingly unclear about what a verticle "instance" actually is, and how it relates to threads and thread pools.在 Vert.x 应用程序中部署 Verticle 时,文档似乎煞费苦心地不清楚 Verticle“实例”实际上是什么,以及它与线程和线程池的关系。 In fact, it's rather unclear about anything relating to the actual threading model...事实上,与实际线程 model 相关的任何事情都不清楚......

From the documentation;来自文档;

Vert.x works differently here. Vert.x 在这里的工作方式不同。 Instead of a single event loop, each Vertx instance maintains several event loops.每个 Vertx 实例都维护多个事件循环,而不是单个事件循环。 By default we choose the number based on the number of available cores on the machine, but this can be overridden.默认情况下,我们根据机器上可用内核的数量选择数量,但这可以被覆盖。

Also from the documentation;也来自文档;

The number of instances of the verticle to instantiate in the Vert.x server.要在 Vert.x 服务器中实例化的 Verticle 实例数。 Each verticle instance is strictly single threaded so to scale your application across available cores you might want to deploy more than one instance.每个 Verticle 实例都是严格的单线程,因此要跨可用核心扩展您的应用程序,您可能需要部署多个实例。 If omitted a single instance will be deployed.如果省略,将部署单个实例。 We'll talk more about scaling later on in this user manual.我们稍后将在本用户手册中详细讨论缩放。

Let's assume I want to deploy a simple REST API that is written as a single Verticle, and I would like to have it scale efficiently across available CPU cores.假设我想部署一个简单的 REST API,它被编写为单个 Verticle,并且我想让它在可用的 CPU 内核之间有效地扩展。 What should I do?我应该怎么办?

  • Do I deploy one instance of the verticle per CPU core?我是否为每个 CPU 内核部署一个 Verticle 实例?
  • Do I deploy a single instance of the verticle, as according to at least one part of documentation a single verticle instance will alread maintain several event loops (as I assume 1 event loop = 1 thread)?我是否部署了 Verticle 的单个实例,因为根据文档的至少一部分,单个 Verticle 实例将已经维护多个事件循环(因为我假设 1 个事件循环 = 1 个线程)?
  • If I deploy 4 instances of a verticle, what threads and/or thread pools will actually be created/used?如果我部署 4 个 Verticle 实例,实际上会创建/使用哪些线程和/或线程池?

The Vert.x documentation has this paragraph: Vert.x 文档有这样一段:

Verticles are chunks of code that get deployed and run by Vert.x. Verticle 是由 Vert.x 部署和运行的代码块。 A Vert.x instance maintains N event loop threads (where N by default is core*2) by default.一个 Vert.x 实例默认维护 N 个事件循环线程(其中 N 默认为 core*2)。

If you wanted to utilize all of your cores, you would deploy 2 verticles per core.如果您想利用所有核心,则每个核心需要部署 2 个 Verticle。

Standard verticles are assigned an event loop thread when they are created and the start method is called with that event loop.标准 Verticle 在创建时被分配一个事件循环线程,并使用该事件循环调用 start 方法。 When you call any other methods that takes a handler on a core API from an event loop then Vert.x will guarantee that those handlers, when called, will be executed on the same event loop.当您从事件循环调用任何其他在核心 API 上获取处理程序的方法时,Vert.x 将保证这些处理程序在被调用时将在同一个事件循环上执行。

This means we can guarantee that all the code in your verticle instance is always executed on the same event loop (as long as you don't create your own threads and call it.).这意味着我们可以保证您的 Verticle 实例中的所有代码始终在同一个事件循环上执行(只要您不创建自己的线程并调用它。)。

When you deploy a verticle, it gets assigned an event loop thread.当你部署一个 Verticle 时,它会被分配一个事件循环线程。 That means that the execution of any code written inside a verticle will always be executed on the same event loop that the verticle was deployed on.这意味着在 Verticle 中编写的任何代码的执行将始终在部署 Verticle 的同一事件循环上执行。 This allows you to scale nicely across the available threads.这使您可以很好地跨可用线程进行扩展。

You are right about the clarity of documentation, I posted a related question here您对文档的清晰度是正确的,我在这里发布了一个相关问题

Here's what documentation says -这是文档所说的-

Vert.x works differently here. Vert.x 在这里的工作方式不同。 Instead of a single event loop, each Vertx instance maintains several event loops.每个 Vertx 实例都维护多个事件循环,而不是单个事件循环。 By default we choose the number based on the number of available cores on the machine, but this can be overridden.默认情况下,我们根据机器上可用内核的数量选择数量,但这可以被覆盖。

Even though a Vertx instance maintains multiple event loops, any particular handler will never be executed concurrently, and in most cases (with the exception of worker verticles) will always be called using the exact same event loop.即使 Vertx 实例维护多个事件循环,任何特定的处理程序都不会并发执行,并且在大多数情况下(工作 Verticles 除外)将始终使用完全相同的事件循环来调用。

And here's how I get it -这就是我得到它的方式 -

  • Each Vert.x instance will run a number of event loops that equals the number of available cores.每个 Vert.x 实例将运行多个事件循环,这些事件循环等于可用内核的数量。

  • On a single Vert.x instance you can deploy multiple Veriticles that includes deploying multiple instances of the same Verticle that can run concurrently on different threads.在单个 Vert.x 实例上,您可以部署多个 Veriticle,包括部署可以在不同线程上并发运行的同一 Verticle 的多个实例。

  • Although documentation says that a particular handler only ever gets executed on a single event loop, I think what they mean by a "particular handler" is an object instance of a given Verticle, which doesn't mean that another object instance of this same Verticle can't run on another event loop on the same Vert.x.尽管文档说特定处理程序只会在单个事件循环中执行,但我认为“特定处理程序”的意思是给定 Verticle 的 object 实例,这并不意味着同一个 Verticle 的另一个 object 实例不能在同一个 Vert.x 上的另一个事件循环上运行。

  • Even if this same Verticle runs concurrently on many event loops on the same Vert.x, instances are not sharing any state so are thread-safe.即使同一个 Verticle 在同一个 Vert.x 上的许多事件循环上同时运行,实例也不会共享任何 state,因此是线程安全的。

If you deploy 4 instances of a Verticle on Vert.x running on a 2-cpu machine you will end up with 2 event loops running 2 instances each and each instance will host its own state if I get it right.如果你在 2-cpu 机器上运行的 Vert.x 上部署 4 个 Verticle 实例,你最终会得到 2 个事件循环,每个事件循环运行 2 个实例,如果我做对了,每个实例将拥有自己的 state。

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

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