## [Unreleased]
+### Fixed
+
+- Fix fair share worker choice strategy internals update: ensure virtual task end timestamp is computed at task submission.
+
## [2.4.12] - 2023-05-06
### Added
} else {
this.executeTask(workerNodeKey, submittedTask)
}
+ this.workerChoiceStrategyContext.update(workerNodeKey)
this.checkAndEmitEvents()
// eslint-disable-next-line @typescript-eslint/return-await
return res
workerTasksUsage.medRunTime = median(workerTasksUsage.runTimeHistory)
}
}
- this.workerChoiceStrategyContext.update(workerNodeKey)
}
/**
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
-/**
- * Worker virtual task timestamp.
- */
-interface WorkerVirtualTaskTimestamp {
- start: number
- end?: number
-}
-
/**
* Selects the next worker with a fair share scheduling algorithm.
* Loosely modeled after the fair queueing algorithm: https://en.wikipedia.org/wiki/Fair_queuing.
}
/**
- * Workers' virtual task execution timestamp.
+ * Workers' virtual task end execution timestamp.
*/
- private workersVirtualTaskTimestamp: WorkerVirtualTaskTimestamp[] = []
+ private workersVirtualTaskEndTimestamp: number[] = []
/** @inheritDoc */
public constructor (
/** @inheritDoc */
public reset (): boolean {
- this.workersVirtualTaskTimestamp = []
+ this.workersVirtualTaskEndTimestamp = []
return true
}
/** @inheritDoc */
public update (workerNodeKey: number): boolean {
- this.computeWorkerVirtualTaskTimestamp(workerNodeKey)
+ this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
return true
}
let minWorkerVirtualTaskEndTimestamp = Infinity
let chosenWorkerNodeKey!: number
for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
+ if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
+ this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
+ }
const workerVirtualTaskEndTimestamp =
- this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0
+ this.workersVirtualTaskEndTimestamp[workerNodeKey]
if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
chosenWorkerNodeKey = workerNodeKey
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
- this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1)
+ this.workersVirtualTaskEndTimestamp.splice(workerNodeKey, 1)
return true
}
/**
- * Computes worker virtual task timestamp.
+ * Computes the worker node key virtual task end timestamp.
*
* @param workerNodeKey - The worker node key.
*/
- private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void {
- const workerVirtualTaskStartTimestamp = Math.max(
- performance.now(),
- this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity
- )
- const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime
+ private computeWorkerVirtualTaskEndTimestamp (workerNodeKey: number): void {
+ this.workersVirtualTaskEndTimestamp[workerNodeKey] =
+ this.getWorkerVirtualTaskEndTimestamp(
+ workerNodeKey,
+ this.getWorkerVirtualTaskStartTimestamp(workerNodeKey)
+ )
+ }
+
+ private getWorkerVirtualTaskEndTimestamp (
+ workerNodeKey: number,
+ workerVirtualTaskStartTimestamp: number
+ ): number {
+ const workerVirtualTaskRunTime = this.requiredStatistics.medRunTime
? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
: this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
- this.workersVirtualTaskTimestamp[workerNodeKey] = {
- start: workerVirtualTaskStartTimestamp,
- end: workerVirtualTaskStartTimestamp + workerVirtualTaskTRunTime
- }
+ return workerVirtualTaskStartTimestamp + workerVirtualTaskRunTime
+ }
+
+ private getWorkerVirtualTaskStartTimestamp (workerNodeKey: number): number {
+ return Math.max(
+ performance.now(),
+ this.workersVirtualTaskEndTimestamp[workerNodeKey] ?? -Infinity
+ )
}
}
* Executes the worker choice strategy algorithm in the context.
*
* @returns The key of the worker node.
+ * @throws {@link Error} If the worker node key is null or undefined.
*/
public execute (): number {
- return (
+ const workerNodeKey = (
this.workerChoiceStrategies.get(
this.workerChoiceStrategy
) as IWorkerChoiceStrategy
).choose()
+ if (workerNodeKey == null) {
+ throw new Error('Worker node key chosen is null or undefined')
+ }
+ return workerNodeKey
}
/**
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp
+ ).workersVirtualTaskEndTimestamp
).toBeInstanceOf(Array)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(0)
} else if (
workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(pool.workerNodes.length)
// We need to clean up the resources after our test
await pool.destroy()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(pool.workerNodes.length)
// We need to clean up the resources after our test
await pool.destroy()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(pool.workerNodes.length)
// We need to clean up the resources after our test
await pool.destroy()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp
+ ).workersVirtualTaskEndTimestamp
).toBeInstanceOf(Array)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(0)
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp[0] = 0
+ ).workersVirtualTaskEndTimestamp[0] = performance.now()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(1)
pool.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp
+ ).workersVirtualTaskEndTimestamp
).toBeInstanceOf(Array)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(0)
await pool.destroy()
pool = new DynamicThreadPool(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp
+ ).workersVirtualTaskEndTimestamp
).toBeInstanceOf(Array)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(0)
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp[0] = 0
+ ).workersVirtualTaskEndTimestamp[0] = performance.now()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(1)
pool.setWorkerChoiceStrategy(workerChoiceStrategy)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp
+ ).workersVirtualTaskEndTimestamp
).toBeInstanceOf(Array)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).workersVirtualTaskTimestamp.length
+ ).workersVirtualTaskEndTimestamp.length
).toBe(0)
// We need to clean up the resources after our test
await pool.destroy()