feat: conditional task performance computation at the worker level
[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,
feec6e8c
JB
7 WorkerChoiceStrategyOptions
8} from './selection-strategies-types'
9
10/**
11 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
12 *
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam Response - Type of execution response. This can only be serializable 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 {
feec6e8c
JB
24 /**
25 * Worker node id where the current task will be submitted.
26 */
27 private currentWorkerNodeId: number = 0
28 /**
29 * Current round id.
30 * This is used to determine the current round weight.
31 */
32 private currentRoundId: number = 0
33 /**
34 * Round weights.
35 */
e4854a4e 36 private roundWeights: number[]
feec6e8c
JB
37 /**
38 * Default worker weight.
39 */
40 private readonly defaultWorkerWeight: number
41
42 /** @inheritDoc */
43 public constructor (
44 pool: IPool<Worker, Data, Response>,
45 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
46 ) {
47 super(pool, opts)
b6b32453 48 this.setTaskStatistics(this.opts)
feec6e8c
JB
49 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
50 this.roundWeights = this.getRoundWeights()
51 }
52
53 /** @inheritDoc */
54 public reset (): boolean {
55 this.currentWorkerNodeId = 0
56 this.currentRoundId = 0
57 return true
58 }
59
60 /** @inheritDoc */
61 public update (): boolean {
62 return true
63 }
64
65 /** @inheritDoc */
66 public choose (): number {
297f3bbe
JB
67 let roundId: number | undefined
68 let workerNodeId: number | undefined
69 for (
d3127e84
JB
70 let roundIndex = this.currentRoundId;
71 roundIndex < this.roundWeights.length;
72 roundIndex++
297f3bbe 73 ) {
feec6e8c 74 for (
297f3bbe
JB
75 let workerNodeKey = this.currentWorkerNodeId;
76 workerNodeKey < this.pool.workerNodes.length;
77 workerNodeKey++
feec6e8c 78 ) {
297f3bbe
JB
79 const workerWeight =
80 this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
d3127e84
JB
81 if (workerWeight >= this.roundWeights[roundIndex]) {
82 roundId = roundIndex
297f3bbe
JB
83 workerNodeId = workerNodeKey
84 break
feec6e8c
JB
85 }
86 }
297f3bbe
JB
87 }
88 this.currentRoundId = roundId ?? 0
89 this.currentWorkerNodeId = workerNodeId ?? 0
90 const chosenWorkerNodeKey = this.currentWorkerNodeId
91 if (this.currentWorkerNodeId === this.pool.workerNodes.length - 1) {
92 this.currentWorkerNodeId = 0
93 this.currentRoundId =
94 this.currentRoundId === this.roundWeights.length - 1
95 ? 0
96 : this.currentRoundId + 1
97 } else {
98 this.currentWorkerNodeId = this.currentWorkerNodeId + 1
feec6e8c
JB
99 }
100 return chosenWorkerNodeKey
101 }
102
103 /** @inheritDoc */
104 public remove (workerNodeKey: number): boolean {
105 if (this.currentWorkerNodeId === workerNodeKey) {
106 if (this.pool.workerNodes.length === 0) {
107 this.currentWorkerNodeId = 0
cac70359
JB
108 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
109 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
110 this.currentRoundId =
111 this.currentRoundId === this.roundWeights.length - 1
112 ? 0
113 : this.currentRoundId + 1
feec6e8c
JB
114 }
115 }
116 return true
117 }
118
e4854a4e
JB
119 /** @inheritDoc */
120 public setOptions (opts: WorkerChoiceStrategyOptions): void {
121 super.setOptions(opts)
122 this.roundWeights = this.getRoundWeights()
123 }
124
feec6e8c
JB
125 private getRoundWeights (): number[] {
126 if (this.opts.weights == null) {
127 return [this.defaultWorkerWeight]
128 }
129 return [
130 ...new Set(
131 Object.values(this.opts.weights)
132 .slice()
133 .sort((a, b) => a - b)
134 )
135 ]
136 }
137}