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'
7 WorkerChoiceStrategyOptions
8 } from
'./selection-strategies-types'
11 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
26 * This is used to determine the current round weight.
28 private roundId
: number = 0
32 private roundWeights
: number[]
34 * Default worker weight.
36 private readonly defaultWorkerWeight
: number
40 pool
: IPool
<Worker
, Data
, Response
>,
41 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
44 this.setTaskStatisticsRequirements(this.opts
)
45 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
46 this.roundWeights
= this.getRoundWeights()
50 public reset (): boolean {
51 this.resetWorkerNodeKeyProperties()
57 public update (): boolean {
62 public choose (): number | undefined {
64 let workerNodeId
: number | undefined
66 let roundIndex
= this.roundId
;
67 roundIndex
< this.roundWeights
.length
;
73 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
;
74 workerNodeKey
< this.pool
.workerNodes
.length
;
77 if (!this.isWorkerNodeEligible(workerNodeKey
)) {
81 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
82 if (workerWeight
>= this.roundWeights
[roundIndex
]) {
83 workerNodeId
= workerNodeKey
88 this.roundId
= roundId
89 if (workerNodeId
== null) {
90 this.previousWorkerNodeKey
=
91 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
93 this.nextWorkerNodeKey
= workerNodeId
94 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
95 if (this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1) {
96 this.nextWorkerNodeKey
= 0
98 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
100 this.nextWorkerNodeKey
=
101 (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
103 return chosenWorkerNodeKey
107 public remove (workerNodeKey
: number): boolean {
108 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
109 if (this.pool
.workerNodes
.length
=== 0) {
110 this.nextWorkerNodeKey
= 0
111 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
112 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
114 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
121 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
122 super.setOptions(opts
)
123 this.roundWeights
= this.getRoundWeights()
126 private getRoundWeights (): number[] {
127 if (this.opts
.weights
== null) {
128 return [this.defaultWorkerWeight
]
132 Object.values(this.opts
.weights
)
134 .sort((a
, b
) => a
- b
)