refactor: align worker choice strategy options namespace
[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_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
4 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
5 import type {
6 IWorkerChoiceStrategy,
7 TaskStatisticsRequirements,
8 WorkerChoiceStrategyOptions
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 serializable data.
17 * @typeParam Response - Type of execution response. This can only be serializable 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: {
34 aggregate: false,
35 average: false,
36 median: false
37 },
38 elu: false
39 }
40
41 /**
42 * Worker node id where the current task will be submitted.
43 */
44 private currentWorkerNodeId: number = 0
45 /**
46 * Default worker weight.
47 */
48 private readonly defaultWorkerWeight: number
49 /**
50 * Worker virtual task runtime.
51 */
52 private workerVirtualTaskRunTime: number = 0
53
54 /** @inheritDoc */
55 public constructor (
56 pool: IPool<Worker, Data, Response>,
57 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
58 ) {
59 super(pool, opts)
60 this.setTaskStatisticsRequirements(this.opts)
61 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
62 }
63
64 /** @inheritDoc */
65 public reset (): boolean {
66 this.currentWorkerNodeId = 0
67 this.workerVirtualTaskRunTime = 0
68 return true
69 }
70
71 /** @inheritDoc */
72 public update (): boolean {
73 return true
74 }
75
76 /** @inheritDoc */
77 public choose (): number {
78 const chosenWorkerNodeKey = this.currentWorkerNodeId
79 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
80 const workerWeight =
81 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
82 if (workerVirtualTaskRunTime < workerWeight) {
83 this.workerVirtualTaskRunTime =
84 workerVirtualTaskRunTime +
85 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
86 } else {
87 this.currentWorkerNodeId =
88 this.currentWorkerNodeId === this.pool.workerNodes.length - 1
89 ? 0
90 : this.currentWorkerNodeId + 1
91 this.workerVirtualTaskRunTime = 0
92 }
93 return chosenWorkerNodeKey
94 }
95
96 /** @inheritDoc */
97 public remove (workerNodeKey: number): boolean {
98 if (this.currentWorkerNodeId === workerNodeKey) {
99 if (this.pool.workerNodes.length === 0) {
100 this.currentWorkerNodeId = 0
101 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
102 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
103 }
104 this.workerVirtualTaskRunTime = 0
105 }
106 return true
107 }
108 }