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