From 20016c79549983d09d30b70852ec7fae515d4156 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 10 Jul 2023 18:10:03 +0200 Subject: [PATCH] refactor: cleanup worker choice strategies code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../abstract-worker-choice-strategy.ts | 60 ------------------- .../fair-share-worker-choice-strategy.ts | 6 +- .../least-busy-worker-choice-strategy.ts | 6 +- .../least-elu-worker-choice-strategy.ts | 6 +- .../least-used-worker-choice-strategy.ts | 6 +- .../round-robin-worker-choice-strategy.ts | 3 +- .../selection-strategies-types.ts | 2 +- ...hted-round-robin-worker-choice-strategy.ts | 3 +- src/pools/worker.ts | 2 +- 9 files changed, 18 insertions(+), 76 deletions(-) diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index c4f1e2c6..01beb340 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -24,11 +24,6 @@ export abstract class AbstractWorkerChoiceStrategy< Data = unknown, Response = unknown > implements IWorkerChoiceStrategy { - // /** - // * Toggles finding the last free worker node key. - // */ - // private toggleFindLastFreeWorkerNodeKey: boolean = false - /** * The next worker node key. */ @@ -132,20 +127,6 @@ export abstract class AbstractWorkerChoiceStrategy< return this.pool.workerNodes[workerNodeKey].info.ready } - // /** - // * 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. * If the task statistics require the average runtime, the average runtime is returned. @@ -198,45 +179,4 @@ 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.usage.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.pool.workerNodes.findLastIndex(workerNode => { - // // return workerNode.usage.tasks.executing === 0 - // // }) - // for ( - // let workerNodeKey = this.pool.workerNodes.length - 1; - // workerNodeKey >= 0; - // workerNodeKey-- - // ) { - // if (this.pool.workerNodes[workerNodeKey].usage.tasks.executing === 0) { - // return workerNodeKey - // } - // } - // return -1 - // } } diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index 00c56dad..bf30a476 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -70,8 +70,7 @@ export class FairShareWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - this.fairShareNextWorkerNodeKey() - return this.nextWorkerNodeKey + return this.fairShareNextWorkerNodeKey() } /** @inheritDoc */ @@ -80,7 +79,7 @@ export class FairShareWorkerChoiceStrategy< return true } - private fairShareNextWorkerNodeKey (): void { + private fairShareNextWorkerNodeKey (): number { let minWorkerVirtualTaskEndTimestamp = Infinity for (const [workerNodeKey] of this.pool.workerNodes.entries()) { if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) { @@ -96,6 +95,7 @@ export class FairShareWorkerChoiceStrategy< this.nextWorkerNodeKey = workerNodeKey } } + return this.nextWorkerNodeKey } /** diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index dabad682..9a1550e0 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -61,8 +61,7 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - this.leastBusyNextWorkerNodeKey() - return this.nextWorkerNodeKey + return this.leastBusyNextWorkerNodeKey() } /** @inheritDoc */ @@ -70,7 +69,7 @@ export class LeastBusyWorkerChoiceStrategy< return true } - private leastBusyNextWorkerNodeKey (): void { + private leastBusyNextWorkerNodeKey (): number { let minTime = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTime = @@ -87,5 +86,6 @@ export class LeastBusyWorkerChoiceStrategy< this.nextWorkerNodeKey = workerNodeKey } } + return this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts index 24c612e0..7c837cef 100644 --- a/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-elu-worker-choice-strategy.ts @@ -57,8 +57,7 @@ export class LeastEluWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - this.leastEluNextWorkerNodeKey() - return this.nextWorkerNodeKey + return this.leastEluNextWorkerNodeKey() } /** @inheritDoc */ @@ -66,7 +65,7 @@ export class LeastEluWorkerChoiceStrategy< return true } - private leastEluNextWorkerNodeKey (): void { + private leastEluNextWorkerNodeKey (): number { let minWorkerElu = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerUsage = workerNode.usage @@ -82,5 +81,6 @@ export class LeastEluWorkerChoiceStrategy< this.nextWorkerNodeKey = workerNodeKey } } + return this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index 910d10a3..e8a7218e 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -42,8 +42,7 @@ export class LeastUsedWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - this.leastUsedNextWorkerNodeKey() - return this.nextWorkerNodeKey + return this.leastUsedNextWorkerNodeKey() } /** @inheritDoc */ @@ -51,7 +50,7 @@ export class LeastUsedWorkerChoiceStrategy< return true } - private leastUsedNextWorkerNodeKey (): void { + private leastUsedNextWorkerNodeKey (): number { let minNumberOfTasks = Infinity for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTaskStatistics = workerNode.usage.tasks @@ -70,5 +69,6 @@ export class LeastUsedWorkerChoiceStrategy< this.nextWorkerNodeKey = workerNodeKey } } + return this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index fdf5ac84..222a0349 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -66,10 +66,11 @@ export class RoundRobinWorkerChoiceStrategy< return true } - private roundRobinNextWorkerNodeKey (): void { + private roundRobinNextWorkerNodeKey (): number { this.nextWorkerNodeKey = this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 ? 0 : this.nextWorkerNodeKey + 1 + return this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index e53f3e82..563b04b0 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -147,7 +147,7 @@ export interface TaskStatisticsRequirements { */ export interface StrategyPolicy { /** - * Expects direct usage of dynamic worker. + * Expects direct usage of the newly created dynamic worker. */ readonly useDynamicWorker: boolean } diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index ed38aa14..4921df6d 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -94,7 +94,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< return true } - private weightedRoundRobinNextWorkerNodeKey (): void { + private weightedRoundRobinNextWorkerNodeKey (): number { const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime const workerWeight = this.opts.weights?.[this.nextWorkerNodeKey] ?? this.defaultWorkerWeight @@ -109,5 +109,6 @@ export class WeightedRoundRobinWorkerChoiceStrategy< : this.nextWorkerNodeKey + 1 this.workerVirtualTaskRunTime = 0 } + return this.nextWorkerNodeKey } } diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 58610eaa..507396de 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -238,7 +238,7 @@ export interface IWorkerNode { */ readonly resetUsage: () => void /** - * Gets task usage statistics. + * Gets task worker usage statistics. */ readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined } -- 2.34.1