fix: ensure worker choice is retried at least the pool max size
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
1 import type { IWorker } from '../worker'
2 import type { IPool } from '../pool'
3 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 InternalWorkerChoiceStrategyOptions,
8 TaskStatisticsRequirements
9 } from './selection-strategies-types'
10
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 *
15 * @typeParam Worker - Type of worker which manages the strategy.
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.
18 */
19 export class WeightedRoundRobinWorkerChoiceStrategy<
20 Worker extends IWorker,
21 Data = unknown,
22 Response = unknown
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
25 implements IWorkerChoiceStrategy {
26 /** @inheritDoc */
27 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
28 runTime: {
29 aggregate: true,
30 average: true,
31 median: false
32 },
33 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
34 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
35 }
36
37 /**
38 * Default worker weight.
39 */
40 private readonly defaultWorkerWeight: number
41 /**
42 * Worker node virtual task runtime.
43 */
44 private workerNodeVirtualTaskRunTime: number = 0
45
46 /** @inheritDoc */
47 public constructor (
48 pool: IPool<Worker, Data, Response>,
49 opts: InternalWorkerChoiceStrategyOptions
50 ) {
51 super(pool, opts)
52 this.setTaskStatisticsRequirements(this.opts)
53 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
54 }
55
56 /** @inheritDoc */
57 public reset (): boolean {
58 this.resetWorkerNodeKeyProperties()
59 this.workerNodeVirtualTaskRunTime = 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 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
71 this.weightedRoundRobinNextWorkerNodeKey()
72 this.checkNextWorkerNodeReadiness()
73 return this.nextWorkerNodeKey
74 }
75
76 /** @inheritDoc */
77 public remove (workerNodeKey: number): boolean {
78 if (this.pool.workerNodes.length === 0) {
79 this.reset()
80 }
81 if (this.nextWorkerNodeKey === workerNodeKey) {
82 this.workerNodeVirtualTaskRunTime = 0
83 if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
84 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
85 }
86 }
87 if (
88 this.previousWorkerNodeKey === workerNodeKey &&
89 this.previousWorkerNodeKey > this.pool.workerNodes.length - 1
90 ) {
91 this.previousWorkerNodeKey = this.pool.workerNodes.length - 1
92 }
93 return true
94 }
95
96 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
97 const workerWeight =
98 this.opts.weights?.[
99 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
100 ] ?? this.defaultWorkerWeight
101 if (this.workerNodeVirtualTaskRunTime < workerWeight) {
102 this.workerNodeVirtualTaskRunTime =
103 this.workerNodeVirtualTaskRunTime +
104 this.getWorkerNodeTaskRunTime(
105 this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
106 )
107 } else {
108 this.nextWorkerNodeKey =
109 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
110 ? 0
111 : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
112 this.workerNodeVirtualTaskRunTime = 0
113 }
114 return this.nextWorkerNodeKey
115 }
116 }