fix: fix faire share worker choice stategy internals update
[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
138d29a8
JB
61 /** @inheritDoc */
62 public update (): boolean {
63 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
64 this.computeWorkerVirtualTaskTimestamp(workerNodeKey)
65 }
66 return true
67 }
68
afc003b2 69 /** @inheritDoc */
c923ce56 70 public choose (): number {
23ff945a 71 let minWorkerVirtualTaskEndTimestamp = Infinity
f06e48d8 72 let chosenWorkerNodeKey!: number
08f3f44c 73 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
0d80593b 74 const workerVirtualTaskEndTimestamp =
08f3f44c 75 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0
0d80593b
JB
76 if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
77 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
08f3f44c 78 chosenWorkerNodeKey = workerNodeKey
23ff945a
JB
79 }
80 }
f06e48d8 81 return chosenWorkerNodeKey
23ff945a
JB
82 }
83
afc003b2 84 /** @inheritDoc */
f06e48d8 85 public remove (workerNodeKey: number): boolean {
08f3f44c
JB
86 this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1)
87 return true
97a2abc3
JB
88 }
89
23ff945a 90 /**
08f3f44c 91 * Computes worker virtual task timestamp.
11df3590 92 *
f06e48d8 93 * @param workerNodeKey - The worker node key.
23ff945a 94 */
08f3f44c 95 private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void {
11df3590 96 const workerVirtualTaskStartTimestamp = Math.max(
3fafb1b2 97 performance.now(),
08f3f44c 98 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity
11df3590 99 )
da309861
JB
100 const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
101 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
102 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
08f3f44c 103 this.workersVirtualTaskTimestamp[workerNodeKey] = {
11df3590 104 start: workerVirtualTaskStartTimestamp,
138d29a8 105 end: workerVirtualTaskStartTimestamp + workerVirtualTaskTRunTime
08f3f44c 106 }
23ff945a
JB
107 }
108}