feat: optimize worker choice strategies implementation
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
1 import {
2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
4 } from '../../utils'
5 import type { IPool } from '../pool'
6 import type { IWorker, StrategyData } from '../worker'
7 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
8 import {
9 type IWorkerChoiceStrategy,
10 Measurements,
11 type TaskStatisticsRequirements,
12 type WorkerChoiceStrategyOptions
13 } from './selection-strategies-types'
14
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 *
19 * @typeParam Worker - Type of worker which manages the strategy.
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.
22 */
23 export class FairShareWorkerChoiceStrategy<
24 Worker extends IWorker,
25 Data = unknown,
26 Response = unknown
27 >
28 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
29 implements IWorkerChoiceStrategy {
30 /** @inheritDoc */
31 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
32 runTime: {
33 aggregate: true,
34 average: true,
35 median: false
36 },
37 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
38 elu: {
39 aggregate: true,
40 average: true,
41 median: false
42 }
43 }
44
45 /** @inheritDoc */
46 public constructor (
47 pool: IPool<Worker, Data, Response>,
48 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
49 ) {
50 super(pool, opts)
51 this.setTaskStatisticsRequirements(this.opts)
52 }
53
54 /** @inheritDoc */
55 public reset (): boolean {
56 for (const workerNode of this.pool.workerNodes) {
57 delete workerNode.strategyData?.virtualTaskEndTimestamp
58 }
59 return true
60 }
61
62 /** @inheritDoc */
63 public update (workerNodeKey: number): boolean {
64 this.pool.workerNodes[workerNodeKey].strategyData = {
65 virtualTaskEndTimestamp:
66 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
67 }
68 return true
69 }
70
71 /** @inheritDoc */
72 public choose (): number | undefined {
73 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
74 this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey()
75 return this.nextWorkerNodeKey
76 }
77
78 /** @inheritDoc */
79 public remove (): boolean {
80 return true
81 }
82
83 private fairShareNextWorkerNodeKey (): number | undefined {
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 )
100 }
101
102 /**
103 * Computes the worker node key virtual task end timestamp.
104 *
105 * @param workerNodeKey - The worker node key.
106 * @returns The worker node key virtual task end timestamp.
107 */
108 private computeWorkerNodeVirtualTaskEndTimestamp (
109 workerNodeKey: number
110 ): number {
111 return this.getWorkerNodeVirtualTaskEndTimestamp(
112 workerNodeKey,
113 this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey)
114 )
115 }
116
117 private getWorkerNodeVirtualTaskEndTimestamp (
118 workerNodeKey: number,
119 workerNodeVirtualTaskStartTimestamp: number
120 ): number {
121 const workerNodeTaskRunTime =
122 this.opts.measurement === Measurements.elu
123 ? this.getWorkerNodeTaskElu(workerNodeKey)
124 : this.getWorkerNodeTaskRunTime(workerNodeKey)
125 return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
126 }
127
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
138 }
139 }