56d9dc361fe6e70255877ec6c1e6be6f33b70a70
[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 * 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 *
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.
17 * @typeParam Response - Type of execution response. This can only be serializable data.
18 */
19 export class FairShareWorkerChoiceStrategy<
20 Worker extends IWorker,
21 Data = unknown,
22 Response = unknown
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
25 implements IWorkerChoiceStrategy {
26 /** @inheritDoc */
27 public readonly requiredStatistics: RequiredStatistics = {
28 runTime: true,
29 avgRunTime: true,
30 medRunTime: false,
31 waitTime: false,
32 avgWaitTime: false,
33 medWaitTime: false
34 }
35
36 /**
37 * Workers' virtual task end execution timestamp.
38 */
39 private workersVirtualTaskEndTimestamp: number[] = []
40
41 /** @inheritDoc */
42 public constructor (
43 pool: IPool<Worker, Data, Response>,
44 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
45 ) {
46 super(pool, opts)
47 this.setRequiredStatistics(this.opts)
48 }
49
50 /** @inheritDoc */
51 public reset (): boolean {
52 this.workersVirtualTaskEndTimestamp = []
53 return true
54 }
55
56 /** @inheritDoc */
57 public update (workerNodeKey: number): boolean {
58 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
59 return true
60 }
61
62 /** @inheritDoc */
63 public choose (): number {
64 let minWorkerVirtualTaskEndTimestamp = Infinity
65 let chosenWorkerNodeKey!: number
66 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
67 if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
68 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
69 }
70 const workerVirtualTaskEndTimestamp =
71 this.workersVirtualTaskEndTimestamp[workerNodeKey]
72 if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
73 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
74 chosenWorkerNodeKey = workerNodeKey
75 }
76 }
77 return chosenWorkerNodeKey
78 }
79
80 /** @inheritDoc */
81 public remove (workerNodeKey: number): boolean {
82 this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
83 return true
84 }
85
86 /**
87 * Computes the worker node key virtual task end timestamp.
88 *
89 * @param workerNodeKey - The worker node key.
90 */
91 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
92 this.workersVirtualTaskEndTimestamp[workerNodeKey] =
93 this.getWorkerVirtualTaskEndTimestamp(
94 workerNodeKey,
95 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
96 )
97 }
98
99 private getWorkerVirtualTaskEndTimestamp (
100 workerNodeKey: number,
101 workerVirtualTaskStartTimestamp: number
102 ): number {
103 return (
104 workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey)
105 )
106 }
107
108 private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
109 return Math.max(
110 performance.now(),
111 this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
112 )
113 }
114 }