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 a weighted round robin scheduling algorithm.
13 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
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 WeightedRoundRobinWorkerChoiceStrategy
<
19 Worker
extends IWorker
,
23 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
24 implements IWorkerChoiceStrategy
{
26 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
37 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
41 * Worker node virtual execution time.
43 private workerNodeVirtualTaskExecutionTime
= 0
47 pool
: IPool
<Worker
, Data
, Response
>,
48 opts
?: WorkerChoiceStrategyOptions
51 this.setTaskStatisticsRequirements(this.opts
)
55 public reset (): boolean {
56 this.resetWorkerNodeKeyProperties()
57 this.workerNodeVirtualTaskExecutionTime
= 0
62 public update (): boolean {
67 public choose (): number | undefined {
68 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
69 this.weightedRoundRobinNextWorkerNodeKey()
70 this.checkNextWorkerNodeKey()
71 return this.nextWorkerNodeKey
75 public remove (workerNodeKey
: number): boolean {
76 if (this.pool
.workerNodes
.length
=== 0) {
80 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
81 this.workerNodeVirtualTaskExecutionTime
= 0
82 if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
83 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
87 this.previousWorkerNodeKey
=== workerNodeKey
&&
88 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
90 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
95 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
97 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
98 this.opts
!.weights
![this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
]
99 if (this.workerNodeVirtualTaskExecutionTime
< workerWeight
) {
100 this.workerNodeVirtualTaskExecutionTime
+=
101 this.getWorkerNodeTaskWaitTime(
102 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
104 this.getWorkerNodeTaskRunTime(
105 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
108 this.nextWorkerNodeKey
=
109 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
111 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
112 this.workerNodeVirtualTaskExecutionTime
= 0
114 return this.nextWorkerNodeKey