X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=772a839b1c077d23ae53f73b7a1475daa4c8bddc;hb=2eee72204bc851f616ded11cb5381f96c6dc5cbf;hp=1f46075a07e2a4849b6dcdd4a2f996fcea64e6be;hpb=75de9f41ce00bec38febd6d82653d3d82f1bb884;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 1f46075a..772a839b 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,25 +1,27 @@ +import { EventEmitter } from 'node:events' import { MessageChannel } from 'node:worker_threads' -import { CircularArray } from '../circular-array' -import type { Task } from '../utility-types' + +import { CircularArray } from '../circular-array.js' +import { PriorityQueue } from '../priority-queue.js' +import type { Task } from '../utility-types.js' +import { DEFAULT_TASK_NAME } from '../utils.js' import { - DEFAULT_TASK_NAME, - EMPTY_FUNCTION, - exponentialDelay, + checkWorkerNodeArguments, + createWorker, getWorkerId, - getWorkerType, - sleep -} from '../utils' -import { Deque } from '../deque' + getWorkerType +} from './utils.js' import { - type BackPressureCallback, - type EmptyQueueCallback, + type EventHandler, type IWorker, type IWorkerNode, + type StrategyData, type WorkerInfo, + type WorkerNodeOptions, type WorkerType, WorkerTypes, type WorkerUsage -} from './worker' +} from './worker.js' /** * Worker node. @@ -28,7 +30,8 @@ import { * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventEmitter + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ @@ -36,47 +39,38 @@ implements IWorkerNode { /** @inheritdoc */ public usage: WorkerUsage /** @inheritdoc */ + public strategyData?: StrategyData + /** @inheritdoc */ public messageChannel?: MessageChannel /** @inheritdoc */ public tasksQueueBackPressureSize: number - /** @inheritdoc */ - public onBackPressure?: BackPressureCallback - /** @inheritdoc */ - public onEmptyQueue?: EmptyQueueCallback - private readonly tasksQueue: Deque> - private onEmptyQueueCount: number + private readonly tasksQueue: PriorityQueue> + private setBackPressureFlag: boolean private readonly taskFunctionsUsage: Map /** * Constructs a new worker node. * - * @param worker - The worker. - * @param tasksQueueBackPressureSize - The tasks queue back pressure size. + * @param type - The worker type. + * @param filePath - Path to the worker file. + * @param opts - The worker node options. */ - constructor (worker: Worker, tasksQueueBackPressureSize: number) { - if (worker == null) { - throw new TypeError('Cannot construct a worker node without a worker') - } - - if (tasksQueueBackPressureSize == null) { - throw new TypeError( - 'Cannot construct a worker node without a tasks queue back pressure size' - ) - } - if (!Number.isSafeInteger(tasksQueueBackPressureSize)) { - throw new TypeError( - 'Cannot construct a worker node with a tasks queue back pressure size that is not an integer' - ) - } - this.worker = worker - this.info = this.initWorkerInfo(worker) + constructor (type: WorkerType, filePath: string, opts: WorkerNodeOptions) { + super() + checkWorkerNodeArguments(type, filePath, opts) + this.worker = createWorker(type, filePath, { + env: opts.env, + 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 = tasksQueueBackPressureSize - this.tasksQueue = new Deque>() - this.onEmptyQueueCount = 0 + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize! + this.tasksQueue = new PriorityQueue>(opts.tasksQueueBucketSize) + this.setBackPressureFlag = false this.taskFunctionsUsage = new Map() } @@ -87,38 +81,39 @@ implements IWorkerNode { /** @inheritdoc */ public enqueueTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.push(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) - } - return tasksQueueSize - } - - /** @inheritdoc */ - public unshiftTask (task: Task): number { - const tasksQueueSize = this.tasksQueue.unshift(task) - if (this.onBackPressure != null && this.hasBackPressure()) { - this.onBackPressure(this.info.id as number) + 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 dequeueTask (): Task | undefined { - const task = this.tasksQueue.shift() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { - this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + public dequeueTask (bucket?: number): Task | 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 task } /** @inheritdoc */ - public popTask (): Task | undefined { - const task = this.tasksQueue.pop() - if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { - this.startOnEmptyQueue().catch(EMPTY_FUNCTION) - } - return task + public dequeueLastPrioritizedTask (): Task | undefined { + // Start from the last empty or partially filled bucket + return this.dequeueTask(this.tasksQueue.buckets + 1) } /** @inheritdoc */ @@ -132,39 +127,62 @@ implements IWorkerNode { } /** @inheritdoc */ - public resetUsage (): void { - this.usage = this.initWorkerUsage() - this.taskFunctionsUsage.clear() + public async terminate (): Promise { + const waitWorkerExit = new Promise(resolve => { + this.registerOnceWorkerEventHandler('exit', () => { + resolve() + }) + }) + this.closeMessageChannel() + this.removeAllListeners() + switch (this.info.type) { + case WorkerTypes.thread: + this.worker.unref?.() + await this.worker.terminate?.() + break + case WorkerTypes.cluster: + this.registerOnceWorkerEventHandler('disconnect', () => { + this.worker.kill?.() + }) + this.worker.disconnect?.() + break + } + await waitWorkerExit } /** @inheritdoc */ - public closeChannel (): void { - if (this.messageChannel != null) { - this.messageChannel?.port1.unref() - this.messageChannel?.port2.unref() - this.messageChannel?.port1.close() - this.messageChannel?.port2.close() - delete this.messageChannel - } + public registerWorkerEventHandler ( + event: string, + handler: EventHandler + ): void { + this.worker.on(event, handler) + } + + /** @inheritdoc */ + public registerOnceWorkerEventHandler ( + event: string, + handler: EventHandler + ): void { + this.worker.once(event, handler) } /** @inheritdoc */ public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { - if (!Array.isArray(this.info.taskFunctions)) { + 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.taskFunctions) && - this.info.taskFunctions.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.taskFunctions[1] + name = this.info.taskFunctionsProperties[1].name } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -172,26 +190,30 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } - private async startOnEmptyQueue (): Promise { - if ( - this.onEmptyQueueCount > 0 && - (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) - ) { - this.onEmptyQueueCount = 0 - return + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) + } + + private closeMessageChannel (): void { + if (this.messageChannel != null) { + this.messageChannel.port1.unref() + this.messageChannel.port2.unref() + this.messageChannel.port1.close() + this.messageChannel.port2.close() + delete this.messageChannel } - (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) - ++this.onEmptyQueueCount - await sleep(exponentialDelay(this.onEmptyQueueCount)) - await this.startOnEmptyQueue() } private initWorkerInfo (worker: Worker): WorkerInfo { return { id: getWorkerId(worker), - type: getWorkerType(worker) as WorkerType, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + type: getWorkerType(worker)!, dynamic: false, - ready: false + ready: false, + backPressure: false, + stealing: false } } @@ -212,21 +234,22 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } } @@ -238,7 +261,8 @@ implements IWorkerNode { for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctions as string[])[1]) || + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name === this.info.taskFunctionsProperties![1].name) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize @@ -253,21 +277,22 @@ implements IWorkerNode { get queued (): number { return getTaskFunctionQueueSize() }, + sequentiallyStolen: 0, stolen: 0, failed: 0 }, runTime: { - history: new CircularArray() + history: new CircularArray() }, waitTime: { - history: new CircularArray() + history: new CircularArray() }, elu: { idle: { - history: new CircularArray() + history: new CircularArray() }, active: { - history: new CircularArray() + history: new CircularArray() } } }