docs: refine benchmarks README
[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'
3import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5import type {
6 IWorkerChoiceStrategy,
6c6afb84 7 StrategyPolicy,
feec6e8c
JB
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 serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable data.
17 */
18export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
19 Worker extends IWorker,
20 Data = unknown,
21 Response = unknown
22 >
23 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
24 implements IWorkerChoiceStrategy {
6c6afb84
JB
25 /** @inheritDoc */
26 public readonly strategyPolicy: StrategyPolicy = {
27 useDynamicWorker: true
28 }
29
feec6e8c
JB
30 /**
31 * Worker node id where the current task will be submitted.
32 */
33 private currentWorkerNodeId: number = 0
34 /**
35 * Current round id.
36 * This is used to determine the current round weight.
37 */
38 private currentRoundId: number = 0
39 /**
40 * Round weights.
41 */
e4854a4e 42 private roundWeights: number[]
feec6e8c
JB
43 /**
44 * Default worker weight.
45 */
46 private readonly defaultWorkerWeight: number
47
48 /** @inheritDoc */
49 public constructor (
50 pool: IPool<Worker, Data, Response>,
51 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
52 ) {
53 super(pool, opts)
932fc8be 54 this.setTaskStatisticsRequirements(this.opts)
feec6e8c
JB
55 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
56 this.roundWeights = this.getRoundWeights()
57 }
58
59 /** @inheritDoc */
60 public reset (): boolean {
61 this.currentWorkerNodeId = 0
62 this.currentRoundId = 0
63 return true
64 }
65
66 /** @inheritDoc */
67 public update (): boolean {
68 return true
69 }
70
71 /** @inheritDoc */
72 public choose (): number {
297f3bbe
JB
73 let roundId: number | undefined
74 let workerNodeId: number | undefined
75 for (
d3127e84
JB
76 let roundIndex = this.currentRoundId;
77 roundIndex < this.roundWeights.length;
78 roundIndex++
297f3bbe 79 ) {
feec6e8c 80 for (
297f3bbe
JB
81 let workerNodeKey = this.currentWorkerNodeId;
82 workerNodeKey < this.pool.workerNodes.length;
83 workerNodeKey++
feec6e8c 84 ) {
297f3bbe
JB
85 const workerWeight =
86 this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
d3127e84
JB
87 if (workerWeight >= this.roundWeights[roundIndex]) {
88 roundId = roundIndex
297f3bbe
JB
89 workerNodeId = workerNodeKey
90 break
feec6e8c
JB
91 }
92 }
297f3bbe
JB
93 }
94 this.currentRoundId = roundId ?? 0
95 this.currentWorkerNodeId = workerNodeId ?? 0
96 const chosenWorkerNodeKey = this.currentWorkerNodeId
97 if (this.currentWorkerNodeId === this.pool.workerNodes.length - 1) {
98 this.currentWorkerNodeId = 0
99 this.currentRoundId =
100 this.currentRoundId === this.roundWeights.length - 1
101 ? 0
102 : this.currentRoundId + 1
103 } else {
104 this.currentWorkerNodeId = this.currentWorkerNodeId + 1
feec6e8c
JB
105 }
106 return chosenWorkerNodeKey
107 }
108
109 /** @inheritDoc */
110 public remove (workerNodeKey: number): boolean {
111 if (this.currentWorkerNodeId === workerNodeKey) {
112 if (this.pool.workerNodes.length === 0) {
113 this.currentWorkerNodeId = 0
cac70359
JB
114 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
115 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
116 this.currentRoundId =
117 this.currentRoundId === this.roundWeights.length - 1
118 ? 0
119 : this.currentRoundId + 1
feec6e8c
JB
120 }
121 }
122 return true
123 }
124
e4854a4e
JB
125 /** @inheritDoc */
126 public setOptions (opts: WorkerChoiceStrategyOptions): void {
127 super.setOptions(opts)
128 this.roundWeights = this.getRoundWeights()
129 }
130
feec6e8c
JB
131 private getRoundWeights (): number[] {
132 if (this.opts.weights == null) {
133 return [this.defaultWorkerWeight]
134 }
135 return [
136 ...new Set(
137 Object.values(this.opts.weights)
138 .slice()
139 .sort((a, b) => a - b)
140 )
141 ]
142 }
143}