Merge pull request #773 from poolifier/elu-strategy
[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,
87de9ff5 7 TaskStatisticsRequirements,
2fc5cae3 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 */
87de9ff5 27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
33 waitTime: {
34 aggregate: false,
35 average: false,
36 median: false
37 },
62c15a68 38 elu: false
10fcfaf4
JB
39 }
40
23ff945a 41 /**
b0d6ed8f 42 * Workers' virtual task end execution timestamp.
23ff945a 43 */
b0d6ed8f 44 private workersVirtualTaskEndTimestamp: number[] = []
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)
932fc8be 52 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
53 }
54
afc003b2 55 /** @inheritDoc */
a6f7f1b4 56 public reset (): boolean {
b0d6ed8f 57 this.workersVirtualTaskEndTimestamp = []
ea7a90d3
JB
58 return true
59 }
60
138d29a8 61 /** @inheritDoc */
a4958de2 62 public update (workerNodeKey: number): boolean {
b0d6ed8f 63 this.computeWorkerVirtualTaskEndTimestamp(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()) {
b0d6ed8f
JB
72 if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
73 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
74 }
0d80593b 75 const workerVirtualTaskEndTimestamp =
b0d6ed8f 76 this.workersVirtualTaskEndTimestamp[workerNodeKey]
0d80593b
JB
77 if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
78 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
08f3f44c 79 chosenWorkerNodeKey = workerNodeKey
23ff945a
JB
80 }
81 }
f06e48d8 82 return chosenWorkerNodeKey
23ff945a
JB
83 }
84
afc003b2 85 /** @inheritDoc */
f06e48d8 86 public remove (workerNodeKey: number): boolean {
b0d6ed8f 87 this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
08f3f44c 88 return true
97a2abc3
JB
89 }
90
23ff945a 91 /**
b0d6ed8f 92 * Computes the worker node key virtual task end timestamp.
11df3590 93 *
f06e48d8 94 * @param workerNodeKey - The worker node key.
23ff945a 95 */
b0d6ed8f
JB
96 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
97 this.workersVirtualTaskEndTimestamp[workerNodeKey] =
98 this.getWorkerVirtualTaskEndTimestamp(
99 workerNodeKey,
100 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
101 )
102 }
103
104 private getWorkerVirtualTaskEndTimestamp (
105 workerNodeKey: number,
106 workerVirtualTaskStartTimestamp: number
107 ): number {
f6b641d6
JB
108 return (
109 workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey)
110 )
b0d6ed8f
JB
111 }
112
113 private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
114 return Math.max(
115 performance.now(),
116 this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
117 )
23ff945a
JB
118 }
119}