build: switch default to ESM
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
CommitLineData
d35e5717
JB
1import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js'
2import type { IPool } from '../pool.js'
3import type { IWorker, StrategyData } from '../worker.js'
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
9adcefab
JB
5import {
6 type IWorkerChoiceStrategy,
26ce26ca 7 type InternalWorkerChoiceStrategyOptions,
9adcefab 8 Measurements,
26ce26ca 9 type TaskStatisticsRequirements
d35e5717 10} from './selection-strategies-types.js'
23ff945a 11
23ff945a
JB
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 *
38e795c1 16 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
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.
23ff945a
JB
19 */
20export class FairShareWorkerChoiceStrategy<
f06e48d8 21 Worker extends IWorker,
b2b1d84e
JB
22 Data = unknown,
23 Response = unknown
bf90656c
JB
24 >
25 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 26 implements IWorkerChoiceStrategy {
afc003b2 27 /** @inheritDoc */
87de9ff5 28 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
29 runTime: {
30 aggregate: true,
31 average: true,
32 median: false
33 },
3c93feb9 34 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
5df69fab 35 elu: {
9adcefab
JB
36 aggregate: true,
37 average: true,
5df69fab
JB
38 median: false
39 }
10fcfaf4
JB
40 }
41
2fc5cae3
JB
42 /** @inheritDoc */
43 public constructor (
44 pool: IPool<Worker, Data, Response>,
26ce26ca 45 opts: InternalWorkerChoiceStrategyOptions
2fc5cae3
JB
46 ) {
47 super(pool, opts)
932fc8be 48 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
49 }
50
afc003b2 51 /** @inheritDoc */
a6f7f1b4 52 public reset (): boolean {
f3a91bac
JB
53 for (const workerNode of this.pool.workerNodes) {
54 delete workerNode.strategyData?.virtualTaskEndTimestamp
55 }
ea7a90d3
JB
56 return true
57 }
58
138d29a8 59 /** @inheritDoc */
a4958de2 60 public update (workerNodeKey: number): boolean {
f3a91bac
JB
61 this.pool.workerNodes[workerNodeKey].strategyData = {
62 virtualTaskEndTimestamp:
63 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
64 }
db703c75
JB
65 return true
66 }
67
68 /** @inheritDoc */
b1aae695 69 public choose (): number | undefined {
baca80f7 70 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
fce028d6 71 this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey()
b1aae695 72 return this.nextWorkerNodeKey
9b106837
JB
73 }
74
75 /** @inheritDoc */
f3a91bac 76 public remove (): boolean {
9b106837
JB
77 return true
78 }
79
b1aae695 80 private fairShareNextWorkerNodeKey (): number | undefined {
f3a91bac
JB
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 }
ae3ab61d
JB
89 return this.isWorkerNodeReady(workerNodeKey) &&
90 (workerNode.strategyData.virtualTaskEndTimestamp as number) <
91 ((workerNodes[minWorkerNodeKey].strategyData as StrategyData)
92 .virtualTaskEndTimestamp as number)
f3a91bac
JB
93 ? workerNodeKey
94 : minWorkerNodeKey
95 },
96 0
97 )
97a2abc3
JB
98 }
99
23ff945a 100 /**
b0d6ed8f 101 * Computes the worker node key virtual task end timestamp.
11df3590 102 *
f06e48d8 103 * @param workerNodeKey - The worker node key.
f3a91bac 104 * @returns The worker node key virtual task end timestamp.
23ff945a 105 */
f3a91bac
JB
106 private computeWorkerNodeVirtualTaskEndTimestamp (
107 workerNodeKey: number
108 ): number {
109 return this.getWorkerNodeVirtualTaskEndTimestamp(
110 workerNodeKey,
111 this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey)
112 )
b0d6ed8f
JB
113 }
114
f3a91bac 115 private getWorkerNodeVirtualTaskEndTimestamp (
b0d6ed8f 116 workerNodeKey: number,
f3a91bac 117 workerNodeVirtualTaskStartTimestamp: number
b0d6ed8f 118 ): number {
f3a91bac 119 const workerNodeTaskRunTime =
9adcefab 120 this.opts.measurement === Measurements.elu
f3a91bac
JB
121 ? this.getWorkerNodeTaskElu(workerNodeKey)
122 : this.getWorkerNodeTaskRunTime(workerNodeKey)
123 return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
b0d6ed8f
JB
124 }
125
f3a91bac
JB
126 private getWorkerNodeVirtualTaskStartTimestamp (
127 workerNodeKey: number
128 ): number {
f201a0cd
JB
129 const virtualTaskEndTimestamp =
130 this.pool.workerNodes[workerNodeKey]?.strategyData
131 ?.virtualTaskEndTimestamp
f3a91bac 132 const now = performance.now()
f201a0cd
JB
133 return now < (virtualTaskEndTimestamp ?? -Infinity)
134 ? (virtualTaskEndTimestamp as number)
f3a91bac 135 : now
23ff945a
JB
136 }
137}