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' |
bf90656c JB |
5 | import type { |
6 | IWorkerChoiceStrategy, | |
87de9ff5 | 7 | TaskStatisticsRequirements, |
2fc5cae3 | 8 | WorkerChoiceStrategyOptions |
bf90656c | 9 | } from './selection-strategies-types' |
23ff945a | 10 | |
23ff945a JB |
11 | /** |
12 | * Selects the next worker with a fair share scheduling algorithm. | |
13 | * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing. | |
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. |
23ff945a JB |
18 | */ |
19 | export class FairShareWorkerChoiceStrategy< | |
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 | }, | |
5df69fab JB |
38 | elu: { |
39 | aggregate: false, | |
40 | average: false, | |
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) |
138d29a8 JB |
68 | return true |
69 | } | |
70 | ||
afc003b2 | 71 | /** @inheritDoc */ |
c923ce56 | 72 | public choose (): number { |
23ff945a | 73 | let minWorkerVirtualTaskEndTimestamp = Infinity |
f06e48d8 | 74 | let chosenWorkerNodeKey!: number |
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 | |
08f3f44c | 83 | chosenWorkerNodeKey = workerNodeKey |
23ff945a JB |
84 | } |
85 | } | |
f06e48d8 | 86 | return chosenWorkerNodeKey |
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 { | |
f6b641d6 JB |
112 | return ( |
113 | workerVirtualTaskStartTimestamp + this.getWorkerTaskRunTime(workerNodeKey) | |
114 | ) | |
b0d6ed8f JB |
115 | } |
116 | ||
117 | private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number { | |
118 | return Math.max( | |
119 | performance.now(), | |
120 | this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity | |
121 | ) | |
23ff945a JB |
122 | } |
123 | } |