feat: add custom worker weights to worker choice strategies options
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2 import type { IPool } from '../pool'
3 import type { IWorker } from '../worker'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 RequiredStatistics,
8 WorkerChoiceStrategyOptions
9 } from './selection-strategies-types'
10
11 /**
12 * Worker virtual task timestamp.
13 */
14 interface WorkerVirtualTaskTimestamp {
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 *
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.
25 * @typeParam Response - Type of execution response. This can only be serializable data.
26 */
27 export class FairShareWorkerChoiceStrategy<
28 Worker extends IWorker,
29 Data = unknown,
30 Response = unknown
31 >
32 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
33 implements IWorkerChoiceStrategy {
34 /** @inheritDoc */
35 public readonly requiredStatistics: RequiredStatistics = {
36 runTime: true,
37 avgRunTime: true,
38 medRunTime: false
39 }
40
41 /**
42 * Workers' virtual task execution timestamp.
43 */
44 private workersVirtualTaskTimestamp: WorkerVirtualTaskTimestamp[] = []
45
46 /** @inheritDoc */
47 public constructor (
48 pool: IPool<Worker, Data, Response>,
49 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
50 ) {
51 super(pool, opts)
52 this.checkOptions(this.opts)
53 }
54
55 /** @inheritDoc */
56 public reset (): boolean {
57 this.workersVirtualTaskTimestamp = []
58 return true
59 }
60
61 /** @inheritDoc */
62 public choose (): number {
63 let minWorkerVirtualTaskEndTimestamp = Infinity
64 let chosenWorkerNodeKey!: number
65 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
66 this.computeWorkerVirtualTaskTimestamp(workerNodeKey)
67 const workerLastVirtualTaskEndTimestamp =
68 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0
69 if (
70 workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
71 ) {
72 minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
73 chosenWorkerNodeKey = workerNodeKey
74 }
75 }
76 return chosenWorkerNodeKey
77 }
78
79 /** @inheritDoc */
80 public remove (workerNodeKey: number): boolean {
81 this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1)
82 return true
83 }
84
85 /**
86 * Computes worker virtual task timestamp.
87 *
88 * @param workerNodeKey - The worker node key.
89 */
90 private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void {
91 const workerVirtualTaskStartTimestamp = Math.max(
92 performance.now(),
93 this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity
94 )
95 const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
96 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
97 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
98 this.workersVirtualTaskTimestamp[workerNodeKey] = {
99 start: workerVirtualTaskStartTimestamp,
100 end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0)
101 }
102 }
103 }