Commit | Line | Data |
---|---|---|
d35e5717 | 1 | import type { IPool } from '../pool.js' |
67f3f2d6 | 2 | import type { IWorker } from '../worker.js' |
d35e5717 | 3 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' |
9adcefab JB |
4 | import { |
5 | type IWorkerChoiceStrategy, | |
6 | Measurements, | |
39618ede | 7 | type TaskStatisticsRequirements, |
3a502712 | 8 | type WorkerChoiceStrategyOptions, |
d35e5717 | 9 | } from './selection-strategies-types.js' |
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. | |
38e795c1 | 14 | * @typeParam Worker - Type of worker which manages the strategy. |
e102732c JB |
15 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
16 | * @typeParam Response - Type of execution response. This can only be structured-cloneable data. | |
23ff945a JB |
17 | */ |
18 | export class FairShareWorkerChoiceStrategy< | |
f06e48d8 | 19 | Worker extends IWorker, |
b2b1d84e JB |
20 | Data = unknown, |
21 | Response = unknown | |
bf90656c JB |
22 | > |
23 | extends AbstractWorkerChoiceStrategy<Worker, Data, Response> | |
17393ac8 | 24 | implements IWorkerChoiceStrategy { |
afc003b2 | 25 | /** @inheritDoc */ |
87de9ff5 | 26 | public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { |
932fc8be JB |
27 | runTime: { |
28 | aggregate: true, | |
29 | average: true, | |
3a502712 | 30 | median: false, |
932fc8be | 31 | }, |
e0843544 JB |
32 | waitTime: { |
33 | aggregate: true, | |
34 | average: true, | |
3a502712 | 35 | median: false, |
e0843544 | 36 | }, |
5df69fab | 37 | elu: { |
9adcefab JB |
38 | aggregate: true, |
39 | average: true, | |
3a502712 JB |
40 | median: false, |
41 | }, | |
10fcfaf4 JB |
42 | } |
43 | ||
2fc5cae3 JB |
44 | /** @inheritDoc */ |
45 | public constructor ( | |
46 | pool: IPool<Worker, Data, Response>, | |
39618ede | 47 | opts?: WorkerChoiceStrategyOptions |
2fc5cae3 JB |
48 | ) { |
49 | super(pool, opts) | |
932fc8be | 50 | this.setTaskStatisticsRequirements(this.opts) |
2fc5cae3 JB |
51 | } |
52 | ||
afc003b2 | 53 | /** @inheritDoc */ |
a6f7f1b4 | 54 | public reset (): boolean { |
f3a91bac JB |
55 | for (const workerNode of this.pool.workerNodes) { |
56 | delete workerNode.strategyData?.virtualTaskEndTimestamp | |
57 | } | |
ea7a90d3 JB |
58 | return true |
59 | } | |
60 | ||
138d29a8 | 61 | /** @inheritDoc */ |
a4958de2 | 62 | public update (workerNodeKey: number): boolean { |
f3a91bac JB |
63 | this.pool.workerNodes[workerNodeKey].strategyData = { |
64 | virtualTaskEndTimestamp: | |
3a502712 | 65 | this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey), |
f3a91bac | 66 | } |
db703c75 JB |
67 | return true |
68 | } | |
69 | ||
70 | /** @inheritDoc */ | |
b1aae695 | 71 | public choose (): number | undefined { |
baca80f7 | 72 | this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) |
fce028d6 | 73 | this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey() |
b1aae695 | 74 | return this.nextWorkerNodeKey |
9b106837 JB |
75 | } |
76 | ||
77 | /** @inheritDoc */ | |
f3a91bac | 78 | public remove (): boolean { |
9b106837 JB |
79 | return true |
80 | } | |
81 | ||
b1aae695 | 82 | private fairShareNextWorkerNodeKey (): number | undefined { |
f3a91bac JB |
83 | return this.pool.workerNodes.reduce( |
84 | (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { | |
85 | if (workerNode.strategyData?.virtualTaskEndTimestamp == null) { | |
86 | workerNode.strategyData = { | |
87 | virtualTaskEndTimestamp: | |
3a502712 | 88 | this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey), |
f3a91bac JB |
89 | } |
90 | } | |
ae3ab61d | 91 | return this.isWorkerNodeReady(workerNodeKey) && |
67f3f2d6 JB |
92 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
93 | workerNode.strategyData.virtualTaskEndTimestamp! < | |
94 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
95 | workerNodes[minWorkerNodeKey].strategyData!.virtualTaskEndTimestamp! | |
f3a91bac JB |
96 | ? workerNodeKey |
97 | : minWorkerNodeKey | |
98 | }, | |
99 | 0 | |
100 | ) | |
97a2abc3 JB |
101 | } |
102 | ||
23ff945a | 103 | /** |
b0d6ed8f | 104 | * Computes the worker node key virtual task end timestamp. |
f06e48d8 | 105 | * @param workerNodeKey - The worker node key. |
f3a91bac | 106 | * @returns The worker node key virtual task end timestamp. |
23ff945a | 107 | */ |
f3a91bac JB |
108 | private computeWorkerNodeVirtualTaskEndTimestamp ( |
109 | workerNodeKey: number | |
110 | ): number { | |
111 | return this.getWorkerNodeVirtualTaskEndTimestamp( | |
112 | workerNodeKey, | |
113 | this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey) | |
114 | ) | |
b0d6ed8f JB |
115 | } |
116 | ||
f3a91bac | 117 | private getWorkerNodeVirtualTaskEndTimestamp ( |
b0d6ed8f | 118 | workerNodeKey: number, |
f3a91bac | 119 | workerNodeVirtualTaskStartTimestamp: number |
b0d6ed8f | 120 | ): number { |
e0843544 JB |
121 | const workerNodeTaskExecutionTime = |
122 | this.getWorkerNodeTaskWaitTime(workerNodeKey) + | |
39618ede | 123 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
e0843544 | 124 | (this.opts!.measurement === Measurements.elu |
f3a91bac | 125 | ? this.getWorkerNodeTaskElu(workerNodeKey) |
e0843544 JB |
126 | : this.getWorkerNodeTaskRunTime(workerNodeKey)) |
127 | return workerNodeVirtualTaskStartTimestamp + workerNodeTaskExecutionTime | |
b0d6ed8f JB |
128 | } |
129 | ||
f3a91bac JB |
130 | private getWorkerNodeVirtualTaskStartTimestamp ( |
131 | workerNodeKey: number | |
132 | ): number { | |
f201a0cd JB |
133 | const virtualTaskEndTimestamp = |
134 | this.pool.workerNodes[workerNodeKey]?.strategyData | |
135 | ?.virtualTaskEndTimestamp | |
f3a91bac | 136 | const now = performance.now() |
80115618 | 137 | return now < (virtualTaskEndTimestamp ?? Number.NEGATIVE_INFINITY) |
67f3f2d6 JB |
138 | ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
139 | virtualTaskEndTimestamp! | |
f3a91bac | 140 | : now |
23ff945a JB |
141 | } |
142 | } |