1 import type { IPool
} from
'../pool.js'
2 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
3 import type { IWorker
} from
'../worker.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.
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
17 export class InterleavedWeightedRoundRobinWorkerChoiceStrategy
<
18 Worker
extends IWorker
,
22 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
23 implements IWorkerChoiceStrategy
{
25 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
36 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
46 private roundWeights
: number[]
50 private workerNodeId
= 0
52 * Worker node virtual execution time.
54 private workerNodeVirtualTaskExecutionTime
= 0
58 pool
: IPool
<Worker
, Data
, Response
>,
59 opts
?: WorkerChoiceStrategyOptions
62 this.setTaskStatisticsRequirements(this.opts
)
63 this.roundWeights
= this.getRoundWeights()
67 public reset (): boolean {
68 this.resetWorkerNodeKeyProperties()
71 this.workerNodeVirtualTaskExecutionTime
= 0
76 public update (): boolean {
81 public choose (): number | undefined {
83 let roundIndex
= this.roundId
;
84 roundIndex
< this.roundWeights
.length
;
87 this.roundId
= roundIndex
89 let workerNodeKey
= this.workerNodeId
;
90 workerNodeKey
< this.pool
.workerNodes
.length
;
93 this.workerNodeId
= workerNodeKey
95 this.workerNodeId
!== this.nextWorkerNodeKey
&&
96 this.workerNodeVirtualTaskExecutionTime
!== 0
98 this.workerNodeVirtualTaskExecutionTime
= 0
100 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
101 const workerWeight
= this.opts
!.weights
![workerNodeKey
]
103 this.isWorkerNodeReady(workerNodeKey
) &&
104 workerWeight
>= this.roundWeights
[roundIndex
] &&
105 this.workerNodeVirtualTaskExecutionTime
< workerWeight
107 this.workerNodeVirtualTaskExecutionTime
+=
108 this.getWorkerNodeTaskWaitTime(workerNodeKey
) +
109 this.getWorkerNodeTaskRunTime(workerNodeKey
)
110 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
111 this.nextWorkerNodeKey
= workerNodeKey
112 return this.nextWorkerNodeKey
116 this.interleavedWeightedRoundRobinNextWorkerNodeId()
119 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
120 if (this.pool
.workerNodes
.length
=== 0) {
121 this.workerNodeId
= 0
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) {
139 this.resetWorkerNodeKeyProperties()
140 this.workerNodeId
= 0
141 this.workerNodeVirtualTaskExecutionTime
= 0
145 this.workerNodeId
=== workerNodeKey
&&
146 this.workerNodeId
> this.pool
.workerNodes
.length
- 1
148 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
151 this.previousWorkerNodeKey
=== workerNodeKey
&&
152 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
154 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
160 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
161 super.setOptions(opts
)
162 this.roundWeights
= this.getRoundWeights()
165 private getRoundWeights (): number[] {
168 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
169 Object.values(this.opts
!.weights
!)
171 .sort((a
, b
) => a
- b
)