X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Ffair-share-worker-choice-strategy.ts;h=a337278ba9ce75a074c1b32d751d8f928ec86496;hb=daa30e0e21087d1a3cf24b3dd38f703b1aba6d46;hp=e42d54d9fbabbe847731fd7b0387d9bb8a94f642;hpb=08f3f44cef6256fdbab1a2a56842b291fd6dcd42;p=poolifier.git diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index e42d54d9..a337278b 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -1,28 +1,24 @@ -import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils' +import { + DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS +} from '../../utils' import type { IPool } from '../pool' -import type { IWorker } from '../worker' +import type { IWorker, StrategyData } from '../worker' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy' -import type { - IWorkerChoiceStrategy, - RequiredStatistics, - WorkerChoiceStrategyOptions +import { + type IWorkerChoiceStrategy, + Measurements, + type TaskStatisticsRequirements, + type 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. * * @typeParam Worker - Type of worker which manages the strategy. - * @typeParam Data - Type of data sent to the worker. This can only be serializable data. - * @typeParam Response - Type of execution response. This can only be serializable data. + * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. + * @typeParam Response - Type of execution response. This can only be structured-cloneable data. */ export class FairShareWorkerChoiceStrategy< Worker extends IWorker, @@ -32,72 +28,112 @@ export class FairShareWorkerChoiceStrategy< extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { /** @inheritDoc */ - public readonly requiredStatistics: RequiredStatistics = { - runTime: true, - avgRunTime: true, - medRunTime: false + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { + runTime: { + aggregate: true, + average: true, + median: false + }, + waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS, + elu: { + aggregate: true, + average: true, + median: false + } } - /** - * Workers' virtual task execution timestamp. - */ - private workersVirtualTaskTimestamp: WorkerVirtualTaskTimestamp[] = [] - /** @inheritDoc */ public constructor ( pool: IPool, opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { super(pool, opts) - this.checkOptions(this.opts) + this.setTaskStatisticsRequirements(this.opts) } /** @inheritDoc */ public reset (): boolean { - this.workersVirtualTaskTimestamp = [] + for (const workerNode of this.pool.workerNodes) { + delete workerNode.strategyData?.virtualTaskEndTimestamp + } return true } /** @inheritDoc */ - public choose (): number { - let minWorkerVirtualTaskEndTimestamp = Infinity - let chosenWorkerNodeKey!: number - for (const [workerNodeKey] of this.pool.workerNodes.entries()) { - this.computeWorkerVirtualTaskTimestamp(workerNodeKey) - const workerLastVirtualTaskEndTimestamp = - this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? 0 - if ( - workerLastVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp - ) { - minWorkerVirtualTaskEndTimestamp = workerLastVirtualTaskEndTimestamp - chosenWorkerNodeKey = workerNodeKey - } + public update (workerNodeKey: number): boolean { + this.pool.workerNodes[workerNodeKey].strategyData = { + virtualTaskEndTimestamp: + this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey) } - return chosenWorkerNodeKey + return true } /** @inheritDoc */ - public remove (workerNodeKey: number): boolean { - this.workersVirtualTaskTimestamp.splice(workerNodeKey, 1) + public choose (): number | undefined { + this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey) + this.nextWorkerNodeKey = this.fairShareNextWorkerNodeKey() + return this.nextWorkerNodeKey + } + + /** @inheritDoc */ + public remove (): boolean { return true } + private fairShareNextWorkerNodeKey (): number | undefined { + return this.pool.workerNodes.reduce( + (minWorkerNodeKey, workerNode, workerNodeKey, workerNodes) => { + if (workerNode.strategyData?.virtualTaskEndTimestamp == null) { + workerNode.strategyData = { + virtualTaskEndTimestamp: + this.computeWorkerNodeVirtualTaskEndTimestamp(workerNodeKey) + } + } + return (workerNode.strategyData.virtualTaskEndTimestamp as number) < + ((workerNodes[minWorkerNodeKey].strategyData as StrategyData) + .virtualTaskEndTimestamp as number) + ? workerNodeKey + : minWorkerNodeKey + }, + 0 + ) + } + /** - * Computes worker virtual task timestamp. + * Computes the worker node key virtual task end timestamp. * * @param workerNodeKey - The worker node key. + * @returns The worker node key virtual task end timestamp. */ - private computeWorkerVirtualTaskTimestamp (workerNodeKey: number): void { - const workerVirtualTaskStartTimestamp = Math.max( - performance.now(), - this.workersVirtualTaskTimestamp[workerNodeKey]?.end ?? -Infinity + private computeWorkerNodeVirtualTaskEndTimestamp ( + workerNodeKey: number + ): number { + return this.getWorkerNodeVirtualTaskEndTimestamp( + workerNodeKey, + this.getWorkerNodeVirtualTaskStartTimestamp(workerNodeKey) ) - const workerVirtualTaskTRunTime = this.requiredStatistics.medRunTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime - this.workersVirtualTaskTimestamp[workerNodeKey] = { - start: workerVirtualTaskStartTimestamp, - end: workerVirtualTaskStartTimestamp + (workerVirtualTaskTRunTime ?? 0) - } + } + + private getWorkerNodeVirtualTaskEndTimestamp ( + workerNodeKey: number, + workerNodeVirtualTaskStartTimestamp: number + ): number { + const workerNodeTaskRunTime = + this.opts.measurement === Measurements.elu + ? this.getWorkerNodeTaskElu(workerNodeKey) + : this.getWorkerNodeTaskRunTime(workerNodeKey) + return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime + } + + private getWorkerNodeVirtualTaskStartTimestamp ( + workerNodeKey: number + ): number { + const virtualTaskEndTimestamp = + this.pool.workerNodes[workerNodeKey]?.strategyData + ?.virtualTaskEndTimestamp + const now = performance.now() + return now < (virtualTaskEndTimestamp ?? -Infinity) + ? (virtualTaskEndTimestamp as number) + : now } }