1 import type { IWorker
} from
'../worker'
2 import type { IPool
} from
'../pool'
4 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
5 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
10 TaskStatisticsRequirements
,
11 WorkerChoiceStrategyOptions
12 } from
'./selection-strategies-types'
15 * Selects the next worker with an interleaved weighted round robin scheduling algorithm.
17 * @typeParam Worker - Type of worker which manages the strategy.
18 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
19 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
21 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
22 Worker
extends IWorker
,
26 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
27 implements IWorkerChoiceStrategy
{
29 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
35 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
36 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
42 private roundId
: number = 0
44 * Default worker weight.
46 private readonly defaultWorkerWeight
: number
50 private roundWeights
: number[]
54 private workerNodeId
: number = 0
56 * Worker virtual task runtime.
58 private workerVirtualTaskRunTime
: number = 0
62 pool
: IPool
<Worker
, Data
, Response
>,
63 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
66 this.setTaskStatisticsRequirements(this.opts
)
67 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
68 this.roundWeights
= this.getRoundWeights()
72 public reset (): boolean {
73 this.resetWorkerNodeKeyProperties()
76 this.workerVirtualTaskRunTime
= 0
81 public update (): boolean {
86 public choose (): number | undefined {
88 let roundIndex
= this.roundId
;
89 roundIndex
< this.roundWeights
.length
;
92 this.roundId
= roundIndex
94 let workerNodeKey
= this.workerNodeId
;
95 workerNodeKey
< this.pool
.workerNodes
.length
;
98 this.workerNodeId
= workerNodeKey
100 this.workerNodeId
!== this.nextWorkerNodeKey
&&
101 this.workerVirtualTaskRunTime
!== 0
103 this.workerVirtualTaskRunTime
= 0
106 this.opts
.weights
?.[workerNodeKey
] ?? this.defaultWorkerWeight
108 workerWeight
>= this.roundWeights
[roundIndex
] &&
109 this.workerVirtualTaskRunTime
< workerWeight
111 this.workerVirtualTaskRunTime
=
112 this.workerVirtualTaskRunTime
+
113 this.getWorkerTaskRunTime(workerNodeKey
)
114 this.previousWorkerNodeKey
=
115 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
116 this.nextWorkerNodeKey
= workerNodeKey
117 return this.nextWorkerNodeKey
121 this.interleavedWeightedRoundRobinNextWorkerNodeId()
124 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
126 this.roundId
=== this.roundWeights
.length
- 1 &&
127 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
130 this.workerNodeId
= 0
131 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
132 this.roundId
= this.roundId
+ 1
133 this.workerNodeId
= 0
135 this.workerNodeId
= this.workerNodeId
+ 1
140 public remove (workerNodeKey
: number): boolean {
141 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
142 if (this.pool
.workerNodes
.length
=== 0) {
144 this.workerNodeId
= 0
145 this.nextWorkerNodeKey
= 0
146 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
147 if (this.workerNodeId
=== this.nextWorkerNodeKey
) {
148 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
150 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
152 this.workerVirtualTaskRunTime
= 0
158 public setOptions (opts
: WorkerChoiceStrategyOptions
): void {
159 super.setOptions(opts
)
160 this.roundWeights
= this.getRoundWeights()
163 private getRoundWeights (): number[] {
164 if (this.opts
.weights
== null) {
165 return [this.defaultWorkerWeight
]
169 Object.values(this.opts
.weights
)
171 .sort((a
, b
) => a
- b
)