### Fixed
- Fix queued tasks redistribution on error task execution starvation.
+- Ensure task queueing per worker condition is untangled from the pool busyness semantic.
### Changed
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()
})
describe('Fixed cluster pool test suite', () => {
const numberOfWorkers = 6
+ const tasksConcurrency = 2
const pool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
{
enableTasksQueue: true,
tasksQueueOptions: {
- concurrency: 2
+ concurrency: tasksConcurrency
},
errorHandler: e => console.error(e)
}
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())
}
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
+ )
}
})
describe('Fixed thread pool test suite', () => {
const numberOfThreads = 6
+ const tasksConcurrency = 2
const pool = new FixedThreadPool(
numberOfThreads,
'./tests/worker-files/thread/testWorker.js',
{
enableTasksQueue: true,
tasksQueueOptions: {
- concurrency: 2
+ concurrency: tasksConcurrency
},
errorHandler: e => console.error(e)
}
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())
}
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
+ )
}
})