X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=f3ec3be3d83f4541da3713cbd46406d19be41ac0;hb=6e9c39d3eedabc9f12808490983d36e551a75a28;hp=7a7fc237180d32c28b74250d5943867dcf80d9de;hpb=67e8ef11907ab8ae70740d7c9f4d5d225ed8d522;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 7a7fc237..f3ec3be3 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,15 +1,25 @@ +import { MessageChannel } from 'node:worker_threads' import { CircularArray } from '../circular-array' -import { Queue } from '../queue' +import type { Task } from '../utility-types' +import { + DEFAULT_TASK_NAME, + EMPTY_FUNCTION, + exponentialDelay, + sleep +} from '../utils' +import { Deque } from '../deque' import { type IWorker, type IWorkerNode, - type Task, type WorkerInfo, type WorkerType, WorkerTypes, type WorkerUsage } from './worker' +type EmptyQueueCallback = (workerId: number) => void +type BackPressureCallback = EmptyQueueCallback + /** * Worker node. * @@ -18,24 +28,64 @@ import { */ export class WorkerNode implements IWorkerNode { + /** @inheritdoc */ public readonly worker: Worker + /** @inheritdoc */ public readonly info: WorkerInfo + /** @inheritdoc */ public usage: WorkerUsage - private readonly tasksUsage: Map - private readonly tasksQueue: Queue> + /** @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 taskFunctionsUsage: Map /** * Constructs a new worker node. * * @param worker - The worker. * @param workerType - The worker type. + * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor (worker: Worker, workerType: WorkerType) { + constructor ( + worker: Worker, + workerType: WorkerType, + tasksQueueBackPressureSize: number + ) { + if (worker == null) { + throw new TypeError('Cannot construct a worker node without a worker') + } + if (workerType == null) { + throw new TypeError( + 'Cannot construct a worker node without a worker type' + ) + } + 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, workerType) this.usage = this.initWorkerUsage() - this.tasksUsage = new Map() - this.tasksQueue = new Queue>() + if (workerType === WorkerTypes.thread) { + this.messageChannel = new MessageChannel() + } + this.tasksQueueBackPressureSize = tasksQueueBackPressureSize + this.tasksQueue = new Deque>() + this.onEmptyQueueCount = 0 + this.taskFunctionsUsage = new Map() } /** @inheritdoc */ @@ -43,23 +93,40 @@ implements IWorkerNode { return this.tasksQueue.size } - /** - * Worker node tasks queue maximum size. - * - * @returns The tasks queue maximum size. - */ - private tasksQueueMaxSize (): number { - return this.tasksQueue.maxSize + /** @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 enqueueTask (task: Task): number { - return this.tasksQueue.enqueue(task) + public unshiftTask (task: Task): number { + const tasksQueueSize = this.tasksQueue.unshift(task) + if (this.onBackPressure != null && this.hasBackPressure()) { + this.onBackPressure(this.info.id as number) + } + return tasksQueueSize } /** @inheritdoc */ public dequeueTask (): Task | undefined { - return this.tasksQueue.dequeue() + const task = this.tasksQueue.shift() + if (this.onEmptyQueue != null && this.tasksQueue.size === 0) { + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + } + 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 } /** @inheritdoc */ @@ -67,18 +134,64 @@ implements IWorkerNode { this.tasksQueue.clear() } + /** @inheritdoc */ + public hasBackPressure (): boolean { + return this.tasksQueue.size >= this.tasksQueueBackPressureSize + } + /** @inheritdoc */ public resetUsage (): void { this.usage = this.initWorkerUsage() - this.tasksUsage.clear() + this.taskFunctionsUsage.clear() + } + + /** @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 + } } /** @inheritdoc */ - public getTasksWorkerUsage (name: string): WorkerUsage | undefined { - if (!this.tasksUsage.has(name)) { - this.tasksUsage.set(name, this.initWorkerUsage()) + public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined { + if (!Array.isArray(this.info.taskFunctions)) { + throw new Error( + `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined` + ) + } + if ( + Array.isArray(this.info.taskFunctions) && + this.info.taskFunctions.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` + ) + } + if (name === DEFAULT_TASK_NAME) { + name = this.info.taskFunctions[1] } - return this.tasksUsage.get(name) + if (!this.taskFunctionsUsage.has(name)) { + this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) + } + 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 + } + (this.onEmptyQueue as EmptyQueueCallback)(this.info.id as number) + ++this.onEmptyQueueCount + await sleep(exponentialDelay(this.onEmptyQueueCount)) + await this.startOnEmptyQueue() } private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { @@ -92,10 +205,10 @@ implements IWorkerNode { private initWorkerUsage (): WorkerUsage { const getTasksQueueSize = (): number => { - return this.tasksQueueSize() + return this.tasksQueue.size } const getTasksQueueMaxSize = (): number => { - return this.tasksQueueMaxSize() + return this.tasksQueue.maxSize } return { tasks: { @@ -107,6 +220,48 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + stolen: 0, + failed: 0 + }, + runTime: { + history: new CircularArray() + }, + waitTime: { + history: new CircularArray() + }, + elu: { + idle: { + history: new CircularArray() + }, + active: { + history: new CircularArray() + } + } + } + } + + private initTaskFunctionWorkerUsage (name: string): WorkerUsage { + const getTaskFunctionQueueSize = (): number => { + let taskFunctionQueueSize = 0 + for (const task of this.tasksQueue) { + if ( + (task.name === DEFAULT_TASK_NAME && + name === (this.info.taskFunctions as string[])[1]) || + (task.name !== DEFAULT_TASK_NAME && name === task.name) + ) { + ++taskFunctionQueueSize + } + } + return taskFunctionQueueSize + } + return { + tasks: { + executed: 0, + executing: 0, + get queued (): number { + return getTaskFunctionQueueSize() + }, + stolen: 0, failed: 0 }, runTime: {