Commit | Line | Data |
---|---|---|
85aeb3f3 | 1 | import { MessageChannel } from 'node:worker_threads' |
4b628b48 | 2 | import { CircularArray } from '../circular-array' |
5c4d16da | 3 | import type { Task } from '../utility-types' |
68cbdc84 JB |
4 | import { |
5 | DEFAULT_TASK_NAME, | |
6 | EMPTY_FUNCTION, | |
7 | exponentialDelay, | |
8 | sleep | |
9 | } from '../utils' | |
574b351d | 10 | import { Deque } from '../deque' |
4b628b48 JB |
11 | import { |
12 | type IWorker, | |
13 | type IWorkerNode, | |
4b628b48 JB |
14 | type WorkerInfo, |
15 | type WorkerType, | |
16 | WorkerTypes, | |
17 | type WorkerUsage | |
18 | } from './worker' | |
19 | ||
6e9c39d3 JB |
20 | type EmptyQueueCallback = (workerId: number) => void |
21 | type BackPressureCallback = EmptyQueueCallback | |
22 | ||
60664f48 JB |
23 | /** |
24 | * Worker node. | |
25 | * | |
26 | * @typeParam Worker - Type of worker. | |
27 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. | |
28 | */ | |
4b628b48 JB |
29 | export class WorkerNode<Worker extends IWorker, Data = unknown> |
30 | implements IWorkerNode<Worker, Data> { | |
671d5154 | 31 | /** @inheritdoc */ |
4b628b48 | 32 | public readonly worker: Worker |
671d5154 | 33 | /** @inheritdoc */ |
4b628b48 | 34 | public readonly info: WorkerInfo |
671d5154 | 35 | /** @inheritdoc */ |
4b628b48 | 36 | public usage: WorkerUsage |
20c6f652 | 37 | /** @inheritdoc */ |
26fb3c18 JB |
38 | public messageChannel?: MessageChannel |
39 | /** @inheritdoc */ | |
20c6f652 | 40 | public tasksQueueBackPressureSize: number |
72695f86 | 41 | /** @inheritdoc */ |
6e9c39d3 | 42 | public onBackPressure?: BackPressureCallback |
dd951876 | 43 | /** @inheritdoc */ |
6e9c39d3 | 44 | public onEmptyQueue?: EmptyQueueCallback |
574b351d | 45 | private readonly tasksQueue: Deque<Task<Data>> |
68cbdc84 | 46 | private onEmptyQueueCount: number |
26fb3c18 | 47 | private readonly taskFunctionsUsage: Map<string, WorkerUsage> |
4b628b48 | 48 | |
60664f48 JB |
49 | /** |
50 | * Constructs a new worker node. | |
51 | * | |
52 | * @param worker - The worker. | |
53 | * @param workerType - The worker type. | |
20c6f652 | 54 | * @param tasksQueueBackPressureSize - The tasks queue back pressure size. |
60664f48 | 55 | */ |
20c6f652 JB |
56 | constructor ( |
57 | worker: Worker, | |
58 | workerType: WorkerType, | |
59 | tasksQueueBackPressureSize: number | |
60 | ) { | |
8735b4e5 | 61 | if (worker == null) { |
e695d66f | 62 | throw new TypeError('Cannot construct a worker node without a worker') |
8735b4e5 JB |
63 | } |
64 | if (workerType == null) { | |
e695d66f JB |
65 | throw new TypeError( |
66 | 'Cannot construct a worker node without a worker type' | |
67 | ) | |
8735b4e5 | 68 | } |
20c6f652 | 69 | if (tasksQueueBackPressureSize == null) { |
e695d66f | 70 | throw new TypeError( |
20c6f652 | 71 | 'Cannot construct a worker node without a tasks queue back pressure size' |
8735b4e5 JB |
72 | ) |
73 | } | |
20c6f652 | 74 | if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { |
e695d66f | 75 | throw new TypeError( |
20c6f652 | 76 | 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' |
8735b4e5 JB |
77 | ) |
78 | } | |
4b628b48 JB |
79 | this.worker = worker |
80 | this.info = this.initWorkerInfo(worker, workerType) | |
26fb3c18 | 81 | this.usage = this.initWorkerUsage() |
7884d183 JB |
82 | if (workerType === WorkerTypes.thread) { |
83 | this.messageChannel = new MessageChannel() | |
84 | } | |
20c6f652 | 85 | this.tasksQueueBackPressureSize = tasksQueueBackPressureSize |
26fb3c18 | 86 | this.tasksQueue = new Deque<Task<Data>>() |
68cbdc84 | 87 | this.onEmptyQueueCount = 0 |
26fb3c18 | 88 | this.taskFunctionsUsage = new Map<string, WorkerUsage>() |
4b628b48 JB |
89 | } |
90 | ||
91 | /** @inheritdoc */ | |
92 | public tasksQueueSize (): number { | |
93 | return this.tasksQueue.size | |
94 | } | |
95 | ||
4b628b48 JB |
96 | /** @inheritdoc */ |
97 | public enqueueTask (task: Task<Data>): number { | |
72695f86 JB |
98 | const tasksQueueSize = this.tasksQueue.push(task) |
99 | if (this.onBackPressure != null && this.hasBackPressure()) { | |
0741fbeb | 100 | this.onBackPressure(this.info.id as number) |
72695f86 JB |
101 | } |
102 | return tasksQueueSize | |
103 | } | |
104 | ||
105 | /** @inheritdoc */ | |
106 | public unshiftTask (task: Task<Data>): number { | |
107 | const tasksQueueSize = this.tasksQueue.unshift(task) | |
108 | if (this.onBackPressure != null && this.hasBackPressure()) { | |
0741fbeb | 109 | this.onBackPressure(this.info.id as number) |
72695f86 JB |
110 | } |
111 | return tasksQueueSize | |
4b628b48 JB |
112 | } |
113 | ||
114 | /** @inheritdoc */ | |
115 | public dequeueTask (): Task<Data> | undefined { | |
dd951876 JB |
116 | const task = this.tasksQueue.shift() |
117 | if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { | |
68cbdc84 | 118 | this.startOnEmptyQueue().catch(EMPTY_FUNCTION) |
dd951876 JB |
119 | } |
120 | return task | |
4b628b48 JB |
121 | } |
122 | ||
72695f86 JB |
123 | /** @inheritdoc */ |
124 | public popTask (): Task<Data> | undefined { | |
dd951876 JB |
125 | const task = this.tasksQueue.pop() |
126 | if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { | |
68cbdc84 | 127 | this.startOnEmptyQueue().catch(EMPTY_FUNCTION) |
dd951876 JB |
128 | } |
129 | return task | |
72695f86 JB |
130 | } |
131 | ||
4b628b48 JB |
132 | /** @inheritdoc */ |
133 | public clearTasksQueue (): void { | |
134 | this.tasksQueue.clear() | |
135 | } | |
136 | ||
671d5154 JB |
137 | /** @inheritdoc */ |
138 | public hasBackPressure (): boolean { | |
8735b4e5 | 139 | return this.tasksQueue.size >= this.tasksQueueBackPressureSize |
671d5154 JB |
140 | } |
141 | ||
ff128cc9 | 142 | /** @inheritdoc */ |
4b628b48 JB |
143 | public resetUsage (): void { |
144 | this.usage = this.initWorkerUsage() | |
db0e38ee | 145 | this.taskFunctionsUsage.clear() |
ff128cc9 JB |
146 | } |
147 | ||
3f09ed9f JB |
148 | /** @inheritdoc */ |
149 | public closeChannel (): void { | |
7884d183 JB |
150 | if (this.messageChannel != null) { |
151 | this.messageChannel?.port1.unref() | |
152 | this.messageChannel?.port2.unref() | |
153 | this.messageChannel?.port1.close() | |
154 | this.messageChannel?.port2.close() | |
155 | delete this.messageChannel | |
3f09ed9f JB |
156 | } |
157 | } | |
158 | ||
ff128cc9 | 159 | /** @inheritdoc */ |
db0e38ee | 160 | public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { |
a5d15204 | 161 | if (!Array.isArray(this.info.taskFunctions)) { |
71b2b6d8 | 162 | throw new Error( |
db0e38ee | 163 | `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` |
71b2b6d8 JB |
164 | ) |
165 | } | |
b558f6b5 | 166 | if ( |
71b2b6d8 | 167 | Array.isArray(this.info.taskFunctions) && |
db0e38ee | 168 | this.info.taskFunctions.length < 3 |
b558f6b5 | 169 | ) { |
db0e38ee JB |
170 | throw new Error( |
171 | `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements` | |
172 | ) | |
173 | } | |
174 | if (name === DEFAULT_TASK_NAME) { | |
71b2b6d8 | 175 | name = this.info.taskFunctions[1] |
b558f6b5 | 176 | } |
db0e38ee JB |
177 | if (!this.taskFunctionsUsage.has(name)) { |
178 | this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) | |
ff128cc9 | 179 | } |
db0e38ee | 180 | return this.taskFunctionsUsage.get(name) |
4b628b48 JB |
181 | } |
182 | ||
68cbdc84 | 183 | private async startOnEmptyQueue (): Promise<void> { |
1f0766e7 JB |
184 | if ( |
185 | this.onEmptyQueueCount > 0 && | |
79b197bb | 186 | (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) |
1f0766e7 | 187 | ) { |
60b7a7cc JB |
188 | this.onEmptyQueueCount = 0 |
189 | return | |
190 | } | |
6e9c39d3 | 191 | (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) |
60b7a7cc JB |
192 | ++this.onEmptyQueueCount |
193 | await sleep(exponentialDelay(this.onEmptyQueueCount)) | |
194 | await this.startOnEmptyQueue() | |
68cbdc84 JB |
195 | } |
196 | ||
4b628b48 JB |
197 | private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { |
198 | return { | |
199 | id: this.getWorkerId(worker, workerType), | |
200 | type: workerType, | |
201 | dynamic: false, | |
7884d183 | 202 | ready: false |
4b628b48 JB |
203 | } |
204 | } | |
205 | ||
206 | private initWorkerUsage (): WorkerUsage { | |
207 | const getTasksQueueSize = (): number => { | |
dd951876 | 208 | return this.tasksQueue.size |
4b628b48 | 209 | } |
bf4ef2ca | 210 | const getTasksQueueMaxSize = (): number => { |
dd951876 | 211 | return this.tasksQueue.maxSize |
4b628b48 JB |
212 | } |
213 | return { | |
214 | tasks: { | |
215 | executed: 0, | |
216 | executing: 0, | |
217 | get queued (): number { | |
218 | return getTasksQueueSize() | |
219 | }, | |
220 | get maxQueued (): number { | |
bf4ef2ca | 221 | return getTasksQueueMaxSize() |
4b628b48 | 222 | }, |
68cbdc84 | 223 | stolen: 0, |
4b628b48 JB |
224 | failed: 0 |
225 | }, | |
226 | runTime: { | |
227 | history: new CircularArray() | |
228 | }, | |
229 | waitTime: { | |
230 | history: new CircularArray() | |
231 | }, | |
232 | elu: { | |
233 | idle: { | |
234 | history: new CircularArray() | |
235 | }, | |
236 | active: { | |
237 | history: new CircularArray() | |
238 | } | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
db0e38ee | 243 | private initTaskFunctionWorkerUsage (name: string): WorkerUsage { |
e5ece61d JB |
244 | const getTaskFunctionQueueSize = (): number => { |
245 | let taskFunctionQueueSize = 0 | |
b25a42cd | 246 | for (const task of this.tasksQueue) { |
dd92a715 | 247 | if ( |
e5ece61d JB |
248 | (task.name === DEFAULT_TASK_NAME && |
249 | name === (this.info.taskFunctions as string[])[1]) || | |
250 | (task.name !== DEFAULT_TASK_NAME && name === task.name) | |
dd92a715 | 251 | ) { |
e5ece61d | 252 | ++taskFunctionQueueSize |
b25a42cd JB |
253 | } |
254 | } | |
e5ece61d | 255 | return taskFunctionQueueSize |
b25a42cd JB |
256 | } |
257 | return { | |
258 | tasks: { | |
259 | executed: 0, | |
260 | executing: 0, | |
261 | get queued (): number { | |
e5ece61d | 262 | return getTaskFunctionQueueSize() |
b25a42cd | 263 | }, |
68cbdc84 | 264 | stolen: 0, |
b25a42cd JB |
265 | failed: 0 |
266 | }, | |
267 | runTime: { | |
268 | history: new CircularArray() | |
269 | }, | |
270 | waitTime: { | |
271 | history: new CircularArray() | |
272 | }, | |
273 | elu: { | |
274 | idle: { | |
275 | history: new CircularArray() | |
276 | }, | |
277 | active: { | |
278 | history: new CircularArray() | |
279 | } | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
4b628b48 JB |
284 | /** |
285 | * Gets the worker id. | |
286 | * | |
287 | * @param worker - The worker. | |
60664f48 | 288 | * @param workerType - The worker type. |
4b628b48 JB |
289 | * @returns The worker id. |
290 | */ | |
291 | private getWorkerId ( | |
292 | worker: Worker, | |
293 | workerType: WorkerType | |
294 | ): number | undefined { | |
295 | if (workerType === WorkerTypes.thread) { | |
296 | return worker.threadId | |
297 | } else if (workerType === WorkerTypes.cluster) { | |
298 | return worker.id | |
299 | } | |
300 | } | |
301 | } |