1 import type { IWorker
} from
'../worker'
2 import type { IPool
} from
'../pool'
3 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../../utils'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
7 InternalWorkerChoiceStrategyOptions
,
8 TaskStatisticsRequirements
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 taskStatisticsRequirements
: TaskStatisticsRequirements
= {
32 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
33 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
39 private roundId
: number = 0
41 * Default worker weight.
43 private readonly defaultWorkerWeight
: number
47 private roundWeights
: number[]
51 private workerNodeId
: number = 0
53 * Worker node virtual task runtime.
55 private workerNodeVirtualTaskRunTime
: number = 0
59 pool
: IPool
<Worker
, Data
, Response
>,
60 opts
: InternalWorkerChoiceStrategyOptions
63 this.setTaskStatisticsRequirements(this.opts
)
64 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
65 this.roundWeights
= this.getRoundWeights()
69 public reset (): boolean {
70 this.resetWorkerNodeKeyProperties()
73 this.workerNodeVirtualTaskRunTime
= 0
78 public update (): boolean {
83 public choose (): number | undefined {
85 let roundIndex
= this.roundId
;
86 roundIndex
< this.roundWeights
.length
;
89 this.roundId
= roundIndex
91 let workerNodeKey
= this.workerNodeId
;
92 workerNodeKey
< this.pool
.workerNodes
.length
;
95 this.workerNodeId
= workerNodeKey
97 this.workerNodeId
!== this.nextWorkerNodeKey
&&
98 this.workerNodeVirtualTaskRunTime
!== 0
100 this.workerNodeVirtualTaskRunTime
= 0
103 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
105 this.isWorkerNodeReady(workerNodeKey
) &&
106 workerWeight
>= this.roundWeights
[roundIndex
] &&
107 this.workerNodeVirtualTaskRunTime
< workerWeight
109 this.workerNodeVirtualTaskRunTime
=
110 this.workerNodeVirtualTaskRunTime
+
111 this.getWorkerNodeTaskRunTime(workerNodeKey
)
112 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
113 this.nextWorkerNodeKey
= workerNodeKey
114 return this.nextWorkerNodeKey
118 this.interleavedWeightedRoundRobinNextWorkerNodeId()
121 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
123 this.roundId
=== this.roundWeights
.length
- 1 &&
124 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
127 this.workerNodeId
= 0
128 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
129 this.roundId
= this.roundId
+ 1
130 this.workerNodeId
= 0
132 this.workerNodeId
= this.workerNodeId
+ 1
137 public remove (workerNodeKey
: number): boolean {
138 if (this.pool
.workerNodes
.length
=== 0) {
142 this.workerNodeId
=== workerNodeKey
&&
143 this.workerNodeId
> this.pool
.workerNodes
.length
- 1
145 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
148 this.previousWorkerNodeKey
=== workerNodeKey
&&
149 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
151 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
157 public setOptions (opts
: InternalWorkerChoiceStrategyOptions
): void {
158 super.setOptions(opts
)
159 this.roundWeights
= this.getRoundWeights()
162 private getRoundWeights (): number[] {
163 if (this.opts
.weights
== null) {
164 return [this.defaultWorkerWeight
]
168 Object.values(this.opts
.weights
)
170 .sort((a
, b
) => a
- b
)