### Changed
- Optimize O(1) queue implementation.
+- Optimize worker choice strategies: pre-choose the worker node key by executing the choice algorithm after tasks submission.
## [2.6.2] - 2023-06-12
*/
private toggleFindLastFreeWorkerNodeKey: boolean = false
+ /**
+ * Id of the next worker node.
+ */
+ protected nextWorkerNodeId: number = 0
+
/** @inheritDoc */
public readonly strategyPolicy: StrategyPolicy = {
useDynamicWorker: false
/** @inheritDoc */
public update (workerNodeKey: number): boolean {
this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
- return true
- }
-
- /** @inheritDoc */
- public choose (): number {
let minWorkerVirtualTaskEndTimestamp = Infinity
- let chosenWorkerNodeKey!: number
for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
this.computeWorkerVirtualTaskEndTimestamp(workerNodeKey)
this.workersVirtualTaskEndTimestamp[workerNodeKey]
if (workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp) {
minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
- chosenWorkerNodeKey = workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
}
}
- return chosenWorkerNodeKey
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ return this.nextWorkerNodeId
}
/** @inheritDoc */
}
/**
- * Worker node id where the current task will be submitted.
- */
- private currentWorkerNodeId: number = 0
- /**
- * Current round id.
+ * Round id.
* This is used to determine the current round weight.
*/
- private currentRoundId: number = 0
+ private roundId: number = 0
/**
* Round weights.
*/
/** @inheritDoc */
public reset (): boolean {
- this.currentWorkerNodeId = 0
- this.currentRoundId = 0
+ this.nextWorkerNodeId = 0
+ this.roundId = 0
return true
}
let roundId: number | undefined
let workerNodeId: number | undefined
for (
- let roundIndex = this.currentRoundId;
+ let roundIndex = this.roundId;
roundIndex < this.roundWeights.length;
roundIndex++
) {
for (
- let workerNodeKey = this.currentWorkerNodeId;
+ let workerNodeKey = this.nextWorkerNodeId;
workerNodeKey < this.pool.workerNodes.length;
workerNodeKey++
) {
}
}
}
- this.currentRoundId = roundId ?? 0
- this.currentWorkerNodeId = workerNodeId ?? 0
- const chosenWorkerNodeKey = this.currentWorkerNodeId
- if (this.currentWorkerNodeId === this.pool.workerNodes.length - 1) {
- this.currentWorkerNodeId = 0
- this.currentRoundId =
- this.currentRoundId === this.roundWeights.length - 1
- ? 0
- : this.currentRoundId + 1
+ this.roundId = roundId ?? 0
+ this.nextWorkerNodeId = workerNodeId ?? 0
+ const chosenWorkerNodeKey = this.nextWorkerNodeId
+ if (this.nextWorkerNodeId === this.pool.workerNodes.length - 1) {
+ this.nextWorkerNodeId = 0
+ this.roundId =
+ this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
} else {
- this.currentWorkerNodeId = this.currentWorkerNodeId + 1
+ this.nextWorkerNodeId = this.nextWorkerNodeId + 1
}
return chosenWorkerNodeKey
}
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
- if (this.currentWorkerNodeId === workerNodeKey) {
+ if (this.nextWorkerNodeId === workerNodeKey) {
if (this.pool.workerNodes.length === 0) {
- this.currentWorkerNodeId = 0
- } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
- this.currentWorkerNodeId = this.pool.workerNodes.length - 1
- this.currentRoundId =
- this.currentRoundId === this.roundWeights.length - 1
- ? 0
- : this.currentRoundId + 1
+ this.nextWorkerNodeId = 0
+ } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
+ this.nextWorkerNodeId = this.pool.workerNodes.length - 1
+ this.roundId =
+ this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
}
}
return true
/** @inheritDoc */
public update (): boolean {
- return true
- }
-
- /** @inheritDoc */
- public choose (): number {
let minTime = Infinity
- let leastBusyWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerTime =
workerNode.workerUsage.runTime.aggregate +
workerNode.workerUsage.waitTime.aggregate
if (workerTime === 0) {
- return workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
+ return true
} else if (workerTime < minTime) {
minTime = workerTime
- leastBusyWorkerNodeKey = workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
}
}
- return leastBusyWorkerNodeKey
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ return this.nextWorkerNodeId
}
/** @inheritDoc */
/** @inheritDoc */
public update (): boolean {
- return true
- }
-
- /** @inheritDoc */
- public choose (): number {
let minWorkerElu = Infinity
- let leastEluWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerUsage = workerNode.workerUsage
const workerElu = workerUsage.elu?.active.aggregate ?? 0
if (workerElu === 0) {
- return workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
+ return true
} else if (workerElu < minWorkerElu) {
minWorkerElu = workerElu
- leastEluWorkerNodeKey = workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
}
}
- return leastEluWorkerNodeKey
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ return this.nextWorkerNodeId
}
/** @inheritDoc */
/** @inheritDoc */
public update (): boolean {
- return true
- }
-
- /** @inheritDoc */
- public choose (): number {
- const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
- if (freeWorkerNodeKey !== -1) {
- return freeWorkerNodeKey
- }
let minNumberOfTasks = Infinity
- let leastUsedWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerTaskStatistics = workerNode.workerUsage.tasks
const workerTasks =
workerTaskStatistics.executing +
workerTaskStatistics.queued
if (workerTasks === 0) {
- return workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
+ return true
} else if (workerTasks < minNumberOfTasks) {
minNumberOfTasks = workerTasks
- leastUsedWorkerNodeKey = workerNodeKey
+ this.nextWorkerNodeId = workerNodeKey
}
}
- return leastUsedWorkerNodeKey
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ return this.nextWorkerNodeId
}
/** @inheritDoc */
useDynamicWorker: true
}
- /**
- * Id of the next worker node.
- */
- private nextWorkerNodeId: number = 0
-
/** @inheritDoc */
public constructor (
pool: IPool<Worker, Data, Response>,
}
}
- /**
- * Worker node id where the current task will be submitted.
- */
- private currentWorkerNodeId: number = 0
/**
* Default worker weight.
*/
/** @inheritDoc */
public reset (): boolean {
- this.currentWorkerNodeId = 0
+ this.nextWorkerNodeId = 0
this.workerVirtualTaskRunTime = 0
return true
}
/** @inheritDoc */
public choose (): number {
- const chosenWorkerNodeKey = this.currentWorkerNodeId
+ const chosenWorkerNodeKey = this.nextWorkerNodeId
const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
const workerWeight =
this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
workerVirtualTaskRunTime +
this.getWorkerTaskRunTime(chosenWorkerNodeKey)
} else {
- this.currentWorkerNodeId =
- this.currentWorkerNodeId === this.pool.workerNodes.length - 1
+ this.nextWorkerNodeId =
+ this.nextWorkerNodeId === this.pool.workerNodes.length - 1
? 0
- : this.currentWorkerNodeId + 1
+ : this.nextWorkerNodeId + 1
this.workerVirtualTaskRunTime = 0
}
return chosenWorkerNodeKey
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
- if (this.currentWorkerNodeId === workerNodeKey) {
+ if (this.nextWorkerNodeId === workerNodeKey) {
if (this.pool.workerNodes.length === 0) {
- this.currentWorkerNodeId = 0
- } else if (this.currentWorkerNodeId > this.pool.workerNodes.length - 1) {
- this.currentWorkerNodeId = this.pool.workerNodes.length - 1
+ this.nextWorkerNodeId = 0
+ } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
+ this.nextWorkerNodeId = this.pool.workerNodes.length - 1
}
this.workerVirtualTaskRunTime = 0
}
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
for (const workerNode of pool.workerNodes) {
expect(workerNode.workerUsage).toStrictEqual({
tasks: {
- executed: maxMultiplier,
+ executed: expect.any(Number),
executing: 0,
queued: 0,
failed: 0
utilization: expect.any(Number)
}
})
+ expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
+ expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
+ max * maxMultiplier
+ )
expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0)
expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0)
expect(workerNode.workerUsage.elu.utilization).toBeGreaterThanOrEqual(0)
utilization: expect.any(Number)
}
})
- expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
+ expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
max * maxMultiplier
)
- expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0)
- expect(workerNode.workerUsage.runTime.average).toBeGreaterThan(0)
+ expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThanOrEqual(0)
+ expect(workerNode.workerUsage.runTime.average).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.elu.utilization).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.elu.utilization).toBeLessThanOrEqual(1)
}
utilization: expect.any(Number)
}
})
- expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
+ expect(workerNode.workerUsage.tasks.executed).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
max * maxMultiplier
)
- expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThan(0)
- expect(workerNode.workerUsage.runTime.median).toBeGreaterThan(0)
+ expect(workerNode.workerUsage.runTime.aggregate).toBeGreaterThanOrEqual(0)
+ expect(workerNode.workerUsage.runTime.median).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.elu.utilization).toBeGreaterThanOrEqual(0)
expect(workerNode.workerUsage.elu.utilization).toBeLessThanOrEqual(1)
}
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentRoundId
+ ).roundId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentRoundId
+ ).roundId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentRoundId
+ ).roundId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentRoundId
+ ).roundId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentRoundId
+ ).roundId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBeDefined()
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
pool.workerChoiceStrategyContext.workerChoiceStrategy
- ).currentWorkerNodeId
+ ).nextWorkerNodeId
).toBe(0)
expect(
pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
)
const resetResult = strategy.reset()
expect(resetResult).toBe(true)
- expect(strategy.currentWorkerNodeId).toBe(0)
+ expect(strategy.nextWorkerNodeId).toBe(0)
expect(strategy.workerVirtualTaskRunTime).toBe(0)
})
})