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