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
,
12 type TaskStatisticsRequirements
,
13 type WorkerChoiceStrategyOptions
14 } from
'./selection-strategies-types'
17 * Selects the next worker with a fair share scheduling algorithm.
18 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
20 * @typeParam Worker - Type of worker which manages the strategy.
21 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
22 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
24 export class FairShareWorkerChoiceStrategy
<
25 Worker
extends IWorker
,
29 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
30 implements IWorkerChoiceStrategy
{
32 public readonly strategyPolicy
: StrategyPolicy
= {
33 dynamicWorkerUsage
: false,
34 dynamicWorkerReady
: true
38 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
44 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
53 * Workers' virtual task end execution timestamp.
55 private workersVirtualTaskEndTimestamp
: number[] = []
59 pool
: IPool
<Worker
, Data
, Response
>,
60 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
63 this.setTaskStatisticsRequirements(this.opts
)
67 public reset (): boolean {
68 this.workersVirtualTaskEndTimestamp
= []
73 public update (workerNodeKey
: number): boolean {
74 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
79 public choose (): number | undefined {
80 const chosenWorkerNodeKey
= this.fairShareNextWorkerNodeKey()
81 this.assignChosenWorkerNodeKey(chosenWorkerNodeKey
)
82 return this.nextWorkerNodeKey
86 public remove (workerNodeKey
: number): boolean {
87 this.workersVirtualTaskEndTimestamp
.splice(workerNodeKey
, 1)
91 private fairShareNextWorkerNodeKey (): number | undefined {
92 let minWorkerVirtualTaskEndTimestamp
= Infinity
93 let chosenWorkerNodeKey
: number | undefined
94 for (const [workerNodeKey
] of this.pool
.workerNodes
.entries()) {
95 if (this.workersVirtualTaskEndTimestamp
[workerNodeKey
] == null) {
96 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
98 const workerVirtualTaskEndTimestamp
=
99 this.workersVirtualTaskEndTimestamp
[workerNodeKey
]
101 this.isWorkerNodeEligible(workerNodeKey
) &&
102 workerVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
104 minWorkerVirtualTaskEndTimestamp
= workerVirtualTaskEndTimestamp
105 chosenWorkerNodeKey
= workerNodeKey
108 return chosenWorkerNodeKey
112 * Computes the worker node key virtual task end timestamp.
114 * @param workerNodeKey - The worker node key.
116 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey
: number): void {
117 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] =
118 this.getWorkerVirtualTaskEndTimestamp(
120 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey
)
124 private getWorkerVirtualTaskEndTimestamp (
125 workerNodeKey
: number,
126 workerVirtualTaskStartTimestamp
: number
128 const workerTaskRunTime
=
129 this.opts
.measurement
=== Measurements
.elu
130 ? this.getWorkerTaskElu(workerNodeKey
)
131 : this.getWorkerTaskRunTime(workerNodeKey
)
132 return workerVirtualTaskStartTimestamp
+ workerTaskRunTime
135 private getWorkerVirtualTaskStartTimestamp (workerNodeKey
: number): number {
138 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] ?? -Infinity