idle: workerTasksUsage.elu.idle + message.elu.idle,
active: workerTasksUsage.elu.active + message.elu.active,
utilization:
- workerTasksUsage.elu.utilization + message.elu.utilization
+ (workerTasksUsage.elu.utilization + message.elu.utilization) / 2
}
} else if (message.elu != null) {
workerTasksUsage.elu = message.elu
--- /dev/null
+import { 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,
+ TaskStatistics,
+ WorkerChoiceStrategyOptions
+} from './selection-strategies-types'
+
+/**
+ * Selects the worker with the least ELU.
+ *
+ * @typeParam Worker - Type of worker which manages the strategy.
+ * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
+ */
+export class LeastEluWorkerChoiceStrategy<
+ Worker extends IWorker,
+ Data = unknown,
+ Response = unknown
+ >
+ extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+ implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
+ public readonly taskStatistics: TaskStatistics = {
+ runTime: false,
+ avgRunTime: true,
+ medRunTime: false,
+ waitTime: false,
+ avgWaitTime: false,
+ medWaitTime: false,
+ elu: true
+ }
+
+ /** @inheritDoc */
+ public constructor (
+ pool: IPool<Worker, Data, Response>,
+ opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ super(pool, opts)
+ this.setTaskStatistics(this.opts)
+ }
+
+ /** @inheritDoc */
+ public reset (): boolean {
+ return true
+ }
+
+ /** @inheritDoc */
+ public update (): boolean {
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ // const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
+ // if (freeWorkerNodeKey !== -1) {
+ // return freeWorkerNodeKey
+ // }
+ let minTasksElu = Infinity
+ let leastEluWorkerNodeKey!: number
+ for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
+ const tasksUsage = workerNode.tasksUsage
+ const tasksElu = tasksUsage.elu?.utilization ?? 0
+ if (tasksElu === 0) {
+ return workerNodeKey
+ } else if (tasksElu < minTasksElu) {
+ minTasksElu = tasksElu
+ leastEluWorkerNodeKey = workerNodeKey
+ }
+ }
+ return leastEluWorkerNodeKey
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+}
import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy'
import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy'
import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy'
+import { LeastEluWorkerChoiceStrategy } from './least-elu-worker-choice-strategy'
import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
opts
)
],
+ [
+ WorkerChoiceStrategies.LEAST_ELU,
+ new (LeastEluWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
+ pool,
+ opts
+ )
+ ],
[
WorkerChoiceStrategies.FAIR_SHARE,
new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(