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.
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
= {
37 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
47 private roundWeights
: number[]
51 private workerNodeId
= 0
53 * Worker node virtual execution time.
55 private workerNodeVirtualTaskExecutionTime
= 0
59 pool
: IPool
<Worker
, Data
, Response
>,
60 opts
?: WorkerChoiceStrategyOptions
63 this.setTaskStatisticsRequirements(this.opts
)
64 this.roundWeights
= this.getRoundWeights()
68 public reset (): boolean {
69 this.resetWorkerNodeKeyProperties()
72 this.workerNodeVirtualTaskExecutionTime
= 0
77 public update (): boolean {
82 public choose (): number | undefined {
84 let roundIndex
= this.roundId
;
85 roundIndex
< this.roundWeights
.length
;
88 this.roundId
= roundIndex
90 let workerNodeKey
= this.workerNodeId
;
91 workerNodeKey
< this.pool
.workerNodes
.length
;
94 this.workerNodeId
= workerNodeKey
96 this.workerNodeId
!== this.nextWorkerNodeKey
&&
97 this.workerNodeVirtualTaskExecutionTime
!== 0
99 this.workerNodeVirtualTaskExecutionTime
= 0
101 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
102 const workerWeight
= this.opts
!.weights
![workerNodeKey
]
104 this.isWorkerNodeReady(workerNodeKey
) &&
105 workerWeight
>= this.roundWeights
[roundIndex
] &&
106 this.workerNodeVirtualTaskExecutionTime
< workerWeight
108 this.workerNodeVirtualTaskExecutionTime
+=
109 this.getWorkerNodeTaskWaitTime(workerNodeKey
) +
110 this.getWorkerNodeTaskRunTime(workerNodeKey
)
111 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
112 this.nextWorkerNodeKey
= workerNodeKey
113 return this.nextWorkerNodeKey
117 this.interleavedWeightedRoundRobinNextWorkerNodeId()
120 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
121 if (this.pool
.workerNodes
.length
=== 0) {
122 this.workerNodeId
= 0
124 this.roundId
=== this.roundWeights
.length
- 1 &&
125 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
128 this.workerNodeId
= 0
129 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
130 this.roundId
= this.roundId
+ 1
131 this.workerNodeId
= 0
133 this.workerNodeId
= this.workerNodeId
+ 1
138 public remove (workerNodeKey
: number): boolean {
139 if (this.pool
.workerNodes
.length
=== 0) {
140 this.resetWorkerNodeKeyProperties()
141 this.workerNodeId
= 0
142 this.workerNodeVirtualTaskExecutionTime
= 0
146 this.workerNodeId
=== workerNodeKey
&&
147 this.workerNodeId
> this.pool
.workerNodes
.length
- 1
149 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
152 this.previousWorkerNodeKey
=== workerNodeKey
&&
153 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
155 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
161 public setOptions (opts
: WorkerChoiceStrategyOptions
| undefined): void {
162 super.setOptions(opts
)
163 this.roundWeights
= this.getRoundWeights()
166 private getRoundWeights (): number[] {
169 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
170 Object.values(this.opts
!.weights
!)
172 .sort((a
, b
) => a
- b
)