| 1 | import { cpus } from 'os' |
| 2 | import type { IPoolInternal } from '../pool-internal' |
| 3 | import type { IPoolWorker } from '../pool-worker' |
| 4 | import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' |
| 5 | import type { RequiredStatistics } from './selection-strategies-types' |
| 6 | |
| 7 | /** |
| 8 | * Virtual task runtime. |
| 9 | */ |
| 10 | type TaskRunTime = { |
| 11 | weight: number |
| 12 | runTime: number |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Selects the next worker with a weighted round robin scheduling algorithm. |
| 17 | * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin. |
| 18 | * |
| 19 | * @template Worker Type of worker which manages the strategy. |
| 20 | * @template Data Type of data sent to the worker. This can only be serializable data. |
| 21 | * @template Response Type of response of execution. This can only be serializable data. |
| 22 | */ |
| 23 | export class WeightedRoundRobinWorkerChoiceStrategy< |
| 24 | Worker extends IPoolWorker, |
| 25 | Data, |
| 26 | Response |
| 27 | > extends AbstractWorkerChoiceStrategy<Worker, Data, Response> { |
| 28 | /** @inheritDoc */ |
| 29 | public readonly requiredStatistics: RequiredStatistics = { |
| 30 | runTime: true |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Worker index where the current task will be submitted. |
| 35 | */ |
| 36 | private currentWorkerIndex: number = 0 |
| 37 | /** |
| 38 | * Default worker weight. |
| 39 | */ |
| 40 | private readonly defaultWorkerWeight: number |
| 41 | /** |
| 42 | * Per worker virtual task runtime map. |
| 43 | */ |
| 44 | private readonly workersTaskRunTime: Map<Worker, TaskRunTime> = new Map< |
| 45 | Worker, |
| 46 | TaskRunTime |
| 47 | >() |
| 48 | |
| 49 | /** |
| 50 | * Constructs a worker choice strategy that selects with a weighted round robin scheduling algorithm. |
| 51 | * |
| 52 | * @param pool The pool instance. |
| 53 | */ |
| 54 | public constructor (pool: IPoolInternal<Worker, Data, Response>) { |
| 55 | super(pool) |
| 56 | this.defaultWorkerWeight = this.computeWorkerWeight() |
| 57 | this.initWorkersTaskRunTime() |
| 58 | } |
| 59 | |
| 60 | /** @inheritDoc */ |
| 61 | public reset (): boolean { |
| 62 | this.currentWorkerIndex = 0 |
| 63 | this.workersTaskRunTime.clear() |
| 64 | this.initWorkersTaskRunTime() |
| 65 | return true |
| 66 | } |
| 67 | |
| 68 | /** @inheritDoc */ |
| 69 | public choose (): Worker { |
| 70 | const chosenWorker = this.pool.workers[this.currentWorkerIndex] |
| 71 | if ( |
| 72 | this.isDynamicPool === true && |
| 73 | this.workersTaskRunTime.has(chosenWorker) === false |
| 74 | ) { |
| 75 | this.initWorkerTaskRunTime(chosenWorker) |
| 76 | } |
| 77 | const workerTaskRunTime = |
| 78 | this.workersTaskRunTime.get(chosenWorker)?.runTime ?? 0 |
| 79 | const workerTaskWeight = |
| 80 | this.workersTaskRunTime.get(chosenWorker)?.weight ?? |
| 81 | this.defaultWorkerWeight |
| 82 | if (workerTaskRunTime < workerTaskWeight) { |
| 83 | this.setWorkerTaskRunTime( |
| 84 | chosenWorker, |
| 85 | workerTaskWeight, |
| 86 | workerTaskRunTime + |
| 87 | (this.getWorkerVirtualTaskRunTime(chosenWorker) ?? 0) |
| 88 | ) |
| 89 | } else { |
| 90 | this.currentWorkerIndex = |
| 91 | this.currentWorkerIndex === this.pool.workers.length - 1 |
| 92 | ? 0 |
| 93 | : this.currentWorkerIndex + 1 |
| 94 | this.setWorkerTaskRunTime(chosenWorker, workerTaskWeight, 0) |
| 95 | } |
| 96 | return chosenWorker |
| 97 | } |
| 98 | |
| 99 | private initWorkersTaskRunTime (): void { |
| 100 | for (const worker of this.pool.workers) { |
| 101 | this.initWorkerTaskRunTime(worker) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private initWorkerTaskRunTime (worker: Worker): void { |
| 106 | this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0) |
| 107 | } |
| 108 | |
| 109 | private setWorkerTaskRunTime ( |
| 110 | worker: Worker, |
| 111 | weight: number, |
| 112 | runTime: number |
| 113 | ): void { |
| 114 | this.workersTaskRunTime.set(worker, { |
| 115 | weight, |
| 116 | runTime |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | private getWorkerVirtualTaskRunTime (worker: Worker): number | undefined { |
| 121 | return this.pool.getWorkerAverageTasksRunTime(worker) |
| 122 | } |
| 123 | |
| 124 | private computeWorkerWeight (): number { |
| 125 | let cpusCycleTimeWeight = 0 |
| 126 | for (const cpu of cpus()) { |
| 127 | // CPU estimated cycle time |
| 128 | const numberOfDigits = cpu.speed.toString().length - 1 |
| 129 | const cpuCycleTime = 1 / (cpu.speed / Math.pow(10, numberOfDigits)) |
| 130 | cpusCycleTimeWeight += cpuCycleTime * Math.pow(10, numberOfDigits) |
| 131 | } |
| 132 | return Math.round(cpusCycleTimeWeight / cpus().length) |
| 133 | } |
| 134 | } |