fix: put back properties init in constructor
[poolifier.git] / src / pools / selection-strategies / interleaved-weighted-round-robin-worker-choice-strategy.ts
1 import type { IWorker } from '../worker'
2 import type { IPool } from '../pool'
3 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 InternalWorkerChoiceStrategyOptions,
8 TaskStatisticsRequirements
9 } from './selection-strategies-types'
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.setOptions(this.opts)
60 this.setTaskStatisticsRequirements(this.opts)
61 this.roundWeights = this.getRoundWeights()
62 }
63
64 /** @inheritDoc */
65 public reset (): boolean {
66 this.resetWorkerNodeKeyProperties()
67 this.roundId = 0
68 this.workerNodeId = 0
69 this.workerNodeVirtualTaskRunTime = 0
70 return true
71 }
72
73 /** @inheritDoc */
74 public update (): boolean {
75 return true
76 }
77
78 /** @inheritDoc */
79 public choose (): number | undefined {
80 for (
81 let roundIndex = this.roundId;
82 roundIndex < this.roundWeights.length;
83 roundIndex++
84 ) {
85 this.roundId = roundIndex
86 for (
87 let workerNodeKey = this.workerNodeId;
88 workerNodeKey < this.pool.workerNodes.length;
89 workerNodeKey++
90 ) {
91 this.workerNodeId = workerNodeKey
92 if (
93 this.workerNodeId !== this.nextWorkerNodeKey &&
94 this.workerNodeVirtualTaskRunTime !== 0
95 ) {
96 this.workerNodeVirtualTaskRunTime = 0
97 }
98 const workerWeight = this.opts.weights?.[workerNodeKey] as number
99 if (
100 this.isWorkerNodeReady(workerNodeKey) &&
101 workerWeight >= this.roundWeights[roundIndex] &&
102 this.workerNodeVirtualTaskRunTime < workerWeight
103 ) {
104 this.workerNodeVirtualTaskRunTime =
105 this.workerNodeVirtualTaskRunTime +
106 this.getWorkerNodeTaskRunTime(workerNodeKey)
107 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
108 this.nextWorkerNodeKey = workerNodeKey
109 return this.nextWorkerNodeKey
110 }
111 }
112 }
113 this.interleavedWeightedRoundRobinNextWorkerNodeId()
114 }
115
116 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
117 if (
118 this.roundId === this.roundWeights.length - 1 &&
119 this.workerNodeId === this.pool.workerNodes.length - 1
120 ) {
121 this.roundId = 0
122 this.workerNodeId = 0
123 } else if (this.workerNodeId === this.pool.workerNodes.length - 1) {
124 this.roundId = this.roundId + 1
125 this.workerNodeId = 0
126 } else {
127 this.workerNodeId = this.workerNodeId + 1
128 }
129 }
130
131 /** @inheritDoc */
132 public remove (workerNodeKey: number): boolean {
133 if (this.pool.workerNodes.length === 0) {
134 this.reset()
135 }
136 if (
137 this.workerNodeId === workerNodeKey &&
138 this.workerNodeId > this.pool.workerNodes.length - 1
139 ) {
140 this.workerNodeId = this.pool.workerNodes.length - 1
141 }
142 if (
143 this.previousWorkerNodeKey === workerNodeKey &&
144 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
145 ) {
146 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
147 }
148 return true
149 }
150
151 /** @inheritDoc */
152 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
153 super.setOptions(opts)
154 this.roundWeights = this.getRoundWeights()
155 }
156
157 private getRoundWeights (): number[] {
158 return [
159 ...new Set(
160 Object.values(this.opts.weights as Record<number, number>)
161 .slice()
162 .sort((a, b) => a - b)
163 )
164 ]
165 }
166 }