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 serializable data.
15 * @typeParam Response - Type of execution response. This can only be serializable data.
17 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
25 * Worker node id where the current task will be submitted.
27 private currentWorkerNodeId
: number = 0
30 * This is used to determine the current round weight.
32 private currentRoundId
: number = 0
36 private roundWeights
: number[]
38 * Default worker weight.
40 private readonly defaultWorkerWeight
: number
44 pool
: IPool
<Worker
, Data
, Response
>,
45 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
48 this.setTaskStatisticsRequirements(this.opts
)
49 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
50 this.roundWeights
= this.getRoundWeights()
54 public reset (): boolean {
55 this.currentWorkerNodeId
= 0
56 this.currentRoundId
= 0
61 public update (): boolean {
66 public choose (): number {
67 let roundId
: number | undefined
68 let workerNodeId
: number | undefined
70 let roundIndex
= this.currentRoundId
;
71 roundIndex
< this.roundWeights
.length
;
75 let workerNodeKey
= this.currentWorkerNodeId
;
76 workerNodeKey
< this.pool
.workerNodes
.length
;
80 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
81 if (workerWeight
>= this.roundWeights
[roundIndex
]) {
83 workerNodeId
= workerNodeKey
88 this.currentRoundId
= roundId
?? 0
89 this.currentWorkerNodeId
= workerNodeId
?? 0
90 const chosenWorkerNodeKey
= this.currentWorkerNodeId
91 if (this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1) {
92 this.currentWorkerNodeId
= 0
94 this.currentRoundId
=== this.roundWeights
.length
- 1
96 : this.currentRoundId
+ 1
98 this.currentWorkerNodeId
= this.currentWorkerNodeId
+ 1
100 return chosenWorkerNodeKey
104 public remove (workerNodeKey
: number): boolean {
105 if (this.currentWorkerNodeId
=== workerNodeKey
) {
106 if (this.pool
.workerNodes
.length
=== 0) {
107 this.currentWorkerNodeId
= 0
108 } else if (this.currentWorkerNodeId
> this.pool
.workerNodes
.length
- 1) {
109 this.currentWorkerNodeId
= this.pool
.workerNodes
.length
- 1
110 this.currentRoundId
=
111 this.currentRoundId
=== this.roundWeights
.length
- 1
113 : this.currentRoundId
+ 1
120 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
121 super.setOptions(opts
)
122 this.roundWeights
= this.getRoundWeights()
125 private getRoundWeights (): number[] {
126 if (this.opts
.weights
== null) {
127 return [this.defaultWorkerWeight
]
131 Object.values(this.opts
.weights
)
133 .sort((a
, b
) => a
- b
)