feat: add pool runtime setters
[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
JB
15 start: number
16 end: number
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 /**
f06e48d8 42 * Worker last virtual task execution timestamp.
23ff945a 43 */
ea7a90d3 44 private readonly workerLastVirtualTaskTimestamp: Map<
c923ce56 45 number,
78cea37e 46 WorkerVirtualTaskTimestamp
c923ce56 47 > = new Map<number, WorkerVirtualTaskTimestamp>()
23ff945a 48
2fc5cae3
JB
49 /** @inheritDoc */
50 public constructor (
51 pool: IPool<Worker, Data, Response>,
52 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
53 ) {
54 super(pool, opts)
a20f0ba5 55 this.checkOptions(this.opts)
2fc5cae3
JB
56 }
57
afc003b2 58 /** @inheritDoc */
a6f7f1b4 59 public reset (): boolean {
ea7a90d3
JB
60 this.workerLastVirtualTaskTimestamp.clear()
61 return true
62 }
63
afc003b2 64 /** @inheritDoc */
c923ce56 65 public choose (): number {
23ff945a 66 let minWorkerVirtualTaskEndTimestamp = Infinity
f06e48d8
JB
67 let chosenWorkerNodeKey!: number
68 for (const [index] of this.pool.workerNodes.entries()) {
c923ce56 69 this.computeWorkerLastVirtualTaskTimestamp(index)
23ff945a 70 const workerLastVirtualTaskEndTimestamp =
c923ce56 71 this.workerLastVirtualTaskTimestamp.get(index)?.end ?? 0
23ff945a
JB
72 if (
73 workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
74 ) {
75 minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
f06e48d8 76 chosenWorkerNodeKey = index
23ff945a
JB
77 }
78 }
f06e48d8 79 return chosenWorkerNodeKey
23ff945a
JB
80 }
81
afc003b2 82 /** @inheritDoc */
f06e48d8
JB
83 public remove (workerNodeKey: number): boolean {
84 const deleted = this.workerLastVirtualTaskTimestamp.delete(workerNodeKey)
97a2abc3 85 for (const [key, value] of this.workerLastVirtualTaskTimestamp.entries()) {
f06e48d8 86 if (key > workerNodeKey) {
97a2abc3
JB
87 this.workerLastVirtualTaskTimestamp.set(key - 1, value)
88 }
89 }
f06e48d8 90 return deleted
97a2abc3
JB
91 }
92
23ff945a 93 /**
11df3590
JB
94 * Computes worker last virtual task timestamp.
95 *
f06e48d8 96 * @param workerNodeKey - The worker node key.
23ff945a 97 */
f06e48d8 98 private computeWorkerLastVirtualTaskTimestamp (workerNodeKey: number): void {
11df3590 99 const workerVirtualTaskStartTimestamp = Math.max(
3fafb1b2 100 performance.now(),
f06e48d8 101 this.workerLastVirtualTaskTimestamp.get(workerNodeKey)?.end ?? -Infinity
11df3590 102 )
da309861
JB
103 const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
104 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
105 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
f06e48d8 106 this.workerLastVirtualTaskTimestamp.set(workerNodeKey, {
11df3590 107 start: workerVirtualTaskStartTimestamp,
da309861 108 end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0)
11df3590 109 })
23ff945a
JB
110 }
111}