1 import type { IWorker
} from
'../worker'
2 import type { IPool
} from
'../pool'
4 DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
5 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
7 import { AbstractWorkerChoiceStrategy
} from
'./abstract-worker-choice-strategy'
10 TaskStatisticsRequirements
,
11 WorkerChoiceStrategyOptions
12 } from
'./selection-strategies-types'
15 * Selects the next worker with a weighted round robin scheduling algorithm.
16 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
18 * @typeParam Worker - Type of worker which manages the strategy.
19 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
20 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
22 export class WeightedRoundRobinWorkerChoiceStrategy
<
23 Worker
extends IWorker
,
27 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
28 implements IWorkerChoiceStrategy
{
30 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
36 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
37 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
41 * Default worker weight.
43 private readonly defaultWorkerWeight
: number
45 * Worker virtual task runtime.
47 private workerVirtualTaskRunTime
: number = 0
51 pool
: IPool
<Worker
, Data
, Response
>,
52 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
55 this.setTaskStatisticsRequirements(this.opts
)
56 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
60 public reset (): boolean {
61 this.nextWorkerNodeKey
= 0
62 this.workerVirtualTaskRunTime
= 0
67 public update (): boolean {
72 public choose (): number | undefined {
73 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
74 this.weightedRoundRobinNextWorkerNodeKey()
75 if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey
as number)) {
76 this.nextWorkerNodeKey
= undefined
77 this.previousWorkerNodeKey
=
78 chosenWorkerNodeKey
?? this.previousWorkerNodeKey
80 return chosenWorkerNodeKey
84 public remove (workerNodeKey
: number): boolean {
85 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
86 if (this.pool
.workerNodes
.length
=== 0) {
87 this.nextWorkerNodeKey
= 0
88 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
89 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
91 this.workerVirtualTaskRunTime
= 0
96 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
97 const workerVirtualTaskRunTime
= this.workerVirtualTaskRunTime
100 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
101 ] ?? this.defaultWorkerWeight
102 if (workerVirtualTaskRunTime
< workerWeight
) {
103 this.workerVirtualTaskRunTime
=
104 workerVirtualTaskRunTime
+
105 this.getWorkerTaskRunTime(
106 this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
109 this.nextWorkerNodeKey
=
110 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
112 : (this.nextWorkerNodeKey
?? this.previousWorkerNodeKey
) + 1
113 this.workerVirtualTaskRunTime
= 0
115 return this.nextWorkerNodeKey