Merge pull request #2365 from poolifier/map-execute
[poolifier.git] / src / pools / selection-strategies / least-busy-worker-choice-strategy.ts
CommitLineData
d35e5717 1import type { IPool } from '../pool.js'
e9ed6eee 2import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../utils.js'
d35e5717
JB
3import type { IWorker } from '../worker.js'
4import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
bf90656c
JB
5import type {
6 IWorkerChoiceStrategy,
39618ede 7 TaskStatisticsRequirements,
3a502712 8 WorkerChoiceStrategyOptions,
d35e5717 9} from './selection-strategies-types.js'
168c526f
JB
10
11/**
e4543b14 12 * Selects the least busy worker.
168c526f 13 * @typeParam Worker - Type of worker which manages the strategy.
e102732c
JB
14 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
15 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
168c526f 16 */
e4543b14 17export class LeastBusyWorkerChoiceStrategy<
f06e48d8 18 Worker extends IWorker,
b2b1d84e
JB
19 Data = unknown,
20 Response = unknown
bf90656c
JB
21 >
22 extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
17393ac8 23 implements IWorkerChoiceStrategy {
afc003b2 24 /** @inheritDoc */
87de9ff5 25 public readonly taskStatisticsRequirements: TaskStatisticsRequirements = {
932fc8be
JB
26 runTime: {
27 aggregate: true,
28 average: false,
3a502712 29 median: false,
932fc8be
JB
30 },
31 waitTime: {
32 aggregate: true,
33 average: false,
3a502712 34 median: false,
932fc8be 35 },
3a502712 36 elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
168c526f
JB
37 }
38
2fc5cae3
JB
39 /** @inheritDoc */
40 public constructor (
41 pool: IPool<Worker, Data, Response>,
39618ede 42 opts?: WorkerChoiceStrategyOptions
2fc5cae3
JB
43 ) {
44 super(pool, opts)
932fc8be 45 this.setTaskStatisticsRequirements(this.opts)
2fc5cae3
JB
46 }
47
afc003b2 48 /** @inheritDoc */
168c526f
JB
49 public reset (): boolean {
50 return true
51 }
52
138d29a8
JB
53 /** @inheritDoc */
54 public update (): boolean {
db703c75
JB
55 return true
56 }
57
58 /** @inheritDoc */
b1aae695 59 public choose (): number | undefined {
baca80f7 60 this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
fce028d6 61 this.nextWorkerNodeKey = this.leastBusyNextWorkerNodeKey()
b1aae695 62 return this.nextWorkerNodeKey
9b106837
JB
63 }
64
65 /** @inheritDoc */
66 public remove (): boolean {
67 return true
68 }
69
b1aae695 70 private leastBusyNextWorkerNodeKey (): number | undefined {
f3a91bac
JB
71 return this.pool.workerNodes.reduce(
72 (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => {
ae3ab61d 73 return this.isWorkerNodeReady(workerNodeKey) &&
e0843544
JB
74 (workerNode.usage.waitTime.aggregate ?? 0) +
75 (workerNode.usage.runTime.aggregate ?? 0) <
76 (workerNodes[minWorkerNodeKey].usage.waitTime.aggregate ?? 0) +
77 (workerNodes[minWorkerNodeKey].usage.runTime.aggregate ?? 0)
f3a91bac
JB
78 ? workerNodeKey
79 : minWorkerNodeKey
80 },
81 0
82 )
97a2abc3 83 }
168c526f 84}