]>
Commit | Line | Data |
---|---|---|
d35e5717 | 1 | import type { IPool } from '../pool.js' |
ded253e2 | 2 | import type { IWorker } from '../worker.js' |
bf90656c JB |
3 | import type { |
4 | IWorkerChoiceStrategy, | |
39618ede | 5 | TaskStatisticsRequirements, |
3a502712 | 6 | WorkerChoiceStrategyOptions, |
d35e5717 | 7 | } from './selection-strategies-types.js' |
b3432a63 | 8 | |
97231086 JB |
9 | import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js' |
10 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' | |
11 | ||
b3432a63 JB |
12 | /** |
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. | |
38e795c1 | 15 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
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. | |
b3432a63 JB |
18 | */ |
19 | export class WeightedRoundRobinWorkerChoiceStrategy< | |
f06e48d8 | 20 | Worker extends IWorker, |
b2b1d84e JB |
21 | Data = unknown, |
22 | Response = unknown | |
bf90656c JB |
23 | > |
24 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 25 | implements IWorkerChoiceStrategy { |
afc003b2 | 26 | /** @inheritDoc */ |
ae85b351 | 27 | public override readonly taskStatisticsRequirements: TaskStatisticsRequirements = |
7cec62a4 | 28 | Object.freeze({ |
ae85b351 JB |
29 | elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
30 | runTime: { | |
31 | aggregate: true, | |
32 | average: true, | |
33 | median: false, | |
34 | }, | |
35 | waitTime: { | |
36 | aggregate: true, | |
37 | average: true, | |
38 | median: false, | |
39 | }, | |
7cec62a4 | 40 | }) |
10fcfaf4 | 41 | |
5de88a2a JB |
42 | /** |
43 | * Worker node virtual execution time. | |
44 | */ | |
45 | private workerNodeVirtualTaskExecutionTime = 0 | |
46 | ||
2fc5cae3 | 47 | /** @inheritDoc */ |
da309861 | 48 | public constructor ( |
c4855468 | 49 | pool: IPool<Worker, Data, Response>, |
39618ede | 50 | opts?: WorkerChoiceStrategyOptions |
da309861 JB |
51 | ) { |
52 | super(pool, opts) | |
050477bd | 53 | this.setTaskStatisticsRequirements(this.opts) |
b3432a63 JB |
54 | } |
55 | ||
afc003b2 | 56 | /** @inheritDoc */ |
b1aae695 | 57 | public choose (): number | undefined { |
baca80f7 | 58 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
a38b62f1 | 59 | this.weightedRoundRobinNextWorkerNodeKey() |
8a2bf757 JB |
60 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
61 | if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) { | |
62 | return undefined | |
63 | } | |
64 | return this.checkWorkerNodeKey(this.nextWorkerNodeKey) | |
b3432a63 JB |
65 | } |
66 | ||
afc003b2 | 67 | /** @inheritDoc */ |
f06e48d8 | 68 | public remove (workerNodeKey: number): boolean { |
226b02a3 | 69 | if (this.pool.workerNodes.length === 0) { |
8a2bf757 | 70 | return this.reset() |
226b02a3 | 71 | } |
9b106837 | 72 | if (this.nextWorkerNodeKey === workerNodeKey) { |
e0843544 | 73 | this.workerNodeVirtualTaskExecutionTime = 0 |
97a2abc3 | 74 | } |
226b02a3 | 75 | if ( |
8a2bf757 JB |
76 | this.nextWorkerNodeKey != null && |
77 | this.nextWorkerNodeKey >= workerNodeKey | |
226b02a3 | 78 | ) { |
8a2bf757 JB |
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 | |
84 | } | |
226b02a3 | 85 | } |
08f3f44c | 86 | return true |
2377984d | 87 | } |
9b106837 | 88 | |
97231086 JB |
89 | /** @inheritDoc */ |
90 | public reset (): boolean { | |
91 | this.resetWorkerNodeKeyProperties() | |
92 | this.workerNodeVirtualTaskExecutionTime = 0 | |
93 | return true | |
94 | } | |
95 | ||
96 | /** @inheritDoc */ | |
97 | public update (): boolean { | |
98 | return true | |
9b106837 | 99 | } |
5de88a2a JB |
100 | |
101 | private weightedRoundRobinNextWorkerNodeKey (): number | undefined { | |
8a2bf757 JB |
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] | |
5de88a2a JB |
105 | if (this.workerNodeVirtualTaskExecutionTime < workerWeight) { |
106 | this.workerNodeVirtualTaskExecutionTime += | |
8a2bf757 JB |
107 | this.getWorkerNodeTaskWaitTime(workerNodeKey) + |
108 | this.getWorkerNodeTaskRunTime(workerNodeKey) | |
5de88a2a JB |
109 | } else { |
110 | this.nextWorkerNodeKey = | |
111 | this.nextWorkerNodeKey === this.pool.workerNodes.length - 1 | |
112 | ? 0 | |
8a2bf757 | 113 | : workerNodeKey + 1 |
5de88a2a JB |
114 | this.workerNodeVirtualTaskExecutionTime = 0 |
115 | } | |
116 | return this.nextWorkerNodeKey | |
117 | } | |
b3432a63 | 118 | } |