-import type { AbstractPoolWorker } from '../abstract-pool-worker'
+import {
+ DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+} from '../../utils'
+import type { IPool } from '../pool'
+import type { IWorker, StrategyData } from '../worker'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-
-/**
- * Worker virtual task timestamp.
- */
-type WorkerVirtualTaskTimestamp = {
- start: number
- end: number
-}
+import {
+ type IWorkerChoiceStrategy,
+ Measurements,
+ type TaskStatisticsRequirements,
+ type WorkerChoiceStrategyOptions
+} from './selection-strategies-types'
/**
* Selects the next worker with a fair share scheduling algorithm.
* Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
*
- * @template Worker Type of worker which manages the strategy.
- * @template Data Type of data sent to the worker. This can only be serializable data.
- * @template Response Type of response of execution. This can only be serializable data.
+ * @typeParam Worker - Type of worker which manages the strategy.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
export class FairShareWorkerChoiceStrategy<
- Worker extends AbstractPoolWorker,
- Data,
- Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
- /**
- * Worker last virtual task execution timestamp.
- */
- private workerLastVirtualTaskTimestamp: Map<
- Worker,
- WorkerVirtualTaskTimestamp
- > = new Map<Worker, WorkerVirtualTaskTimestamp>()
+ Worker extends IWorker,
+ Data = unknown,
+ Response = unknown
+ >
+ extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+ implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
+ public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
+ runTime: {
+ aggregate: true,
+ average: true,
+ median: false
+ },
+ waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+ elu: {
+ aggregate: true,
+ average: true,
+ median: false
+ }
+ }
+
+ /** @inheritDoc */
+ public constructor (
+ pool: IPool<Worker, Data, Response>,
+ opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ ) {
+ super(pool, opts)
+ this.setTaskStatisticsRequirements(this.opts)
+ }
/** @inheritDoc */
- public choose (): Worker {
- this.updateWorkerLastVirtualTaskTimestamp()
- let minWorkerVirtualTaskEndTimestamp = Infinity
- let chosenWorker!: Worker
- for (const worker of this.pool.workers) {
- const workerLastVirtualTaskEndTimestamp =
- this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
- if (
- workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
- ) {
- minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
- chosenWorker = worker
- }
+ public reset (): boolean {
+ for (const workerNode of this.pool.workerNodes) {
+ delete workerNode.strategyData?.virtualTaskEndTimestamp
}
- return chosenWorker
+ return true
+ }
+
+ /** @inheritDoc */
+ public update (workerNodeKey: number): boolean {
+ this.pool.workerNodes[workerNodeKey].strategyData = {
+ virtualTaskEndTimestamp:
+ this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
+ }
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number | undefined {
+ this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+ this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey()
+ return this.nextWorkerNodeKey
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+
+ private fairShareNextWorkerNodeKey (): number | undefined {
+ return this.pool.workerNodes.reduce(
+ (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
+ if (workerNode.strategyData?.virtualTaskEndTimestamp == null) {
+ workerNode.strategyData = {
+ virtualTaskEndTimestamp:
+ this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey)
+ }
+ }
+ return this.isWorkerNodeReady(workerNodeKey) &&
+ (workerNode.strategyData.virtualTaskEndTimestamp as number) <
+ ((workerNodes[minWorkerNodeKey].strategyData as StrategyData)
+ .virtualTaskEndTimestamp as number)
+ ? workerNodeKey
+ : minWorkerNodeKey
+ },
+ 0
+ )
}
/**
- * Compute workers last virtual task timestamp.
+ * Computes the worker node key virtual task end timestamp.
+ *
+ * @param workerNodeKey - The worker node key.
+ * @returns The worker node key virtual task end timestamp.
*/
- private updateWorkerLastVirtualTaskTimestamp () {
- for (const worker of this.pool.workers) {
- const workerVirtualTaskStartTimestamp = Math.max(
- Date.now(),
- this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
- )
- const workerVirtualTaskEndTimestamp =
- workerVirtualTaskStartTimestamp +
- (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
- this.workerLastVirtualTaskTimestamp.set(worker, {
- start: workerVirtualTaskStartTimestamp,
- end: workerVirtualTaskEndTimestamp
- })
- }
+ private computeWorkerNodeVirtualTaskEndTimestamp (
+ workerNodeKey: number
+ ): number {
+ return this.getWorkerNodeVirtualTaskEndTimestamp(
+ workerNodeKey,
+ this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey)
+ )
+ }
+
+ private getWorkerNodeVirtualTaskEndTimestamp (
+ workerNodeKey: number,
+ workerNodeVirtualTaskStartTimestamp: number
+ ): number {
+ const workerNodeTaskRunTime =
+ this.opts.measurement === Measurements.elu
+ ? this.getWorkerNodeTaskElu(workerNodeKey)
+ : this.getWorkerNodeTaskRunTime(workerNodeKey)
+ return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
+ }
+
+ private getWorkerNodeVirtualTaskStartTimestamp (
+ workerNodeKey: number
+ ): number {
+ const virtualTaskEndTimestamp =
+ this.pool.workerNodes[workerNodeKey]?.strategyData
+ ?.virtualTaskEndTimestamp
+ const now = performance.now()
+ return now < (virtualTaskEndTimestamp ?? -Infinity)
+ ? (virtualTaskEndTimestamp as number)
+ : now
}
}