X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fabstract-worker-choice-strategy.ts;h=121647b697139e9c068170d266d96f2c0837edad;hb=7ebef000df647c4fbb722120dc48f42575382af0;hp=a060cea3a5ba4486059fec1f6b45dac515f155e3;hpb=6c6afb8463782af0689101b7c67cea80df83018f;p=poolifier.git diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index a060cea3..121647b6 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -21,10 +21,15 @@ export abstract class AbstractWorkerChoiceStrategy< Data = unknown, Response = unknown > implements IWorkerChoiceStrategy { + // /** + // * Toggles finding the last free worker node key. + // */ + // private toggleFindLastFreeWorkerNodeKey: boolean = false + /** - * Toggles finding the last free worker node key. + * Id of the next worker node. */ - private toggleFindLastFreeWorkerNodeKey: boolean = false + protected nextWorkerNodeId: number = 0 /** @inheritDoc */ public readonly strategyPolicy: StrategyPolicy = { @@ -128,24 +133,23 @@ export abstract class AbstractWorkerChoiceStrategy< /** @inheritDoc */ public setOptions (opts: WorkerChoiceStrategyOptions): void { - opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS - this.setTaskStatisticsRequirements(opts) - this.opts = opts + this.opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS + this.setTaskStatisticsRequirements(this.opts) } - /** - * Finds a free worker node key. - * - * @returns The free worker node key or `-1` if there is no free worker node. - */ - protected findFreeWorkerNodeKey (): number { - if (this.toggleFindLastFreeWorkerNodeKey) { - this.toggleFindLastFreeWorkerNodeKey = false - return this.findLastFreeWorkerNodeKey() - } - this.toggleFindLastFreeWorkerNodeKey = true - return this.findFirstFreeWorkerNodeKey() - } + // /** + // * Finds a free worker node key. + // * + // * @returns The free worker node key or `-1` if there is no free worker node. + // */ + // protected findFreeWorkerNodeKey (): number { + // if (this.toggleFindLastFreeWorkerNodeKey) { + // this.toggleFindLastFreeWorkerNodeKey = false + // return this.findLastFreeWorkerNodeKey() + // } + // this.toggleFindLastFreeWorkerNodeKey = true + // return this.findFirstFreeWorkerNodeKey() + // } /** * Gets the worker task runtime. @@ -171,8 +175,8 @@ export abstract class AbstractWorkerChoiceStrategy< */ protected getWorkerTaskWaitTime (workerNodeKey: number): number { return this.taskStatisticsRequirements.waitTime.median - ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median - : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average + ? this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.median + : this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.average } /** @@ -200,46 +204,46 @@ export abstract class AbstractWorkerChoiceStrategy< return Math.round(cpusCycleTimeWeight / cpus().length) } - /** - * Finds the first free worker node key based on the number of tasks the worker has applied. - * - * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned. - * - * If no free worker is found, `-1` is returned. - * - * @returns A worker node key if there is one, `-1` otherwise. - */ - private findFirstFreeWorkerNodeKey (): number { - return this.pool.workerNodes.findIndex(workerNode => { - return workerNode.workerUsage.tasks.executing === 0 - }) - } - - /** - * Finds the last free worker node key based on the number of tasks the worker has applied. - * - * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned. - * - * If no free worker is found, `-1` is returned. - * - * @returns A worker node key if there is one, `-1` otherwise. - */ - private findLastFreeWorkerNodeKey (): number { - // It requires node >= 18.0.0: - // return this.workerNodes.findLastIndex(workerNode => { - // return workerNode.workerUsage.tasks.executing === 0 - // }) - for ( - let workerNodeKey = this.pool.workerNodes.length - 1; - workerNodeKey >= 0; - workerNodeKey-- - ) { - if ( - this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0 - ) { - return workerNodeKey - } - } - return -1 - } + // /** + // * Finds the first free worker node key based on the number of tasks the worker has applied. + // * + // * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned. + // * + // * If no free worker is found, `-1` is returned. + // * + // * @returns A worker node key if there is one, `-1` otherwise. + // */ + // private findFirstFreeWorkerNodeKey (): number { + // return this.pool.workerNodes.findIndex(workerNode => { + // return workerNode.workerUsage.tasks.executing === 0 + // }) + // } + + // /** + // * Finds the last free worker node key based on the number of tasks the worker has applied. + // * + // * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned. + // * + // * If no free worker is found, `-1` is returned. + // * + // * @returns A worker node key if there is one, `-1` otherwise. + // */ + // private findLastFreeWorkerNodeKey (): number { + // // It requires node >= 18.0.0: + // // return this.workerNodes.findLastIndex(workerNode => { + // // return workerNode.workerUsage.tasks.executing === 0 + // // }) + // for ( + // let workerNodeKey = this.pool.workerNodes.length - 1; + // workerNodeKey >= 0; + // workerNodeKey-- + // ) { + // if ( + // this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0 + // ) { + // return workerNodeKey + // } + // } + // return -1 + // } }