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
43 private roundWeights
: number[]
47 private workerNodeId
: number = 0
49 * Worker node virtual task runtime.
51 private workerNodeVirtualTaskRunTime
: number = 0
55 pool
: IPool
<Worker
, Data
, Response
>,
56 opts
: InternalWorkerChoiceStrategyOptions
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 const workerWeight
= this.opts
.weights
?.[workerNodeKey
] as number
99 this.isWorkerNodeReady(workerNodeKey
) &&
100 workerWeight
>= this.roundWeights
[roundIndex
] &&
101 this.workerNodeVirtualTaskRunTime
< workerWeight
103 this.workerNodeVirtualTaskRunTime
=
104 this.workerNodeVirtualTaskRunTime
+
105 this.getWorkerNodeTaskRunTime(workerNodeKey
)
106 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
107 this.nextWorkerNodeKey
= workerNodeKey
108 return this.nextWorkerNodeKey
112 this.interleavedWeightedRoundRobinNextWorkerNodeId()
115 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
117 this.roundId
=== this.roundWeights
.length
- 1 &&
118 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
121 this.workerNodeId
= 0
122 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
123 this.roundId
= this.roundId
+ 1
124 this.workerNodeId
= 0
126 this.workerNodeId
= this.workerNodeId
+ 1
131 public remove (workerNodeKey
: number): boolean {
132 if (this.pool
.workerNodes
.length
=== 0) {
136 this.workerNodeId
=== workerNodeKey
&&
137 this.workerNodeId
> this.pool
.workerNodes
.length
- 1
139 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
142 this.previousWorkerNodeKey
=== workerNodeKey
&&
143 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
145 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
151 public setOptions (opts
: InternalWorkerChoiceStrategyOptions
): void {
152 super.setOptions(opts
)
153 this.roundWeights
= this.getRoundWeights()
156 private getRoundWeights (): number[] {
159 Object.values(this.opts
.weights
as Record
<number, number>)
161 .sort((a
, b
) => a
- b
)