1 import type { IWorker
} from
'../worker.js'
2 import type { IPool
} from
'../pool.js'
3 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../../utils.js'
4 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
7 TaskStatisticsRequirements
,
8 WorkerChoiceStrategyOptions
9 } from
'./selection-strategies-types.js'
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
43 private roundWeights
: number[]
47 private workerNodeId
= 0
49 * Worker node virtual task runtime.
51 private workerNodeVirtualTaskRunTime
= 0
55 pool
: IPool
<Worker
, Data
, Response
>,
56 opts
?: WorkerChoiceStrategyOptions
59 this.setTaskStatisticsRequirements(this.opts
)
60 this.roundWeights
= this.getRoundWeights()
64 public reset (): boolean {
65 this.resetWorkerNodeKeyProperties()
68 this.workerNodeVirtualTaskRunTime
= 0
73 public update (): boolean {
78 public choose (): number | undefined {
80 let roundIndex
= this.roundId
;
81 roundIndex
< this.roundWeights
.length
;
84 this.roundId
= roundIndex
86 let workerNodeKey
= this.workerNodeId
;
87 workerNodeKey
< this.pool
.workerNodes
.length
;
90 this.workerNodeId
= workerNodeKey
92 this.workerNodeId
!== this.nextWorkerNodeKey
&&
93 this.workerNodeVirtualTaskRunTime
!== 0
95 this.workerNodeVirtualTaskRunTime
= 0
97 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
98 const workerWeight
= this.opts
!.weights
![workerNodeKey
]
100 this.isWorkerNodeReady(workerNodeKey
) &&
101 workerWeight
>= this.roundWeights
[roundIndex
] &&
102 this.workerNodeVirtualTaskRunTime
< workerWeight
104 this.workerNodeVirtualTaskRunTime
=
105 this.workerNodeVirtualTaskRunTime
+
106 this.getWorkerNodeTaskRunTime(workerNodeKey
)
107 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
108 this.nextWorkerNodeKey
= workerNodeKey
109 return this.nextWorkerNodeKey
113 this.interleavedWeightedRoundRobinNextWorkerNodeId()
116 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
117 if (this.pool
.workerNodes
.length
=== 0) {
118 this.workerNodeId
= 0
120 this.roundId
=== this.roundWeights
.length
- 1 &&
121 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
124 this.workerNodeId
= 0
125 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
126 this.roundId
= this.roundId
+ 1
127 this.workerNodeId
= 0
129 this.workerNodeId
= this.workerNodeId
+ 1
134 public remove (workerNodeKey
: number): boolean {
135 if (this.pool
.workerNodes
.length
=== 0) {
136 this.resetWorkerNodeKeyProperties()
137 this.workerNodeId
= 0
138 this.workerNodeVirtualTaskRunTime
= 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
: WorkerChoiceStrategyOptions
| undefined): void {
158 super.setOptions(opts
)
159 this.roundWeights
= this.getRoundWeights()
162 private getRoundWeights (): number[] {
165 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
166 Object.values(this.opts
!.weights
!)
168 .sort((a
, b
) => a
- b
)