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'
11 TaskStatisticsRequirements
,
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies-types'
16 * Selects the next worker with a weighted round robin scheduling algorithm.
17 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
19 * @typeParam Worker - Type of worker which manages the strategy.
20 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
23 export class WeightedRoundRobinWorkerChoiceStrategy
<
24 Worker
extends IWorker
,
28 extends AbstractWorkerChoiceStrategy
<Worker
, Data
, Response
>
29 implements IWorkerChoiceStrategy
{
31 public readonly strategyPolicy
: StrategyPolicy
= {
32 dynamicWorkerUsage
: false,
33 dynamicWorkerReady
: true
37 public readonly taskStatisticsRequirements
: TaskStatisticsRequirements
= {
43 waitTime
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
,
44 elu
: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
48 * Default worker weight.
50 private readonly defaultWorkerWeight
: number
52 * Worker virtual task runtime.
54 private workerVirtualTaskRunTime
: number = 0
58 pool
: IPool
<Worker
, Data
, Response
>,
59 opts
: WorkerChoiceStrategyOptions
= DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
62 this.setTaskStatisticsRequirements(this.opts
)
63 this.defaultWorkerWeight
= this.computeDefaultWorkerWeight()
67 public reset (): boolean {
68 this.nextWorkerNodeKey
= 0
69 this.workerVirtualTaskRunTime
= 0
74 public update (): boolean {
79 public choose (): number | undefined {
80 const chosenWorkerNodeKey
= this.nextWorkerNodeKey
81 this.weightedRoundRobinNextWorkerNodeKey()
82 if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey
as number)) {
83 this.nextWorkerNodeKey
= undefined
85 return chosenWorkerNodeKey
89 public remove (workerNodeKey
: number): boolean {
90 if (this.nextWorkerNodeKey
=== workerNodeKey
) {
91 if (this.pool
.workerNodes
.length
=== 0) {
92 this.nextWorkerNodeKey
= 0
93 } else if (this.nextWorkerNodeKey
> this.pool
.workerNodes
.length
- 1) {
94 this.nextWorkerNodeKey
= this.pool
.workerNodes
.length
- 1
96 this.workerVirtualTaskRunTime
= 0
101 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
102 const workerVirtualTaskRunTime
= this.workerVirtualTaskRunTime
104 this.opts
.weights
?.[this.nextWorkerNodeKey
?? 0] ??
105 this.defaultWorkerWeight
106 if (workerVirtualTaskRunTime
< workerWeight
) {
107 this.workerVirtualTaskRunTime
=
108 workerVirtualTaskRunTime
+
109 this.getWorkerTaskRunTime(this.nextWorkerNodeKey
?? 0)
111 this.nextWorkerNodeKey
=
112 this.nextWorkerNodeKey
=== this.pool
.workerNodes
.length
- 1
114 : (this.nextWorkerNodeKey
?? 0) + 1
115 this.workerVirtualTaskRunTime
= 0
117 return this.nextWorkerNodeKey