X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker-node.ts;h=d29a6e323f35208e22b6f87ca4610fb0af1b3e34;hb=6a677734f19a229fe6a863acd0e6aa0b0d762d6f;hp=64400d9eb1426be9ffe546b45855b606bfec05c0;hpb=75f30e744551af87736f998db9ad02be7206e89e;p=poolifier.git diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 64400d9e..d29a6e32 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -1,7 +1,7 @@ import { EventEmitter } from 'node:events' import { MessageChannel } from 'node:worker_threads' -import { CircularArray } from '../circular-array.js' +import { CircularBuffer } from '../circular-buffer.js' import { PriorityQueue } from '../priority-queue.js' import type { Task } from '../utility-types.js' import { DEFAULT_TASK_NAME } from '../utils.js' @@ -9,23 +9,23 @@ import { checkWorkerNodeArguments, createWorker, getWorkerId, - getWorkerType + 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' /** * Worker node. - * * @typeParam Worker - Type of worker. * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. */ @@ -45,12 +45,11 @@ export class WorkerNode /** @inheritdoc */ public tasksQueueBackPressureSize: number private readonly tasksQueue: PriorityQueue> - private onBackPressureStarted: boolean + private setBackPressureFlag: boolean private readonly taskFunctionsUsage: Map /** * Constructs a new worker node. - * * @param type - The worker type. * @param filePath - Path to the worker file. * @param opts - The worker node options. @@ -60,7 +59,7 @@ export class WorkerNode checkWorkerNodeArguments(type, filePath, opts) this.worker = createWorker(type, filePath, { env: opts.env, - workerOptions: opts.workerOptions + workerOptions: opts.workerOptions, }) this.info = this.initWorkerInfo(this.worker) this.usage = this.initWorkerUsage() @@ -69,11 +68,19 @@ export class WorkerNode } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize! - this.tasksQueue = new PriorityQueue>(opts.tasksQueueBucketSize) - this.onBackPressureStarted = false + this.tasksQueue = new PriorityQueue>( + opts.tasksQueueBucketSize, + opts.tasksQueuePriority + ) + this.setBackPressureFlag = false this.taskFunctionsUsage = new Map() } + /** @inheritdoc */ + public setTasksQueuePriority (enablePriority: boolean): void { + this.tasksQueue.enablePriority = enablePriority + } + /** @inheritdoc */ public tasksQueueSize (): number { return this.tasksQueue.size @@ -82,17 +89,38 @@ export class WorkerNode /** @inheritdoc */ public enqueueTask (task: Task): number { const tasksQueueSize = this.tasksQueue.enqueue(task, task.priority) - if (this.hasBackPressure() && !this.onBackPressureStarted) { - this.onBackPressureStarted = true + if ( + !this.setBackPressureFlag && + this.hasBackPressure() && + !this.info.backPressure + ) { + this.setBackPressureFlag = true + this.info.backPressure = true this.emit('backPressure', { workerId: this.info.id }) - this.onBackPressureStarted = false + this.setBackPressureFlag = false } return tasksQueueSize } /** @inheritdoc */ public dequeueTask (bucket?: number): Task | undefined { - return this.tasksQueue.dequeue(bucket) + 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 dequeueLastPrioritizedTask (): Task | undefined { + // Start from the last empty or partially filled bucket + return this.dequeueTask(this.tasksQueue.buckets + 1) } /** @inheritdoc */ @@ -191,7 +219,8 @@ export class WorkerNode type: getWorkerType(worker)!, dynamic: false, ready: false, - stealing: false + stealing: false, + backPressure: false, } } @@ -214,22 +243,22 @@ export class WorkerNode }, sequentiallyStolen: 0, stolen: 0, - failed: 0 + failed: 0, }, runTime: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, waitTime: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, elu: { idle: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, active: { - history: new CircularArray() - } - } + history: new CircularBuffer(MeasurementHistorySize), + }, + }, } } @@ -257,22 +286,22 @@ export class WorkerNode }, sequentiallyStolen: 0, stolen: 0, - failed: 0 + failed: 0, }, runTime: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, waitTime: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, elu: { idle: { - history: new CircularArray() + history: new CircularBuffer(MeasurementHistorySize), }, active: { - history: new CircularArray() - } - } + history: new CircularBuffer(MeasurementHistorySize), + }, + }, } } }