nodejs / nodejs/node

Cannot set up `cluster.schedulingPolicy` in ESM

Đang mở
#49,240 17 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

cluster
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.4k
Merge trung bình
4 ngày 3 giờ
Pull request đã merge (30 ngày)
272

Mô tả

Version

v18.17.1

Platform

Linux xxx 6.2.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

cluster

What steps will reproduce the bug?

Run the following code in Node.js:

import * as cluster from 'cluster'

if (cluster.isPrimary) { 
    process.env.NODE_CLUSTER_SCHED_POLICY = 'none'
    console.log(`process.env.NODE_CLUSTER_SCHED_POLICY: ${process.env.NODE_CLUSTER_SCHED_POLICY}`)
    for (let index = 0; index < 2; index++) { 
        cluster.fork()
        setTimeout(() => console.log(`cluster.schedulingPolicy: ${cluster.schedulingPolicy}`), 1000)
    }
} else { 
    setTimeout(() => null, 1000)
}

cluster-error.txt
Attached as cluster-error.txt. You will see that it prints the following:

$ node cluster-error.js 
process.env.NODE_CLUSTER_SCHED_POLICY: 'none'
cluster.schedulingPolicy: 2
cluster.schedulingPolicy: 2

So the env variable NODE_CLUSTER_SCHED_POLICY has no effect, cluster.schedulingPolicy has the value 2 which is the default cluster.SCHED_RR. However running it with the env variable from the console works as expected:

$ NODE_CLUSTER_SCHED_POLICY='none' node cluster-error.js 
process.env.NODE_CLUSTER_SCHED_POLICY: none
cluster.schedulingPolicy: 1
cluster.schedulingPolicy: 1

I cannot set the cluster.schedulingPolicy as per the docs: cluster.schedulingPolicy = cluster.SCHED_NONE since the cluster module is imported:

import * as cluster from 'cluster'
cluster.schedulingPolicy = cluster.SCHED_NONE

so I get a TypeError:

cluster.schedulingPolicy = cluster.SCHED_NONE
                         ^

TypeError: Cannot assign to read only property 'schedulingPolicy' of object '[object Module]'
    at file:///home/alex/projects/loadtest/cluster-error.js:2:26
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
How often does it reproduce? Is there a required condition?

With the attached code it works (as in doesn't work) always.

What is the expected behavior? Why is that the expected behavior?

I expect to see the same output as with the env variable NODE_CLUSTER_SCHED_POLICY='none', i.e. the cluster.schedulingPolicy is set to 1 cluster.SCHED_NONE:

$ node cluster-error.js 
process.env.NODE_CLUSTER_SCHED_POLICY: none
cluster.schedulingPolicy: 1
cluster.schedulingPolicy: 1

(Fake output.)

What do you see instead?

Instead I see cluster.schedulingPolicy has the default value of 2 cluster.SCHED_RR:

$ node cluster-error.js 
process.env.NODE_CLUSTER_SCHED_POLICY: none
cluster.schedulingPolicy: 2
cluster.schedulingPolicy: 2
Additional information

I am the author of the loadtest package and I am converting it to multi-core. I really see a big difference of more than 30% when running the test server in the default round-robin mode and in the none mode I want to set:

  • cluster.SCHED_RR: 9600 rps.
  • cluster.SCHED_NONE: 12700 rps.

It is true that load on processors is a bit more uneven in none, but that doesn't seem to matter much. I want my lightning fast speed! ⚡

Besides, I think that the documentation should be updated because the recommended way of setting cluster.schedulingPolicy does not work. I would think that the best solution is to have a setting in cluster.settings that can be set with cluster.setupPrimary([settings]). As suggested by @bnoordhuis in this hopeful comment.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ ESM và đọc lib/internal/cluster/primary.js cùng với tài liệu về cluster.schedulingPolicy, cluster.settings và cluster.setupPrimary. Theo dõi cách policy được khởi tạo và xác định đường dẫn cấu hình ESM được hỗ trợ; hoàn thành khi trường hợp ESM được báo cáo đã được xử lý và tài liệu mô tả chính xác cách thiết lập scheduling policy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, node.js
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.