X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=de65f27012634c493d697a0ae9e3972a7f5234db;hb=3ff2b9103a0933bd1575a979c5e0c2baad285eea;hp=09e4a7b5cd8831360365a7a88e338e3b921eaf91;hpb=1416660efeb6aabf366295ebc79067ccc4df9897;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 09e4a7b5..de65f270 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,16 +1,26 @@ 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 } from '../utils' +import { + DEFAULT_TASK_NAME, + EMPTY_FUNCTION, + exponentialDelay, + getWorkerId, + getWorkerType, + sleep +} from '../utils' +import { Deque } from '../deque' import { type IWorker, type IWorkerNode, + type StrategyData, type WorkerInfo, + type WorkerNodeEventDetail, type WorkerType, WorkerTypes, type WorkerUsage } from './worker' +import { checkWorkerNodeArguments } from './utils' /** * Worker node. @@ -19,52 +29,45 @@ import { * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ export class WorkerNode -implements IWorkerNode { + extends EventTarget + implements IWorkerNode { /** @inheritdoc */ public readonly worker: Worker /** @inheritdoc */ public readonly info: WorkerInfo /** @inheritdoc */ + public usage: WorkerUsage + /** @inheritdoc */ + public strategyData?: StrategyData + /** @inheritdoc */ public messageChannel?: MessageChannel /** @inheritdoc */ - public usage: WorkerUsage + public tasksQueueBackPressureSize: number + private readonly tasksQueue: Deque> + private onBackPressureStarted: boolean + private onEmptyQueueCount: number private readonly taskFunctionsUsage: Map - private readonly tasksQueue: Queue> - private readonly tasksQueueBackPressureSize: number /** * Constructs a new worker node. * * @param worker - The worker. - * @param workerType - The worker type. - * @param poolMaxSize - The pool maximum size. + * @param tasksQueueBackPressureSize - The tasks queue back pressure size. */ - constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) { - if (worker == null) { - throw new Error('Cannot construct a worker node without a worker') - } - if (workerType == null) { - throw new Error('Cannot construct a worker node without a worker type') - } - if (poolMaxSize == null) { - throw new Error( - 'Cannot construct a worker node without a pool maximum size' - ) - } - if (isNaN(poolMaxSize)) { - throw new Error( - 'Cannot construct a worker node with a NaN pool maximum size' - ) - } + constructor (worker: Worker, tasksQueueBackPressureSize: number) { + super() + checkWorkerNodeArguments(worker, tasksQueueBackPressureSize) this.worker = worker - this.info = this.initWorkerInfo(worker, workerType) - if (workerType === WorkerTypes.thread) { + this.info = this.initWorkerInfo(worker) + this.usage = this.initWorkerUsage() + if (this.info.type === WorkerTypes.thread) { this.messageChannel = new MessageChannel() } - this.usage = this.initWorkerUsage() + this.tasksQueueBackPressureSize = tasksQueueBackPressureSize + this.tasksQueue = new Deque>() + this.onBackPressureStarted = false + this.onEmptyQueueCount = 0 this.taskFunctionsUsage = new Map() - this.tasksQueue = new Queue>() - this.tasksQueueBackPressureSize = Math.pow(poolMaxSize, 2) } /** @inheritdoc */ @@ -72,23 +75,52 @@ implements IWorkerNode { return this.tasksQueue.size } - /** - * 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.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false + } + 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.hasBackPressure() && !this.onBackPressureStarted) { + this.onBackPressureStarted = true + this.dispatchEvent( + new CustomEvent('backpressure', { + detail: { workerId: this.info.id as number } + }) + ) + this.onBackPressureStarted = false + } + return tasksQueueSize } /** @inheritdoc */ public dequeueTask (): Task | undefined { - return this.tasksQueue.dequeue() + const task = this.tasksQueue.shift() + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + } + return task + } + + /** @inheritdoc */ + public popTask (): Task | undefined { + const task = this.tasksQueue.pop() + if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) { + this.startOnEmptyQueue().catch(EMPTY_FUNCTION) + } + return task } /** @inheritdoc */ @@ -110,31 +142,31 @@ implements IWorkerNode { /** @inheritdoc */ public closeChannel (): void { if (this.messageChannel != null) { - this.messageChannel?.port1.unref() - this.messageChannel?.port2.unref() - this.messageChannel?.port1.close() - this.messageChannel?.port2.close() + 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)) { + if (!Array.isArray(this.info.taskFunctionNames)) { 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 + Array.isArray(this.info.taskFunctionNames) && + this.info.taskFunctionNames.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] + name = this.info.taskFunctionNames[1] } if (!this.taskFunctionsUsage.has(name)) { this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name)) @@ -142,10 +174,33 @@ implements IWorkerNode { return this.taskFunctionsUsage.get(name) } - private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo { + /** @inheritdoc */ + public deleteTaskFunctionWorkerUsage (name: string): boolean { + return this.taskFunctionsUsage.delete(name) + } + + private async startOnEmptyQueue (): Promise { + if ( + this.onEmptyQueueCount > 0 && + (this.usage.tasks.executing > 0 || this.tasksQueue.size > 0) + ) { + this.onEmptyQueueCount = 0 + return + } + ++this.onEmptyQueueCount + this.dispatchEvent( + new CustomEvent('emptyqueue', { + detail: { workerId: this.info.id as number } + }) + ) + await sleep(exponentialDelay(this.onEmptyQueueCount)) + await this.startOnEmptyQueue() + } + + private initWorkerInfo (worker: Worker): WorkerInfo { return { - id: this.getWorkerId(worker, workerType), - type: workerType, + id: getWorkerId(worker), + type: getWorkerType(worker) as WorkerType, dynamic: false, ready: false } @@ -153,10 +208,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: { @@ -168,76 +223,64 @@ implements IWorkerNode { get maxQueued (): number { return getTasksQueueMaxSize() }, + 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() } } } } private initTaskFunctionWorkerUsage (name: string): WorkerUsage { - const getTaskQueueSize = (): number => { - let taskQueueSize = 0 + const getTaskFunctionQueueSize = (): number => { + let taskFunctionQueueSize = 0 for (const task of this.tasksQueue) { - if (task.name === name) { - ++taskQueueSize + if ( + (task.name === DEFAULT_TASK_NAME && + name === (this.info.taskFunctionNames as string[])[1]) || + (task.name !== DEFAULT_TASK_NAME && name === task.name) + ) { + ++taskFunctionQueueSize } } - return taskQueueSize + return taskFunctionQueueSize } return { tasks: { executed: 0, executing: 0, get queued (): number { - return getTaskQueueSize() + return getTaskFunctionQueueSize() }, + 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() } } } } - - /** - * Gets the worker id. - * - * @param worker - The worker. - * @param workerType - The worker type. - * @returns The worker id. - */ - private getWorkerId ( - worker: Worker, - workerType: WorkerType - ): number | undefined { - if (workerType === WorkerTypes.thread) { - return worker.threadId - } else if (workerType === WorkerTypes.cluster) { - return worker.id - } - } }