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