X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=724785f534b6a4c3b971d97aad8b7d7d598de666;hb=ec3e56520d46660ea043d5184f5d8246c834d252;hp=ab93fc007b698f56b029aa4043182c87b330ab3b;hpb=a4958de2101f06e7096b83adbca82fcfd532a721;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ab93fc00..724785f5 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -146,6 +146,9 @@ export abstract class AbstractPool< this.opts.workerChoiceStrategyOptions = opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + this.checkValidWorkerChoiceStrategyOptions( + this.opts.workerChoiceStrategyOptions + ) this.opts.enableEvents = opts.enableEvents ?? true this.opts.enableTasksQueue = opts.enableTasksQueue ?? false if (this.opts.enableTasksQueue) { @@ -179,6 +182,14 @@ export abstract class AbstractPool< 'Invalid worker choice strategy options: must be a plain object' ) } + if ( + workerChoiceStrategyOptions.weights != null && + Object.keys(workerChoiceStrategyOptions.weights).length !== this.size + ) { + throw new Error( + 'Invalid worker choice strategy options: must have a weight for each worker node' + ) + } } private checkValidTasksQueueOptions ( @@ -252,6 +263,10 @@ export abstract class AbstractPool< runTimeHistory: new CircularArray(), avgRunTime: 0, medRunTime: 0, + waitTime: 0, + waitTimeHistory: new CircularArray(), + avgWaitTime: 0, + medWaitTime: 0, error: 0 }) } @@ -329,18 +344,20 @@ export abstract class AbstractPool< /** @inheritDoc */ public async execute (data?: Data, name?: string): Promise { - const [workerNodeKey, workerNode] = this.chooseWorkerNode() + const submissionTimestamp = performance.now() + const workerNodeKey = this.chooseWorkerNode() const submittedTask: Task = { name, // eslint-disable-next-line @typescript-eslint/consistent-type-assertions data: data ?? ({} as Data), + submissionTimestamp, id: crypto.randomUUID() } const res = new Promise((resolve, reject) => { this.promiseResponseMap.set(submittedTask.id as string, { resolve, reject, - worker: workerNode.worker + worker: this.workerNodes[workerNodeKey].worker }) }) if ( @@ -354,6 +371,7 @@ export abstract class AbstractPool< } else { this.executeTask(workerNodeKey, submittedTask) } + this.workerChoiceStrategyContext.update(workerNodeKey) this.checkAndEmitEvents() // eslint-disable-next-line @typescript-eslint/return-await return res @@ -412,13 +430,21 @@ export abstract class AbstractPool< worker: Worker, message: MessageValue ): void { - const workerNodeKey = this.getWorkerNodeKey(worker) - const workerTasksUsage = this.workerNodes[workerNodeKey].tasksUsage + const workerTasksUsage = + this.workerNodes[this.getWorkerNodeKey(worker)].tasksUsage --workerTasksUsage.running ++workerTasksUsage.run if (message.error != null) { ++workerTasksUsage.error } + this.updateRunTimeTasksUsage(workerTasksUsage, message) + this.updateWaitTasksUsage(workerTasksUsage, message) + } + + private updateRunTimeTasksUsage ( + workerTasksUsage: TasksUsage, + message: MessageValue + ): void { if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) { workerTasksUsage.runTime += message.runTime ?? 0 if ( @@ -428,22 +454,47 @@ export abstract class AbstractPool< workerTasksUsage.avgRunTime = workerTasksUsage.runTime / workerTasksUsage.run } - if (this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime) { - workerTasksUsage.runTimeHistory.push(message.runTime ?? 0) + if ( + this.workerChoiceStrategyContext.getRequiredStatistics().medRunTime && + message.runTime != null + ) { + workerTasksUsage.runTimeHistory.push(message.runTime) workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory) } } - this.workerChoiceStrategyContext.update(workerNodeKey) + } + + private updateWaitTasksUsage ( + workerTasksUsage: TasksUsage, + message: MessageValue + ): void { + if (this.workerChoiceStrategyContext.getRequiredStatistics().waitTime) { + workerTasksUsage.waitTime += message.waitTime ?? 0 + if ( + this.workerChoiceStrategyContext.getRequiredStatistics().avgWaitTime && + workerTasksUsage.run !== 0 + ) { + workerTasksUsage.avgWaitTime = + workerTasksUsage.waitTime / workerTasksUsage.run + } + if ( + this.workerChoiceStrategyContext.getRequiredStatistics().medWaitTime && + message.waitTime != null + ) { + workerTasksUsage.waitTimeHistory.push(message.waitTime) + workerTasksUsage.medWaitTime = median(workerTasksUsage.waitTimeHistory) + } + } } /** * Chooses a worker node for the next task. * - * The default uses a round robin algorithm to distribute the load. + * The default worker choice strategy uses a round robin algorithm to distribute the load. * - * @returns [worker node key, worker node]. + * @returns The worker node key */ - protected chooseWorkerNode (): [number, WorkerNode] { + protected chooseWorkerNode (): number { let workerNodeKey: number if (this.type === PoolType.DYNAMIC && !this.full && this.internalBusy()) { const workerCreated = this.createAndSetupWorker() @@ -463,7 +514,7 @@ export abstract class AbstractPool< } else { workerNodeKey = this.workerChoiceStrategyContext.execute() } - return [workerNodeKey, this.workerNodes[workerNodeKey]] + return workerNodeKey } /** @@ -597,6 +648,10 @@ export abstract class AbstractPool< runTimeHistory: new CircularArray(), avgRunTime: 0, medRunTime: 0, + waitTime: 0, + waitTimeHistory: new CircularArray(), + avgWaitTime: 0, + medWaitTime: 0, error: 0 }, tasksQueue: new Queue>()