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