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'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the next worker with a fair share scheduling algorithm.
13 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
15 * @typeParam Worker - Type of worker which manages the strategy.
16 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
17 * @typeParam Response - Type of execution response. This can only be serializable data.
19 export class FairShareWorkerChoiceStrategy
<
20 Worker
extends IWorker
,
24 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
25 implements IWorkerChoiceStrategy
{
27 public readonly requiredStatistics
: RequiredStatistics
= {
34 * Workers' virtual task end execution timestamp.
36 private workersVirtualTaskEndTimestamp
: number[] = []
40 pool
: IPool
<Worker
, Data
, Response
>,
41 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
44 this.setRequiredStatistics(this.opts
)
48 public reset (): boolean {
49 this.workersVirtualTaskEndTimestamp
= []
54 public update (workerNodeKey
: number): boolean {
55 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
60 public choose (): number {
61 let minWorkerVirtualTaskEndTimestamp
= Infinity
62 let chosenWorkerNodeKey
!: number
63 for (const [workerNodeKey
] of this.pool
.workerNodes
.entries()) {
64 if (this.workersVirtualTaskEndTimestamp
[workerNodeKey
] == null) {
65 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
67 const workerVirtualTaskEndTimestamp
=
68 this.workersVirtualTaskEndTimestamp
[workerNodeKey
]
69 if (workerVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
) {
70 minWorkerVirtualTaskEndTimestamp
= workerVirtualTaskEndTimestamp
71 chosenWorkerNodeKey
= workerNodeKey
74 return chosenWorkerNodeKey
78 public remove (workerNodeKey
: number): boolean {
79 this.workersVirtualTaskEndTimestamp
.splice(workerNodeKey
, 1)
84 * Computes the worker node key virtual task end timestamp.
86 * @param workerNodeKey - The worker node key.
88 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey
: number): void {
89 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] =
90 this.getWorkerVirtualTaskEndTimestamp(
92 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey
)
96 private getWorkerVirtualTaskEndTimestamp (
97 workerNodeKey
: number,
98 workerVirtualTaskStartTimestamp
: number
100 const workerVirtualTaskRunTime
= this.requiredStatistics
.medRunTime
101 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medRunTime
102 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgRunTime
103 return workerVirtualTaskStartTimestamp
+ workerVirtualTaskRunTime
106 private getWorkerVirtualTaskStartTimestamp (workerNodeKey
: number): number {
109 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] ?? -Infinity