feat: add worker choice strategies retry mechanism
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
1 import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
4 } from '../../utils'
5 import type { IPool } from '../pool'
6 import type { IWorker } from '../worker'
7 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
8 import {
9 type IWorkerChoiceStrategy,
10 Measurements,
11 type TaskStatisticsRequirements,
12 type WorkerChoiceStrategyOptions
13 } from './selection-strategies-types'
14
15 /**
16 * Selects the next worker with a fair share scheduling algorithm.
17 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
18 *
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
22 */
23 export class FairShareWorkerChoiceStrategy<
24 Worker extends IWorker,
25 Data = unknown,
26 Response = unknown
27 >
28 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
29 implements IWorkerChoiceStrategy {
30 /** @inheritDoc */
31 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
32 runTime: {
33 aggregate: true,
34 average: true,
35 median: false
36 },
37 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
38 elu: {
39 aggregate: true,
40 average: true,
41 median: false
42 }
43 }
44
45 /**
46 * Workers' virtual task end execution timestamp.
47 */
48 private workersVirtualTaskEndTimestamp: number[] = []
49
50 /** @inheritDoc */
51 public constructor (
52 pool: IPool<Worker, Data, Response>,
53 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
54 ) {
55 super(pool, opts)
56 this.setTaskStatisticsRequirements(this.opts)
57 }
58
59 /** @inheritDoc */
60 public reset (): boolean {
61 this.workersVirtualTaskEndTimestamp = []
62 return true
63 }
64
65 /** @inheritDoc */
66 public update (workerNodeKey: number): boolean {
67 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
68 return true
69 }
70
71 /** @inheritDoc */
72 public choose (): number {
73 return this.fairShareNextWorkerNodeKey()
74 }
75
76 /** @inheritDoc */
77 public remove (workerNodeKey: number): boolean {
78 this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
79 return true
80 }
81
82 private fairShareNextWorkerNodeKey (): number {
83 let minWorkerVirtualTaskEndTimestamp = Infinity
84 for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
85 if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
86 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
87 }
88 const workerVirtualTaskEndTimestamp =
89 this.workersVirtualTaskEndTimestamp[workerNodeKey]
90 if (
91 this.isWorkerNodeEligible(workerNodeKey) &&
92 workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
93 ) {
94 minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
95 this.nextWorkerNodeKey = workerNodeKey
96 }
97 }
98 return this.nextWorkerNodeKey
99 }
100
101 /**
102 * Computes the worker node key virtual task end timestamp.
103 *
104 * @param workerNodeKey - The worker node key.
105 */
106 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
107 this.workersVirtualTaskEndTimestamp[workerNodeKey] =
108 this.getWorkerVirtualTaskEndTimestamp(
109 workerNodeKey,
110 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
111 )
112 }
113
114 private getWorkerVirtualTaskEndTimestamp (
115 workerNodeKey: number,
116 workerVirtualTaskStartTimestamp: number
117 ): number {
118 const workerTaskRunTime =
119 this.opts.measurement === Measurements.elu
120 ? this.getWorkerTaskElu(workerNodeKey)
121 : this.getWorkerTaskRunTime(workerNodeKey)
122 return workerVirtualTaskStartTimestamp + workerTaskRunTime
123 }
124
125 private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
126 return Math.max(
127 performance.now(),
128 this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
129 )
130 }
131 }