feat: add tasks wait time account per worker
[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,
33 medWaitTime: false
10fcfaf4
JB
34 }
35
23ff945a 36 /**
b0d6ed8f 37 * Workers' virtual task end execution timestamp.
23ff945a 38 */
b0d6ed8f 39 private workersVirtualTaskEndTimestamp: number[] = []
23ff945a 40
2fc5cae3
JB
41 /** @inheritDoc */
42 public constructor (
43 pool: IPool<Worker, Data, Response>,
44 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
45 ) {
46 super(pool, opts)
49be33fe 47 this.setRequiredStatistics(this.opts)
2fc5cae3
JB
48 }
49
afc003b2 50 /** @inheritDoc */
a6f7f1b4 51 public reset (): boolean {
b0d6ed8f 52 this.workersVirtualTaskEndTimestamp = []
ea7a90d3
JB
53 return true
54 }
55
138d29a8 56 /** @inheritDoc */
a4958de2 57 public update (workerNodeKey: number): boolean {
b0d6ed8f 58 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
138d29a8
JB
59 return true
60 }
61
afc003b2 62 /** @inheritDoc */
c923ce56 63 public choose (): number {
23ff945a 64 let minWorkerVirtualTaskEndTimestamp = Infinity
f06e48d8 65 let chosenWorkerNodeKey!: number
08f3f44c 66 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
b0d6ed8f
JB
67 if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
68 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
69 }
0d80593b 70 const workerVirtualTaskEndTimestamp =
b0d6ed8f 71 this.workersVirtualTaskEndTimestamp[workerNodeKey]
0d80593b
JB
72 if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
73 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
08f3f44c 74 chosenWorkerNodeKey = workerNodeKey
23ff945a
JB
75 }
76 }
f06e48d8 77 return chosenWorkerNodeKey
23ff945a
JB
78 }
79
afc003b2 80 /** @inheritDoc */
f06e48d8 81 public remove (workerNodeKey: number): boolean {
b0d6ed8f 82 this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
08f3f44c 83 return true
97a2abc3
JB
84 }
85
23ff945a 86 /**
b0d6ed8f 87 * Computes the worker node key virtual task end timestamp.
11df3590 88 *
f06e48d8 89 * @param workerNodeKey - The worker node key.
23ff945a 90 */
b0d6ed8f
JB
91 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
92 this.workersVirtualTaskEndTimestamp[workerNodeKey] =
93 this.getWorkerVirtualTaskEndTimestamp(
94 workerNodeKey,
95 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
96 )
97 }
98
99 private getWorkerVirtualTaskEndTimestamp (
100 workerNodeKey: number,
101 workerVirtualTaskStartTimestamp: number
102 ): number {
f6b641d6
JB
103 return (
104 workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey)
105 )
b0d6ed8f
JB
106 }
107
108 private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
109 return Math.max(
110 performance.now(),
111 this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
112 )
23ff945a
JB
113 }
114}