2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
5 import type { IPool
} from
'../pool'
6 import type { IWorker
} from
'../worker'
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
9 type IWorkerChoiceStrategy
,
11 type TaskStatisticsRequirements
,
12 type WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
16 * Selects the next worker with a fair share scheduling algorithm.
17 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23 export class FairShareWorkerChoiceStrategy
<
24 Worker
extends IWorker
,
28 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
29 implements IWorkerChoiceStrategy
{
31 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
37 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
46 * Workers' virtual task end execution timestamp.
48 private workersVirtualTaskEndTimestamp
: number[] = []
52 pool
: IPool
<Worker
, Data
, Response
>,
53 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
56 this.setTaskStatisticsRequirements(this.opts
)
60 public reset (): boolean {
61 this.workersVirtualTaskEndTimestamp
= []
66 public update (workerNodeKey
: number): boolean {
67 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
72 public choose (): number | undefined {
73 const chosenWorkerNodeKey
= this.fairShareNextWorkerNodeKey()
74 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey
)
75 return this.nextWorkerNodeKey
79 public remove (workerNodeKey
: number): boolean {
80 this.workersVirtualTaskEndTimestamp
.splice(workerNodeKey
, 1)
84 private fairShareNextWorkerNodeKey (): number | undefined {
85 let minWorkerVirtualTaskEndTimestamp
= Infinity
86 let chosenWorkerNodeKey
: number | undefined
87 for (const [workerNodeKey
] of this.pool
.workerNodes
.entries()) {
88 if (this.workersVirtualTaskEndTimestamp
[workerNodeKey
] == null) {
89 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
91 const workerVirtualTaskEndTimestamp
=
92 this.workersVirtualTaskEndTimestamp
[workerNodeKey
]
94 this.isWorkerNodeEligible(workerNodeKey
) &&
95 workerVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
97 minWorkerVirtualTaskEndTimestamp
= workerVirtualTaskEndTimestamp
98 chosenWorkerNodeKey
= workerNodeKey
101 return chosenWorkerNodeKey
105 * Computes the worker node key virtual task end timestamp.
107 * @param workerNodeKey - The worker node key.
109 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey
: number): void {
110 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] =
111 this.getWorkerVirtualTaskEndTimestamp(
113 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey
)
117 private getWorkerVirtualTaskEndTimestamp (
118 workerNodeKey
: number,
119 workerVirtualTaskStartTimestamp
: number
121 const workerTaskRunTime
=
122 this.opts
.measurement
=== Measurements
.elu
123 ? this.getWorkerTaskElu(workerNodeKey
)
124 : this.getWorkerTaskRunTime(workerNodeKey
)
125 return workerVirtualTaskStartTimestamp
+ workerTaskRunTime
128 private getWorkerVirtualTaskStartTimestamp (workerNodeKey
: number): number {
131 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] ?? -Infinity