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 * Worker virtual task timestamp.
14 interface WorkerVirtualTaskTimestamp
{
20 * Selects the next worker with a fair share scheduling algorithm.
21 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
23 * @typeParam Worker - Type of worker which manages the strategy.
24 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
25 * @typeParam Response - Type of execution response. This can only be serializable data.
27 export class FairShareWorkerChoiceStrategy
<
28 Worker
extends IWorker
,
32 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
33 implements IWorkerChoiceStrategy
{
35 public readonly requiredStatistics
: RequiredStatistics
= {
42 * Worker last virtual task execution timestamp.
44 private readonly workerLastVirtualTaskTimestamp
: Map
<
46 WorkerVirtualTaskTimestamp
47 > = new Map
<number, WorkerVirtualTaskTimestamp
>()
51 pool
: IPool
<Worker
, Data
, Response
>,
52 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
55 this.checkOptions(this.opts
)
59 public reset (): boolean {
60 this.workerLastVirtualTaskTimestamp
.clear()
65 public choose (): number {
66 let minWorkerVirtualTaskEndTimestamp
= Infinity
67 let chosenWorkerNodeKey
!: number
68 for (const [index
] of this.pool
.workerNodes
.entries()) {
69 this.computeWorkerLastVirtualTaskTimestamp(index
)
70 const workerLastVirtualTaskEndTimestamp
=
71 this.workerLastVirtualTaskTimestamp
.get(index
)?.end
?? 0
73 workerLastVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
75 minWorkerVirtualTaskEndTimestamp
= workerLastVirtualTaskEndTimestamp
76 chosenWorkerNodeKey
= index
79 return chosenWorkerNodeKey
83 public remove (workerNodeKey
: number): boolean {
84 const deleted
= this.workerLastVirtualTaskTimestamp
.delete(workerNodeKey
)
85 for (const [key
, value
] of this.workerLastVirtualTaskTimestamp
.entries()) {
86 if (key
> workerNodeKey
) {
87 this.workerLastVirtualTaskTimestamp
.set(key
- 1, value
)
94 * Computes worker last virtual task timestamp.
96 * @param workerNodeKey - The worker node key.
98 private computeWorkerLastVirtualTaskTimestamp (workerNodeKey
: number): void {
99 const workerVirtualTaskStartTimestamp
= Math.max(
101 this.workerLastVirtualTaskTimestamp
.get(workerNodeKey
)?.end
?? -Infinity
103 const workerVirtualTaskTRunTime
= this.requiredStatistics
.medRunTime
104 ? this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.medRunTime
105 : this.pool
.workerNodes
[workerNodeKey
].tasksUsage
.avgRunTime
106 this.workerLastVirtualTaskTimestamp
.set(workerNodeKey
, {
107 start
: workerVirtualTaskStartTimestamp
,
108 end
: workerVirtualTaskStartTimestamp
+ (workerVirtualTaskTRunTime
?? 0)