IWorkerChoiceStrategy
>
- /**
- * The number of times the worker choice strategy in the context has been retried.
- */
- private retriesCount = 0
-
/**
* Worker choice strategy context constructor.
*
* Executes the worker choice strategy in the context algorithm.
*
* @returns The key of the worker node.
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined .
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If the maximum consecutive worker choice strategy executions has been reached.
*/
public execute (): number {
- const workerNodeKey = (
- this.workerChoiceStrategies.get(
- this.workerChoiceStrategy
- ) as IWorkerChoiceStrategy
- ).choose()
- if (
- workerNodeKey == null &&
- (this.retriesCount < (this.opts.retries as number) ||
- this.opts.retries === Infinity)
- ) {
- this.retriesCount++
- return this.execute()
- } else if (workerNodeKey == null) {
+ const workerChoiceStrategy = this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ let workerNodeKey: number | undefined
+ const maxExecutionCount = 10000
+ let executionCount = 0
+ let chooseCount = 0
+ let retriesCount = 0
+ do {
+ if (workerChoiceStrategy.hasPoolWorkerNodesReady()) {
+ workerNodeKey = workerChoiceStrategy.choose()
+ if (chooseCount > 0) {
+ retriesCount++
+ }
+ chooseCount++
+ }
+ executionCount++
+ } while (
+ executionCount < maxExecutionCount &&
+ (!workerChoiceStrategy.hasPoolWorkerNodesReady() ||
+ (workerNodeKey == null && retriesCount < (this.opts.retries as number)))
+ )
+ if (executionCount >= maxExecutionCount) {
+ throw new RangeError(
+ `Worker choice strategy consecutive executions has exceeded the maximum of ${maxExecutionCount}`
+ )
+ }
+ if (workerNodeKey == null) {
throw new Error(
- `Worker node key chosen is null or undefined after ${this.retriesCount} retries`
+ `Worker node key chosen is null or undefined after ${retriesCount} retries`
)
}
- this.retriesCount = 0
return workerNodeKey
}
)
})
- it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
+ it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
fixedPool
)
const workerChoiceStrategyStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
+ hasPoolWorkerNodesReady: stub().returns(true),
choose: stub().returns(0)
}
)
const workerChoiceStrategyUndefinedStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
+ hasPoolWorkerNodesReady: stub().returns(true),
choose: stub().returns(undefined)
}
)
const workerChoiceStrategyNullStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
+ hasPoolWorkerNodesReady: stub().returns(true),
choose: stub().returns(null)
}
)
)
})
- it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
+ it('Verify that execute() retry until a worker node is ready and chosen', () => {
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
+ )
+ const workerChoiceStrategyStub = createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ hasPoolWorkerNodesReady: stub()
+ .onCall(0)
+ .returns(false)
+ .onCall(1)
+ .returns(false)
+ .onCall(2)
+ .returns(false)
+ .onCall(3)
+ .returns(false)
+ .onCall(4)
+ .returns(false)
+ .onCall(6)
+ .returns(false)
+ .onCall(7)
+ .returns(false)
+ .onCall(8)
+ .returns(false)
+ .returns(true),
+ choose: stub().returns(1)
+ }
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ workerChoiceStrategyStub
+ )
+ const chosenWorkerKey = workerChoiceStrategyContext.execute()
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategyContext.workerChoiceStrategy
+ ).hasPoolWorkerNodesReady.callCount
+ ).toBe(12)
+ expect(
+ workerChoiceStrategyContext.workerChoiceStrategies.get(
+ workerChoiceStrategyContext.workerChoiceStrategy
+ ).choose.callCount
+ ).toBe(1)
+ expect(chosenWorkerKey).toBe(1)
+ })
+
+ it('Verify that execute() throws error if worker choice strategy consecutive executions has been reached', () => {
+ const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+ fixedPool
+ )
+ const workerChoiceStrategyStub = createStubInstance(
+ RoundRobinWorkerChoiceStrategy,
+ {
+ hasPoolWorkerNodesReady: stub().returns(false),
+ choose: stub().returns(0)
+ }
+ )
+ expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+ WorkerChoiceStrategies.ROUND_ROBIN
+ )
+ workerChoiceStrategyContext.workerChoiceStrategies.set(
+ workerChoiceStrategyContext.workerChoiceStrategy,
+ workerChoiceStrategyStub
+ )
+ expect(() => workerChoiceStrategyContext.execute()).toThrow(
+ new RangeError(
+ 'Worker choice strategy consecutive executions has exceeded the maximum of 10000'
+ )
+ )
+ })
+
+ it('Verify that execute() return the worker node key chosen by the strategy with dynamic pool', () => {
const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
dynamicPool
)
const workerChoiceStrategyStub = createStubInstance(
RoundRobinWorkerChoiceStrategy,
{
+ hasPoolWorkerNodesReady: stub().returns(true),
choose: stub().returns(0)
}
)