fix: untangle tasks queuing condition from pool busyness
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 21 Jul 2023 19:22:14 +0000 (21:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 21 Jul 2023 19:22:14 +0000 (21:22 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index d424256f85a22a34505951cba68c33efb3c1de37..d4295cd40daf3886c63b7ab58529f42f02a9ec36 100644 (file)
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Fixed
 
 - Fix queued tasks redistribution on error task execution starvation.
+- Ensure task queueing per worker condition is untangled from the pool busyness semantic.
 
 ### Changed
 
index 7c71c8557f0edcdc6a31ea12fd67649b2a8d0521..fa3406a376660c0de442be50778729187fc3fe02 100644 (file)
@@ -642,14 +642,14 @@ export abstract class AbstractPool<
         workerNodeKey
       })
       if (
-        this.opts.enableTasksQueue === true &&
-        (this.busy ||
-          this.workerNodes[workerNodeKey].usage.tasks.executing >=
+        this.opts.enableTasksQueue === false ||
+        (this.opts.enableTasksQueue === true &&
+          this.workerNodes[workerNodeKey].usage.tasks.executing <
             (this.opts.tasksQueueOptions?.concurrency as number))
       ) {
-        this.enqueueTask(workerNodeKey, task)
-      } else {
         this.executeTask(workerNodeKey, task)
+      } else {
+        this.enqueueTask(workerNodeKey, task)
       }
       this.checkAndEmitEvents()
     })
index f71529a911949f285d0c54ebd8be5b27d67d0e3d..0415d9c1eaa7a94cdc1f1260a9bac060c5703c04 100644 (file)
@@ -5,6 +5,7 @@ const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils')
 
 describe('Fixed cluster pool test suite', () => {
   const numberOfWorkers = 6
+  const tasksConcurrency = 2
   const pool = new FixedClusterPool(
     numberOfWorkers,
     './tests/worker-files/cluster/testWorker.js',
@@ -18,7 +19,7 @@ describe('Fixed cluster pool test suite', () => {
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
-        concurrency: 2
+        concurrency: tasksConcurrency
       },
       errorHandler: e => console.error(e)
     }
@@ -104,7 +105,7 @@ describe('Fixed cluster pool test suite', () => {
 
   it('Verify that tasks queuing is working', async () => {
     const promises = new Set()
-    const maxMultiplier = 2
+    const maxMultiplier = 3 // Must be greater than tasksConcurrency
     for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
       promises.add(queuePool.execute())
     }
@@ -114,23 +115,32 @@ describe('Fixed cluster pool test suite', () => {
         queuePool.opts.tasksQueueOptions.concurrency
       )
       expect(workerNode.usage.tasks.executed).toBe(0)
-      expect(workerNode.usage.tasks.queued).toBeGreaterThan(0)
-      expect(workerNode.usage.tasks.maxQueued).toBeGreaterThan(0)
+      expect(workerNode.usage.tasks.queued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
     }
-    expect(queuePool.info.executingTasks).toBe(numberOfWorkers)
+    expect(queuePool.info.executingTasks).toBe(
+      numberOfWorkers * queuePool.opts.tasksQueueOptions.concurrency
+    )
     expect(queuePool.info.queuedTasks).toBe(
-      numberOfWorkers * maxMultiplier - numberOfWorkers
+      numberOfWorkers *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
     )
     expect(queuePool.info.maxQueuedTasks).toBe(
-      numberOfWorkers * maxMultiplier - numberOfWorkers
+      numberOfWorkers *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
     )
     await Promise.all(promises)
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.usage.tasks.executing).toBe(0)
-      expect(workerNode.usage.tasks.executed).toBeGreaterThan(0)
-      expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier)
+      expect(workerNode.usage.tasks.executed).toBe(maxMultiplier)
       expect(workerNode.usage.tasks.queued).toBe(0)
-      expect(workerNode.usage.tasks.maxQueued).toBe(1)
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
     }
   })
 
index 99b867786b62d95454dc660201f14c55254094d5..e4b99a6378d226535f83e47fb5516e3d394212f2 100644 (file)
@@ -5,6 +5,7 @@ const { waitPoolEvents, waitWorkerEvents } = require('../../test-utils')
 
 describe('Fixed thread pool test suite', () => {
   const numberOfThreads = 6
+  const tasksConcurrency = 2
   const pool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/testWorker.js',
@@ -18,7 +19,7 @@ describe('Fixed thread pool test suite', () => {
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
-        concurrency: 2
+        concurrency: tasksConcurrency
       },
       errorHandler: e => console.error(e)
     }
@@ -104,7 +105,7 @@ describe('Fixed thread pool test suite', () => {
 
   it('Verify that tasks queuing is working', async () => {
     const promises = new Set()
-    const maxMultiplier = 2
+    const maxMultiplier = 3 // Must be greater than tasksConcurrency
     for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
       promises.add(queuePool.execute())
     }
@@ -114,23 +115,32 @@ describe('Fixed thread pool test suite', () => {
         queuePool.opts.tasksQueueOptions.concurrency
       )
       expect(workerNode.usage.tasks.executed).toBe(0)
-      expect(workerNode.usage.tasks.queued).toBeGreaterThan(0)
-      expect(workerNode.usage.tasks.maxQueued).toBeGreaterThan(0)
+      expect(workerNode.usage.tasks.queued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
     }
-    expect(queuePool.info.executingTasks).toBe(numberOfThreads)
+    expect(queuePool.info.executingTasks).toBe(
+      numberOfThreads * queuePool.opts.tasksQueueOptions.concurrency
+    )
     expect(queuePool.info.queuedTasks).toBe(
-      numberOfThreads * maxMultiplier - numberOfThreads
+      numberOfThreads *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
     )
     expect(queuePool.info.maxQueuedTasks).toBe(
-      numberOfThreads * maxMultiplier - numberOfThreads
+      numberOfThreads *
+        (maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency)
     )
     await Promise.all(promises)
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.usage.tasks.executing).toBe(0)
-      expect(workerNode.usage.tasks.executed).toBeGreaterThan(0)
-      expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier)
+      expect(workerNode.usage.tasks.executed).toBe(maxMultiplier)
       expect(workerNode.usage.tasks.queued).toBe(0)
-      expect(workerNode.usage.tasks.maxQueued).toBe(1)
+      expect(workerNode.usage.tasks.maxQueued).toBe(
+        maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
+      )
     }
   })