Commit | Line | Data |
---|---|---|
f06e48d8 | 1 | import type { IWorker } from '../worker' |
65d7a1c9 JB |
2 | import type { IPool } from '../pool' |
3 | import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' | |
b3432a63 | 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
bf90656c JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
87de9ff5 | 7 | TaskStatisticsRequirements, |
da309861 | 8 | WorkerChoiceStrategyOptions |
bf90656c | 9 | } from './selection-strategies-types' |
b3432a63 | 10 | |
b3432a63 JB |
11 | /** |
12 | * Selects the next worker with a weighted round robin scheduling algorithm. | |
13 | * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin. | |
14 | * | |
38e795c1 JB |
15 | * @typeParam Worker - Type of worker which manages the strategy. |
16 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
02706357 | 17 | * @typeParam Response - Type of execution response. This can only be serializable 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 */ |
87de9ff5 | 27 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
28 | runTime: { |
29 | aggregate: true, | |
30 | average: true, | |
31 | median: false | |
32 | }, | |
33 | waitTime: { | |
34 | aggregate: false, | |
35 | average: false, | |
36 | median: false | |
37 | }, | |
62c15a68 | 38 | elu: false |
10fcfaf4 JB |
39 | } |
40 | ||
b3432a63 | 41 | /** |
f06e48d8 | 42 | * Worker node id where the current task will be submitted. |
b3432a63 | 43 | */ |
f06e48d8 | 44 | private currentWorkerNodeId: number = 0 |
b3432a63 JB |
45 | /** |
46 | * Default worker weight. | |
47 | */ | |
777af0ac | 48 | private readonly defaultWorkerWeight: number |
b3432a63 | 49 | /** |
08f3f44c | 50 | * Worker virtual task runtime. |
b3432a63 | 51 | */ |
08f3f44c | 52 | private workerVirtualTaskRunTime: number = 0 |
b3432a63 | 53 | |
2fc5cae3 | 54 | /** @inheritDoc */ |
da309861 | 55 | public constructor ( |
c4855468 | 56 | pool: IPool<Worker, Data, Response>, |
2fc5cae3 | 57 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
da309861 JB |
58 | ) { |
59 | super(pool, opts) | |
932fc8be | 60 | this.setTaskStatisticsRequirements(this.opts) |
08f3f44c | 61 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
b3432a63 JB |
62 | } |
63 | ||
afc003b2 | 64 | /** @inheritDoc */ |
a6f7f1b4 | 65 | public reset (): boolean { |
f06e48d8 | 66 | this.currentWorkerNodeId = 0 |
08f3f44c | 67 | this.workerVirtualTaskRunTime = 0 |
ea7a90d3 JB |
68 | return true |
69 | } | |
70 | ||
138d29a8 JB |
71 | /** @inheritDoc */ |
72 | public update (): boolean { | |
73 | return true | |
74 | } | |
75 | ||
afc003b2 | 76 | /** @inheritDoc */ |
c923ce56 | 77 | public choose (): number { |
f06e48d8 | 78 | const chosenWorkerNodeKey = this.currentWorkerNodeId |
138d29a8 JB |
79 | const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime |
80 | const workerWeight = | |
08f3f44c | 81 | this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight |
138d29a8 | 82 | if (workerVirtualTaskRunTime < workerWeight) { |
08f3f44c | 83 | this.workerVirtualTaskRunTime = |
0d80593b | 84 | workerVirtualTaskRunTime + |
f6b641d6 | 85 | this.getWorkerTaskRunTime(chosenWorkerNodeKey) |
b3432a63 | 86 | } else { |
f06e48d8 JB |
87 | this.currentWorkerNodeId = |
88 | this.currentWorkerNodeId === this.pool.workerNodes.length - 1 | |
b3432a63 | 89 | ? 0 |
f06e48d8 | 90 | : this.currentWorkerNodeId + 1 |
08f3f44c | 91 | this.workerVirtualTaskRunTime = 0 |
b3432a63 | 92 | } |
f06e48d8 | 93 | return chosenWorkerNodeKey |
b3432a63 JB |
94 | } |
95 | ||
afc003b2 | 96 | /** @inheritDoc */ |
f06e48d8 JB |
97 | public remove (workerNodeKey: number): boolean { |
98 | if (this.currentWorkerNodeId === workerNodeKey) { | |
99 | if (this.pool.workerNodes.length === 0) { | |
100 | this.currentWorkerNodeId = 0 | |
97f4fd90 JB |
101 | } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { |
102 | this.currentWorkerNodeId = this.pool.workerNodes.length - 1 | |
78ab2555 | 103 | } |
08f3f44c | 104 | this.workerVirtualTaskRunTime = 0 |
97a2abc3 | 105 | } |
08f3f44c | 106 | return true |
2377984d | 107 | } |
b3432a63 | 108 | } |