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.setOptions(this.opts
)
63 public reset (): boolean {
64 this.resetWorkerNodeKeyProperties()
67 this.workerNodeVirtualTaskRunTime
= 0
72 public update (): boolean {
77 public choose (): number | undefined {
79 let roundIndex
= this.roundId
;
80 roundIndex
< this.roundWeights
.length
;
83 this.roundId
= roundIndex
85 let workerNodeKey
= this.workerNodeId
;
86 workerNodeKey
< this.pool
.workerNodes
.length
;
89 this.workerNodeId
= workerNodeKey
91 this.workerNodeId
!== this.nextWorkerNodeKey
&&
92 this.workerNodeVirtualTaskRunTime
!== 0
94 this.workerNodeVirtualTaskRunTime
= 0
96 const workerWeight
= this.opts
.weights
?.[workerNodeKey
] as number
98 this.isWorkerNodeReady(workerNodeKey
) &&
99 workerWeight
>= this.roundWeights
[roundIndex
] &&
100 this.workerNodeVirtualTaskRunTime
< workerWeight
102 this.workerNodeVirtualTaskRunTime
=
103 this.workerNodeVirtualTaskRunTime
+
104 this.getWorkerNodeTaskRunTime(workerNodeKey
)
105 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
106 this.nextWorkerNodeKey
= workerNodeKey
107 return this.nextWorkerNodeKey
111 this.interleavedWeightedRoundRobinNextWorkerNodeId()
114 private interleavedWeightedRoundRobinNextWorkerNodeId (): void {
116 this.roundId
=== this.roundWeights
.length
- 1 &&
117 this.workerNodeId
=== this.pool
.workerNodes
.length
- 1
120 this.workerNodeId
= 0
121 } else if (this.workerNodeId
=== this.pool
.workerNodes
.length
- 1) {
122 this.roundId
= this.roundId
+ 1
123 this.workerNodeId
= 0
125 this.workerNodeId
= this.workerNodeId
+ 1
130 public remove (workerNodeKey
: number): boolean {
131 if (this.pool
.workerNodes
.length
=== 0) {
135 this.workerNodeId
=== workerNodeKey
&&
136 this.workerNodeId
> this.pool
.workerNodes
.length
- 1
138 this.workerNodeId
= this.pool
.workerNodes
.length
- 1
141 this.previousWorkerNodeKey
=== workerNodeKey
&&
142 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
144 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
150 public setOptions (opts
: InternalWorkerChoiceStrategyOptions
): void {
151 super.setOptions(opts
)
152 this.roundWeights
= this.getRoundWeights()
155 private getRoundWeights (): number[] {
158 Object.values(this.opts
.weights
as Record
<number, number>)
160 .sort((a
, b
) => a
- b
)