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