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 for (const [workerNodeKey
] of this.pool
.workerNodes
.entries()) {
76 if (this.workersVirtualTaskEndTimestamp
[workerNodeKey
] == null) {
77 this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey
)
79 const workerVirtualTaskEndTimestamp
=
80 this.workersVirtualTaskEndTimestamp
[workerNodeKey
]
81 if (workerVirtualTaskEndTimestamp
< minWorkerVirtualTaskEndTimestamp
) {
82 minWorkerVirtualTaskEndTimestamp
= workerVirtualTaskEndTimestamp
83 this.nextWorkerNodeId
= workerNodeKey
86 return this.nextWorkerNodeId
90 public remove (workerNodeKey
: number): boolean {
91 this.workersVirtualTaskEndTimestamp
.splice(workerNodeKey
, 1)
96 * Computes the worker node key virtual task end timestamp.
98 * @param workerNodeKey - The worker node key.
100 private computeWorkerVirtualTaskEndTimestamp (workerNodeKey
: number): void {
101 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] =
102 this.getWorkerVirtualTaskEndTimestamp(
104 this.getWorkerVirtualTaskStartTimestamp(workerNodeKey
)
108 private getWorkerVirtualTaskEndTimestamp (
109 workerNodeKey
: number,
110 workerVirtualTaskStartTimestamp
: number
112 const workerTaskRunTime
=
113 this.opts
.measurement
=== Measurements
.elu
114 ? this.getWorkerTaskElu(workerNodeKey
)
115 : this.getWorkerTaskRunTime(workerNodeKey
)
116 return workerVirtualTaskStartTimestamp
+ workerTaskRunTime
119 private getWorkerVirtualTaskStartTimestamp (workerNodeKey
: number): number {
122 this.workersVirtualTaskEndTimestamp
[workerNodeKey
] ?? -Infinity