fix: fix worker choice strategies behavior
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
d35e5717 1import type { IPool } from '../pool.js'
e9ed6eee 2import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
ded253e2 3import type { IWorker } from '../worker.js'
d35e5717 4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
bf90656c
JB
5import type {
6 IWorkerChoiceStrategy,
39618ede
JB
7 TaskStatisticsRequirements,
8 WorkerChoiceStrategyOptions
d35e5717 9} from './selection-strategies-types.js'
b3432a63 10
b3432a63
JB
11/**
12 * Selects the next worker with a weighted round robin scheduling algorithm.
13 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
14 *
38e795c1 15 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
16 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
17 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b3432a63
JB
18 */
19export class WeightedRoundRobinWorkerChoiceStrategy<
f06e48d8 20 Worker extends IWorker,
b2b1d84e
JB
21 Data = unknown,
22 Response = unknown
bf90656c
JB
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 25 implements IWorkerChoiceStrategy {
afc003b2 26 /** @inheritDoc */
87de9ff5 27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
e0843544
JB
33 waitTime: {
34 aggregate: true,
35 average: true,
36 median: false
37 },
3c93feb9 38 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4
JB
39 }
40
b3432a63 41 /**
e0843544 42 * Worker node virtual execution time.
b3432a63 43 */
e0843544 44 private workerNodeVirtualTaskExecutionTime = 0
b3432a63 45
2fc5cae3 46 /** @inheritDoc */
da309861 47 public constructor (
c4855468 48 pool: IPool<Worker, Data, Response>,
39618ede 49 opts?: WorkerChoiceStrategyOptions
da309861
JB
50 ) {
51 super(pool, opts)
050477bd 52 this.setTaskStatisticsRequirements(this.opts)
b3432a63
JB
53 }
54
afc003b2 55 /** @inheritDoc */
a6f7f1b4 56 public reset (): boolean {
39a43af7 57 this.resetWorkerNodeKeyProperties()
e0843544 58 this.workerNodeVirtualTaskExecutionTime = 0
ea7a90d3
JB
59 return true
60 }
61
138d29a8
JB
62 /** @inheritDoc */
63 public update (): boolean {
64 return true
65 }
66
afc003b2 67 /** @inheritDoc */
b1aae695 68 public choose (): number | undefined {
baca80f7 69 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
a38b62f1 70 this.weightedRoundRobinNextWorkerNodeKey()
8e8d9101 71 this.checkNextWorkerNodeKey()
a38b62f1 72 return this.nextWorkerNodeKey
b3432a63
JB
73 }
74
afc003b2 75 /** @inheritDoc */
f06e48d8 76 public remove (workerNodeKey: number): boolean {
226b02a3
JB
77 if (this.pool.workerNodes.length === 0) {
78 this.reset()
153179f2 79 return true
226b02a3 80 }
9b106837 81 if (this.nextWorkerNodeKey === workerNodeKey) {
e0843544 82 this.workerNodeVirtualTaskExecutionTime = 0
0dd90fe1
JB
83 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
84 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
85 }
97a2abc3 86 }
226b02a3
JB
87 if (
88 this.previousWorkerNodeKey === workerNodeKey &&
89 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
90 ) {
91 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
92 }
08f3f44c 93 return true
2377984d 94 }
9b106837 95
b1aae695 96 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
67f3f2d6
JB
97 const workerWeight =
98 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
52738541 99 this.opts!.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]
e0843544
JB
100 if (this.workerNodeVirtualTaskExecutionTime < workerWeight) {
101 this.workerNodeVirtualTaskExecutionTime +=
102 this.getWorkerNodeTaskWaitTime(
103 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
104 ) +
a38b62f1 105 this.getWorkerNodeTaskRunTime(
7c7bb289 106 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
a38b62f1
JB
107 )
108 } else {
109 this.nextWorkerNodeKey =
110 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
111 ? 0
112 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
e0843544 113 this.workerNodeVirtualTaskExecutionTime = 0
a38b62f1 114 }
20016c79 115 return this.nextWorkerNodeKey
9b106837 116 }
b3432a63 117}