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