Can you please clarify how worker verticle works?

406 views
Skip to first unread message

andrea.p...@gmail.com

unread,
Nov 17, 2017, 3:34:01 AM11/17/17
to vert.x
Good morning and hello everybody,

i am new to the forum and basically i am learning how to use correctly vertx, and write pretty code.
I have searched a lot in the documentation and various blogs to try to understand how worker verticle works.
Actually I cannot understand how DeplymentOptions works.

Basically here we have two statements that are used to set the worker verticle threads.
setInstances() and set setWorkerPoolSize()

Can you explain exactly what these statements do (because the documentation lacks of details)?
What i have learned so far and i am pretty sure is that setInstances() tell the system to deploy N worker verticles of a particular type.
I cannot understand what setWorkerPoolSize() does.

For example what should i achieve with this Option?

DeploymentOptions workerOpts = new DeploymentOptions().setWorker(true).setInstances(5).setWorkerPoolSize(10);

And more if I have many different workers, lets say workerType1 and workerType2, what i do achieve if i set 2 different worker options?

DeploymentOptions workerOpts1 = new DeploymentOptions().setWorker(true).setInstances(5).setWorkerPoolSize(10);
DeploymentOptions workerOpts2 = new DeploymentOptions().setWorker(true).setInstances(5).setWorkerPoolSize(10);

vertx.deployVerticle(WorkerType1.class.getName(),
workerOpts1, res -> {
....
});
vertx.deployVerticle(
workerType2.class.getName(), workerOpts2, res -> {
...
});

What setWorkerPoolSize do in this case?

Is there a way to have a unique worker pool size without specify the instances so that vertx just use a worker when needed?
After some tests i saw that if i do not specify the instances then messages to the worker are serialized because it seems that the defaul is to have only one worker.
So again what the setWorkerPoolSize() does?

Thanks in advance.
Andrea.

Léo Silva

unread,
Nov 18, 2017, 4:28:41 PM11/18/17
to vert.x
Hello Andrea, I hope I can help with your questions.

So, the method setWorkerPoolSize in DeploymentOptions does exactly what the name says, but it is maybe trick, so, I want to provide you a code that you can run and check by yourself and will explain about that.

The code:

Result:
So, basically the output of this code is:

Verticle number 1 running! Worker thread: vert.x-eventloop-thread-0
Verticle number 1 running executeBlocking! Worker thread: vert.x-worker-thread-0


Verticle number 2 running! Worker thread: vert.x-worker-thread-1
Verticle number 2 running executeBlocking! Worker thread: vert.x-worker-thread-1
Verticle number 4 running! Worker thread: vert.x-worker-thread-3
Verticle number 4 running executeBlocking! Worker thread: vert.x-worker-thread-3
Verticle number 3 running! Worker thread: vert.x-worker-thread-2
Verticle number 3 running executeBlocking! Worker thread: vert.x-worker-thread-2
Verticle number 5 running! Worker thread: vert.x-worker-thread-4
Verticle number 6 running! Worker thread: vert.x-worker-thread-5
Verticle number 5 running executeBlocking! Worker thread: vert.x-worker-thread-4
Verticle number 6 running executeBlocking! Worker thread: vert.x-worker-thread-5
Verticle number 7 running! Worker thread: vert.x-worker-thread-6
Verticle number 7 running executeBlocking! Worker thread: vert.x-worker-thread-6
Verticle number 8 running! Worker thread: vert.x-worker-thread-7
Verticle number 8 running executeBlocking! Worker thread: vert.x-worker-thread-7
Verticle number 9 running! Worker thread: vert.x-worker-thread-8
Verticle number 9 running executeBlocking! Worker thread: vert.x-worker-thread-8
Verticle number 10 running! Worker thread: vert.x-worker-thread-9
Verticle number 10 running executeBlocking! Worker thread: vert.x-worker-thread-9
Verticle number 11 running! Worker thread: vert.x-worker-thread-10
Verticle number 11 running executeBlocking! Worker thread: vert.x-worker-thread-10


