fix: fix back pressure detection
[poolifier.git] / src / pools / abstract-pool.ts
index eb47bc2f24f67b983b4829a763705c6fb8b45828..dceef6decb534f083510d90a947a05861c2ec2df 100644 (file)
@@ -28,13 +28,12 @@ import {
   PoolTypes,
   type TasksQueueOptions
 } from './pool'
-import {
-  type IWorker,
-  type IWorkerNode,
-  type WorkerInfo,
-  type WorkerType,
-  WorkerTypes,
-  type WorkerUsage
+import type {
+  IWorker,
+  IWorkerNode,
+  WorkerInfo,
+  WorkerType,
+  WorkerUsage
 } from './worker'
 import {
   type MeasurementStatisticsRequirements,
@@ -107,7 +106,9 @@ export abstract class AbstractPool<
     protected readonly opts: PoolOptions<Worker>
   ) {
     if (!this.isMain()) {
-      throw new Error('Cannot start a pool from a worker!')
+      throw new Error(
+        'Cannot start a pool from a worker with the same type as the pool'
+      )
     }
     this.checkNumberOfWorkers(this.numberOfWorkers)
     this.checkFilePath(this.filePath)
@@ -117,7 +118,10 @@ export abstract class AbstractPool<
     this.executeTask = this.executeTask.bind(this)
     this.enqueueTask = this.enqueueTask.bind(this)
     this.dequeueTask = this.dequeueTask.bind(this)
-    this.checkAndEmitEvents = this.checkAndEmitEvents.bind(this)
+    this.checkAndEmitTaskExecutionEvents =
+      this.checkAndEmitTaskExecutionEvents.bind(this)
+    this.checkAndEmitTaskQueuingEvents =
+      this.checkAndEmitTaskQueuingEvents.bind(this)
 
     if (this.opts.enableEvents === true) {
       this.emitter = new PoolEmitter()
@@ -203,9 +207,10 @@ export abstract class AbstractPool<
       this.opts.workerChoiceStrategy =
         opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
       this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy)
-      this.opts.workerChoiceStrategyOptions =
-        opts.workerChoiceStrategyOptions ??
-        DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+      this.opts.workerChoiceStrategyOptions = {
+        ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
+        ...opts.workerChoiceStrategyOptions
+      }
       this.checkValidWorkerChoiceStrategyOptions(
         this.opts.workerChoiceStrategyOptions
       )
@@ -243,6 +248,22 @@ export abstract class AbstractPool<
         'Invalid worker choice strategy options: must be a plain object'
       )
     }
+    if (
+      workerChoiceStrategyOptions.choiceRetries != null &&
+      !Number.isSafeInteger(workerChoiceStrategyOptions.choiceRetries)
+    ) {
+      throw new TypeError(
+        'Invalid worker choice strategy options: choice retries must be an integer'
+      )
+    }
+    if (
+      workerChoiceStrategyOptions.choiceRetries != null &&
+      workerChoiceStrategyOptions.choiceRetries <= 0
+    ) {
+      throw new RangeError(
+        `Invalid worker choice strategy options: choice retries '${workerChoiceStrategyOptions.choiceRetries}' must be greater than zero`
+      )
+    }
     if (
       workerChoiceStrategyOptions.weights != null &&
       Object.keys(workerChoiceStrategyOptions.weights).length !== this.maxSize
@@ -282,7 +303,7 @@ export abstract class AbstractPool<
       tasksQueueOptions.concurrency <= 0
     ) {
       throw new Error(
-        `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
+        `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}' is a negative integer or zero`
       )
     }
   }
@@ -350,6 +371,9 @@ export abstract class AbstractPool<
           0
         )
       }),
