From: Jérôme Benoit Date: Tue, 13 Aug 2024 16:42:09 +0000 (+0200) Subject: refactor: cleanup worker condition checks at task stealing X-Git-Tag: v4.2.0~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d52d4773cc0ed2936aea894224a7edc3dab8359a;p=poolifier.git refactor: cleanup worker condition checks at task stealing Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 4c387477..29dd42db 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1894,6 +1894,42 @@ export abstract class AbstractPool< } } + private readonly stealTask = ( + sourceWorkerNode: IWorkerNode, + destinationWorkerNodeKey: number + ): Task | undefined => { + const destinationWorkerInfo = this.getWorkerInfo(destinationWorkerNodeKey) + if (destinationWorkerInfo == null) { + throw new Error( + `Worker node with key '${destinationWorkerNodeKey.toString()}' not found in pool` + ) + } + // Avoid cross task stealing. Could be smarter by checking stealing/stolen worker ids pair. + if ( + !sourceWorkerNode.info.ready || + sourceWorkerNode.info.stolen || + sourceWorkerNode.info.stealing || + !destinationWorkerInfo.ready || + destinationWorkerInfo.stolen || + destinationWorkerInfo.stealing + ) { + return + } + destinationWorkerInfo.stealing = true + sourceWorkerNode.info.stolen = true + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const task = sourceWorkerNode.dequeueLastPrioritizedTask()! + sourceWorkerNode.info.stolen = false + destinationWorkerInfo.stealing = false + this.handleTask(destinationWorkerNodeKey, task) + this.updateTaskStolenStatisticsWorkerUsage( + destinationWorkerNodeKey, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + task.name! + ) + return task + } + private readonly handleWorkerNodeIdleEvent = ( eventDetail: WorkerNodeEventDetail, previousStolenTask?: Task @@ -1904,12 +1940,6 @@ export abstract class AbstractPool< "WorkerNode event detail 'workerNodeKey' property must be defined" ) } - const workerInfo = this.getWorkerInfo(workerNodeKey) - if (workerInfo == null) { - throw new Error( - `Worker node with key '${workerNodeKey.toString()}' not found in pool` - ) - } if ( this.cannotStealTask() || (this.info.stealingWorkerNodes ?? 0) > @@ -1960,31 +1990,6 @@ export abstract class AbstractPool< }) } - private readonly stealTask = ( - sourceWorkerNode: IWorkerNode, - destinationWorkerNodeKey: number - ): Task | undefined => { - const destinationWorkerInfo = this.getWorkerInfo(destinationWorkerNodeKey) - if (destinationWorkerInfo == null) { - throw new Error( - `Worker node with key '${destinationWorkerNodeKey.toString()}' not found in pool` - ) - } - destinationWorkerInfo.stealing = true - sourceWorkerNode.info.stolen = true - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const task = sourceWorkerNode.dequeueLastPrioritizedTask()! - sourceWorkerNode.info.stolen = false - destinationWorkerInfo.stealing = false - this.handleTask(destinationWorkerNodeKey, task) - this.updateTaskStolenStatisticsWorkerUsage( - destinationWorkerNodeKey, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - task.name! - ) - return task - } - private readonly workerNodeStealTask = ( workerNodeKey: number ): Task | undefined => { @@ -1996,9 +2001,6 @@ export abstract class AbstractPool< ) const sourceWorkerNode = workerNodes.find( (sourceWorkerNode, sourceWorkerNodeKey) => - sourceWorkerNode.info.ready && - !sourceWorkerNode.info.stolen && - !sourceWorkerNode.info.stealing && sourceWorkerNodeKey !== workerNodeKey && sourceWorkerNode.usage.tasks.queued > 0 ) @@ -2039,9 +2041,6 @@ export abstract class AbstractPool< for (const [workerNodeKey, workerNode] of workerNodes.entries()) { if ( sourceWorkerNode.usage.tasks.queued > 0 && - workerNode.info.ready && - !workerNode.info.stolen && - !workerNode.info.stealing && workerNode.info.id !== workerId && workerNode.usage.tasks.queued < // eslint-disable-next-line @typescript-eslint/no-non-null-assertion