| 1 | import { |
| 2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
| 3 | buildInternalWorkerChoiceStrategyOptions |
| 4 | } from '../../utils' |
| 5 | import type { IPool } from '../pool' |
| 6 | import type { IWorker } from '../worker' |
| 7 | import type { |
| 8 | IWorkerChoiceStrategy, |
| 9 | InternalWorkerChoiceStrategyOptions, |
| 10 | MeasurementStatisticsRequirements, |
| 11 | StrategyPolicy, |
| 12 | TaskStatisticsRequirements |
| 13 | } from './selection-strategies-types' |
| 14 | |
| 15 | /** |
| 16 | * Worker choice strategy abstract base class. |
| 17 | * |
| 18 | * @typeParam Worker - Type of worker which manages the strategy. |
| 19 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
| 20 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. |
| 21 | */ |
| 22 | export abstract class AbstractWorkerChoiceStrategy< |
| 23 | Worker extends IWorker, |
| 24 | Data = unknown, |
| 25 | Response = unknown |
| 26 | > implements IWorkerChoiceStrategy { |
| 27 | /** |
| 28 | * The next worker node key. |
| 29 | */ |
| 30 | protected nextWorkerNodeKey: number | undefined = 0 |
| 31 | |
| 32 | /** |
| 33 | * The previous worker node key. |
| 34 | */ |
| 35 | protected previousWorkerNodeKey: number = 0 |
| 36 | |
| 37 | /** @inheritDoc */ |
| 38 | public readonly strategyPolicy: StrategyPolicy = { |
| 39 | dynamicWorkerUsage: false, |
| 40 | dynamicWorkerReady: true |
| 41 | } |
| 42 | |
| 43 | /** @inheritDoc */ |
| 44 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
| 45 | runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
| 46 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
| 47 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Constructs a worker choice strategy bound to the pool. |
| 52 | * |
| 53 | * @param pool - The pool instance. |
| 54 | * @param opts - The worker choice strategy options. |
| 55 | */ |
| 56 | public constructor ( |
| 57 | protected readonly pool: IPool<Worker, Data, Response>, |
| 58 | protected opts: InternalWorkerChoiceStrategyOptions |
| 59 | ) { |
| 60 | this.setOptions(this.opts) |
| 61 | this.choose = this.choose.bind(this) |
| 62 | } |
| 63 | |
| 64 | protected setTaskStatisticsRequirements ( |
| 65 | opts: InternalWorkerChoiceStrategyOptions |
| 66 | ): void { |
| 67 | this.toggleMedianMeasurementStatisticsRequirements( |
| 68 | this.taskStatisticsRequirements.runTime, |
| 69 | opts.runTime?.median as boolean |
| 70 | ) |
| 71 | this.toggleMedianMeasurementStatisticsRequirements( |
| 72 | this.taskStatisticsRequirements.waitTime, |
| 73 | opts.waitTime?.median as boolean |
| 74 | ) |
| 75 | this.toggleMedianMeasurementStatisticsRequirements( |
| 76 | this.taskStatisticsRequirements.elu, |
| 77 | opts.elu?.median as boolean |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | private toggleMedianMeasurementStatisticsRequirements ( |
| 82 | measurementStatisticsRequirements: MeasurementStatisticsRequirements, |
| 83 | toggleMedian: boolean |
| 84 | ): void { |
| 85 | if (measurementStatisticsRequirements.average && toggleMedian) { |
| 86 | measurementStatisticsRequirements.average = false |
| 87 | measurementStatisticsRequirements.median = toggleMedian |
| 88 | } |
| 89 | if (measurementStatisticsRequirements.median && !toggleMedian) { |
| 90 | measurementStatisticsRequirements.average = true |
| 91 | measurementStatisticsRequirements.median = toggleMedian |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected resetWorkerNodeKeyProperties (): void { |
| 96 | this.nextWorkerNodeKey = 0 |
| 97 | this.previousWorkerNodeKey = 0 |
| 98 | } |
| 99 | |
| 100 | /** @inheritDoc */ |
| 101 | public abstract reset (): boolean |
| 102 | |
| 103 | /** @inheritDoc */ |
| 104 | public abstract update (workerNodeKey: number): boolean |
| 105 | |
| 106 | /** @inheritDoc */ |
| 107 | public abstract choose (): number | undefined |
| 108 | |
| 109 | /** @inheritDoc */ |
| 110 | public abstract remove (workerNodeKey: number): boolean |
| 111 | |
| 112 | /** @inheritDoc */ |
| 113 | public setOptions (opts: InternalWorkerChoiceStrategyOptions): void { |
| 114 | this.opts = buildInternalWorkerChoiceStrategyOptions( |
| 115 | this.pool.info.maxSize, |
| 116 | opts |
| 117 | ) |
| 118 | this.setTaskStatisticsRequirements(this.opts) |
| 119 | } |
| 120 | |
| 121 | /** @inheritDoc */ |
| 122 | public hasPoolWorkerNodesReady (): boolean { |
| 123 | return this.pool.workerNodes.some(workerNode => workerNode.info.ready) |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Whether the worker node is ready or not. |
| 128 | * |
| 129 | * @param workerNodeKey - The worker node key. |
| 130 | * @returns Whether the worker node is ready or not. |
| 131 | */ |
| 132 | protected isWorkerNodeReady (workerNodeKey: number): boolean { |
| 133 | return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check the next worker node readiness. |
| 138 | */ |
| 139 | protected checkNextWorkerNodeReadiness (): void { |
| 140 | if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) { |
| 141 | delete this.nextWorkerNodeKey |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Gets the worker node task runtime. |
| 147 | * If the task statistics require the average runtime, the average runtime is returned. |
| 148 | * If the task statistics require the median runtime , the median runtime is returned. |
| 149 | * |
| 150 | * @param workerNodeKey - The worker node key. |
| 151 | * @returns The worker node task runtime. |
| 152 | */ |
| 153 | protected getWorkerNodeTaskRunTime (workerNodeKey: number): number { |
| 154 | return this.taskStatisticsRequirements.runTime.median |
| 155 | ? this.pool.workerNodes[workerNodeKey].usage.runTime.median ?? 0 |
| 156 | : this.pool.workerNodes[workerNodeKey].usage.runTime.average ?? 0 |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Gets the worker node task wait time. |
| 161 | * If the task statistics require the average wait time, the average wait time is returned. |
| 162 | * If the task statistics require the median wait time, the median wait time is returned. |
| 163 | * |
| 164 | * @param workerNodeKey - The worker node key. |
| 165 | * @returns The worker node task wait time. |
| 166 | */ |
| 167 | protected getWorkerNodeTaskWaitTime (workerNodeKey: number): number { |
| 168 | return this.taskStatisticsRequirements.waitTime.median |
| 169 | ? this.pool.workerNodes[workerNodeKey].usage.waitTime.median ?? 0 |
| 170 | : this.pool.workerNodes[workerNodeKey].usage.waitTime.average ?? 0 |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets the worker node task ELU. |
| 175 | * If the task statistics require the average ELU, the average ELU is returned. |
| 176 | * If the task statistics require the median ELU, the median ELU is returned. |
| 177 | * |
| 178 | * @param workerNodeKey - The worker node key. |
| 179 | * @returns The worker node task ELU. |
| 180 | */ |
| 181 | protected getWorkerNodeTaskElu (workerNodeKey: number): number { |
| 182 | return this.taskStatisticsRequirements.elu.median |
| 183 | ? this.pool.workerNodes[workerNodeKey].usage.elu.active.median ?? 0 |
| 184 | : this.pool.workerNodes[workerNodeKey].usage.elu.active.average ?? 0 |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets safely the previous worker node key. |
| 189 | * |
| 190 | * @param workerNodeKey - The worker node key. |
| 191 | */ |
| 192 | protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void { |
| 193 | this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey |
| 194 | } |
| 195 | } |