Skip to content

Commit 2c17456

Browse files
committed
refactor: cleanup worker selection strategies code
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent bd5f61e commit 2c17456

8 files changed

+16
-11
lines changed

src/pools/selection-strategies/abstract-worker-choice-strategy.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export abstract class AbstractWorkerChoiceStrategy<
4747
/** @inheritDoc */
4848
public readonly taskStatisticsRequirements: TaskStatisticsRequirements =
4949
Object.freeze({
50-
runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
51-
waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
52-
elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
50+
runTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
51+
waitTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
52+
elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
5353
})
5454

5555
/**
@@ -186,7 +186,9 @@ export abstract class AbstractWorkerChoiceStrategy<
186186
* @param workerNodeKey - The worker node key.
187187
*/
188188
protected setPreviousWorkerNodeKey(workerNodeKey: number | undefined): void {
189-
this.previousWorkerNodeKey = workerNodeKey != null && workerNodeKey >= 0
189+
this.previousWorkerNodeKey = workerNodeKey != null &&
190+
workerNodeKey >= 0 &&
191+
workerNodeKey < this.pool.workerNodes.length
190192
? workerNodeKey
191193
: this.previousWorkerNodeKey
192194
}

src/pools/selection-strategies/fair-share-worker-choice-strategy.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export class FairShareWorkerChoiceStrategy<
6868
/** @inheritDoc */
6969
public update(workerNodeKey: number): boolean {
7070
this.pool.workerNodes[workerNodeKey].strategyData = {
71+
...this.pool.workerNodes[workerNodeKey].strategyData,
7172
virtualTaskEndTimestamp: this.computeWorkerNodeVirtualTaskEndTimestamp(
7273
workerNodeKey,
7374
),
@@ -103,13 +104,15 @@ export class FairShareWorkerChoiceStrategy<
103104
}
104105
if (minWorkerNodeKey === -1) {
105106
workerNode.strategyData = {
107+
...workerNode.strategyData,
106108
virtualTaskEndTimestamp: this
107109
.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey),
108110
}
109111
return workerNodeKey
110112
}
111113
if (workerNode.strategyData?.virtualTaskEndTimestamp == null) {
112114
workerNode.strategyData = {
115+
...workerNode.strategyData,
113116
virtualTaskEndTimestamp: this
114117
.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey),
115118
}

src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
4040
average: true,
4141
median: false,
4242
},
43-
elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
43+
elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
4444
})
4545

4646
/**

src/pools/selection-strategies/least-busy-worker-choice-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class LeastBusyWorkerChoiceStrategy<
3939
average: false,
4040
median: false,
4141
},
42-
elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
42+
elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
4343
})
4444

4545
/** @inheritDoc */

src/pools/selection-strategies/least-elu-worker-choice-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class LeastEluWorkerChoiceStrategy<
2929
/** @inheritDoc */
3030
public override readonly taskStatisticsRequirements:
3131
TaskStatisticsRequirements = Object.freeze({
32-
runTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
32+
runTime: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
3333
waitTime: {
3434
aggregate: true,
3535
average: false,

src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
4141
average: true,
4242
median: false,
4343
},
44-
elu: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
44+
elu: { ...DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS },
4545
})
4646

4747
/**

src/pools/selection-strategies/worker-choice-strategies-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class WorkerChoiceStrategiesContext<
179179
}
180180
if (workerNodeKey == null) {
181181
throw new Error(
182-
`Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${retriesCount.toString()} retries`,
182+
`Worker node key chosen by ${workerChoiceStrategy.name} is null or undefined after ${retriesCount.toString()} retries (max: ${this.retries.toString()})`,
183183
)
184184
}
185185
return workerNodeKey

tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Worker choice strategies context test suite', () => {
9393
)
9494
expect(() => workerChoiceStrategiesContext.execute()).toThrow(
9595
new Error(
96-
`Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries} retries`,
96+
`Worker node key chosen by ${workerChoiceStrategyUndefinedStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`,
9797
),
9898
)
9999
workerChoiceStrategyUndefinedStub.choose.restore()
@@ -111,7 +111,7 @@ describe('Worker choice strategies context test suite', () => {
111111
)
112112
expect(() => workerChoiceStrategiesContext.execute()).toThrow(
113113
new Error(
114-
`Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries} retries`,
114+
`Worker node key chosen by ${workerChoiceStrategyNullStub.name} is null or undefined after ${workerChoiceStrategiesContext.retries.toString()} retries (max: ${workerChoiceStrategiesContext.retries.toString()})`,
115115
),
116116
)
117117
workerChoiceStrategyNullStub.choose.restore()

0 commit comments

Comments
 (0)