fix: fix fair share algorithm implementation
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
bbeadd16 1import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
08f3f44c 2import type { IPool } from '../pool'
f06e48d8 3import type { IWorker } from '../worker'
10fcfaf4
JB
4import type {
5 IWorkerChoiceStrategy,
da309861
JB
6 RequiredStatistics,
7 WorkerChoiceStrategyOptions
10fcfaf4 8} from './selection-strategies-types'
bdaf31cd
JB
9
10/**
9cd39dd4 11 * Worker choice strategy abstract base class.
bdaf31cd 12 *
38e795c1
JB
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.
02706357 15 * @typeParam Response - Type of execution response. This can only be serializable data.
bdaf31cd
JB
16 */
17export abstract class AbstractWorkerChoiceStrategy<
f06e48d8 18 Worker extends IWorker,
b2b1d84e
JB
19 Data = unknown,
20 Response = unknown
17393ac8 21> implements IWorkerChoiceStrategy {
cb70b19d
JB
22 /**
23 * Toggles finding the last free worker node key.
24 */
25 private toggleFindLastFreeWorkerNodeKey: boolean = false
afc003b2 26 /** @inheritDoc */
da309861 27 public readonly requiredStatistics: RequiredStatistics = {
c6bd2650 28 runTime: false,
78099a15
JB
29 avgRunTime: false,
30 medRunTime: false
10fcfaf4 31 }
bdaf31cd
JB
32
33 /**
6533c3e6 34 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 35 *
38e795c1 36 * @param pool - The pool instance.
da309861 37 * @param opts - The worker choice strategy options.
bdaf31cd
JB
38 */
39 public constructor (
c4855468 40 protected readonly pool: IPool<Worker, Data, Response>,
a20f0ba5 41 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
b8f3418c 42 ) {
7254e419 43 this.choose = this.choose.bind(this)
b8f3418c 44 }
bdaf31cd 45
2fc5cae3 46 protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
f9f00b5f 47 if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
2fc5cae3
JB
48 this.requiredStatistics.avgRunTime = false
49 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
da309861 50 }
a20f0ba5
JB
51 if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
52 this.requiredStatistics.avgRunTime = true
53 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
54 }
08f3f44c
JB
55 if (
56 opts.weights != null &&
57 Object.keys(opts.weights).length < this.pool.size
58 ) {
59 throw new Error(
60 'Worker choice strategy options must have a weight for each worker node.'
61 )
62 }
da309861
JB
63 }
64
afc003b2 65 /** @inheritDoc */
a6f7f1b4 66 public abstract reset (): boolean
ea7a90d3 67
138d29a8 68 /** @inheritDoc */
a4958de2 69 public abstract update (workerNodeKey: number): boolean
138d29a8 70
afc003b2 71 /** @inheritDoc */
c923ce56 72 public abstract choose (): number
97a2abc3 73
afc003b2 74 /** @inheritDoc */
f06e48d8 75 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
76
77 /** @inheritDoc */
78 public setOptions (opts: WorkerChoiceStrategyOptions): void {
d01e4d67 79 opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
a20f0ba5
JB
80 this.checkOptions(opts)
81 this.opts = opts
82 }
cb70b19d
JB
83
84 /**
85 * Finds a free worker node key.
86 *
87 * @returns The free worker node key or `-1` if there is no free worker node.
88 */
89 protected findFreeWorkerNodeKey (): number {
90 if (this.toggleFindLastFreeWorkerNodeKey) {
91 this.toggleFindLastFreeWorkerNodeKey = false
e0ae6100 92 return this.findLastFreeWorkerNodeKey()
cb70b19d
JB
93 }
94 this.toggleFindLastFreeWorkerNodeKey = true
e0ae6100
JB
95 return this.findFirstFreeWorkerNodeKey()
96 }
97
98 /**
99 * Finds the first free worker node key based on the number of tasks the worker has applied.
100 *
101 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
102 *
103 * If no free worker is found, `-1` is returned.
104 *
105 * @returns A worker node key if there is one, `-1` otherwise.
106 */
107 private findFirstFreeWorkerNodeKey (): number {
108 return this.pool.workerNodes.findIndex(workerNode => {
a4958de2 109 return workerNode.tasksUsage.running === 0
e0ae6100
JB
110 })
111 }
112
113 /**
114 * Finds the last free worker node key based on the number of tasks the worker has applied.
115 *
116 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
117 *
118 * If no free worker is found, `-1` is returned.
119 *
120 * @returns A worker node key if there is one, `-1` otherwise.
121 */
122 private findLastFreeWorkerNodeKey (): number {
0e7c56b0 123 // It requires node >= 18.0.0:
e0ae6100 124 // return this.workerNodes.findLastIndex(workerNode => {
a4958de2 125 // return workerNode.tasksUsage.running === 0
e0ae6100
JB
126 // })
127 for (let i = this.pool.workerNodes.length - 1; i >= 0; i--) {
a4958de2 128 if (this.pool.workerNodes[i].tasksUsage.running === 0) {
e0ae6100
JB
129 return i
130 }
131 }
132 return -1
cb70b19d 133 }
bdaf31cd 134}