1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
6 type IWorkerChoiceStrategy
,
8 type TaskStatisticsRequirements
,
9 type WorkerChoiceStrategyOptions
10 } from
'./selection-strategies-types'
13 * Selects the next worker with a fair share scheduling algorithm.
14 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
16 * @typeParam Worker - Type of worker which manages the strategy.
17 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
18 * @typeParam Response - Type of execution response. This can only be serializable data.
20 export class FairShareWorkerChoiceStrategy
<
21 Worker
extends IWorker
,
25 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
26 implements IWorkerChoiceStrategy
{
28 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
47 * Workers' virtual task end execution timestamp.
49 private workersVirtualTaskEndTimestamp
: number[] = []
53 pool
: IPool
<Worker
, Data
, Response
>,
54 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
57 this.setTaskStatisticsRequirements(this.opts
)
61 public reset (): boolean {
62 this.workersVirtualTaskEndTimestamp
= []
67 public update (workerNodeKey
: number): boolean {
68 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
73 public choose (): number {
74 let minWorkerVirtualTaskEndTimestamp
= Infinity
75 let chosenWorkerNodeKey
!: number
76 for (const [workerNodeKey
] of this.pool
.workerNodes
.entries()) {
77 if (this.workersVirtualTaskEndTimestamp
[workerNodeKey
] == null) {
78 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
80 const workerVirtualTaskEndTimestamp
=
81 this.workersVirtualTaskEndTimestamp
[workerNodeKey
]
82 if (workerVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
) {
83 minWorkerVirtualTaskEndTimestamp
= workerVirtualTaskEndTimestamp
84 chosenWorkerNodeKey
= workerNodeKey
87 return chosenWorkerNodeKey
91 public remove (workerNodeKey
: number): boolean {
92 this.workersVirtualTaskEndTimestamp
.splice(workerNodeKey
, 1)
97 * Computes the worker node key virtual task end timestamp.
99 * @param workerNodeKey - The worker node key.
101 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey
: number): void {
102 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] =
103 this.getWorkerVirtualTaskEndTimestamp(
105 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey
)
109 private getWorkerVirtualTaskEndTimestamp (
110 workerNodeKey
: number,
111 workerVirtualTaskStartTimestamp
: number
113 const workerTaskRunTime
=
114 this.opts
.measurement
=== Measurements
.elu
115 ? this.getWorkerTaskElu(workerNodeKey
)
116 : this.getWorkerTaskRunTime(workerNodeKey
)
117 return workerVirtualTaskStartTimestamp
+ workerTaskRunTime
120 private getWorkerVirtualTaskStartTimestamp (workerNodeKey
: number): number {
123 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] ?? -Infinity