17ae66478606990c44244f44c46a9d8e5f8fb943
[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 RequiredStatistics,
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 requiredStatistics: RequiredStatistics = {
28 runTime: true,
29 avgRunTime: true,
30 medRunTime: false,
31 waitTime: false,
32 avgWaitTime: false,
33 medWaitTime: false
34 }
35
36 /**
37 * Worker node id where the current task will be submitted.
38 */
39 private currentWorkerNodeId: number = 0
40 /**
41 * Default worker weight.
42 */
43 private readonly defaultWorkerWeight: number
44 /**
45 * Worker virtual task runtime.
46 */
47 private workerVirtualTaskRunTime: number = 0
48
49 /** @inheritDoc */
50 public constructor (
51 pool: IPool<Worker, Data, Response>,
52 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
53 ) {
54 super(pool, opts)
55 this.setRequiredStatistics(this.opts)
56 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
57 }
58
59 /** @inheritDoc */
60 public reset (): boolean {
61 this.currentWorkerNodeId = 0
62 this.workerVirtualTaskRunTime = 0
63 return true
64 }
65
66 /** @inheritDoc */
67 public update (): boolean {
68 return true
69 }
70
71 /** @inheritDoc */
72 public choose (): number {
73 const chosenWorkerNodeKey = this.currentWorkerNodeId
74 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
75 const workerWeight =
76 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
77 if (workerVirtualTaskRunTime < workerWeight) {
78 this.workerVirtualTaskRunTime =
79 workerVirtualTaskRunTime +
80 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
81 } else {
82 this.currentWorkerNodeId =
83 this.currentWorkerNodeId === this.pool.workerNodes.length - 1
84 ? 0
85 : this.currentWorkerNodeId + 1
86 this.workerVirtualTaskRunTime = 0
87 }
88 return chosenWorkerNodeKey
89 }
90
91 /** @inheritDoc */
92 public remove (workerNodeKey: number): boolean {
93 if (this.currentWorkerNodeId === workerNodeKey) {
94 if (this.pool.workerNodes.length === 0) {
95 this.currentWorkerNodeId = 0
96 } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
97 this.currentWorkerNodeId = this.pool.workerNodes.length - 1
98 }
99 this.workerVirtualTaskRunTime = 0
100 }
101 return true
102 }
103 }