perf: use a single map to store pool workers and their related data
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
CommitLineData
ea7a90d3 1import type { IPoolWorker } from '../pool-worker'
23ff945a 2import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
10fcfaf4 3import type { RequiredStatistics } from './selection-strategies-types'
23ff945a
JB
4
5/**
6 * Worker virtual task timestamp.
7 */
78cea37e 8interface WorkerVirtualTaskTimestamp {
23ff945a
JB
9 start: number
10 end: number
11}
12
13/**
14 * Selects the next worker with a fair share scheduling algorithm.
15 * Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
16 *
38e795c1
JB
17 * @typeParam Worker - Type of worker which manages the strategy.
18 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
19 * @typeParam Response - Type of response of execution. This can only be serializable data.
23ff945a
JB
20 */
21export class FairShareWorkerChoiceStrategy<
ea7a90d3 22 Worker extends IPoolWorker,
23ff945a
JB
23 Data,
24 Response
25> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
38e795c1 26 /** {@inheritDoc} */
ea7a90d3 27 public readonly requiredStatistics: RequiredStatistics = {
10fcfaf4
JB
28 runTime: true
29 }
30
23ff945a
JB
31 /**
32 * Worker last virtual task execution timestamp.
33 */
ea7a90d3 34 private readonly workerLastVirtualTaskTimestamp: Map<
78cea37e
JB
35 Worker,
36 WorkerVirtualTaskTimestamp
23ff945a
JB
37 > = new Map<Worker, WorkerVirtualTaskTimestamp>()
38
38e795c1 39 /** {@inheritDoc} */
a6f7f1b4 40 public reset (): boolean {
ea7a90d3
JB
41 this.workerLastVirtualTaskTimestamp.clear()
42 return true
43 }
44
38e795c1 45 /** {@inheritDoc} */
23ff945a 46 public choose (): Worker {
23ff945a
JB
47 let minWorkerVirtualTaskEndTimestamp = Infinity
48 let chosenWorker!: Worker
ffcbbad8
JB
49 for (const value of this.pool.workers.values()) {
50 const worker = value.worker
11df3590 51 this.computeWorkerLastVirtualTaskTimestamp(worker)
23ff945a
JB
52 const workerLastVirtualTaskEndTimestamp =
53 this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
54 if (
55 workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
56 ) {
57 minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp
58 chosenWorker = worker
59 }
60 }
61 return chosenWorker
62 }
63
64 /**
11df3590
JB
65 * Computes worker last virtual task timestamp.
66 *
38e795c1 67 * @param worker - The worker.
23ff945a 68 */
11df3590
JB
69 private computeWorkerLastVirtualTaskTimestamp (worker: Worker): void {
70 const workerVirtualTaskStartTimestamp = Math.max(
71 Date.now(),
72 this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? -Infinity
73 )
11df3590
JB
74 this.workerLastVirtualTaskTimestamp.set(worker, {
75 start: workerVirtualTaskStartTimestamp,
61d849ff
JB
76 end:
77 workerVirtualTaskStartTimestamp +
78 (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
11df3590 79 })
23ff945a
JB
80 }
81}