fix: fix worker choice strategy retries mechanism on some edge cases
[poolifier.git] / src / pools / selection-strategies / fair-share-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'
23ff945a 7import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
9adcefab
JB
8import {
9 type IWorkerChoiceStrategy,
10 Measurements,
b1aae695 11 type StrategyPolicy,
9adcefab
JB
12 type TaskStatisticsRequirements,
13 type WorkerChoiceStrategyOptions
bf90656c 14} from './selection-strategies-types'
23ff945a 15
23ff945a
JB
16/**
17 * Selects the next worker with a fair share scheduling algorithm.
18 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
19 *
38e795c1 20 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
21 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
22 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23ff945a
JB
23 */
24export class FairShareWorkerChoiceStrategy<
f06e48d8 25 Worker extends IWorker,
b2b1d84e
JB
26 Data = unknown,
27 Response = unknown
bf90656c
JB
28 >
29 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 30 implements IWorkerChoiceStrategy {
b1aae695
JB
31 /** @inheritDoc */
32 public readonly strategyPolicy: StrategyPolicy = {
33 dynamicWorkerUsage: false,
34 dynamicWorkerReady: true
35 }
36
afc003b2 37 /** @inheritDoc */
87de9ff5 38 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
39 runTime: {
40 aggregate: true,
41 average: true,
42 median: false
43 },
3c93feb9 44 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
5df69fab 45 elu: {
9adcefab
JB
46 aggregate: true,
47 average: true,
5df69fab
JB
48 median: false
49 }
10fcfaf4
JB
50 }
51
23ff945a 52 /**
b0d6ed8f 53 * Workers' virtual task end execution timestamp.
23ff945a 54 */
b0d6ed8f 55 private workersVirtualTaskEndTimestamp: number[] = []
23ff945a 56
2fc5cae3
JB
57 /** @inheritDoc */
58 public constructor (
59 pool: IPool<Worker, Data, Response>,
60 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
61 ) {
62 super(pool, opts)
932fc8be 63 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
64 }
65
afc003b2 66 /** @inheritDoc */
a6f7f1b4 67 public reset (): boolean {
b0d6ed8f 68 this.workersVirtualTaskEndTimestamp = []
ea7a90d3
JB
69 return true
70 }
71
138d29a8 72 /** @inheritDoc */
a4958de2 73 public update (workerNodeKey: number): boolean {
b0d6ed8f 74 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
db703c75
JB
75 return true
76 }
77
78 /** @inheritDoc */
b1aae695
JB
79 public choose (): number | undefined {
80 const chosenWorkerNodeKey = this.fairShareNextWorkerNodeKey()
81 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
82 return this.nextWorkerNodeKey
9b106837
JB
83 }
84
85 /** @inheritDoc */
86 public remove (workerNodeKey: number): boolean {
87 this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
88 return true
89 }
90
b1aae695 91 private fairShareNextWorkerNodeKey (): number | undefined {
23ff945a 92 let minWorkerVirtualTaskEndTimestamp = Infinity
b1aae695 93 let chosenWorkerNodeKey: number | undefined
08f3f44c 94 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
b0d6ed8f
JB
95 if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
96 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
97 }
0d80593b 98 const workerVirtualTaskEndTimestamp =
b0d6ed8f 99 this.workersVirtualTaskEndTimestamp[workerNodeKey]
19dbc45b 100 if (
8990357d 101 this.isWorkerNodeEligible(workerNodeKey) &&
19dbc45b
JB
102 workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
103 ) {
0d80593b 104 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
b1aae695 105 chosenWorkerNodeKey = workerNodeKey
23ff945a
JB
106 }
107 }
b1aae695 108 return chosenWorkerNodeKey
97a2abc3
JB
109 }
110
23ff945a 111 /**
b0d6ed8f 112 * Computes the worker node key virtual task end timestamp.
11df3590 113 *
f06e48d8 114 * @param workerNodeKey - The worker node key.
23ff945a 115 */
b0d6ed8f
JB
116 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
117 this.workersVirtualTaskEndTimestamp[workerNodeKey] =
118 this.getWorkerVirtualTaskEndTimestamp(
119 workerNodeKey,
120 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
121 )
122 }
123
124 private getWorkerVirtualTaskEndTimestamp (
125 workerNodeKey: number,
126 workerVirtualTaskStartTimestamp: number
127 ): number {
9adcefab
JB
128 const workerTaskRunTime =
129 this.opts.measurement === Measurements.elu
130 ? this.getWorkerTaskElu(workerNodeKey)
131 : this.getWorkerTaskRunTime(workerNodeKey)
132 return workerVirtualTaskStartTimestamp + workerTaskRunTime
b0d6ed8f
JB
133 }
134
135 private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
136 return Math.max(
137 performance.now(),
138 this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
139 )
23ff945a
JB
140 }
141}