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