Go deep into Hystrix’s underlying execution process

This is the 8th day of my participation in Gwen Challenge

Following up on the previous article [SpringCloud Series] dive into the underlying Hystrix execution process (1)

5. Check whether the thread pool/queue/semaphore is full

If the command pool and queue are full, or if the Semaphore semaphore is full, the command is not executed and the Fallback demotion mechanism is invoked, sending a REJECT message to the breaker statistics.

6. The command execution

Call HystrixCommand’s Run () method or HystrixObservableCommand object’s Construct () method to actually execute the command

7. Disconnect health check

Hystrix sends success, failure, Reject, Timeout, and other events for each dependent service to the Circuit breaker, which counts the number of these events. Determine whether to break the circuit according to the proportion of abnormal events * (5s abnormal times 20 times to open the circuit breaker). If the circuit breaker is opened, the circuit breaker will be directly disconnected within a period of time and the result of degradation will be returned.

8. Invoke the fallback degradation mechanism

HystrixObservableCommand implements the resumeWithFallback() method, which returns an Observable that provides the degrade result. If a Fallback is not implemented, or if it throws an exception, Hystrix returns an Observable, but no data.

Hystrix invokes the Fallback degradation mechanism in the following scenarios

  • The circuit breaker is on
  • Thread pool/queue/Semaphore is full
  • Command Execution Timeout
  • Run () or Construct () throws an exception

In a degrade mechanism, it is recommended to return the default results, such as static code logic, or to extract data from an in-memory cache, with minimal network requests.

Different command execution modes have different results when the fallback is empty or an exception occurs

  • For execute(), throw an exception directly
  • For queue(), return a Future that throws an exception when you call get()
  • For Observe (), return an Observable, but call subscribe() to subscribe it immediately throws the caller’s onError() method.
  • ToObservable () returns an Observable, but calls to subscribe() immediately throw the caller’s onError() method.

Different ways of execution

  • Execute () : Gets a future.get () and a single result
  • Queue () : Returns a Future
  • Observe () : Subscribe to an Observable immediately and start 8 steps to return a copy of the Observable.
  • ToObservable () : Returns a raw Observable that must be subscribed manually to perform the eight steps.