build: switch default to ESM
[poolifier.git] / src / pools / selection-strategies / interleaved-weighted-round-robin-worker-choice-strategy.ts
1 import type { IWorker } from '../worker.js'
2 import type { IPool } from '../pool.js'
3 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
5 import type {
6 IWorkerChoiceStrategy,
7 InternalWorkerChoiceStrategyOptions,
8 TaskStatisticsRequirements
9 } from './selection-strategies-types.js'
10
11 /**
12 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
13 *
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 */
18 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
24 implements IWorkerChoiceStrategy {
25 /** @inheritDoc */
26 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
27 runTime: {
28 aggregate: true,
29 average: true,
30 median: false
31 },
32 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
33 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
34 }
35
36 /**
37 * Round id.
38 */
39 private roundId: number = 0
40 /**
41 * Round weights.
42 */
43 private roundWeights: number[]
44 /**
45 * Worker node id.
46 */
47 private workerNodeId: number = 0
48 /**
49 * Worker node virtual task runtime.
50 */
51 private workerNodeVirtualTaskRunTime: number = 0
52
53 /** @inheritDoc */
54 public constructor (
55 pool: IPool<Worker, Data, Response>,
56 opts: InternalWorkerChoiceStrategyOptions
57 ) {
58 super(pool, opts)
59 this.setTaskStatisticsRequirements(this.opts)
60 this.roundWeights = this.getRoundWeights()
61 }
62
63 /** @inheritDoc */
64 public reset (): boolean {
65 this.resetWorkerNodeKeyProperties()
66 this.roundId = 0
67 this.workerNodeId = 0
68 this.workerNodeVirtualTaskRunTime = 0
69 return true
70 }
71
72 /** @inheritDoc */
73 public update (): boolean {
74 return true
75 }
76
77 /** @inheritDoc */
78 public choose (): number | undefined {
79 for (
80 let roundIndex = this.roundId;
81 roundIndex < this.roundWeights.length;
82 roundIndex++
83 ) {
84 this.roundId = roundIndex
85 for (
86 let workerNodeKey = this.workerNodeId;
87 workerNodeKey < this.pool.workerNodes.length;
88 workerNodeKey++
89 ) {
90 this.workerNodeId = workerNodeKey
91 if (
92 this.workerNodeId !== this.nextWorkerNodeKey &&
93 this.workerNodeVirtualTaskRunTime !== 0
94 ) {
95 this.workerNodeVirtualTaskRunTime = 0
96 }
97 const workerWeight = this.opts.weights?.[workerNodeKey] as number
98 if (
99 this.isWorkerNodeReady(workerNodeKey) &&
100 workerWeight >= this.roundWeights[roundIndex] &&
101 this.workerNodeVirtualTaskRunTime < workerWeight
102 ) {
103 this.workerNodeVirtualTaskRunTime =
104 this.workerNodeVirtualTaskRunTime +
105 this.getWorkerNodeTaskRunTime(workerNodeKey)
106 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
107 this.nextWorkerNodeKey = workerNodeKey
108 return this.nextWorkerNodeKey
109 }
110 }
111 }
112 this.interleavedWeightedRoundRobinNextWorkerNodeId()
113 }
114
115 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
116 if (
117 this.roundId === this.roundWeights.length - 1 &&
118 this.workerNodeId === this.pool.workerNodes.length - 1
119 ) {
120 this.roundId = 0
121 this.workerNodeId = 0
122 } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
123 this.roundId = this.roundId + 1
124 this.workerNodeId = 0
125 } else {
126 this.workerNodeId = this.workerNodeId + 1
127 }
128 }
129
130 /** @inheritDoc */
131 public remove (workerNodeKey: number): boolean {
132 if (this.pool.workerNodes.length === 0) {
133 this.reset()
134 }
135 if (
136 this.workerNodeId === workerNodeKey &&
137 this.workerNodeId > this.pool.workerNodes.length - 1
138 ) {
139 this.workerNodeId = this.pool.workerNodes.length - 1
140 }
141 if (
142 this.previousWorkerNodeKey === workerNodeKey &&
143 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
144 ) {
145 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
146 }
147 return true
148 }
149
150 /** @inheritDoc */
151 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
152 super.setOptions(opts)
153 this.roundWeights = this.getRoundWeights()
154 }
155
156 private getRoundWeights (): number[] {
157 return [
158 ...new Set(
159 Object.values(this.opts.weights as Record<number, number>)
160 .slice()
161 .sort((a, b) => a - b)
162 )
163 ]
164 }
165 }