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