1 import type { IPool
} from
'../pool.js'
2 import type { IWorker
} from
'../worker.js'
5 TaskStatisticsRequirements
,
6 WorkerChoiceStrategyOptions
,
7 } from
'./selection-strategies-types.js'
9 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
} from
'../utils.js'
10 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy.js'
13 * Selects the next worker with a weighted round robin scheduling algorithm.
14 * 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 override
readonly taskStatisticsRequirements
: TaskStatisticsRequirements
=
29 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
43 * Worker node virtual execution time.
45 private workerNodeVirtualTaskExecutionTime
= 0
49 pool
: IPool
<Worker
, Data
, Response
>,
50 opts
?: WorkerChoiceStrategyOptions
53 this.setTaskStatisticsRequirements(this.opts
)
57 public choose (): number | undefined {
58 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey
)
59 this.weightedRoundRobinNextWorkerNodeKey()
60 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61 if (!this.isWorkerNodeReady(this.nextWorkerNodeKey
!)) {
64 return this.checkWorkerNodeKey(this.nextWorkerNodeKey
)
68 public remove (workerNodeKey
: number): boolean {
69 if (this.pool
.workerNodes
.length
=== 0) {
72 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
73 this.workerNodeVirtualTaskExecutionTime
= 0
76 this.nextWorkerNodeKey
!= null &&
77 this.nextWorkerNodeKey
>= workerNodeKey
79 this.nextWorkerNodeKey
=
80 (this.nextWorkerNodeKey
- 1 + this.pool
.workerNodes
.length
) %
81 this.pool
.workerNodes
.length
82 if (this.previousWorkerNodeKey
>= workerNodeKey
) {
83 this.previousWorkerNodeKey
= this.nextWorkerNodeKey
90 public reset (): boolean {
91 this.resetWorkerNodeKeyProperties()
92 this.workerNodeVirtualTaskExecutionTime
= 0
97 public update (): boolean {
101 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
102 const workerNodeKey
= this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
103 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
104 const workerWeight
= this.opts
!.weights
![workerNodeKey
]
105 if (this.workerNodeVirtualTaskExecutionTime
< workerWeight
) {
106 this.workerNodeVirtualTaskExecutionTime
+=
107 this.getWorkerNodeTaskWaitTime(workerNodeKey
) +
108 this.getWorkerNodeTaskRunTime(workerNodeKey
)
110 this.nextWorkerNodeKey
=
111 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
114 this.workerNodeVirtualTaskExecutionTime
= 0
116 return this.nextWorkerNodeKey