refactor: spell fixes
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2 import type { IPool } from '../pool'
3 import type { IWorker } from '../worker'
4 import type {
5 IWorkerChoiceStrategy,
6 RequiredStatistics,
7 WorkerChoiceStrategyOptions
8 } from './selection-strategies-types'
9
10 /**
11 * Worker choice strategy abstract base class.
12 *
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
15 * @typeParam Response - Type of execution response. This can only be serializable data.
16 */
17 export abstract class AbstractWorkerChoiceStrategy<
18 Worker extends IWorker,
19 Data = unknown,
20 Response = unknown
21 > implements IWorkerChoiceStrategy {
22 /**
23 * Toggles finding the last free worker node key.
24 */
25 private toggleFindLastFreeWorkerNodeKey: boolean = false
26 /** @inheritDoc */
27 public readonly requiredStatistics: RequiredStatistics = {
28 runTime: false,
29 avgRunTime: false,
30 medRunTime: false,
31 waitTime: false,
32 avgWaitTime: false,
33 medWaitTime: false
34 }
35
36 /**
37 * Constructs a worker choice strategy bound to the pool.
38 *
39 * @param pool - The pool instance.
40 * @param opts - The worker choice strategy options.
41 */
42 public constructor (
43 protected readonly pool: IPool<Worker, Data, Response>,
44 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
45 ) {
46 this.choose = this.choose.bind(this)
47 }
48
49 protected setRequiredStatistics (opts: WorkerChoiceStrategyOptions): void {
50 if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
51 this.requiredStatistics.avgRunTime = false
52 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
53 }
54 if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
55 this.requiredStatistics.avgRunTime = true
56 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
57 }
58 if (this.requiredStatistics.avgWaitTime && opts.medWaitTime === true) {
59 this.requiredStatistics.avgWaitTime = false
60 this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean
61 }
62 if (this.requiredStatistics.medWaitTime && opts.medWaitTime === false) {
63 this.requiredStatistics.avgWaitTime = true
64 this.requiredStatistics.medWaitTime = opts.medWaitTime as boolean
65 }
66 }
67
68 /** @inheritDoc */
69 public abstract reset (): boolean
70
71 /** @inheritDoc */
72 public abstract update (workerNodeKey: number): boolean
73
74 /** @inheritDoc */
75 public abstract choose (): number
76
77 /** @inheritDoc */
78 public abstract remove (workerNodeKey: number): boolean
79
80 /** @inheritDoc */
81 public setOptions (opts: WorkerChoiceStrategyOptions): void {
82 opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
83 this.setRequiredStatistics(opts)
84 this.opts = opts
85 }
86
87 /**
88 * Finds a free worker node key.
89 *
90 * @returns The free worker node key or `-1` if there is no free worker node.
91 */
92 protected findFreeWorkerNodeKey (): number {
93 if (this.toggleFindLastFreeWorkerNodeKey) {
94 this.toggleFindLastFreeWorkerNodeKey = false
95 return this.findLastFreeWorkerNodeKey()
96 }
97 this.toggleFindLastFreeWorkerNodeKey = true
98 return this.findFirstFreeWorkerNodeKey()
99 }
100
101 /**
102 * Gets the worker task runtime.
103 * If the required statistics are `avgRunTime`, the average runtime is returned.
104 * If the required statistics are `medRunTime`, the median runtime is returned.
105 *
106 * @param workerNodeKey - The worker node key.
107 * @returns The worker task runtime.
108 */
109 protected getWorkerTaskRunTime (workerNodeKey: number): number {
110 return this.requiredStatistics.medRunTime
111 ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
112 : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
113 }
114
115 /**
116 * Finds the first free worker node key based on the number of tasks the worker has applied.
117 *
118 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
119 *
120 * If no free worker is found, `-1` is returned.
121 *
122 * @returns A worker node key if there is one, `-1` otherwise.
123 */
124 private findFirstFreeWorkerNodeKey (): number {
125 return this.pool.workerNodes.findIndex(workerNode => {
126 return workerNode.tasksUsage.running === 0
127 })
128 }
129
130 /**
131 * Finds the last free worker node key based on the number of tasks the worker has applied.
132 *
133 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
134 *
135 * If no free worker is found, `-1` is returned.
136 *
137 * @returns A worker node key if there is one, `-1` otherwise.
138 */
139 private findLastFreeWorkerNodeKey (): number {
140 // It requires node >= 18.0.0:
141 // return this.workerNodes.findLastIndex(workerNode => {
142 // return workerNode.tasksUsage.running === 0
143 // })
144 for (
145 let workerNodeKey = this.pool.workerNodes.length - 1;
146 workerNodeKey >= 0;
147 workerNodeKey--
148 ) {
149 if (this.pool.workerNodes[workerNodeKey].tasksUsage.running === 0) {
150 return workerNodeKey
151 }
152 }
153 return -1
154 }
155 }