1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
7 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Worker choice strategy abstract base class.
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.
17 export abstract class AbstractWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
21 > implements IWorkerChoiceStrategy
{
23 * Toggles finding the last free worker node key.
25 private toggleFindLastFreeWorkerNodeKey
: boolean = false
27 public readonly requiredStatistics
: RequiredStatistics
= {
37 * Constructs a worker choice strategy bound to the pool.
39 * @param pool - The pool instance.
40 * @param opts - The worker choice strategy options.
43 protected readonly pool
: IPool
<Worker
, Data
, Response
>,
44 protected opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
46 this.choose
= this.choose
.bind(this)
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
54 if (this.requiredStatistics
.medRunTime
&& opts
.medRunTime
=== false) {
55 this.requiredStatistics
.avgRunTime
= true
56 this.requiredStatistics
.medRunTime
= opts
.medRunTime
as boolean
58 if (this.requiredStatistics
.avgWaitTime
&& opts
.medWaitTime
=== true) {
59 this.requiredStatistics
.avgWaitTime
= false
60 this.requiredStatistics
.medWaitTime
= opts
.medWaitTime
as boolean
62 if (this.requiredStatistics
.medWaitTime
&& opts
.medWaitTime
=== false) {
63 this.requiredStatistics
.avgWaitTime
= true
64 this.requiredStatistics
.medWaitTime
= opts
.medWaitTime
as boolean
69 public abstract reset (): boolean
72 public abstract update (workerNodeKey
: number): boolean
75 public abstract choose (): number
78 public abstract remove (workerNodeKey
: number): boolean
81 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
82 opts
= opts
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
83 this.setRequiredStatistics(opts
)
88 * Finds a free worker node key.
90 * @returns The free worker node key or `-1` if there is no free worker node.
92 protected findFreeWorkerNodeKey (): number {
93 if (this.toggleFindLastFreeWorkerNodeKey
) {
94 this.toggleFindLastFreeWorkerNodeKey
= false
95 return this.findLastFreeWorkerNodeKey()
97 this.toggleFindLastFreeWorkerNodeKey
= true
98 return this.findFirstFreeWorkerNodeKey()
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.
106 * @param workerNodeKey - The worker node key.
107 * @returns The worker task runtime.
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
116 * Finds the first free worker node key based on the number of tasks the worker has applied.
118 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
120 * If no free worker is found, `-1` is returned.
122 * @returns A worker node key if there is one, `-1` otherwise.
124 private findFirstFreeWorkerNodeKey (): number {
125 return this.pool
.workerNodes
.findIndex(workerNode
=> {
126 return workerNode
.tasksUsage
.running
=== 0
131 * Finds the last free worker node key based on the number of tasks the worker has applied.
133 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
135 * If no free worker is found, `-1` is returned.
137 * @returns A worker node key if there is one, `-1` otherwise.
139 private findLastFreeWorkerNodeKey (): number {
140 // It requires node >= 18.0.0:
141 // return this.workerNodes.findLastIndex(workerNode => {
142 // return workerNode.tasksUsage.running === 0
145 let workerNodeKey
= this.pool
.workerNodes
.length
- 1;
149 if (this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.running
=== 0) {