cdbfbee5bb0b73e6436f5aad3d94b45e764d3713
[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_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 StrategyPolicy,
8 WorkerChoiceStrategyOptions
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 strategyPolicy: StrategyPolicy = {
27 dynamicWorkerUsage: false,
28 dynamicWorkerReady: true
29 }
30
31 /**
32 * Round id.
33 * This is used to determine the current round weight.
34 */
35 private roundId: number = 0
36 /**
37 * Round weights.
38 */
39 private roundWeights: number[]
40 /**
41 * Default worker weight.
42 */
43 private readonly defaultWorkerWeight: number
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 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
53 this.roundWeights = this.getRoundWeights()
54 }
55
56 /** @inheritDoc */
57 public reset (): boolean {
58 this.nextWorkerNodeKey = 0
59 this.roundId = 0
60 return true
61 }
62
63 /** @inheritDoc */
64 public update (): boolean {
65 return true
66 }
67
68 /** @inheritDoc */
69 public choose (): number | undefined {
70 let roundId: number | undefined
71 let workerNodeId: number | undefined
72 for (
73 let roundIndex = this.roundId;
74 roundIndex < this.roundWeights.length;
75 roundIndex++
76 ) {
77 for (
78 let workerNodeKey = this.nextWorkerNodeKey ?? 0;
79 workerNodeKey < this.pool.workerNodes.length;
80 workerNodeKey++
81 ) {
82 const workerWeight =
83 this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
84 if (
85 this.isWorkerNodeEligible(workerNodeKey) &&
86 workerWeight >= this.roundWeights[roundIndex]
87 ) {
88 roundId = roundIndex
89 workerNodeId = workerNodeKey
90 break
91 }
92 }
93 }
94 this.roundId = roundId as number
95 this.nextWorkerNodeKey = workerNodeId
96 const chosenWorkerNodeKey = this.nextWorkerNodeKey
97 if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) {
98 this.nextWorkerNodeKey = 0
99 this.roundId =
100 this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
101 } else {
102 this.nextWorkerNodeKey = (this.nextWorkerNodeKey ?? 0) + 1
103 }
104 return chosenWorkerNodeKey
105 }
106
107 /** @inheritDoc */
108 public remove (workerNodeKey: number): boolean {
109 if (this.nextWorkerNodeKey === workerNodeKey) {
110 if (this.pool.workerNodes.length === 0) {
111 this.nextWorkerNodeKey = 0
112 } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
113 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
114 this.roundId =
115 this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
116 }
117 }
118 return true
119 }
120
121 /** @inheritDoc */
122 public setOptions (opts: WorkerChoiceStrategyOptions): void {
123 super.setOptions(opts)
124 this.roundWeights = this.getRoundWeights()
125 }
126
127 private getRoundWeights (): number[] {
128 if (this.opts.weights == null) {
129 return [this.defaultWorkerWeight]
130 }
131 return [
132 ...new Set(
133 Object.values(this.opts.weights)
134 .slice()
135 .sort((a, b) => a - b)
136 )
137 ]
138 }
139 }