The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## [2.3.2] - 2022-14-10
+
+### Changed
+
+- Optimize fair share worker selection strategy implementation.
+
+### Fixed
+
+- Fix WRR worker selection strategy: ensure the condition triggering the round robin can be fulfilled.
+
## [2.3.1] - 2022-13-10
### Added
{
"name": "poolifier",
- "version": "2.3.1",
+ "version": "2.3.2",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "poolifier",
- "version": "2.3.1",
+ "version": "2.3.2",
"license": "MIT",
"devDependencies": {
"@types/node": "^18.8.5",
{
"name": "poolifier",
- "version": "2.3.1",
+ "version": "2.3.2",
"description": "A fast, easy to use Node.js Worker Thread Pool and Cluster Pool implementation",
"main": "lib/index.js",
"scripts": {
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.eslint.reportPaths=reports/eslint.json
sonar.projectName=poolifier
-sonar.projectVersion=2.3.1
+sonar.projectVersion=2.3.2
sonar.host.url=https://sonarcloud.io
sonar.sources=src
sonar.tests=tests
/**
* Removes the given worker from the pool.
*
- * @param worker Worker that will be removed.
+ * @param worker The worker that will be removed.
*/
protected removeWorker (worker: Worker): void {
// Clean worker from data structure
/**
* Registers a listener callback on a given worker.
*
- * @param worker A worker.
- * @param listener A message listener callback.
+ * @param worker The worker which should register a listener.
+ * @param listener The message listener callback.
*/
protected abstract registerWorkerMessageListener<
Message extends Data | Response
/** @inheritDoc */
public choose (): Worker {
- this.computeWorkerLastVirtualTaskTimestamp()
let minWorkerVirtualTaskEndTimestamp = Infinity
let chosenWorker!: Worker
for (const worker of this.pool.workers) {
+ this.computeWorkerLastVirtualTaskTimestamp(worker)
const workerLastVirtualTaskEndTimestamp =
this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? 0
if (
}
/**
- * Computes workers last virtual task timestamp.
+ * Computes worker last virtual task timestamp.
+ *
+ * @param worker The worker.
*/
- private computeWorkerLastVirtualTaskTimestamp () {
- for (const worker of this.pool.workers) {
- const workerVirtualTaskStartTimestamp = Math.max(
- Date.now(),
- this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? -Infinity
- )
- const workerVirtualTaskEndTimestamp =
- workerVirtualTaskStartTimestamp +
- (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
- this.workerLastVirtualTaskTimestamp.set(worker, {
- start: workerVirtualTaskStartTimestamp,
- end: workerVirtualTaskEndTimestamp
- })
- }
+ private computeWorkerLastVirtualTaskTimestamp (worker: Worker): void {
+ const workerVirtualTaskStartTimestamp = Math.max(
+ Date.now(),
+ this.workerLastVirtualTaskTimestamp.get(worker)?.end ?? -Infinity
+ )
+ const workerVirtualTaskEndTimestamp =
+ workerVirtualTaskStartTimestamp +
+ (this.pool.getWorkerAverageTasksRunTime(worker) ?? 0)
+ this.workerLastVirtualTaskTimestamp.set(worker, {
+ start: workerVirtualTaskStartTimestamp,
+ end: workerVirtualTaskEndTimestamp
+ })
}
}
runTime: true
}
- /**
- * Worker index where the previous task was submitted.
- */
- private previousWorkerIndex: number = 0
/**
* Worker index where the current task will be submitted.
*/
/** @inheritDoc */
public reset (): boolean {
- this.previousWorkerIndex = 0
this.currentWorkerIndex = 0
this.workersTaskRunTime.clear()
this.initWorkersTaskRunTime()
/** @inheritDoc */
public choose (): Worker {
- const currentWorker = this.pool.workers[this.currentWorkerIndex]
+ let chosenWorker = this.pool.workers[this.currentWorkerIndex]
if (
this.isDynamicPool === true &&
- this.workersTaskRunTime.has(currentWorker) === false
+ this.workersTaskRunTime.has(chosenWorker) === false
) {
- this.initWorkerTaskRunTime(currentWorker)
+ this.initWorkerTaskRunTime(chosenWorker)
}
- const workerVirtualTaskRunTime =
- this.getWorkerVirtualTaskRunTime(currentWorker) ?? 0
const workerTaskWeight =
- this.workersTaskRunTime.get(currentWorker)?.weight ??
+ this.workersTaskRunTime.get(chosenWorker)?.weight ??
this.defaultWorkerWeight
- if (this.currentWorkerIndex === this.previousWorkerIndex) {
- const workerTaskRunTime =
- (this.workersTaskRunTime.get(currentWorker)?.runTime ?? 0) +
- workerVirtualTaskRunTime
+ if (
+ (this.workersTaskRunTime.get(chosenWorker)?.runTime ?? 0) <
+ workerTaskWeight
+ ) {
this.setWorkerTaskRunTime(
- currentWorker,
+ chosenWorker,
workerTaskWeight,
- workerTaskRunTime
+ (this.workersTaskRunTime.get(chosenWorker)?.runTime ?? 0) +
+ (this.getWorkerVirtualTaskRunTime(chosenWorker) ?? 0)
)
} else {
- this.setWorkerTaskRunTime(currentWorker, workerTaskWeight, 0)
- }
- if (workerVirtualTaskRunTime < workerTaskWeight) {
- this.previousWorkerIndex = this.currentWorkerIndex
- } else {
- this.previousWorkerIndex = this.currentWorkerIndex
this.currentWorkerIndex =
this.pool.workers.length - 1 === this.currentWorkerIndex
? 0
: this.currentWorkerIndex + 1
+ chosenWorker = this.pool.workers[this.currentWorkerIndex]
+ this.setWorkerTaskRunTime(chosenWorker, workerTaskWeight, 0)
}
- return this.pool.workers[this.currentWorkerIndex]
+ return chosenWorker
}
private initWorkersTaskRunTime (): void {
expect(pool.opts.workerChoiceStrategy).toBe(
WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .previousWorkerIndex
- ).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.currentWorkerIndex
max,
'./tests/worker-files/thread/testWorker.js'
)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .previousWorkerIndex
- ).toBeUndefined()
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.currentWorkerIndex
.workersTaskRunTime
).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .previousWorkerIndex
- ).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.currentWorkerIndex
max,
'./tests/worker-files/thread/testWorker.js'
)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.previousWorkerIndex
- ).toBeUndefined()
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.workerChoiceStrategy.currentWorkerIndex
.workerChoiceStrategy.workersTaskRunTime
).toBeUndefined()
pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
- expect(
- pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
- .workerChoiceStrategy.previousWorkerIndex
- ).toBe(0)
expect(
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.workerChoiceStrategy.currentWorkerIndex
it('Verify that reset() resets internals', () => {
const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool)
- strategy.previousWorkerIndex = TestUtils.generateRandomInteger()
strategy.currentWorkerIndex = TestUtils.generateRandomInteger()
const workersTaskRunTimeClearStub = sinon
.stub(strategy.workersTaskRunTime, 'clear')
.returns()
const resetResult = strategy.reset()
expect(resetResult).toBe(true)
- expect(strategy.previousWorkerIndex).toBe(0)
expect(strategy.currentWorkerIndex).toBe(0)
expect(workersTaskRunTimeClearStub.calledOnce).toBe(true)
expect(initWorkersTaskRunTimeStub.calledOnce).toBe(true)