X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=03fc0df192f3ce6689306ba3f6eb38c12dabed40;hb=be71219a2e687e8a46a167503df4f5620807db0b;hp=c2acd65b26290f576a1deb64630ceca5cb580466;hpb=60664f486ce7ff5a437b52c545cf219eca5aebd2;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index c2acd65b..03fc0df1 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,9 +1,18 @@ +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 BackPressureCallback, + type EmptyQueueCallback, type IWorker, type IWorkerNode, - type Task, type WorkerInfo, type WorkerType, WorkerTypes, @@ -18,23 +27,64 @@ import { */ export class WorkerNode implements IWorkerNode { + /** @inheritdoc */ public readonly worker: Worker + /** @inheritdoc */ public readonly info: WorkerInfo + /** @inheritdoc */ public usage: WorkerUsage - 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. - * @internal + * @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.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 */ @@ -42,23 +92,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 */ @@ -66,8 +133,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.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 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] + } + 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 { @@ -75,16 +198,16 @@ implements IWorkerNode { id: this.getWorkerId(worker, workerType), type: workerType, dynamic: false, - started: true + ready: false } } 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: { @@ -96,6 +219,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: {