refactor: factor out inputs type check
[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 /**
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
afc003b2 61 /** @inheritDoc */
c923ce56 62 public choose (): number {
23ff945a 63 let minWorkerVirtualTaskEndTimestamp = Infinity
f06e48d8 64 let chosenWorkerNodeKey!: number
08f3f44c
JB
65 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
66 this.computeWorkerVirtualTaskTimestamp(workerNodeKey)
0d80593b 67 const workerVirtualTaskEndTimestamp =
08f3f44c 68 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0
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 {
08f3f44c
JB
79 this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1)
80 return true
97a2abc3
JB
81 }
82
23ff945a 83 /**
08f3f44c 84 * Computes worker virtual task timestamp.
11df3590 85 *
f06e48d8 86 * @param workerNodeKey - The worker node key.
23ff945a 87 */
08f3f44c 88 private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void {
11df3590 89 const workerVirtualTaskStartTimestamp = Math.max(
3fafb1b2 90 performance.now(),
08f3f44c 91 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity
11df3590 92 )
da309861
JB
93 const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
94 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
95 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
08f3f44c 96 this.workersVirtualTaskTimestamp[workerNodeKey] = {
11df3590 97 start: workerVirtualTaskStartTimestamp,
da309861 98 end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0)
08f3f44c 99 }
23ff945a
JB
100 }
101}