c6b87ade001759855115117d155a170a2e104c08
[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, StrategyData } 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 (workerNode.strategyData.virtualTaskEndTimestamp as number) <
91 ((workerNodes[minWorkerNodeKey].strategyData as StrategyData)
92 .virtualTaskEndTimestamp as number)
93 ? workerNodeKey
94 : minWorkerNodeKey
95 },
96 0
97 )
98 }
99
100 /**
101 * Computes the worker node key virtual task end timestamp.
102 *
103 * @param workerNodeKey - The worker node key.
104 * @returns The worker node key virtual task end timestamp.
105 */
106 private computeWorkerNodeVirtualTaskEndTimestamp (
107 workerNodeKey: number
108 ): number {
109 return this.getWorkerNodeVirtualTaskEndTimestamp(
110 workerNodeKey,
111 this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey)
112 )
113 }
114
115 private getWorkerNodeVirtualTaskEndTimestamp (
116 workerNodeKey: number,
117 workerNodeVirtualTaskStartTimestamp: number
118 ): number {
119 const workerNodeTaskRunTime =
120 this.opts.measurement === Measurements.elu
121 ? this.getWorkerNodeTaskElu(workerNodeKey)
122 : this.getWorkerNodeTaskRunTime(workerNodeKey)
123 return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
124 }
125
126 private getWorkerNodeVirtualTaskStartTimestamp (
127 workerNodeKey: number
128 ): number {
129 const virtualTaskEndTimestamp =
130 this.pool.workerNodes[workerNodeKey]?.strategyData
131 ?.virtualTaskEndTimestamp
132 const now = performance.now()
133 return now < (virtualTaskEndTimestamp ?? -Infinity)
134 ? (virtualTaskEndTimestamp as number)
135 : now
136 }
137 }