Commit | Line | Data |
---|---|---|
feec6e8c JB |
1 | import type { IWorker } from '../worker' |
2 | import type { IPool } from '../pool' | |
3 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' | |
4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' | |
5 | import type { | |
6 | IWorkerChoiceStrategy, | |
6c6afb84 | 7 | StrategyPolicy, |
feec6e8c JB |
8 | WorkerChoiceStrategyOptions |
9 | } from './selection-strategies-types' | |
10 | ||
11 | /** | |
12 | * Selects the next worker with an interleaved weighted round robin scheduling algorithm. | |
13 | * | |
14 | * @typeParam Worker - Type of worker which manages the strategy. | |
e102732c JB |
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. | |
feec6e8c JB |
17 | */ |
18 | export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< | |
19 | Worker extends IWorker, | |
20 | Data = unknown, | |
21 | Response = unknown | |
22 | > | |
23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
24 | implements IWorkerChoiceStrategy { | |
6c6afb84 JB |
25 | /** @inheritDoc */ |
26 | public readonly strategyPolicy: StrategyPolicy = { | |
b1aae695 JB |
27 | dynamicWorkerUsage: false, |
28 | dynamicWorkerReady: true | |
6c6afb84 JB |
29 | } |
30 | ||
feec6e8c | 31 | /** |
d33be430 | 32 | * Round id. |
feec6e8c JB |
33 | * This is used to determine the current round weight. |
34 | */ | |
d33be430 | 35 | private roundId: number = 0 |
feec6e8c JB |
36 | /** |
37 | * Round weights. | |
38 | */ | |
e4854a4e | 39 | private roundWeights: number[] |
feec6e8c JB |
40 | /** |
41 | * Default worker weight. | |
42 | */ | |
43 | private readonly defaultWorkerWeight: number | |
44 | ||
45 | /** @inheritDoc */ | |
46 | public constructor ( | |
47 | pool: IPool<Worker, Data, Response>, | |
48 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
49 | ) { | |
50 | super(pool, opts) | |
932fc8be | 51 | this.setTaskStatisticsRequirements(this.opts) |
feec6e8c JB |
52 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
53 | this.roundWeights = this.getRoundWeights() | |
54 | } | |
55 | ||
56 | /** @inheritDoc */ | |
57 | public reset (): boolean { | |
9b106837 | 58 | this.nextWorkerNodeKey = 0 |
d33be430 | 59 | this.roundId = 0 |
feec6e8c JB |
60 | return true |
61 | } | |
62 | ||
63 | /** @inheritDoc */ | |
64 | public update (): boolean { | |
65 | return true | |
66 | } | |
67 | ||
68 | /** @inheritDoc */ | |
b1aae695 | 69 | public choose (): number | undefined { |
297f3bbe JB |
70 | let roundId: number | undefined |
71 | let workerNodeId: number | undefined | |
72 | for ( | |
d33be430 | 73 | let roundIndex = this.roundId; |
d3127e84 JB |
74 | roundIndex < this.roundWeights.length; |
75 | roundIndex++ | |
297f3bbe | 76 | ) { |
feec6e8c | 77 | for ( |
b1aae695 | 78 | let workerNodeKey = this.nextWorkerNodeKey ?? 0; |
297f3bbe JB |
79 | workerNodeKey < this.pool.workerNodes.length; |
80 | workerNodeKey++ | |
feec6e8c | 81 | ) { |
297f3bbe JB |
82 | const workerWeight = |
83 | this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight | |
3f7d99f5 | 84 | if ( |
8990357d | 85 | this.isWorkerNodeEligible(workerNodeKey) && |
3f7d99f5 JB |
86 | workerWeight >= this.roundWeights[roundIndex] |
87 | ) { | |
d3127e84 | 88 | roundId = roundIndex |
297f3bbe JB |
89 | workerNodeId = workerNodeKey |
90 | break | |
feec6e8c JB |
91 | } |
92 | } | |
297f3bbe | 93 | } |
b1aae695 JB |
94 | this.roundId = roundId as number |
95 | this.nextWorkerNodeKey = workerNodeId | |
9b106837 JB |
96 | const chosenWorkerNodeKey = this.nextWorkerNodeKey |
97 | if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) { | |
98 | this.nextWorkerNodeKey = 0 | |
d33be430 JB |
99 | this.roundId = |
100 | this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 | |
297f3bbe | 101 | } else { |
b1aae695 | 102 | this.nextWorkerNodeKey = (this.nextWorkerNodeKey ?? 0) + 1 |
feec6e8c JB |
103 | } |
104 | return chosenWorkerNodeKey | |
105 | } | |
106 | ||
107 | /** @inheritDoc */ | |
108 | public remove (workerNodeKey: number): boolean { | |
9b106837 | 109 | if (this.nextWorkerNodeKey === workerNodeKey) { |
feec6e8c | 110 | if (this.pool.workerNodes.length === 0) { |
9b106837 JB |
111 | this.nextWorkerNodeKey = 0 |
112 | } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { | |
113 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 | |
d33be430 JB |
114 | this.roundId = |
115 | this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 | |
feec6e8c JB |
116 | } |
117 | } | |
118 | return true | |
119 | } | |
120 | ||
e4854a4e JB |
121 | /** @inheritDoc */ |
122 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
123 | super.setOptions(opts) | |
124 | this.roundWeights = this.getRoundWeights() | |
125 | } | |
126 | ||
feec6e8c JB |
127 | private getRoundWeights (): number[] { |
128 | if (this.opts.weights == null) { | |
129 | return [this.defaultWorkerWeight] | |
130 | } | |
131 | return [ | |
132 | ...new Set( | |
133 | Object.values(this.opts.weights) | |
134 | .slice() | |
135 | .sort((a, b) => a - b) | |
136 | ) | |
137 | ] | |
138 | } | |
139 | } |