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 serializable data.
16 * @typeParam Response - Type of execution response. This can only be serializable 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
31 * Worker node id where the current task will be submitted.
33 private currentWorkerNodeId
: number = 0
36 * This is used to determine the current round weight.
38 private currentRoundId
: number = 0
42 private roundWeights
: number[]
44 * Default worker weight.
46 private readonly defaultWorkerWeight
: number
50 pool
: IPool
<Worker
, Data
, Response
>,
51 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
54 this.setTaskStatisticsRequirements(this.opts
)
55 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
56 this.roundWeights
= this.getRoundWeights()
60 public reset (): boolean {
61 this.currentWorkerNodeId
= 0
62 this.currentRoundId
= 0
67 public update (): boolean {
72 public choose (): number {
73 let roundId
: number | undefined
74 let workerNodeId
: number | undefined
76 let roundIndex
= this.currentRoundId
;
77 roundIndex
< this.roundWeights
.length
;
81 let workerNodeKey
= this.currentWorkerNodeId
;
82 workerNodeKey
< this.pool
.workerNodes
.length
;
86 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
87 if (workerWeight
>= this.roundWeights
[roundIndex
]) {
89 workerNodeId
= workerNodeKey
94 this.currentRoundId
= roundId
?? 0
95 this.currentWorkerNodeId
= workerNodeId
?? 0
96 const chosenWorkerNodeKey
= this.currentWorkerNodeId
97 if (this.currentWorkerNodeId
=== this.pool
.workerNodes
.length
- 1) {
98 this.currentWorkerNodeId
= 0
100 this.currentRoundId
=== this.roundWeights
.length
- 1
102 : this.currentRoundId
+ 1
104 this.currentWorkerNodeId
= this.currentWorkerNodeId
+ 1
106 return chosenWorkerNodeKey
110 public remove (workerNodeKey
: number): boolean {
111 if (this.currentWorkerNodeId
=== workerNodeKey
) {
112 if (this.pool
.workerNodes
.length
=== 0) {
113 this.currentWorkerNodeId
= 0
114 } else if (this.currentWorkerNodeId
> this.pool
.workerNodes
.length
- 1) {
115 this.currentWorkerNodeId
= this.pool
.workerNodes
.length
- 1
116 this.currentRoundId
=
117 this.currentRoundId
=== this.roundWeights
.length
- 1
119 : this.currentRoundId
+ 1
126 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
127 super.setOptions(opts
)
128 this.roundWeights
= this.getRoundWeights()
131 private getRoundWeights (): number[] {
132 if (this.opts
.weights
== null) {
133 return [this.defaultWorkerWeight
]
137 Object.values(this.opts
.weights
)
139 .sort((a
, b
) => a
- b
)