-import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
+import {
+ DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+} from '../../utils'
import type { IPool } from '../pool'
import type { IWorker } from '../worker'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
+ StrategyPolicy,
TaskStatisticsRequirements,
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
+ public readonly strategyPolicy: StrategyPolicy = {
+ dynamicWorkerUsage: false,
+ dynamicWorkerReady: true
+ }
+
/** @inheritDoc */
public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
runTime: {
average: false,
median: false
},
- elu: {
- aggregate: false,
- average: false,
- median: false
- }
+ elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
}
/** @inheritDoc */
}
/** @inheritDoc */
- public choose (): number {
+ public choose (): number | undefined {
+ const chosenWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
+ this.assignChosenWorkerNodeKey(chosenWorkerNodeKey)
+ return this.nextWorkerNodeKey
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+
+ private leastBusyNextWorkerNodeKey (): number | undefined {
let minTime = Infinity
+ let chosenWorkerNodeKey: number | undefined
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerTime =
- workerNode.workerUsage.runTime.aggregate +
- workerNode.workerUsage.waitTime.aggregate
- if (workerTime === 0) {
- this.nextWorkerNodeId = workerNodeKey
+ (workerNode.usage.runTime?.aggregate ?? 0) +
+ (workerNode.usage.waitTime?.aggregate ?? 0)
+ if (this.isWorkerNodeEligible(workerNodeKey) && workerTime === 0) {
+ chosenWorkerNodeKey = workerNodeKey
break
- } else if (workerTime < minTime) {
+ } else if (
+ this.isWorkerNodeEligible(workerNodeKey) &&
+ workerTime < minTime
+ ) {
minTime = workerTime
- this.nextWorkerNodeId = workerNodeKey
+ chosenWorkerNodeKey = workerNodeKey
}
}
- return this.nextWorkerNodeId
- }
-
- /** @inheritDoc */
- public remove (): boolean {
- return true
+ return chosenWorkerNodeKey
}
}