Commit | Line | Data |
---|---|---|
bdaf31cd | 1 | import type { IPoolInternal } from '../pool-internal' |
f06e48d8 | 2 | import type { IWorker } from '../worker' |
51fe3d3c JB |
3 | import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy' |
4 | import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy' | |
5 | import { LessUsedWorkerChoiceStrategy } from './less-used-worker-choice-strategy' | |
6 | import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy' | |
bdaf31cd JB |
7 | import type { |
8 | IWorkerChoiceStrategy, | |
97a2abc3 | 9 | RequiredStatistics, |
da309861 JB |
10 | WorkerChoiceStrategy, |
11 | WorkerChoiceStrategyOptions | |
bdaf31cd JB |
12 | } from './selection-strategies-types' |
13 | import { WorkerChoiceStrategies } from './selection-strategies-types' | |
51fe3d3c | 14 | import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy' |
bdaf31cd JB |
15 | |
16 | /** | |
17 | * The worker choice strategy context. | |
18 | * | |
38e795c1 JB |
19 | * @typeParam Worker - Type of worker. |
20 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. | |
21 | * @typeParam Response - Type of response of execution. This can only be serializable data. | |
bdaf31cd JB |
22 | */ |
23 | export class WorkerChoiceStrategyContext< | |
f06e48d8 | 24 | Worker extends IWorker, |
b2b1d84e JB |
25 | Data = unknown, |
26 | Response = unknown | |
bdaf31cd | 27 | > { |
b529c323 | 28 | private readonly workerChoiceStrategies: Map< |
95c83464 | 29 | WorkerChoiceStrategy, |
17393ac8 | 30 | IWorkerChoiceStrategy |
b529c323 | 31 | > |
bdaf31cd JB |
32 | |
33 | /** | |
34 | * Worker choice strategy context constructor. | |
35 | * | |
38e795c1 | 36 | * @param pool - The pool instance. |
a22cdf86 | 37 | * @param workerChoiceStrategyType - The worker choice strategy. |
da309861 | 38 | * @param opts - The worker choice strategy options. |
bdaf31cd JB |
39 | */ |
40 | public constructor ( | |
3300e7bc | 41 | pool: IPoolInternal<Worker, Data, Response>, |
da309861 JB |
42 | private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, |
43 | opts: WorkerChoiceStrategyOptions = { medRunTime: false } | |
bdaf31cd | 44 | ) { |
1086026a | 45 | this.execute.bind(this) |
b529c323 JB |
46 | this.workerChoiceStrategies = new Map< |
47 | WorkerChoiceStrategy, | |
17393ac8 | 48 | IWorkerChoiceStrategy |
b529c323 JB |
49 | >([ |
50 | [ | |
51 | WorkerChoiceStrategies.ROUND_ROBIN, | |
da309861 | 52 | new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool, opts) |
b529c323 JB |
53 | ], |
54 | [ | |
55 | WorkerChoiceStrategies.LESS_USED, | |
da309861 | 56 | new LessUsedWorkerChoiceStrategy<Worker, Data, Response>(pool, opts) |
b529c323 JB |
57 | ], |
58 | [ | |
59 | WorkerChoiceStrategies.LESS_BUSY, | |
da309861 | 60 | new LessBusyWorkerChoiceStrategy<Worker, Data, Response>(pool, opts) |
b529c323 JB |
61 | ], |
62 | [ | |
63 | WorkerChoiceStrategies.FAIR_SHARE, | |
da309861 | 64 | new FairShareWorkerChoiceStrategy<Worker, Data, Response>(pool, opts) |
b529c323 JB |
65 | ], |
66 | [ | |
67 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, | |
da309861 JB |
68 | new WeightedRoundRobinWorkerChoiceStrategy<Worker, Data, Response>( |
69 | pool, | |
70 | opts | |
71 | ) | |
b529c323 JB |
72 | ] |
73 | ]) | |
bdaf31cd JB |
74 | } |
75 | ||
97a2abc3 | 76 | /** |
51fe3d3c | 77 | * Gets the worker choice strategy in the context required statistics. |
97a2abc3 JB |
78 | * |
79 | * @returns The required statistics. | |
80 | */ | |
81 | public getRequiredStatistics (): RequiredStatistics { | |
95c83464 JB |
82 | return ( |
83 | this.workerChoiceStrategies.get( | |
84 | this.workerChoiceStrategyType | |
17393ac8 | 85 | ) as IWorkerChoiceStrategy |
95c83464 | 86 | ).requiredStatistics |
97a2abc3 JB |
87 | } |
88 | ||
bdaf31cd | 89 | /** |
bdede008 | 90 | * Sets the worker choice strategy to use in the context. |
bdaf31cd | 91 | * |
38e795c1 | 92 | * @param workerChoiceStrategy - The worker choice strategy to set. |
bdaf31cd JB |
93 | */ |
94 | public setWorkerChoiceStrategy ( | |
95 | workerChoiceStrategy: WorkerChoiceStrategy | |
96 | ): void { | |
aee46736 | 97 | if (this.workerChoiceStrategyType !== workerChoiceStrategy) { |
b2b1d84e | 98 | this.workerChoiceStrategyType = workerChoiceStrategy |
b2b1d84e | 99 | } |
aee46736 | 100 | this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset() |
bdaf31cd JB |
101 | } |
102 | ||
103 | /** | |
51fe3d3c | 104 | * Executes the worker choice strategy algorithm in the context. |
bdaf31cd | 105 | * |
f06e48d8 | 106 | * @returns The key of the worker node. |
bdaf31cd | 107 | */ |
c923ce56 | 108 | public execute (): number { |
17393ac8 JB |
109 | return ( |
110 | this.workerChoiceStrategies.get( | |
111 | this.workerChoiceStrategyType | |
112 | ) as IWorkerChoiceStrategy | |
113 | ).choose() | |
bdaf31cd | 114 | } |
97a2abc3 JB |
115 | |
116 | /** | |
f06e48d8 | 117 | * Removes a worker node key from the worker choice strategy in the context. |
97a2abc3 | 118 | * |
f06e48d8 | 119 | * @param workerNodeKey - The key of the worker node. |
97a2abc3 JB |
120 | * @returns `true` if the removal is successful, `false` otherwise. |
121 | */ | |
f06e48d8 | 122 | public remove (workerNodeKey: number): boolean { |
95c83464 JB |
123 | return ( |
124 | this.workerChoiceStrategies.get( | |
125 | this.workerChoiceStrategyType | |
17393ac8 | 126 | ) as IWorkerChoiceStrategy |
f06e48d8 | 127 | ).remove(workerNodeKey) |
95c83464 | 128 | } |
bdaf31cd | 129 | } |