fix: fix worker choice strategy retries mechanism on some edge cases
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
CommitLineData
3c93feb9
JB
1import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
4} from '../../utils'
2fc5cae3 5import type { IPool } from '../pool'
f06e48d8 6import type { IWorker } from '../worker'
168c526f 7import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
bf90656c
JB
8import type {
9 IWorkerChoiceStrategy,
b1aae695 10 StrategyPolicy,
87de9ff5 11 TaskStatisticsRequirements,
2fc5cae3 12 WorkerChoiceStrategyOptions
bf90656c 13} from './selection-strategies-types'
168c526f
JB
14
15/**
e4543b14 16 * Selects the least busy worker.
168c526f
JB
17 *
18 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
168c526f 21 */
e4543b14 22export class LeastBusyWorkerChoiceStrategy<
f06e48d8 23 Worker extends IWorker,
b2b1d84e
JB
24 Data = unknown,
25 Response = unknown
bf90656c
JB
26 >
27 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 28 implements IWorkerChoiceStrategy {
b1aae695
JB
29 /** @inheritDoc */
30 public readonly strategyPolicy: StrategyPolicy = {
31 dynamicWorkerUsage: false,
32 dynamicWorkerReady: true
33 }
34
afc003b2 35 /** @inheritDoc */
87de9ff5 36 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
37 runTime: {
38 aggregate: true,
39 average: false,
40 median: false
41 },
42 waitTime: {
43 aggregate: true,
44 average: false,
45 median: false
46 },
3c93feb9 47 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
168c526f
JB
48 }
49
2fc5cae3
JB
50 /** @inheritDoc */
51 public constructor (
52 pool: IPool<Worker, Data, Response>,
53 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
54 ) {
55 super(pool, opts)
932fc8be 56 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
57 }
58
afc003b2 59 /** @inheritDoc */
168c526f
JB
60 public reset (): boolean {
61 return true
62 }
63
138d29a8
JB
64 /** @inheritDoc */
65 public update (): boolean {
db703c75
JB
66 return true
67 }
68
69 /** @inheritDoc */
b1aae695
JB
70 public choose (): number | undefined {
71 const chosenWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
72 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
73 return this.nextWorkerNodeKey
9b106837
JB
74 }
75
76 /** @inheritDoc */
77 public remove (): boolean {
78 return true
79 }
80
b1aae695 81 private leastBusyNextWorkerNodeKey (): number | undefined {
1c6fe997 82 let minTime = Infinity
b1aae695 83 let chosenWorkerNodeKey: number | undefined
08f3f44c 84 for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
1c6fe997 85 const workerTime =
71514351
JB
86 (workerNode.usage.runTime?.aggregate ?? 0) +
87 (workerNode.usage.waitTime?.aggregate ?? 0)
8990357d 88 if (this.isWorkerNodeEligible(workerNodeKey) && workerTime === 0) {
b1aae695 89 chosenWorkerNodeKey = workerNodeKey
5ea80606 90 break
9b106837 91 } else if (
8990357d 92 this.isWorkerNodeEligible(workerNodeKey) &&
9b106837
JB
93 workerTime < minTime
94 ) {
1c6fe997 95 minTime = workerTime
b1aae695 96 chosenWorkerNodeKey = workerNodeKey
168c526f
JB
97 }
98 }
b1aae695 99 return chosenWorkerNodeKey
97a2abc3 100 }
168c526f 101}