-import { MessageChannel } from 'node:worker_threads'
import { EventEmitter } from 'node:events'
-import { CircularArray } from '../circular-array.js'
+import { MessageChannel } from 'node:worker_threads'
+
+import { CircularBuffer } from '../circular-buffer.js'
+import { PriorityQueue } from '../priority-queue.js'
import type { Task } from '../utility-types.js'
-import { DEFAULT_TASK_NAME, getWorkerId, getWorkerType } from '../utils.js'
-import { Deque } from '../deque.js'
+import { DEFAULT_TASK_NAME } from '../utils.js'
+import {
+ checkWorkerNodeArguments,
+ createWorker,
+ getWorkerId,
+ getWorkerType,
+} from './utils.js'
import {
type EventHandler,
type IWorker,
type IWorkerNode,
+ MeasurementHistorySize,
type StrategyData,
type WorkerInfo,
type WorkerNodeOptions,
type WorkerType,
WorkerTypes,
- type WorkerUsage
+ type WorkerUsage,
} from './worker.js'
-import { checkWorkerNodeArguments, createWorker } from './utils.js'
/**
* Worker node.
- *
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
*/
public messageChannel?: MessageChannel
/** @inheritdoc */
public tasksQueueBackPressureSize: number
- private readonly tasksQueue: Deque<Task<Data>>
- private onBackPressureStarted: boolean
+ private readonly tasksQueue: PriorityQueue<Task<Data>>
+ private setBackPressureFlag: boolean
private readonly taskFunctionsUsage: Map<string, WorkerUsage>
/**
* Constructs a new worker node.
- *
* @param type - The worker type.
* @param filePath - Path to the worker file.
* @param opts - The worker node options.
checkWorkerNodeArguments(type, filePath, opts)
this.worker = createWorker<Worker>(type, filePath, {
env: opts.env,
- workerOptions: opts.workerOptions
+ workerOptions: opts.workerOptions,
})
this.info = this.initWorkerInfo(this.worker)
this.usage = this.initWorkerUsage()
if (this.info.type === WorkerTypes.thread) {
this.messageChannel = new MessageChannel()
}
- this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize
- this.tasksQueue = new Deque<Task<Data>>()
- this.onBackPressureStarted = false
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize!
+ this.tasksQueue = new PriorityQueue<Task<Data>>(
+ opts.tasksQueueBucketSize,
+ opts.tasksQueuePriority
+ )
+ this.setBackPressureFlag = false
this.taskFunctionsUsage = new Map<string, WorkerUsage>()
}
+ /** @inheritdoc */
+ public setTasksQueuePriority (enablePriority: boolean): void {
+ this.tasksQueue.enablePriority = enablePriority
+ }
+
/** @inheritdoc */
public tasksQueueSize (): number {
return this.tasksQueue.size
/** @inheritdoc */
public enqueueTask (task: Task<Data>): number {
- const tasksQueueSize = this.tasksQueue.push(task)
- if (this.hasBackPressure() && !this.onBackPressureStarted) {
- this.onBackPressureStarted = true
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.emit('backPressure', { workerId: this.info.id! })
- this.onBackPressureStarted = false
+ const tasksQueueSize = this.tasksQueue.enqueue(task, task.priority)
+ if (
+ !this.setBackPressureFlag &&
+ this.hasBackPressure() &&
+ !this.info.backPressure
+ ) {
+ this.setBackPressureFlag = true
+ this.info.backPressure = true
+ this.emit('backPressure', { workerId: this.info.id })
+ this.setBackPressureFlag = false
}
return tasksQueueSize
}
/** @inheritdoc */
- public unshiftTask (task: Task<Data>): number {
- const tasksQueueSize = this.tasksQueue.unshift(task)
- if (this.hasBackPressure() && !this.onBackPressureStarted) {
- this.onBackPressureStarted = true
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- this.emit('backPressure', { workerId: this.info.id! })
- this.onBackPressureStarted = false
+ public dequeueTask (bucket?: number): Task<Data> | undefined {
+ const task = this.tasksQueue.dequeue(bucket)
+ if (
+ !this.setBackPressureFlag &&
+ !this.hasBackPressure() &&
+ this.info.backPressure
+ ) {
+ this.setBackPressureFlag = true
+ this.info.backPressure = false
+ this.setBackPressureFlag = false
}
- return tasksQueueSize
+ return task
}
/** @inheritdoc */
- public dequeueTask (): Task<Data> | undefined {
- return this.tasksQueue.shift()
- }
-
- /** @inheritdoc */
- public popTask (): Task<Data> | undefined {
- return this.tasksQueue.pop()
+ public dequeueLastPrioritizedTask (): Task<Data> | undefined {
+ // Start from the last empty or partially filled bucket
+ return this.dequeueTask(this.tasksQueue.buckets + 1)
}
/** @inheritdoc */
return this.tasksQueue.size >= this.tasksQueueBackPressureSize
}
- /** @inheritdoc */
- public resetUsage (): void {
- this.usage = this.initWorkerUsage()
- this.taskFunctionsUsage.clear()
- }
-
/** @inheritdoc */
public async terminate (): Promise<void> {
const waitWorkerExit = new Promise<void>(resolve => {
this.removeAllListeners()
switch (this.info.type) {
case WorkerTypes.thread:
+ this.worker.unref?.()
await this.worker.terminate?.()
break
case WorkerTypes.cluster:
/** @inheritdoc */
public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
- if (!Array.isArray(this.info.taskFunctionNames)) {
+ if (!Array.isArray(this.info.taskFunctionsProperties)) {
throw new Error(
- `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
+ `Cannot get task function worker usage for task function name '${name}' when task function properties list is not yet defined`
)
}
if (
- Array.isArray(this.info.taskFunctionNames) &&
- this.info.taskFunctionNames.length < 3
+ Array.isArray(this.info.taskFunctionsProperties) &&
+ this.info.taskFunctionsProperties.length < 3
) {
throw new Error(
- `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
+ `Cannot get task function worker usage for task function name '${name}' when task function properties list has less than 3 elements`
)
}
if (name === DEFAULT_TASK_NAME) {
- name = this.info.taskFunctionNames[1]
+ name = this.info.taskFunctionsProperties[1].name
}
if (!this.taskFunctionsUsage.has(name)) {
this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
type: getWorkerType(worker)!,
dynamic: false,
ready: false,
- stealing: false
+ stealing: false,
+ backPressure: false,
}
}
},
sequentiallyStolen: 0,
stolen: 0,
- failed: 0
+ failed: 0,
},
runTime: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
waitTime: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
elu: {
idle: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
active: {
- history: new CircularArray<number>()
- }
- }
+ history: new CircularBuffer(MeasurementHistorySize),
+ },
+ },
}
}
if (
(task.name === DEFAULT_TASK_NAME &&
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- name === this.info.taskFunctionNames![1]) ||
+ name === this.info.taskFunctionsProperties![1].name) ||
(task.name !== DEFAULT_TASK_NAME && name === task.name)
) {
++taskFunctionQueueSize
},
sequentiallyStolen: 0,
stolen: 0,
- failed: 0
+ failed: 0,
},
runTime: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
waitTime: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
elu: {
idle: {
- history: new CircularArray<number>()
+ history: new CircularBuffer(MeasurementHistorySize),
},
active: {
- history: new CircularArray<number>()
- }
- }
+ history: new CircularBuffer(MeasurementHistorySize),
+ },
+ },
}
}
}