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 dynamicWorkerUsage
: false,
28 dynamicWorkerReady
: true
33 * This is used to determine the current round weight.
35 private roundId
: number = 0
39 private roundWeights
: number[]
41 * Default worker weight.
43 private readonly defaultWorkerWeight
: number
47 pool
: IPool
<Worker
, Data
, Response
>,
48 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
51 this.setTaskStatisticsRequirements(this.opts
)
52 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
53 this.roundWeights
= this.getRoundWeights()
57 public reset (): boolean {
58 this.nextWorkerNodeKey
= 0
64 public update (): boolean {
69 public choose (): number | undefined {
70 let roundId
: number | undefined
71 let workerNodeId
: number | undefined
73 let roundIndex
= this.roundId
;
74 roundIndex
< this.roundWeights
.length
;
78 let workerNodeKey
= this.nextWorkerNodeKey
?? 0;
79 workerNodeKey
< this.pool
.workerNodes
.length
;
83 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
85 this.isWorkerNodeEligible(workerNodeKey
) &&
86 workerWeight
>= this.roundWeights
[roundIndex
]
89 workerNodeId
= workerNodeKey
94 this.roundId
= roundId
as number
95 this.nextWorkerNodeKey
= workerNodeId
96 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
97 if (this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1) {
98 this.nextWorkerNodeKey
= 0
100 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
102 this.nextWorkerNodeKey
= (this.nextWorkerNodeKey
?? 0) + 1
104 return chosenWorkerNodeKey
108 public remove (workerNodeKey
: number): boolean {
109 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
110 if (this.pool
.workerNodes
.length
=== 0) {
111 this.nextWorkerNodeKey
= 0
112 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
113 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
115 this.roundId
=== this.roundWeights
.length
- 1 ? 0 : this.roundId
+ 1
122 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
123 super.setOptions(opts
)
124 this.roundWeights
= this.getRoundWeights()
127 private getRoundWeights (): number[] {
128 if (this.opts
.weights
== null) {
129 return [this.defaultWorkerWeight
]
133 Object.values(this.opts
.weights
)
135 .sort((a
, b
) => a
- b
)