Commit | Line | Data |
---|---|---|
3c93feb9 JB |
1 | import { |
2 | DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, | |
3 | DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
4 | } from '../../utils' | |
2fc5cae3 | 5 | import type { IPool } from '../pool' |
f06e48d8 | 6 | import type { IWorker } from '../worker' |
23ff945a | 7 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
9adcefab JB |
8 | import { |
9 | type IWorkerChoiceStrategy, | |
10 | Measurements, | |
11 | type TaskStatisticsRequirements, | |
12 | type WorkerChoiceStrategyOptions | |
bf90656c | 13 | } from './selection-strategies-types' |
23ff945a | 14 | |
23ff945a JB |
15 | /** |
16 | * Selects the next worker with a fair share scheduling algorithm. | |
17 | * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing. | |
18 | * | |
38e795c1 | 19 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
20 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
21 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
23ff945a JB |
22 | */ |
23 | export class FairShareWorkerChoiceStrategy< | |
f06e48d8 | 24 | Worker extends IWorker, |
b2b1d84e JB |
25 | Data = unknown, |
26 | Response = unknown | |
bf90656c JB |
27 | > |
28 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 29 | implements IWorkerChoiceStrategy { |
afc003b2 | 30 | /** @inheritDoc */ |
87de9ff5 | 31 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
32 | runTime: { |
33 | aggregate: true, | |
34 | average: true, | |
35 | median: false | |
36 | }, | |
3c93feb9 | 37 | waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, |
5df69fab | 38 | elu: { |
9adcefab JB |
39 | aggregate: true, |
40 | average: true, | |
5df69fab JB |
41 | median: false |
42 | } | |
10fcfaf4 JB |
43 | } |
44 | ||
23ff945a | 45 | /** |
b0d6ed8f | 46 | * Workers' virtual task end execution timestamp. |
23ff945a | 47 | */ |
b0d6ed8f | 48 | private workersVirtualTaskEndTimestamp: number[] = [] |
23ff945a | 49 | |
2fc5cae3 JB |
50 | /** @inheritDoc */ |
51 | public constructor ( | |
52 | pool: IPool<Worker, Data, Response>, | |
53 | opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS | |
54 | ) { | |
55 | super(pool, opts) | |
932fc8be | 56 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
57 | } |
58 | ||
afc003b2 | 59 | /** @inheritDoc */ |
a6f7f1b4 | 60 | public reset (): boolean { |
b0d6ed8f | 61 | this.workersVirtualTaskEndTimestamp = [] |
ea7a90d3 JB |
62 | return true |
63 | } | |
64 | ||
138d29a8 | 65 | /** @inheritDoc */ |
a4958de2 | 66 | public update (workerNodeKey: number): boolean { |
b0d6ed8f | 67 | this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey) |
db703c75 JB |
68 | return true |
69 | } | |
70 | ||
71 | /** @inheritDoc */ | |
b1aae695 JB |
72 | public choose (): number | undefined { |
73 | const chosenWorkerNodeKey = this.fairShareNextWorkerNodeKey() | |
74 | this.assignChosenWorkerNodeKey(chosenWorkerNodeKey) | |
75 | return this.nextWorkerNodeKey | |
9b106837 JB |
76 | } |
77 | ||
78 | /** @inheritDoc */ | |
79 | public remove (workerNodeKey: number): boolean { | |
80 | this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1) | |
81 | return true | |
82 | } | |
83 | ||
b1aae695 | 84 | private fairShareNextWorkerNodeKey (): number | undefined { |
23ff945a | 85 | let minWorkerVirtualTaskEndTimestamp = Infinity |
b1aae695 | 86 | let chosenWorkerNodeKey: number | undefined |
08f3f44c | 87 | for (const [workerNodeKey] of this.pool.workerNodes.entries()) { |
b0d6ed8f JB |
88 | if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) { |
89 | this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey) | |
90 | } | |
0d80593b | 91 | const workerVirtualTaskEndTimestamp = |
b0d6ed8f | 92 | this.workersVirtualTaskEndTimestamp[workerNodeKey] |
19dbc45b | 93 | if ( |
8990357d | 94 | this.isWorkerNodeEligible(workerNodeKey) && |
19dbc45b JB |
95 | workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp |
96 | ) { | |
0d80593b | 97 | minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp |
b1aae695 | 98 | chosenWorkerNodeKey = workerNodeKey |
23ff945a JB |
99 | } |
100 | } | |
b1aae695 | 101 | return chosenWorkerNodeKey |
97a2abc3 JB |
102 | } |
103 | ||
23ff945a | 104 | /** |
b0d6ed8f | 105 | * Computes the worker node key virtual task end timestamp. |
11df3590 | 106 | * |
f06e48d8 | 107 | * @param workerNodeKey - The worker node key. |
23ff945a | 108 | */ |
b0d6ed8f JB |
109 | private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void { |
110 | this.workersVirtualTaskEndTimestamp[workerNodeKey] = | |
111 | this.getWorkerVirtualTaskEndTimestamp( | |
112 | workerNodeKey, | |
113 | this.getWorkerVirtualTaskStartTimestamp(workerNodeKey) | |
114 | ) | |
115 | } | |
116 | ||
117 | private getWorkerVirtualTaskEndTimestamp ( | |
118 | workerNodeKey: number, | |
119 | workerVirtualTaskStartTimestamp: number | |
120 | ): number { | |
9adcefab JB |
121 | const workerTaskRunTime = |
122 | this.opts.measurement === Measurements.elu | |
123 | ? this.getWorkerTaskElu(workerNodeKey) | |
124 | : this.getWorkerTaskRunTime(workerNodeKey) | |
125 | return workerVirtualTaskStartTimestamp + workerTaskRunTime | |
b0d6ed8f JB |
126 | } |
127 | ||
128 | private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number { | |
129 | return Math.max( | |
130 | performance.now(), | |
131 | this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity | |
132 | ) | |
23ff945a JB |
133 | } |
134 | } |