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 = { | |
27 | useDynamicWorker: true | |
28 | } | |
29 | ||
feec6e8c | 30 | /** |
d33be430 | 31 | * Round id. |
feec6e8c JB |
32 | * This is used to determine the current round weight. |
33 | */ | |
d33be430 | 34 | private roundId: number = 0 |
feec6e8c JB |
35 | /** |
36 | * Round weights. | |
37 | */ | |
e4854a4e | 38 | private roundWeights: number[] |
feec6e8c JB |
39 | /** |
40 | * Default worker weight. | |
41 | */ | |
42 | private readonly defaultWorkerWeight: number | |
43 | ||
44 | /** @inheritDoc */ | |
45 | public constructor ( | |
46 | pool: IPool<Worker, Data, Response>, | |
47 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
48 | ) { | |
49 | super(pool, opts) | |
932fc8be | 50 | this.setTaskStatisticsRequirements(this.opts) |
feec6e8c JB |
51 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
52 | this.roundWeights = this.getRoundWeights() | |
53 | } | |
54 | ||
55 | /** @inheritDoc */ | |
56 | public reset (): boolean { | |
9b106837 | 57 | this.nextWorkerNodeKey = 0 |
d33be430 | 58 | this.roundId = 0 |
feec6e8c JB |
59 | return true |
60 | } | |
61 | ||
62 | /** @inheritDoc */ | |
63 | public update (): boolean { | |
64 | return true | |
65 | } | |
66 | ||
67 | /** @inheritDoc */ | |
68 | public choose (): number { | |
297f3bbe JB |
69 | let roundId: number | undefined |
70 | let workerNodeId: number | undefined | |
71 | for ( | |
d33be430 | 72 | let roundIndex = this.roundId; |
d3127e84 JB |
73 | roundIndex < this.roundWeights.length; |
74 | roundIndex++ | |
297f3bbe | 75 | ) { |
feec6e8c | 76 | for ( |
9b106837 | 77 | let workerNodeKey = this.nextWorkerNodeKey; |
297f3bbe JB |
78 | workerNodeKey < this.pool.workerNodes.length; |
79 | workerNodeKey++ | |
feec6e8c | 80 | ) { |
297f3bbe JB |
81 | const workerWeight = |
82 | this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight | |
3f7d99f5 JB |
83 | if ( |
84 | this.isWorkerNodeReady(workerNodeKey) && | |
85 | workerWeight >= this.roundWeights[roundIndex] | |
86 | ) { | |
d3127e84 | 87 | roundId = roundIndex |
297f3bbe JB |
88 | workerNodeId = workerNodeKey |
89 | break | |
feec6e8c JB |
90 | } |
91 | } | |
297f3bbe | 92 | } |
d33be430 | 93 | this.roundId = roundId ?? 0 |
9b106837 JB |
94 | this.nextWorkerNodeKey = workerNodeId ?? 0 |
95 | const chosenWorkerNodeKey = this.nextWorkerNodeKey | |
96 | if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) { | |
97 | this.nextWorkerNodeKey = 0 | |
d33be430 JB |
98 | this.roundId = |
99 | this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 | |
297f3bbe | 100 | } else { |
9b106837 | 101 | this.nextWorkerNodeKey = this.nextWorkerNodeKey + 1 |
feec6e8c JB |
102 | } |
103 | return chosenWorkerNodeKey | |
104 | } | |
105 | ||
106 | /** @inheritDoc */ | |
107 | public remove (workerNodeKey: number): boolean { | |
9b106837 | 108 | if (this.nextWorkerNodeKey === workerNodeKey) { |
feec6e8c | 109 | if (this.pool.workerNodes.length === 0) { |
9b106837 JB |
110 | this.nextWorkerNodeKey = 0 |
111 | } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) { | |
112 | this.nextWorkerNodeKey = this.pool.workerNodes.length - 1 | |
d33be430 JB |
113 | this.roundId = |
114 | this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1 | |
feec6e8c JB |
115 | } |
116 | } | |
117 | return true | |
118 | } | |
119 | ||
e4854a4e JB |
120 | /** @inheritDoc */ |
121 | public setOptions (opts: WorkerChoiceStrategyOptions): void { | |
122 | super.setOptions(opts) | |
123 | this.roundWeights = this.getRoundWeights() | |
124 | } | |
125 | ||
feec6e8c JB |
126 | private getRoundWeights (): number[] { |
127 | if (this.opts.weights == null) { | |
128 | return [this.defaultWorkerWeight] | |
129 | } | |
130 | return [ | |
131 | ...new Set( | |
132 | Object.values(this.opts.weights) | |
133 | .slice() | |
134 | .sort((a, b) => a - b) | |
135 | ) | |
136 | ] | |
137 | } | |
138 | } |