2 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
3 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
5 import type { IPool
} from
'../pool'
6 import type { IWorker
, StrategyData
} from
'../worker'
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
9 type IWorkerChoiceStrategy
,
11 type TaskStatisticsRequirements
,
12 type WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
16 * Selects the next worker with a fair share scheduling algorithm.
17 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23 export class FairShareWorkerChoiceStrategy
<
24 Worker
extends IWorker
,
28 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
29 implements IWorkerChoiceStrategy
{
31 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
37 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
47 pool
: IPool
<Worker
, Data
, Response
>,
48 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
51 this.setTaskStatisticsRequirements(this.opts
)
55 public reset (): boolean {
56 for (const workerNode
of this.pool
.workerNodes
) {
57 delete workerNode
.strategyData
?.virtualTaskEndTimestamp
63 public update (workerNodeKey
: number): boolean {
64 this.pool
.workerNodes
[workerNodeKey
].strategyData
= {
65 virtualTaskEndTimestamp
:
66 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey
)
72 public choose (): number | undefined {
73 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
74 this.nextWorkerNodeKey
= this.fairShareNextWorkerNodeKey()
75 return this.nextWorkerNodeKey
79 public remove (): boolean {
83 private fairShareNextWorkerNodeKey (): number | undefined {
84 return this.pool
.workerNodes
.reduce(
85 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
86 if (workerNode
.strategyData
?.virtualTaskEndTimestamp
== null) {
87 workerNode
.strategyData
= {
88 virtualTaskEndTimestamp
:
89 this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey
)
92 return (workerNode
.strategyData
.virtualTaskEndTimestamp
as number) <
93 ((workerNodes
[minWorkerNodeKey
].strategyData
as StrategyData
)
94 .virtualTaskEndTimestamp
as number)
103 * Computes the worker node key virtual task end timestamp.
105 * @param workerNodeKey - The worker node key.
106 * @returns The worker node key virtual task end timestamp.
108 private computeWorkerNodeVirtualTaskEndTimestamp (
109 workerNodeKey
: number
111 return this.getWorkerNodeVirtualTaskEndTimestamp(
113 this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey
)
117 private getWorkerNodeVirtualTaskEndTimestamp (
118 workerNodeKey
: number,
119 workerNodeVirtualTaskStartTimestamp
: number
121 const workerNodeTaskRunTime
=
122 this.opts
.measurement
=== Measurements
.elu
123 ? this.getWorkerNodeTaskElu(workerNodeKey
)
124 : this.getWorkerNodeTaskRunTime(workerNodeKey
)
125 return workerNodeVirtualTaskStartTimestamp
+ workerNodeTaskRunTime
128 private getWorkerNodeVirtualTaskStartTimestamp (
129 workerNodeKey
: number
131 const now
= performance
.now()
133 (this.pool
.workerNodes
[workerNodeKey
]?.strategyData
134 ?.virtualTaskEndTimestamp
?? -Infinity)
135 ? (this.pool
.workerNodes
[workerNodeKey
]?.strategyData
136 ?.virtualTaskEndTimestamp
as number)