Thursday, December 06, 2018

When will Hystrix Throw ThreadPoolRejection Error when Synchronous Queue is used?

Hystrix uses SynchronousQueue when MaxQueueSize has been set to -1, and uses LinkedBlockingQueue when MaxQueueSize is set to >=0.

When LinkedBlockingQueue is used and the number of concurrent requests is beyond (threadPool.CoreSize + queueSizeRejectionThreshold), ThreadPoolRejection Exception will be thrown.

When SynchronousQueue is used and the number of concurrent requests is beyond (threadPool.CoreSize), ThreadPoolRejection Exception will be thrown.

Here's a snippet providing it when SynchronousQueue is used:


submitting:0
submitting:1
submitting:2
submitting:3
submitting:4
submitting:5
running: 1
running: 2
running: 0
running: 4
running: 3
submitting:6
get Object of 6
Exception in thread "main" com.netflix.hystrix.exception.HystrixRuntimeException: TestHystrix$1 could not be queued for execution and no fallback available.
 at com.netflix.hystrix.AbstractCommand$21.call(AbstractCommand.java:783)
 at com.netflix.hystrix.AbstractCommand$21.call(AbstractCommand.java:768)
 at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$1.onError(OperatorOnErrorResumeNextViaFunction.java:99)
 at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71)
 at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71)
...
Note that ThreadPoolRejection starts to happen when all 5 threads in the threadPool have been taken.