feat: add custom worker weights to worker choice strategies options
[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)
23ff945a 67 const workerLastVirtualTaskEndTimestamp =
08f3f44c 68 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0
23ff945a
JB
69 if (
70 workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
71 ) {
72 minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
08f3f44c 73 chosenWorkerNodeKey = workerNodeKey
23ff945a
JB
74 }
75 }
f06e48d8 76 return chosenWorkerNodeKey
23ff945a
JB
77 }
78
afc003b2 79 /** @inheritDoc */
f06e48d8 80 public remove (workerNodeKey: number): boolean {
08f3f44c
JB
81 this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1)
82 return true
97a2abc3
JB
83 }
84
23ff945a 85 /**
08f3f44c 86 * Computes worker virtual task timestamp.
11df3590 87 *
f06e48d8 88 * @param workerNodeKey - The worker node key.
23ff945a 89 */
08f3f44c 90 private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void {
11df3590 91 const workerVirtualTaskStartTimestamp = Math.max(
3fafb1b2 92 performance.now(),
08f3f44c 93 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity
11df3590 94 )
da309861
JB
95 const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
96 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
97 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
08f3f44c 98 this.workersVirtualTaskTimestamp[workerNodeKey] = {
11df3590 99 start: workerVirtualTaskStartTimestamp,
da309861 100 end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0)
08f3f44c 101 }
23ff945a
JB
102 }
103}