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.
15 * @typeParam Worker - Type of worker which manages the strategy.
16 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
17 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
19 export class WeightedRoundRobinWorkerChoiceStrategy
<
20 Worker
extends IWorker
,
24 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
25 implements IWorkerChoiceStrategy
{
27 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
38 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
42 * Worker node virtual execution time.
44 private workerNodeVirtualTaskExecutionTime
= 0
48 pool
: IPool
<Worker
, Data
, Response
>,
49 opts
?: WorkerChoiceStrategyOptions
52 this.setTaskStatisticsRequirements(this.opts
)
56 public reset (): boolean {
57 this.resetWorkerNodeKeyProperties()
58 this.workerNodeVirtualTaskExecutionTime
= 0
63 public update (): boolean {
68 public choose (): number | undefined {
69 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
70 this.weightedRoundRobinNextWorkerNodeKey()
71 this.checkNextWorkerNodeKey()
72 return this.nextWorkerNodeKey
76 public remove (workerNodeKey
: number): boolean {
77 if (this.pool
.workerNodes
.length
=== 0) {
81 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
82 this.workerNodeVirtualTaskExecutionTime
= 0
83 if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
84 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
88 this.previousWorkerNodeKey
=== workerNodeKey
&&
89 this.previousWorkerNodeKey
> this.pool
.workerNodes
.length
- 1
91 this.previousWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
96 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
98 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
99 this.opts
!.weights
![this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
]
100 if (this.workerNodeVirtualTaskExecutionTime
< workerWeight
) {
101 this.workerNodeVirtualTaskExecutionTime
+=
102 this.getWorkerNodeTaskWaitTime(
103 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
105 this.getWorkerNodeTaskRunTime(
106 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
109 this.nextWorkerNodeKey
=
110 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
112 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
113 this.workerNodeVirtualTaskExecutionTime
= 0
115 return this.nextWorkerNodeKey