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