Verticle number 12 running! Worker thread: vert.x-worker-thread-11
Verticle number 13 running! Worker thread: vert.x-worker-thread-12
Verticle number 14 running! Worker thread: vert.x-worker-thread-13
Verticle number 13 running executeBlocking! Worker thread: vert.x-worker-thread-12
Verticle number 14 running executeBlocking! Worker thread: vert.x-worker-thread-13
Verticle number 15 running! Worker thread: vert.x-worker-thread-14
Verticle number 12 running executeBlocking! Worker thread: vert.x-worker-thread-11
Verticle number 17 running! Worker thread: vert.x-worker-thread-16
Verticle number 16 running! Worker thread: vert.x-worker-thread-15
Verticle number 15 running executeBlocking! Worker thread: vert.x-worker-thread-14
Verticle number 17 running executeBlocking! Worker thread: vert.x-worker-thread-16
Verticle number 16 running executeBlocking! Worker thread: vert.x-worker-thread-15
Verticle number 19 running! Worker thread: vert.x-worker-thread-18
Verticle number 19 running executeBlocking! Worker thread: vert.x-worker-thread-18
Verticle number 20 running! Worker thread: vert.x-worker-thread-0
Verticle number 20 running executeBlocking! Worker thread: vert.x-worker-thread-0
Verticle number 21 running! Worker thread: vert.x-worker-thread-19
Verticle number 21 running executeBlocking! Worker thread: vert.x-worker-thread-19
Verticle number 18 running! Worker thread: vert.x-worker-thread-17
Verticle number 18 running executeBlocking! Worker thread: vert.x-worker-thread-17


Verticle number 22 running! Worker thread: newOne-0
Verticle number 23 running! Worker thread: newOne-1
Verticle number 22 running executeBlocking! Worker thread: newOne-0
Verticle number 23 running executeBlocking! Worker thread: newOne-1
Verticle number 24 running! Worker thread: newOne-2
Verticle number 24 running executeBlocking! Worker thread: newOne-2
Verticle number 25 running! Worker thread: newOne-3
Verticle number 25 running executeBlocking! Worker thread: newOne-3
Verticle number 26 running! Worker thread: newOne-4
Verticle number 26 running executeBlocking! Worker thread: newOne-4
Verticle number 27 running! Worker thread: newOne-5
Verticle number 27 running executeBlocking! Worker thread: newOne-5
Verticle number 28 running! Worker thread: newOne-6
Verticle number 28 running executeBlocking! Worker thread: newOne-6
Verticle number 29 running! Worker thread: newOne-7
Verticle number 29 running executeBlocking! Worker thread: newOne-7
Verticle number 30 running! Worker thread: newOne-8
Verticle number 30 running executeBlocking! Worker thread: newOne-8
Verticle number 31 running! Worker thread: newOne-9
Verticle number 31 running executeBlocking! Worker thread: newOne-9

Explanation:
Vert.x has an internal WorkerPool of threads to run blocking code (eg: vertx.executeBlocking), so when you run vertx.executeBlocking you are always picking up one thread from vertx internal worker pool.
When you create a worker verticle without any option you are basically reusing the same internal worker pool for this verticle, and even when you specify a specific number of worker pool size it still uses the same internal worker pool... so as soon as you want to have a dedicated worker pool for your verticle you should specify a name, as in my example.

I hope it helps!

[]'s

andrea.p...@gmail.com

unread,
Nov 22, 2017, 10:03:09 AM11/22/17
to vert.x
Thank you very much. At least now is more clear how Worker Verticles works.
The really tricky things is that the pool is not handled as is expected to work.
Usually a pool is just the maxmimum number of concurrent threads of a particular job (for example a pool of db connection).
Here instead we have also the setInstances() method that fix the maximum number of instances you want to run concurrently.
So is not chear why you can also specify the  setWorkerPoolSize().

I cannot understand what  setInstances(10).setWorkerPoolSize(100) means.
So we have a max number of instances (concurrent threads) that is 10 taken from a pool of 100 threads. What do the other 90 threads do all the time?
Am I missing something?

Thanks.

Andrea.
Reply all
Reply to author
Forward
0 new messages