chore: generate documentation
[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 {
d33be430 67 this.nextWorkerNodeId = 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 {
d33be430 79 const chosenWorkerNodeKey = this.nextWorkerNodeId
138d29a8
JB
80 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
81 const workerWeight =
08f3f44c 82 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
138d29a8 83 if (workerVirtualTaskRunTime < workerWeight) {
08f3f44c 84 this.workerVirtualTaskRunTime =
0d80593b 85 workerVirtualTaskRunTime +
f6b641d6 86 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
b3432a63 87 } else {
d33be430
JB
88 this.nextWorkerNodeId =
89 this.nextWorkerNodeId === this.pool.workerNodes.length - 1
b3432a63 90 ? 0
d33be430 91 : this.nextWorkerNodeId + 1
08f3f44c 92 this.workerVirtualTaskRunTime = 0
b3432a63 93 }
f06e48d8 94 return chosenWorkerNodeKey
b3432a63
JB
95 }
96
afc003b2 97 /** @inheritDoc */
f06e48d8 98 public remove (workerNodeKey: number): boolean {
d33be430 99 if (this.nextWorkerNodeId === workerNodeKey) {
f06e48d8 100 if (this.pool.workerNodes.length === 0) {
d33be430
JB
101 this.nextWorkerNodeId = 0
102 } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
103 this.nextWorkerNodeId = this.pool.workerNodes.length - 1
78ab2555 104 }
08f3f44c 105 this.workerVirtualTaskRunTime = 0
97a2abc3 106 }
08f3f44c 107 return true
2377984d 108 }
b3432a63 109}