1 import type { IWorker
} from
'../worker'
2 import type { IPool
} from
'../pool'
4 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
5 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
11 TaskStatisticsRequirements
,
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
16 * Selects the next worker with a weighted round robin scheduling algorithm.
17 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
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 WeightedRoundRobinWorkerChoiceStrategy
<
24 Worker
extends IWorker
,
28 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
29 implements IWorkerChoiceStrategy
{
31 public readonly strategyPolicy
: StrategyPolicy
= {
32 dynamicWorkerUsage
: false,
33 dynamicWorkerReady
: true
37 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
43 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
44 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
48 * Default worker weight.
50 private readonly defaultWorkerWeight
: number
52 * Worker virtual task runtime.
54 private workerVirtualTaskRunTime
: number = 0
58 pool
: IPool
<Worker
, Data
, Response
>,
59 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
62 this.setTaskStatisticsRequirements(this.opts
)
63 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
67 public reset (): boolean {
68 this.nextWorkerNodeKey
= 0
69 this.workerVirtualTaskRunTime
= 0
74 public update (): boolean {
79 public choose (): number | undefined {
80 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
81 this.weightedRoundRobinNextWorkerNodeKey()
82 if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey
as number)) {
83 this.nextWorkerNodeKey
= undefined
84 this.previousWorkerNodeKey
=
85 chosenWorkerNodeKey
?? this.previousWorkerNodeKey
87 return chosenWorkerNodeKey
91 public remove (workerNodeKey
: number): boolean {
92 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
93 if (this.pool
.workerNodes
.length
=== 0) {
94 this.nextWorkerNodeKey
= 0
95 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
96 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
98 this.workerVirtualTaskRunTime
= 0
103 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
104 const workerVirtualTaskRunTime
= this.workerVirtualTaskRunTime
107 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
108 ] ?? this.defaultWorkerWeight
109 if (workerVirtualTaskRunTime
< workerWeight
) {
110 this.workerVirtualTaskRunTime
=
111 workerVirtualTaskRunTime
+
112 this.getWorkerTaskRunTime(
113 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
116 this.nextWorkerNodeKey
=
117 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
119 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
120 this.workerVirtualTaskRunTime
= 0
122 return this.nextWorkerNodeKey