+      ...(this.opts.enableTasksQueue === true && {
+        backPressure: this.hasBackPressure()
+      }),
       failedTasks: this.workerNodes.reduce(
         (accumulator, workerNode) =>
           accumulator + workerNode.usage.tasks.failed,
@@ -361,14 +385,14 @@ export abstract class AbstractPool<
           minimum: round(
             Math.min(
               ...this.workerNodes.map(
-                workerNode => workerNode.usage.runTime?.minimum ?? Infinity
+                (workerNode) => workerNode.usage.runTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
             Math.max(
               ...this.workerNodes.map(
-                workerNode => workerNode.usage.runTime?.maximum ?? -Infinity
+                (workerNode) => workerNode.usage.runTime?.maximum ?? -Infinity
               )
             )
           ),
@@ -389,7 +413,7 @@ export abstract class AbstractPool<
             median: round(
               median(
                 this.workerNodes.map(
-                  workerNode => workerNode.usage.runTime?.median ?? 0
+                  (workerNode) => workerNode.usage.runTime?.median ?? 0
                 )
               )
             )
@@ -402,14 +426,14 @@ export abstract class AbstractPool<
           minimum: round(
             Math.min(
               ...this.workerNodes.map(
-                workerNode => workerNode.usage.waitTime?.minimum ?? Infinity
+                (workerNode) => workerNode.usage.waitTime?.minimum ?? Infinity
               )
             )
           ),
           maximum: round(
             Math.max(
               ...this.workerNodes.map(
-                workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity
+                (workerNode) => workerNode.usage.waitTime?.maximum ?? -Infinity
               )
             )
           ),
@@ -430,7 +454,7 @@ export abstract class AbstractPool<
             median: round(
               median(
                 this.workerNodes.map(
-                  workerNode => workerNode.usage.waitTime?.median ?? 0
+                  (workerNode) => workerNode.usage.waitTime?.median ?? 0
                 )
               )
             )
@@ -505,7 +529,9 @@ export abstract class AbstractPool<
    * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
    */
   private checkMessageWorkerId (message: MessageValue<Response>): void {
-    if (
+    if (message.workerId == null) {
+      throw new Error('Worker message received without worker id')
+    } else if (
       message.workerId != null &&
       this.getWorkerNodeKeyByWorkerId(message.workerId) === -1
     ) {
@@ -523,7 +549,7 @@ export abstract class AbstractPool<
    */
   private getWorkerNodeKeyByWorker (worker: Worker): number {
     return this.workerNodes.findIndex(
-      workerNode => workerNode.worker === worker
+      (workerNode) => workerNode.worker === worker
     )
   }
 
@@ -535,7 +561,7 @@ export abstract class AbstractPool<
    */
   private getWorkerNodeKeyByWorkerId (workerId: number): number {
     return this.workerNodes.findIndex(
-      workerNode => workerNode.info.id === workerId
+      (workerNode) => workerNode.info.id === workerId
     )
   }
 
@@ -554,7 +580,7 @@ export abstract class AbstractPool<
     }
     for (const [workerNodeKey, workerNode] of this.workerNodes.entries()) {
       workerNode.resetUsage()
-      this.sendWorkerStatisticsMessageToWorker(workerNodeKey)
+      this.sendStatisticsMessageToWorker(workerNodeKey)
     }
   }
 
@@ -563,7 +589,10 @@ export abstract class AbstractPool<
     workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
   ): void {
     this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions)
-    this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions
+    this.opts.workerChoiceStrategyOptions = {
+      ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS,
+      ...workerChoiceStrategyOptions
+    }
     this.workerChoiceStrategyContext.setOptions(
       this.opts.workerChoiceStrategyOptions
     )
@@ -625,7 +654,7 @@ export abstract class AbstractPool<
     if (this.opts.enableTasksQueue === true) {
       return (
         this.workerNodes.findIndex(
-          workerNode =>
+          (workerNode) =>
             workerNode.info.ready &&
             workerNode.usage.tasks.executing <
               (this.opts.tasksQueueOptions?.concurrency as number)
@@ -634,13 +663,26 @@ export abstract class AbstractPool<
     } else {
       return (
         this.workerNodes.findIndex(
-          workerNode =>
+          (workerNode) =>
             workerNode.info.ready && workerNode.usage.tasks.executing === 0
         ) === -1
       )
     }
   }
 
+  /** @inheritDoc */
+  public listTaskFunctions (): string[] {
+    for (const workerNode of this.workerNodes) {
+      if (
+        Array.isArray(workerNode.info.taskFunctions) &&
+        workerNode.info.taskFunctions.length > 0
+      ) {
+        return workerNode.info.taskFunctions
+      }
+    }
+    return []
+  }
+
   /** @inheritDoc */
   public async execute (
     data?: Data,
@@ -651,18 +693,35 @@ export abstract class AbstractPool<
       if (name != null && typeof name !== 'string') {
         reject(new TypeError('name argument must be a string'))
       }
+      if (
+        name != null &&
+        typeof name === 'string' &&
+        name.trim().length === 0
+      ) {
+        reject(new TypeError('name argument must not be an empty string'))
+      }
       if (transferList != null && !Array.isArray(transferList)) {
         reject(new TypeError('transferList argument must be an array'))
       }
       const timestamp = performance.now()
       const workerNodeKey = this.chooseWorkerNode()
+      const workerInfo = this.getWorkerInfo(workerNodeKey)
+      if (
+        name != null &&
+        Array.isArray(workerInfo.taskFunctions) &&
+        !workerInfo.taskFunctions.includes(name)
+      ) {
+        reject(
+          new Error(`Task function '${name}' is not registered in the pool`)
+        )
+      }
       const task: Task<Data> = {
         name: name ?? DEFAULT_TASK_NAME,
         // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
         data: data ?? ({} as Data),
         transferList,
         timestamp,
-        workerId: this.getWorkerInfo(workerNodeKey).id as number,
+        workerId: workerInfo.id as number,
         taskId: randomUUID()
       }
       this.promiseResponseMap.set(task.taskId as string, {
@@ -680,7 +739,6 @@ export abstract class AbstractPool<
       } else {
         this.enqueueTask(workerNodeKey, task)
       }
-      this.checkAndEmitEvents()
     })
   }
 
@@ -691,6 +749,23 @@ export abstract class AbstractPool<
         await this.destroyWorkerNode(workerNodeKey)
       })
     )
