]> Piment Noir Git Repositories - poolifier.git/blob - src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
769a5c02b238d8301b8a3b65d32d25c0c1d7062f
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
1 import type { IPool } from '../pool.js'
2 import type { IWorker } from '../worker.js'
3 import type {
4 IWorkerChoiceStrategy,
5 TaskStatisticsRequirements,
6 WorkerChoiceStrategyOptions,
7 } from './selection-strategies-types.js'
8
9 import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
10 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
11
12 /**
13 * Selects the next worker with a weighted round robin scheduling algorithm.
14 * Loosely modeled after the weighted round robin queueing algorithm: https://en.wikipedia.org/wiki/Weighted_round_robin.
15 * @typeParam Worker - Type of worker which manages the strategy.
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.
18 */
19 export class WeightedRoundRobinWorkerChoiceStrategy<
20 Worker extends IWorker,
21 Data = unknown,
22 Response = unknown
23 >
24 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
25 implements IWorkerChoiceStrategy {
26 /** @inheritDoc */
27 public override readonly taskStatisticsRequirements: TaskStatisticsRequirements =
28 Object.freeze({
29 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
30 runTime: {
31 aggregate: true,
32 average: true,
33 median: false,
34 },
35 waitTime: {
36 aggregate: true,
37 average: true,
38 median: false,
39 },
40 })
41
42 /**
43 * Worker node virtual execution time.
44 */
45 private workerNodeVirtualTaskExecutionTime = 0
46
47 /** @inheritDoc */
48 public constructor (
49 pool: IPool<Worker, Data, Response>,
50 opts?: WorkerChoiceStrategyOptions
51 ) {
52 super(pool, opts)
53 this.setTaskStatisticsRequirements(this.opts)
54 }
55
56 /** @inheritDoc */
57 public choose (): number | undefined {
58 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
59 this.weightedRoundRobinNextWorkerNodeKey()
60 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
61 if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) {
62 return undefined
63 }
64 return this.checkWorkerNodeKey(this.nextWorkerNodeKey)
65 }
66
67 /** @inheritDoc */
68 public remove (workerNodeKey: number): boolean {
69 if (this.pool.workerNodes.length === 0) {
70 return this.reset()
71 }
72 if (this.nextWorkerNodeKey === workerNodeKey) {
73 this.workerNodeVirtualTaskExecutionTime = 0
74 }
75 if (
76 this.nextWorkerNodeKey != null &&
77 this.nextWorkerNodeKey >= workerNodeKey
78 ) {
79 this.nextWorkerNodeKey =
80 (this.nextWorkerNodeKey - 1 + this.pool.workerNodes.length) %
81 this.pool.workerNodes.length
82 if (this.previousWorkerNodeKey >= workerNodeKey) {
83 this.previousWorkerNodeKey = this.nextWorkerNodeKey
84 }
85 }
86 return true
87 }
88
89 /** @inheritDoc */
90 public reset (): boolean {
91 this.resetWorkerNodeKeyProperties()
92 this.workerNodeVirtualTaskExecutionTime = 0
93 return true
94 }
95
96 /** @inheritDoc */
97 public update (): boolean {
98 return true
99 }
100
101 private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
102 const workerNodeKey = this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
103 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
104 const workerWeight = this.opts!.weights![workerNodeKey]
105 if (this.workerNodeVirtualTaskExecutionTime < workerWeight) {
106 this.workerNodeVirtualTaskExecutionTime +=
107 this.getWorkerNodeTaskWaitTime(workerNodeKey) +
108 this.getWorkerNodeTaskRunTime(workerNodeKey)
109 } else {
110 this.nextWorkerNodeKey =
111 this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
112 ? 0
113 : workerNodeKey + 1
114 this.workerNodeVirtualTaskExecutionTime = 0
115 }
116 return this.nextWorkerNodeKey
117 }
118 }