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, | |
6c6afb84 | 7 | StrategyPolicy, |
87de9ff5 | 8 | TaskStatisticsRequirements, |
da309861 | 9 | WorkerChoiceStrategyOptions |
bf90656c | 10 | } from './selection-strategies-types' |
b3432a63 | 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. | |
15 | * | |
38e795c1 JB |
16 | * @typeParam Worker - Type of worker which manages the strategy. |
17 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
02706357 | 18 | * @typeParam Response - Type of execution response. This can only be serializable data. |
b3432a63 JB |
19 | */ |
20 | export class WeightedRoundRobinWorkerChoiceStrategy< | |
f06e48d8 | 21 | Worker extends IWorker, |
b2b1d84e JB |
22 | Data = unknown, |
23 | Response = unknown | |
bf90656c JB |
24 | > |
25 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 26 | implements IWorkerChoiceStrategy { |
6c6afb84 JB |
27 | /** @inheritDoc */ |
28 | public readonly strategyPolicy: StrategyPolicy = { | |
29 | useDynamicWorker: true | |
30 | } | |
31 | ||
afc003b2 | 32 | /** @inheritDoc */ |
87de9ff5 | 33 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
34 | runTime: { |
35 | aggregate: true, | |
36 | average: true, | |
37 | median: false | |
38 | }, | |
39 | waitTime: { | |
40 | aggregate: false, | |
41 | average: false, | |
42 | median: false | |
43 | }, | |
5df69fab JB |
44 | elu: { |
45 | aggregate: false, | |
46 | average: false, | |
47 | median: false | |
48 | } | |
10fcfaf4 JB |
49 | } |
50 | ||
b3432a63 | 51 | /** |
f06e48d8 | 52 | * Worker node id where the current task will be submitted. |
b3432a63 | 53 | */ |
f06e48d8 | 54 | private currentWorkerNodeId: number = 0 |
b3432a63 JB |
55 | /** |
56 | * Default worker weight. | |
57 | */ | |
777af0ac | 58 | private readonly defaultWorkerWeight: number |
b3432a63 | 59 | /** |
08f3f44c | 60 | * Worker virtual task runtime. |
b3432a63 | 61 | */ |
08f3f44c | 62 | private workerVirtualTaskRunTime: number = 0 |
b3432a63 | 63 | |
2fc5cae3 | 64 | /** @inheritDoc */ |
da309861 | 65 | public constructor ( |
c4855468 | 66 | pool: IPool<Worker, Data, Response>, |
2fc5cae3 | 67 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS |
da309861 JB |
68 | ) { |
69 | super(pool, opts) | |
932fc8be | 70 | this.setTaskStatisticsRequirements(this.opts) |
08f3f44c | 71 | this.defaultWorkerWeight = this.computeDefaultWorkerWeight() |
b3432a63 JB |
72 | } |
73 | ||
afc003b2 | 74 | /** @inheritDoc */ |
a6f7f1b4 | 75 | public reset (): boolean { |
f06e48d8 | 76 | this.currentWorkerNodeId = 0 |
08f3f44c | 77 | this.workerVirtualTaskRunTime = 0 |
ea7a90d3 JB |
78 | return true |
79 | } | |
80 | ||
138d29a8 JB |
81 | /** @inheritDoc */ |
82 | public update (): boolean { | |
83 | return true | |
84 | } | |
85 | ||
afc003b2 | 86 | /** @inheritDoc */ |
c923ce56 | 87 | public choose (): number { |
f06e48d8 | 88 | const chosenWorkerNodeKey = this.currentWorkerNodeId |
138d29a8 JB |
89 | const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime |
90 | const workerWeight = | |
08f3f44c | 91 | this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight |
138d29a8 | 92 | if (workerVirtualTaskRunTime < workerWeight) { |
08f3f44c | 93 | this.workerVirtualTaskRunTime = |
0d80593b | 94 | workerVirtualTaskRunTime + |
f6b641d6 | 95 | this.getWorkerTaskRunTime(chosenWorkerNodeKey) |
b3432a63 | 96 | } else { |
f06e48d8 JB |
97 | this.currentWorkerNodeId = |
98 | this.currentWorkerNodeId === this.pool.workerNodes.length - 1 | |
b3432a63 | 99 | ? 0 |
f06e48d8 | 100 | : this.currentWorkerNodeId + 1 |
08f3f44c | 101 | this.workerVirtualTaskRunTime = 0 |
b3432a63 | 102 | } |
f06e48d8 | 103 | return chosenWorkerNodeKey |
b3432a63 JB |
104 | } |
105 | ||
afc003b2 | 106 | /** @inheritDoc */ |
f06e48d8 JB |
107 | public remove (workerNodeKey: number): boolean { |
108 | if (this.currentWorkerNodeId === workerNodeKey) { | |
109 | if (this.pool.workerNodes.length === 0) { | |
110 | this.currentWorkerNodeId = 0 | |
97f4fd90 JB |
111 | } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) { |
112 | this.currentWorkerNodeId = this.pool.workerNodes.length - 1 | |
78ab2555 | 113 | } |
08f3f44c | 114 | this.workerVirtualTaskRunTime = 0 |
97a2abc3 | 115 | } |
08f3f44c | 116 | return true |
2377984d | 117 | } |
b3432a63 | 118 | } |