feat: add worker choice strategies retry mechanism
[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 = {
32 useDynamicWorker: true
33 }
34
afc003b2 35 /** @inheritDoc */
87de9ff5 36 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
37 runTime: {
38 aggregate: true,
39 average: true,
40 median: false
41 },
3c93feb9
JB
42 waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
43 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
10fcfaf4
JB
44 }
45
b3432a63
JB
46 /**
47 * Default worker weight.
48 */
777af0ac 49 private readonly defaultWorkerWeight: number
b3432a63 50 /**
08f3f44c 51 * Worker virtual task runtime.
b3432a63 52 */
08f3f44c 53 private workerVirtualTaskRunTime: number = 0
b3432a63 54
2fc5cae3 55 /** @inheritDoc */
da309861 56 public constructor (
c4855468 57 pool: IPool<Worker, Data, Response>,
2fc5cae3 58 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
da309861
JB
59 ) {
60 super(pool, opts)
932fc8be 61 this.setTaskStatisticsRequirements(this.opts)
08f3f44c 62 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
b3432a63
JB
63 }
64
afc003b2 65 /** @inheritDoc */
a6f7f1b4 66 public reset (): boolean {
9b106837 67 this.nextWorkerNodeKey = 0
08f3f44c 68 this.workerVirtualTaskRunTime = 0
ea7a90d3
JB
69 return true
70 }
71
138d29a8
JB
72 /** @inheritDoc */
73 public update (): boolean {
74 return true
75 }
76
afc003b2 77 /** @inheritDoc */
c923ce56 78 public choose (): number {
9b106837 79 const chosenWorkerNodeKey = this.nextWorkerNodeKey
949194eb
JB
80 do {
81 this.weightedRoundRobinNextWorkerNodeKey()
8990357d 82 } while (!this.isWorkerNodeEligible(this.nextWorkerNodeKey))
f06e48d8 83 return chosenWorkerNodeKey
b3432a63
JB
84 }
85
afc003b2 86 /** @inheritDoc */
f06e48d8 87 public remove (workerNodeKey: number): boolean {
9b106837 88 if (this.nextWorkerNodeKey === workerNodeKey) {
f06e48d8 89 if (this.pool.workerNodes.length === 0) {
9b106837
JB
90 this.nextWorkerNodeKey = 0
91 } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
92 this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
78ab2555 93 }
08f3f44c 94 this.workerVirtualTaskRunTime = 0
97a2abc3 95 }
08f3f44c 96 return true
2377984d 97 }
9b106837 98
20016c79 99 private weightedRoundRobinNextWorkerNodeKey (): number {
9b106837
JB
100 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
101 const workerWeight =
102 this.opts.weights?.[this.nextWorkerNodeKey] ?? this.defaultWorkerWeight
103 if (workerVirtualTaskRunTime < workerWeight) {
104 this.workerVirtualTaskRunTime =
105 workerVirtualTaskRunTime +
106 this.getWorkerTaskRunTime(this.nextWorkerNodeKey)
107 } else {
108 this.nextWorkerNodeKey =
109 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
110 ? 0
111 : this.nextWorkerNodeKey + 1
112 this.workerVirtualTaskRunTime = 0
113 }
20016c79 114 return this.nextWorkerNodeKey
9b106837 115 }
b3432a63 116}