Request are not distributed across their worker process
Asked Answered
E

3

7

I was just experimenting worker process hence try this:

const http = require("http");
const cluster = require("cluster");
const CPUs = require("os").cpus();
const numCPUs = CPUs.length;

if (cluster.isMaster) {
  console.log("This is the master process: ", process.pid);
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on("exit", (worker) => {
    console.log(`worker process ${process.pid} has died`);
    console.log(`Only ${Object.keys(cluster.workers).length} remaining...`);
  });
} else {
  http
    .createServer((req, res) => {
      res.end(`process: ${process.pid}`);
      if (req.url === "/kill") {
        process.exit();
      }
      console.log(`serving from ${process.pid}`);
    })
    .listen(3000);
}

I use loadtest to check "Are Request distributed across their worker process?" But I got same process.pid

This is the master process:  6984
serving from 13108
serving from 13108
serving from 13108
serving from 13108
serving from 13108
...

Even when I kill one of them, I get the same process.pid

worker process 6984 has died
Only 3 remaining...
serving from 5636
worker process 6984 has died
Only 2 remaining...
worker process 6984 has died
Only 1 remaining...

How I am getting same process.pid when I killed that? And Why my requests are not distributed across their worker process?

Even when I use pm2 to test cluster mood using:

$ pm2 start app.js -i 3
[PM2] Starting app.js in cluster_mode (3 instances)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.9mb   │
│ 1  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.8mb   │
│ 2  │ app                │ cluster  │ 0    │ online    │ 0%       │ 31.8mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

for loadtest -n 50000 http://localhost:3000 I check pm2 monit:

$ pm2 monit

┌─ Process List ───────────────────────────────────────────────────┐┌──  app Logs  ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│[ 0] app                         Mem:  43 MB    CPU: 34 %  online ││                                                                                                                                                              │ 
│[ 1] app                         Mem:  28 MB    CPU:  0 %  online ││                                                                                                                                                              │ 
│[ 2] app                         Mem:  27 MB    CPU:  0 %  online ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
│                                                                  ││                                                                                                                                                              │ 
└──────────────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
┌─ Custom Metrics ─────────────────────────────────────────────────┐┌─ Metadata ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ Heap Size                                             20.81 MiB  ││ App Name              app                                                                                                                                    │ 
│ Heap Usage                                              45.62 %  ││ Namespace             default                                                                                                                                │ 
│ Used Heap Size                                         9.49 MiB  ││ Version               N/A                                                                                                                                    │ 
│ Active requests                                               0  ││ Restarts              0                                                                                                                                      │ 
└──────────────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
 left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit                                                                                                                           To go further check out https://pm2.io/

But surprisingly, app1 and app2 never hit any request as well as it didn't show any app log.

Update 1

I still couldn't figure out any solution. If any further query need please ask for that. I faced that issue first time. That's why maybe I was unable to represent where the exact problem occurring.

Update 2

After getting some answer I try to test it again with a simple node server:

Using pm2 without any config:

gif1

Using config suggested from @Naor Tedgi's answer:

gif1

Now the server is not running at all.

Ender answered 20/1, 2021 at 14:16 Comment(6)
Which OS are you using?Workbench
Windows 10 64-bit OS, Node v14.10.0 @WorkbenchEnder
When using pm2, make sure to strip out all cluster/master-related code from your codeDorie
Other than that, it's possible that it's OS-related like Miki said. There is no configuration needed to make node/pm2 distribute requests between workers in a cluster. On a second though, I believe pm2 actually uses cluster module internally, so OS is the culprit, it's most likely that using pm2 won't change anything.Dorie
I strip out all cluster/master-related code, See my update (GIF). Is there any way to trace "Did Windows OS allow pm2 to create worker processes?" or any OS related check? @DorieEnder
Well, it probably did allow, according to the GIF, but doesn't route the messagesDorie
W
0

I got this

enter image description here

Probably it is OS related,I am on Ubuntu 20.04.

Workbench answered 28/1, 2021 at 10:1 Comment(0)
I
0

you don't have cluster mode enabled if you want to use pm2 as load balancer you need to add exec_mode cluster

add this config file name it config.js

module.exports = {
  apps : [{
    script    : "app.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

and run pm2 start config.js

then all CPU usage will divide equally

tasted on os macOS Catalina 10.15.7 node v14.15.4

enter image description here

Irmgard answered 28/1, 2021 at 10:7 Comment(4)
Let me try this. I will share my update @Naor Tedgi. And thanks for your responseEnder
I follow what you said but still the problem is not solved. Check my update please.Ender
remove -i 1 from the pm2 start scriptIrmgard
I just follow their docs which said to use -i max/-1 to enable the cluster mode (auto-detect the number of available CPUs). Then how could I produce multi instances of my server? @Naor TedgiEnder
D
0

Not sure why, but it seems that for whatever reason cluster doesn't behave on your machine the way it should.

In lieu of using node.js for balancing you can go for nginx then. On nginx part it's fairly easy if one of the available strategies is enough for you: http://nginx.org/en/docs/http/load_balancing.html

Then you need to make sure that your node processes are assigned different ports. In pm2 you can either use https://pm2.keymetrics.io/docs/usage/environment/ to either manually increment port based on the instance id or delegate it fully to pm2.

Needless to say, you'll have to send your requests to nginx in this case.

Dorie answered 30/1, 2021 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.