1 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../../utils'
2 import type { IPool
} from
'../pool'
3 import type { IWorker
, StrategyData
} from
'../worker'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
6 type IWorkerChoiceStrategy
,
7 type InternalWorkerChoiceStrategyOptions
,
9 type TaskStatisticsRequirements
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 structured-cloneable data.
18 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
20 export class FairShareWorkerChoiceStrategy
<
21 Worker
extends IWorker
,
25 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
26 implements IWorkerChoiceStrategy
{
28 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
34 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
44 pool
: IPool
<Worker
, Data
, Response
>,
45 opts
: InternalWorkerChoiceStrategyOptions
48 this.setTaskStatisticsRequirements(this.opts
)
52 public reset (): boolean {
53 for (const workerNode
of this.pool
.workerNodes
) {
54 delete workerNode
.strategyData
?.virtualTaskEndTimestamp
60 public update (workerNodeKey
: number): boolean {
61 this.pool
.workerNodes
[workerNodeKey
].strategyData
= {
62 virtualTaskEndTimestamp
:
63 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey
)
69 public choose (): number | undefined {
70 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
71 this.nextWorkerNodeKey
= this.fairShareNextWorkerNodeKey()
72 return this.nextWorkerNodeKey
76 public remove (): boolean {
80 private fairShareNextWorkerNodeKey (): number | undefined {
81 return this.pool
.workerNodes
.reduce(
82 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
83 if (workerNode
.strategyData
?.virtualTaskEndTimestamp
== null) {
84 workerNode
.strategyData
= {
85 virtualTaskEndTimestamp
:
86 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey
)
89 return this.isWorkerNodeReady(workerNodeKey
) &&
90 (workerNode
.strategyData
.virtualTaskEndTimestamp
as number) <
91 ((workerNodes
[minWorkerNodeKey
].strategyData
as StrategyData
)
92 .virtualTaskEndTimestamp
as number)
101 * Computes the worker node key virtual task end timestamp.
103 * @param workerNodeKey - The worker node key.
104 * @returns The worker node key virtual task end timestamp.
106 private computeWorkerNodeVirtualTaskEndTimestamp (
107 workerNodeKey
: number
109 return this.getWorkerNodeVirtualTaskEndTimestamp(
111 this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey
)
115 private getWorkerNodeVirtualTaskEndTimestamp (
116 workerNodeKey
: number,
117 workerNodeVirtualTaskStartTimestamp
: number
119 const workerNodeTaskRunTime
=
120 this.opts
.measurement
=== Measurements
.elu
121 ? this.getWorkerNodeTaskElu(workerNodeKey
)
122 : this.getWorkerNodeTaskRunTime(workerNodeKey
)
123 return workerNodeVirtualTaskStartTimestamp
+ workerNodeTaskRunTime
126 private getWorkerNodeVirtualTaskStartTimestamp (
127 workerNodeKey
: number
129 const virtualTaskEndTimestamp
=
130 this.pool
.workerNodes
[workerNodeKey
]?.strategyData
131 ?.virtualTaskEndTimestamp
132 const now
= performance
.now()
133 return now
< (virtualTaskEndTimestamp
?? -Infinity)
134 ? (virtualTaskEndTimestamp
as number)