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