fix: ensure worker choice is retried at least the pool max size
[poolifier.git] / src / pools / selection-strategies / interleaved-weighted-round-robin-worker-choice-strategy.ts
CommitLineData
feec6e8c
JB
1import type { IWorker } from '../worker'
2import type { IPool } from '../pool'
26ce26ca 3import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils'
feec6e8c
JB
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5import type {
6 IWorkerChoiceStrategy,
26ce26ca
JB
7 InternalWorkerChoiceStrategyOptions,
8 TaskStatisticsRequirements
feec6e8c
JB
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.
e102732c
JB
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.
feec6e8c
JB
17 */
18export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
24 implements IWorkerChoiceStrategy {
619f403b
JB
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
feec6e8c 36 /**
d33be430 37 * Round id.
feec6e8c 38 */
d33be430 39 private roundId: number = 0
619f403b
JB
40 /**
41 * Default worker weight.
42 */
43 private readonly defaultWorkerWeight: number
feec6e8c
JB
44 /**
45 * Round weights.
46 */
e4854a4e 47 private roundWeights: number[]
feec6e8c 48 /**
619f403b 49 * Worker node id.
feec6e8c 50 */
619f403b
JB
51 private workerNodeId: number = 0
52 /**
f3a91bac 53 * Worker node virtual task runtime.
619f403b 54 */
f3a91bac 55 private workerNodeVirtualTaskRunTime: number = 0
feec6e8c
JB
56
57 /** @inheritDoc */
58 public constructor (
59 pool: IPool<Worker, Data, Response>,
26ce26ca 60 opts: InternalWorkerChoiceStrategyOptions
feec6e8c
JB
61 ) {
62 super(pool, opts)
932fc8be 63 this.setTaskStatisticsRequirements(this.opts)
feec6e8c
JB
64 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
65 this.roundWeights = this.getRoundWeights()
66 }
67
68 /** @inheritDoc */
69 public reset (): boolean {
39a43af7 70 this.resetWorkerNodeKeyProperties()
d33be430 71 this.roundId = 0
619f403b 72 this.workerNodeId = 0
f3a91bac 73 this.workerNodeVirtualTaskRunTime = 0
feec6e8c
JB
74 return true
75 }
76
77 /** @inheritDoc */
78 public update (): boolean {
79 return true
80 }
81
82 /** @inheritDoc */
b1aae695 83 public choose (): number | undefined {
297f3bbe 84 for (
d33be430 85 let roundIndex = this.roundId;
d3127e84
JB
86 roundIndex < this.roundWeights.length;
87 roundIndex++
297f3bbe 88 ) {
619f403b 89 this.roundId = roundIndex
feec6e8c 90 for (
619f403b 91 let workerNodeKey = this.workerNodeId;
297f3bbe
JB
92 workerNodeKey < this.pool.workerNodes.length;
93 workerNodeKey++
feec6e8c 94 ) {
619f403b 95 this.workerNodeId = workerNodeKey
619f403b
JB
96 if (
97 this.workerNodeId !== this.nextWorkerNodeKey &&
f3a91bac 98 this.workerNodeVirtualTaskRunTime !== 0
619f403b 99 ) {
f3a91bac 100 this.workerNodeVirtualTaskRunTime = 0
619f403b 101 }
297f3bbe
JB
102 const workerWeight =
103 this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
619f403b 104 if (
97256a85 105 this.isWorkerNodeReady(workerNodeKey) &&
619f403b 106 workerWeight >= this.roundWeights[roundIndex] &&
f3a91bac 107 this.workerNodeVirtualTaskRunTime < workerWeight
619f403b 108 ) {
f3a91bac
JB
109 this.workerNodeVirtualTaskRunTime =
110 this.workerNodeVirtualTaskRunTime +
111 this.getWorkerNodeTaskRunTime(workerNodeKey)
baca80f7 112 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
619f403b
JB
113 this.nextWorkerNodeKey = workerNodeKey
114 return this.nextWorkerNodeKey
feec6e8c
JB
115 }
116 }
297f3bbe 117 }
619f403b
JB
118 this.interleavedWeightedRoundRobinNextWorkerNodeId()
119 }
120
121 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
a38b62f1
JB
122 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 }
feec6e8c
JB
134 }
135
136 /** @inheritDoc */
137 public remove (workerNodeKey: number): boolean {
226b02a3
JB
138 if (this.pool.workerNodes.length === 0) {
139 this.reset()
140 }
141 if (
142 this.workerNodeId === workerNodeKey &&
143 this.workerNodeId > this.pool.workerNodes.length - 1
144 ) {
145 this.workerNodeId = this.pool.workerNodes.length - 1
146 }
147 if (
148 this.previousWorkerNodeKey === workerNodeKey &&
149 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
150 ) {
151 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
feec6e8c
JB
152 }
153 return true
154 }
155
e4854a4e 156 /** @inheritDoc */
26ce26ca 157 public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
e4854a4e
JB
158 super.setOptions(opts)
159 this.roundWeights = this.getRoundWeights()
160 }
161
feec6e8c
JB
162 private getRoundWeights (): number[] {
163 if (this.opts.weights == null) {
164 return [this.defaultWorkerWeight]
165 }
166 return [
167 ...new Set(
168 Object.values(this.opts.weights)
169 .slice()
b808b625 170 .sort((a, b) => a - b)
feec6e8c
JB
171 )
172 ]
173 }
174}