1 import type { IWorker
} from
'../worker'
2 import type { IPool
} from
'../pool'
3 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
} from
'../../utils'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types'
12 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
14 * @typeParam Worker - Type of worker which manages the strategy.
15 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
16 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
18 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly strategyPolicy
: StrategyPolicy
= {
27 useDynamicWorker
: true
32 * This is used to determine the current round weight.
34 private roundId
: number = 0
38 private roundWeights
: number[]
40 * Default worker weight.
42 private readonly defaultWorkerWeight
: number
46 pool
: IPool
<Worker
, Data
, Response
>,
47 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
50 this.setTaskStatisticsRequirements(this.opts
)
51 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
52 this.roundWeights
= this.getRoundWeights()
56 public reset (): boolean {
57 this.nextWorkerNodeKey
= 0
63 public update (): boolean {
68 public choose (): number {
69 let roundId
: number | undefined
70 let workerNodeId
: number | undefined
72 let roundIndex
= this.roundId
;
73 roundIndex
< this.roundWeights
.length
;
77 let workerNodeKey
= this.nextWorkerNodeKey
;
78 workerNodeKey
< this.pool
.workerNodes
.length
;
82 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
83 // if (this.isWorkerNodeReady(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex]) {
84 if (workerWeight
>= this.roundWeights
[roundIndex
]) {
86 workerNodeId
= workerNodeKey
91 this.roundId
= roundId
?? 0
92 this.nextWorkerNodeKey
= workerNodeId
?? 0
93 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
94 if (this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1) {
95 this.nextWorkerNodeKey
= 0
97 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
99 this.nextWorkerNodeKey
= this.nextWorkerNodeKey
+ 1
101 return chosenWorkerNodeKey
105 public remove (workerNodeKey
: number): boolean {
106 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
107 if (this.pool
.workerNodes
.length
=== 0) {
108 this.nextWorkerNodeKey
= 0
109 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
110 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
112 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
119 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
120 super.setOptions(opts
)
121 this.roundWeights
= this.getRoundWeights()
124 private getRoundWeights (): number[] {
125 if (this.opts
.weights
== null) {
126 return [this.defaultWorkerWeight
]
130 Object.values(this.opts
.weights
)
132 .sort((a
, b
) => a
- b
)