+    this.emitter?.emit(PoolEvents.destroy)
+  }
+
+  protected async sendKillMessageToWorker (
+    workerNodeKey: number,
+    workerId: number
+  ): Promise<void> {
+    await new Promise<void>((resolve, reject) => {
+      this.registerWorkerMessageListener(workerNodeKey, (message) => {
+        if (message.kill === 'success') {
+          resolve()
+        } else if (message.kill === 'failure') {
+          reject(new Error(`Worker ${workerId} kill message handling failed`))
+        }
+      })
+      this.sendToWorker(workerNodeKey, { kill: true, workerId })
+    })
   }
 
   /**
@@ -729,11 +804,13 @@ export abstract class AbstractPool<
     const workerUsage = this.workerNodes[workerNodeKey].usage
     ++workerUsage.tasks.executing
     this.updateWaitTimeWorkerUsage(workerUsage, task)
-    const taskWorkerUsage = this.workerNodes[workerNodeKey].getTaskWorkerUsage(
-      task.name as string
-    ) as WorkerUsage
-    ++taskWorkerUsage.tasks.executing
-    this.updateWaitTimeWorkerUsage(taskWorkerUsage, task)
+    if (this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey)) {
+      const taskFunctionWorkerUsage = this.workerNodes[
+        workerNodeKey
+      ].getTaskFunctionWorkerUsage(task.name as string) as WorkerUsage
+      ++taskFunctionWorkerUsage.tasks.executing
+      this.updateWaitTimeWorkerUsage(taskFunctionWorkerUsage, task)
+    }
   }
 
   /**
@@ -751,12 +828,30 @@ export abstract class AbstractPool<
     this.updateTaskStatisticsWorkerUsage(workerUsage, message)
     this.updateRunTimeWorkerUsage(workerUsage, message)
     this.updateEluWorkerUsage(workerUsage, message)
-    const taskWorkerUsage = this.workerNodes[workerNodeKey].getTaskWorkerUsage(
-      message.taskPerformance?.name ?? DEFAULT_TASK_NAME
-    ) as WorkerUsage
-    this.updateTaskStatisticsWorkerUsage(taskWorkerUsage, message)
-    this.updateRunTimeWorkerUsage(taskWorkerUsage, message)
-    this.updateEluWorkerUsage(taskWorkerUsage, message)
+    if (this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey)) {
+      const taskFunctionWorkerUsage = this.workerNodes[
+        workerNodeKey
+      ].getTaskFunctionWorkerUsage(
+        message.taskPerformance?.name ?? DEFAULT_TASK_NAME
+      ) as WorkerUsage
+      this.updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage, message)
+      this.updateRunTimeWorkerUsage(taskFunctionWorkerUsage, message)
+      this.updateEluWorkerUsage(taskFunctionWorkerUsage, message)
+    }
+  }
+
+  /**
+   * Whether the worker node shall update its task function worker usage or not.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
+   */
+  private shallUpdateTaskFunctionWorkerUsage (workerNodeKey: number): boolean {
+    const workerInfo = this.getWorkerInfo(workerNodeKey)
+    return (
+      Array.isArray(workerInfo.taskFunctions) &&
+      workerInfo.taskFunctions.length > 2
+    )
   }
 
   private updateTaskStatisticsWorkerUsage (
@@ -764,7 +859,19 @@ export abstract class AbstractPool<
     message: MessageValue<Response>
   ): void {
     const workerTaskStatistics = workerUsage.tasks
-    --workerTaskStatistics.executing
+    if (
+      workerTaskStatistics.executing != null &&
+      workerTaskStatistics.executing > 0
+    ) {
+      --workerTaskStatistics.executing
+    } else if (
+      workerTaskStatistics.executing != null &&
+      workerTaskStatistics.executing < 0
+    ) {
+      throw new Error(
+        'Worker usage statistic for tasks executing cannot be negative'
+      )
+    }
     if (message.taskError == null) {
       ++workerTaskStatistics.executed
     } else {
@@ -886,9 +993,10 @@ export abstract class AbstractPool<
   protected createAndSetupWorkerNode (): number {
     const worker = this.createWorker()
 
+    worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
     worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
     worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
-    worker.on('error', error => {
+    worker.on('error', (error) => {
       const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
       const workerInfo = this.getWorkerInfo(workerNodeKey)
       workerInfo.ready = false
@@ -905,7 +1013,6 @@ export abstract class AbstractPool<
         this.redistributeQueuedTasks(workerNodeKey)
       }
     })
-    worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
     worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
     worker.once('exit', () => {
       this.removeWorkerNode(worker)
@@ -925,7 +1032,7 @@ export abstract class AbstractPool<
    */
   protected createAndSetupDynamicWorkerNode (): number {
     const workerNodeKey = this.createAndSetupWorkerNode()
-    this.registerWorkerMessageListener(workerNodeKey, message => {
+    this.registerWorkerMessageListener(workerNodeKey, (message) => {
       const localWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
         message.workerId
       )
@@ -933,14 +1040,16 @@ export abstract class AbstractPool<
       // Kill message received from worker
       if (
         isKillBehavior(KillBehaviors.HARD, message.kill) ||
-        (message.kill != null &&
+        (isKillBehavior(KillBehaviors.SOFT, message.kill) &&
           ((this.opts.enableTasksQueue === false &&
             workerUsage.tasks.executing === 0) ||
             (this.opts.enableTasksQueue === true &&
               workerUsage.tasks.executing === 0 &&
               this.tasksQueueSize(localWorkerNodeKey) === 0)))
       ) {
-        this.destroyWorkerNode(localWorkerNodeKey).catch(EMPTY_FUNCTION)
+        this.destroyWorkerNode(localWorkerNodeKey).catch((error) => {
+          this.emitter?.emit(PoolEvents.error, error)
+        })
       }
     })
     const workerInfo = this.getWorkerInfo(workerNodeKey)
@@ -979,8 +1088,8 @@ export abstract class AbstractPool<
     this.registerWorkerMessageListener(workerNodeKey, this.workerListener())
     // Send the startup message to worker.
     this.sendStartupMessageToWorker(workerNodeKey)
-    // Send the worker statistics message to worker.
-    this.sendWorkerStatisticsMessageToWorker(workerNodeKey)
+    // Send the statistics message to worker.
+    this.sendStatisticsMessageToWorker(workerNodeKey)
   }
 
   /**
@@ -991,11 +1100,11 @@ export abstract class AbstractPool<
   protected abstract sendStartupMessageToWorker (workerNodeKey: number): void
 
   /**
-   * Sends the worker statistics message to worker given its worker node key.
+   * Sends the statistics message to worker given its worker node key.
    *
    * @param workerNodeKey - The worker node key.
    */
-  private sendWorkerStatisticsMessageToWorker (workerNodeKey: number): void {
+  private sendStatisticsMessageToWorker (workerNodeKey: number): void {
     this.sendToWorker(workerNodeKey, {
       statistics: {
         runTime:
@@ -1058,41 +1167,50 @@ export abstract class AbstractPool<
    * @returns The listener function to execute when a message is received from a worker.
    */
   protected workerListener (): (message: MessageValue<Response>) => void {
-    return message => {
+    return (message) => {
       this.checkMessageWorkerId(message)
-      if (message.ready != null) {
+      if (message.ready != null && message.taskFunctions != null) {
         // Worker ready response received from worker
         this.handleWorkerReadyResponse(message)
       } else if (message.taskId != null) {
         // Task execution response received from worker
         this.handleTaskExecutionResponse(message)
+      } else if (message.taskFunctions != null) {
+        // Task functions message received from worker
+        this.getWorkerInfo(
+          this.getWorkerNodeKeyByWorkerId(message.workerId)
+        ).taskFunctions = message.taskFunctions
       }
     }
   }
 
   private handleWorkerReadyResponse (message: MessageValue<Response>): void {
-    this.getWorkerInfo(
+    if (message.ready === false) {
+      throw new Error(`Worker ${message.workerId} failed to initialize`)
+    }
+    const workerInfo = this.getWorkerInfo(
       this.getWorkerNodeKeyByWorkerId(message.workerId)
-    ).ready = message.ready as boolean
+    )
+    workerInfo.ready = message.ready as boolean
+    workerInfo.taskFunctions = message.taskFunctions
     if (this.emitter != null && this.ready) {
       this.emitter.emit(PoolEvents.ready, this.info)
     }
   }
 
   private handleTaskExecutionResponse (message: MessageValue<Response>): void {
-    const promiseResponse = this.promiseResponseMap.get(
-      message.taskId as string
-    )
+    const { taskId, taskError, data } = message
+    const promiseResponse = this.promiseResponseMap.get(taskId as string)
     if (promiseResponse != null) {
-      if (message.taskError != null) {
-        this.emitter?.emit(PoolEvents.taskError, message.taskError)
-        promiseResponse.reject(message.taskError.message)
+      if (taskError != null) {
+        this.emitter?.emit(PoolEvents.taskError, taskError)
+        promiseResponse.reject(taskError.message)
       } else {
-        promiseResponse.resolve(message.data as Response)
+        promiseResponse.resolve(data as Response)
       }
       const workerNodeKey = promiseResponse.workerNodeKey
       this.afterTaskExecutionHook(workerNodeKey, message)
-      this.promiseResponseMap.delete(message.taskId as string)
+      this.promiseResponseMap.delete(taskId as string)
       if (
         this.opts.enableTasksQueue === true &&
         this.tasksQueueSize(workerNodeKey) > 0 &&
@@ -1108,7 +1226,7 @@ export abstract class AbstractPool<
     }
   }
 
-  private checkAndEmitEvents (): void {
+  private checkAndEmitTaskExecutionEvents (): void {
     if (this.emitter != null) {
       if (this.busy) {
         this.emitter.emit(PoolEvents.busy, this.info)
@@ -1119,6 +1237,12 @@ export abstract class AbstractPool<
     }
   }
 
+  private checkAndEmitTaskQueuingEvents (): void {
+    if (this.hasBackPressure()) {
+      this.emitter?.emit(PoolEvents.backPressure, this.info)
+    }
+  }
+
   /**
    * Gets the worker information given its worker node key.
    *
@@ -1137,7 +1261,11 @@ export abstract class AbstractPool<
    * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
    */
   private addWorkerNode (worker: Worker): number {
-    const workerNode = new WorkerNode<Worker, Data>(worker, this.worker)
+    const workerNode = new WorkerNode<Worker, Data>(
+      worker,
+      this.worker,
+      this.maxSize
+    )
     // Flag the worker node as ready at pool startup.
     if (this.starting) {
       workerNode.info.ready = true
@@ -1145,7 +1273,7 @@ export abstract class AbstractPool<
     this.workerNodes.push(workerNode)
     const workerNodeKey = this.getWorkerNodeKeyByWorker(worker)
     if (workerNodeKey === -1) {
-      throw new Error('Worker node not found')
+      throw new Error('Worker node added not found')
     }
     return workerNodeKey
   }
@@ -1163,6 +1291,23 @@ export abstract class AbstractPool<
     }
   }
 
+  /** @inheritDoc */
+  public hasWorkerNodeBackPressure (workerNodeKey: number): boolean {
+    return (
+      this.opts.enableTasksQueue === true &&
+      this.workerNodes[workerNodeKey].hasBackPressure()
+    )
+  }
+
+  private hasBackPressure (): boolean {
+    return (
+      this.opts.enableTasksQueue === true &&
+      this.workerNodes.findIndex(
+        (workerNode) => !workerNode.hasBackPressure()
+      ) === -1
+    )
+  }
+
   /**
    * Executes the given task on the worker given its worker node key.
    *
@@ -1171,17 +1316,14 @@ export abstract class AbstractPool<
    */
   private executeTask (workerNodeKey: number, task: Task<Data>): void {
     this.beforeTaskExecutionHook(workerNodeKey, task)
-    this.sendToWorker(
-      workerNodeKey,
-      task,
-      this.worker === WorkerTypes.thread && task.transferList != null
-        ? task.transferList
-        : undefined
-    )
+    this.sendToWorker(workerNodeKey, task, task.transferList)
+    this.checkAndEmitTaskExecutionEvents()
   }
 
   private enqueueTask (workerNodeKey: number, task: Task<Data>): number {
-    return this.workerNodes[workerNodeKey].enqueueTask(task)
+    const tasksQueueSize = this.workerNodes[workerNodeKey].enqueueTask(task)
+    this.checkAndEmitTaskQueuingEvents()
+    return tasksQueueSize
   }
 
   private dequeueTask (workerNodeKey: number): Task<Data> | undefined {