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