fix: prepare code to fix pool internal IPC for cluster worker
[poolifier.git] / src / pools / selection-strategies / weighted-round-robin-worker-choice-strategy.ts
CommitLineData
f06e48d8 1import type { IWorker } from '../worker'
65d7a1c9
JB
2import type { IPool } from '../pool'
3import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
b3432a63 4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
bf90656c
JB
5import type {
6 IWorkerChoiceStrategy,
6c6afb84 7 StrategyPolicy,
87de9ff5 8 TaskStatisticsRequirements,
da309861 9 WorkerChoiceStrategyOptions
bf90656c 10} from './selection-strategies-types'
b3432a63 11
b3432a63
JB
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 *
38e795c1 16 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
17 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
18 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
b3432a63
JB
19 */
20export class WeightedRoundRobinWorkerChoiceStrategy<
f06e48d8 21 Worker extends IWorker,
b2b1d84e
JB
22 Data = unknown,
23 Response = unknown
bf90656c
JB
24 >
25 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 26 implements IWorkerChoiceStrategy {
6c6afb84
JB
27 /** @inheritDoc */
28 public readonly strategyPolicy: StrategyPolicy = {
29 useDynamicWorker: true
30 }
31
afc003b2 32 /** @inheritDoc */
87de9ff5 33 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
34 runTime: {
35 aggregate: true,
36 average: true,
37 median: false
38 },
39 waitTime: {
40 aggregate: false,
41 average: false,
42 median: false
43 },
5df69fab
JB
44 elu: {
45 aggregate: false,
46 average: false,
47 median: false
48 }
10fcfaf4
JB
49 }
50
b3432a63
JB
51 /**
52 * Default worker weight.
53 */
777af0ac 54 private readonly defaultWorkerWeight: number
b3432a63 55 /**
08f3f44c 56 * Worker virtual task runtime.
b3432a63 57 */
08f3f44c 58 private workerVirtualTaskRunTime: number = 0
b3432a63 59
2fc5cae3 60 /** @inheritDoc */
da309861 61 public constructor (
c4855468 62 pool: IPool<Worker, Data, Response>,
2fc5cae3 63 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
da309861
JB
64 ) {
65 super(pool, opts)
932fc8be 66 this.setTaskStatisticsRequirements(this.opts)
08f3f44c 67 this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
b3432a63
JB
68 }
69
afc003b2 70 /** @inheritDoc */
a6f7f1b4 71 public reset (): boolean {
d33be430 72 this.nextWorkerNodeId = 0
08f3f44c 73 this.workerVirtualTaskRunTime = 0
ea7a90d3
JB
74 return true
75 }
76
138d29a8
JB
77 /** @inheritDoc */
78 public update (): boolean {
79 return true
80 }
81
afc003b2 82 /** @inheritDoc */
c923ce56 83 public choose (): number {
d33be430 84 const chosenWorkerNodeKey = this.nextWorkerNodeId
138d29a8
JB
85 const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
86 const workerWeight =
08f3f44c 87 this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
138d29a8 88 if (workerVirtualTaskRunTime < workerWeight) {
08f3f44c 89 this.workerVirtualTaskRunTime =
0d80593b 90 workerVirtualTaskRunTime +
f6b641d6 91 this.getWorkerTaskRunTime(chosenWorkerNodeKey)
b3432a63 92 } else {
d33be430
JB
93 this.nextWorkerNodeId =
94 this.nextWorkerNodeId === this.pool.workerNodes.length - 1
b3432a63 95 ? 0
d33be430 96 : this.nextWorkerNodeId + 1
08f3f44c 97 this.workerVirtualTaskRunTime = 0
b3432a63 98 }
f06e48d8 99 return chosenWorkerNodeKey
b3432a63
JB
100 }
101
afc003b2 102 /** @inheritDoc */
f06e48d8 103 public remove (workerNodeKey: number): boolean {
d33be430 104 if (this.nextWorkerNodeId === workerNodeKey) {
f06e48d8 105 if (this.pool.workerNodes.length === 0) {
d33be430
JB
106 this.nextWorkerNodeId = 0
107 } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
108 this.nextWorkerNodeId = this.pool.workerNodes.length - 1
78ab2555 109 }
08f3f44c 110 this.workerVirtualTaskRunTime = 0
97a2abc3 111 }
08f3f44c 112 return true
2377984d 113 }
b3432a63 114}