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