熔断器 Hystrix 源码解析 —— 命令合并执行

豪猪的刺长在哪里:Hystrix隔离舱、熔断状态机、请求缓存合并与线程池推算的源码推演 Hystrix虽已进入维护模式,但它把隔离、熔断、降级、请求缓存、请求合并与监控组合成一套完整的容错范式,至今仍是理解微服务韧性的最佳样本。本文先从慢依赖耗尽线程的雪崩推演出发,拆解命令模式与九道执行关口,结合源码剖析线程池与信号量隔离、三态熔断状态机与五百毫秒二十桶的健康统计、请求上下文缓存与定时节拍合并,并用虚拟时间仿真量化隔离、熔断与合并的收益和代价,最后给出线程池与超时推算、配置优先级、线程上下文传递等生产实践要点,以及向Resilience4j和Sentinel迁移的思想演进对照。 阅读详情

摘要: 原创出处 http://www.iocoder.cn/Hystrix/command-collapser-execute/ 「芋道源码」欢迎转载,保留摘要,谢谢!

本文主要基于 Hystrix 1.5.X 版本

  • 1. 概述

  • 2. HystrixCollapser

    • 2.1 构造方法

    • 2.2 执行命令方式

    • 2.3 核心方法

  • 3. RequestCollapserFactory

  • 4. RequestCollapser

    • 4.1 构造方法

    • 4.2 RequestBatch

    • 4.3 #submitRequest(arg)

    • 4.4 #createNewBatchAndExecutePreviousIfNeeded(previousBatch)

  • 5. CollapserTimer

    • 5.1 RealCollapserTimer

    • 5.2 CollapsedTask

  • 666. 彩蛋


  • 友情提示:欢迎关注公众号【芋道源码】。????关注后,拉你进【源码圈】微信群和【芋艿】搞基嗨皮。

  • 友情提示:欢迎关注公众号【芋道源码】。????关注后,拉你进【源码圈】微信群和【芋艿】搞基嗨皮。

  • 友情提示:欢迎关注公众号【芋道源码】。????关注后,拉你进【源码圈】微信群和【芋艿】搞基嗨皮。


1. 概述

本文主要分享 Hystrix 命令合并执行。

在 《【翻译】Hystrix文档-实现原理》「请求合并」 中,对 Hystrix 命令合并执行的概念、原理、使用场景、优缺点已经做了非常详细透彻的分享,所以胖友可以先认真阅读学习下。

命令合并执行整体流程如下图 :

FROM 《【翻译】Hystrix文档-实现原理》「请求合并」

  • 第一步,提交单个命令请求到请求队列( RequestQueue )

  • 第二部,定时任务( TimerTask ) 固定周期从请求队列获取多个命令执行,合并执行。

在官方提供的示例中,我们通过 CommandCollapserGetValueForKey 熟悉命令合并执行的使用。


推荐 Spring Cloud 书籍:

  • 请支持正版。下载盗版,等于主动编写低级 BUG 。

  • 程序猿DD —— 《Spring Cloud微服务实战》

  • 周立 —— 《Spring Cloud与Docker微服务架构实战》

  • 两书齐买,京东包邮。

2. HystrixCollapser

com.netflix.hystrix.HystrixCollapser ,命令合并器抽象父类。

NOTE : com.netflix.hystrix.HystrixObservableCollapser ,另一种命令合并器抽象父类,本文暂不解析。

2.1 构造方法

HystrixCollapser 构造方法,代码如下 :

  1. public abstract class HystrixCollapser<BatchReturnType, ResponseType, RequestArgumentType>

  2.        implements HystrixExecutable<ResponseType>, HystrixObservable<ResponseType> {

  3.    private final RequestCollapserFactory<BatchReturnType, ResponseType, RequestArgumentType> collapserFactory;

  4.    private final HystrixRequestCache requestCache;

  5.    private final HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> collapserInstanceWrapper;

  6.    private final HystrixCollapserMetrics metrics;

  7.    /* package for tests */ HystrixCollapser(HystrixCollapserKey collapserKey, Scope scope, CollapserTimer timer, HystrixCollapserProperties.Setter propertiesBuilder, HystrixCollapserMetrics metrics) {

  8.        if (collapserKey == null || collapserKey.name().trim().equals("")) {

  9.            String defaultKeyName = getDefaultNameFromClass(getClass());

  10.            collapserKey = HystrixCollapserKey.Factory.asKey(defaultKeyName);

  11.        }

  12.        HystrixCollapserProperties properties = HystrixPropertiesFactory.getCollapserProperties(collapserKey, propertiesBuilder);

  13.        this.collapserFactory = new RequestCollapserFactory<BatchReturnType, ResponseType, RequestArgumentType>(collapserKey, scope, timer, properties);

  14.        this.requestCache = HystrixRequestCache.getInstance(collapserKey, HystrixPlugins.getInstance().getConcurrencyStrategy());

  15.        if (metrics == null) {

  16.            this.metrics = HystrixCollapserMetrics.getInstance(collapserKey, properties);

  17.        } else {

  18.            this.metrics = metrics;

  19.        }

  20.        final HystrixCollapser<BatchReturnType, ResponseType, RequestArgumentType> self = this;

  21.         /* strategy: HystrixMetricsPublisherCollapser */

  22.        HystrixMetricsPublisherFactory.createOrRetrievePublisherForCollapser(collapserKey, this.metrics, properties);

  23.        /**

  24.         * Used to pass public method invocation to the underlying implementation in a separate package while leaving the methods 'protected' in this class.

  25.         */

  26.        collapserInstanceWrapper = new HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType>() {

  27.            @Override

  28.            public Collection<Collection<CollapsedRequest<ResponseType, RequestArgumentType>>> shardRequests(Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests) {

  29.                Collection<Collection<CollapsedRequest<ResponseType, RequestArgumentType>>> shards = self.shardRequests(requests);

  30.                self.metrics.markShards(shards.size());

  31.                return shards;

  32.            }

  33.            @Override

  34.            public Observable<BatchReturnType> createObservableCommand(Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests) {

  35.                final HystrixCommand<BatchReturnType> command = self.createCommand(requests);

  36.                command.markAsCollapsedCommand(this.getCollapserKey(), requests.size());

  37.                self.metrics.markBatch(requests.size());

  38.                return command.toObservable();

  39.            }

  40.            @Override

  41.            public Observable<Void> mapResponseToRequests(Observable<BatchReturnType> batchResponse, final Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests) {

  42.                return batchResponse.single().doOnNext(new Action1<BatchReturnType>() {

  43.                    @Override

  44.                    public void call(BatchReturnType batchReturnType) {

  45.                        // this is a blocking call in HystrixCollapser

  46.                        self.mapResponseToRequests(batchReturnType, requests);

  47.                    }

  48.                }).ignoreElements().cast(Void.class);

  49.            }

  50.            @Override

  51.            public HystrixCollapserKey getCollapserKey() {

  52.                return self.getCollapserKey();

  53.            }

  54.        };

  55.    }

  56. }

  • BatchReturnType 泛型,多个命令合并执行返回结果类型。

  • ResponseType 泛型,单个命令执行返回结果类型。

  • RequestArgumentType 泛型,单个命令参数类型。

  • collapserFactory 属性,RequestCollapser 工厂,在 「3. RequestCollapserFactory」 详细解析。

  • requestCache 属性,TODO 【2012】【请求上下文】

  • collapserInstanceWrapper 属性,命令合并器包装器。

    • com.netflix.hystrix.collapser.HystrixCollapserBridge 接口,点击 链接 查看代码。

    • HystrixCollapserBridge ,为 RequestBatch 透明调用 HystrixCollapser 或 HystrixObservableCollapser 的方法不同的实现。参见 《桥接模式》 。

  • metrics 属性,TODO 【2002】【metrics】

2.2 执行命令方式

在 《Hystrix 源码解析 —— 执行命令方式》 中,我们已经看了 HystrixCommand 提供的四种执行命令方式。

HystrixCollapser 类似于 HystrixCommand ,也提供四种相同的执行命令方式,其中如下三种方式代码基本类似,我们就给下传送门,就不重复啰嗦了 :

  • #observe() 方法 :传送门 。

  • #queue() 方法 :传送门 。

  • #execute() 方法 :传送门 。

下面一起来看看 #toObservable() 方法的实现,代码如下 :

  1.  1: public Observable<ResponseType> toObservable() {

  2.  2:     // when we callback with the data we want to do the work

  3.  3:     // on a separate thread than the one giving us the callback

  4.  4:     return toObservable(Schedulers.computation());

  5.  5: }

  6.  6:

  7.  7: public Observable<ResponseType> toObservable(Scheduler observeOn) {

  8.  8:     return Observable.defer(new Func0<Observable<ResponseType>>() {

  9.  9:         @Override

  10. 10:         public Observable<ResponseType> call() {

  11. 11:             // // 缓存开关、缓存KEY

  12. 12:             final boolean isRequestCacheEnabled = getProperties().requestCacheEnabled().get();

  13. 13:             final String cacheKey = getCacheKey();

  14. 14:

  15. 15:             // 优先从缓存中获取

  16. 16:             /* try from cache first */

  17. 17:             if (isRequestCacheEnabled) {

  18. 18:                 HystrixCachedObservable<ResponseType> fromCache = requestCache.get(cacheKey);

  19. 19:                 if (fromCache != null) {

  20. 20:                     metrics.markResponseFromCache();

  21. 21:                     return fromCache.toObservable();

  22. 22:                 }

  23. 23:             }

  24. 24:

  25. 25:             // 获得 RequestCollapser

  26. 26:             RequestCollapser<BatchReturnType, ResponseType, RequestArgumentType> requestCollapser = collapserFactory.getRequestCollapser(collapserInstanceWrapper);

  27. 27:

  28. 28:             // 提交 命令请求

  29. 29:             Observable<ResponseType> response = requestCollapser.submitRequest(getRequestArgument());

  30. 30:

  31. 31:             // 获得 缓存Observable

  32. 32:             if (isRequestCacheEnabled && cacheKey != null) {

  33. 33:                 HystrixCachedObservable<ResponseType> toCache = HystrixCachedObservable.from(response);

  34. 34:                 HystrixCachedObservable<ResponseType> fromCache = requestCache.putIfAbsent(cacheKey, toCache);

  35. 35:                 if (fromCache == null) {

  36. 36:                     return toCache.toObservable();

  37. 37:                 } else {

  38. 38:                     toCache.unsubscribe(); // 取消订阅

  39. 39:                     return fromCache.toObservable();

  40. 40:                 }

  41. 41:             }

  42. 42:

  43. 43:             // 获得 非缓存Observable

  44. 44:             return response;

  45. 45:         }

  46. 46:     });

  47. 47: }

  • observeOn 方法参数,实际方法暂未用到,跳过无视。

  • 第 11 至 13 行 :缓存存开关、KEY 。

  • 【反向】第 32 至 41 行 :获得【缓存 Observable】。这块代码和 AbstractCommand#toObservavle(...) 类似,在《Hystrix 源码解析 —— 执行结果缓存》「4. AbstractCommand#toObservavle(...)」 有详细解析。

  • 【反向】第 44 行 :获得【非缓存 Observable】。

  • 注意 :返回的 Observable ,很可能命令实际并未执行,或者说并未执行完成,此时在 #queue() / #execute() 方法,通过 BlockingObservable 阻塞等待执行完成。BlockingObservable 在 《RxJava 源码解析 —— BlockingObservable》 有详细解析。

  • 第 26 行 :调用 RequestCollapserFactory#getRequestCollapser() ,获得 RequestCollapser 。在 「3. RequestCollapserFactory」 详细解析。

  • 第 29 行 :提交单个命令请求到请求队列( RequestQueue ),即命令合并执行整体流程第一步。在 「4. RequestCollapser」 详细解析。

2.3 核心方法

  • #getRequestArgument(...) 抽象方法,获得单个命令参数。代码如下 :

  1. public abstract RequestArgumentType getRequestArgument();


  • #createCommand(...) 抽象方法,将多个命令请求合并,创建一个 HystrixCommand 。代码如下 :

  1. protected abstract HystrixCommand<BatchReturnType> createCommand(Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests);


  • #mapResponseToRequests(...) 抽象方法,将一个 HystrixCommand 的执行结果,映射回对应的命令请求们。

  1. protected abstract void mapResponseToRequests(BatchReturnType batchResponse, Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests);


  • #shardRequests(...) 方法,将多个命令请求分片成 N 个【多个命令请求】。默认实现下,不进行分片。代码如下 :

  1. protected Collection<Collection<CollapsedRequest<ResponseType, RequestArgumentType>>> shardRequests(Collection<CollapsedRequest<ResponseType, RequestArgumentType>> requests) {

  2.    return Collections.singletonList(requests);

  3. }


  • 在未重写 #shardRequests(...) 的情况下,整体方法流程如下 :

  • 在重写 #shardRequests(...) 的情况下,整体方法流程如下 :

    • 本图中命令请求分片仅仅是例子,实际根据重写的逻辑不同而不同。

3. RequestCollapserFactory

com.netflix.hystrix.collapser.RequestCollapserFactory ,RequestCollapser 工厂。

  1. public class RequestCollapserFactory<BatchReturnType, ResponseType, RequestArgumentType> {

  2.    private final CollapserTimer timer;

  3.    private final HystrixCollapserKey collapserKey;

  4.    private final HystrixCollapserProperties properties;

  5.    private final HystrixConcurrencyStrategy concurrencyStrategy;

  6.    private final Scope scope;

  7.    public RequestCollapserFactory(HystrixCollapserKey collapserKey, Scope scope, CollapserTimer timer, HystrixCollapserProperties properties) {

  8.         /* strategy: ConcurrencyStrategy */

  9.        this.concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();

  10.        this.timer = timer;

  11.        this.scope = scope;

  12.        this.collapserKey = collapserKey;

  13.        this.properties = properties;

  14.    }

  • timer 属性,命令合并器的定时器,在 「5. CollapserTimer」 详细解析。

  • collapserKey 属性,命令合并器标识,实现类似 HystrixThreadPoolKey 。

    • HystrixCollapserKey ,点击 链接 查看代码。

    • HystrixThreadPoolKey ,在 《Hystrix 源码解析 —— 命令执行(二)之执行隔离策略》「3. HystrixThreadPoolKey」 有详细解析。

  • properties 属性,命令合并器属性配置。

  • concurrencyStrategy 属性,并发策略,在 《Hystrix 源码解析 —— 命令执行(二)之执行隔离策略》「4. HystrixConcurrencyStrategy」 有详细解析。

  • scope 属性,命令请求作用域。目前有两种作用域 :

    • REQUEST :请求上下文( HystrixRequestContext )。

      Typically this means that requests within a single user-request (ie. HTTP request) are collapsed.
      No interaction with other user requests.
      1 queue per user request.

    • GLOBAL :全局。

      Requests from any thread (ie. all HTTP requests) within the JVM will be collapsed.
      1 queue for entire app.


调用 #getRequestCollapser() 方法,获得 RequestCollapser 。代码如下 :

  1. public RequestCollapser<BatchReturnType, ResponseType, RequestArgumentType> getRequestCollapser(HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> commandCollapser) {

  2.   if (Scopes.REQUEST == Scopes.valueOf(getScope().name())) {

  3.       return getCollapserForUserRequest(commandCollapser);

  4.   } else if (Scopes.GLOBAL == Scopes.valueOf(getScope().name())) {

  5.       return getCollapserForGlobalScope(commandCollapser);

  6.   } else {

  7.       logger.warn("Invalid Scope: {}  Defaulting to REQUEST scope.", getScope());

  8.       return getCollapserForUserRequest(commandCollapser);

  9.   }

  10. }

  • 根据 scope 不同,调用两个不同方法,获得 RequestCollapser 。这两个方法大体逻辑相同,优先从缓存中查找满足条件的 RequestCollapser 返回;若不存在,则创建满足条件的 RequestCollapser 添加到缓存并返回。

    • REQUEST :调用 #getCollapserForUserRequest() 方法,TODO 【2012】【请求上下文】。

    • GLOBAL :调用 #getCollapserForGlobalScope() 方法,点击 链接 查看中文注释的代码。

4. RequestCollapser

com.netflix.hystrix.collapser.RequestCollapser ,命令请求合并器。主要用于 :

  • 提交单个命令请求到请求队列( RequestQueue )。

  • 接收来自定时任务提交的多个命令,合并执行。

4.1 构造方法

RequestCollapser 构造方法,代码如下 :

  1. public class RequestCollapser<BatchReturnType, ResponseType, RequestArgumentType> {

  2.    private final HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> commandCollapser;

  3.    // batch can be null once shutdown

  4.    private final AtomicReference<RequestBatch<BatchReturnType, ResponseType, RequestArgumentType>> batch = new AtomicReference<RequestBatch<BatchReturnType, ResponseType, RequestArgumentType>>();

  5.    private final AtomicReference<Reference<TimerListener>> timerListenerReference = new AtomicReference<Reference<TimerListener>>();

  6.    private final AtomicBoolean timerListenerRegistered = new AtomicBoolean();

  7.    private final CollapserTimer timer;

  8.    private final HystrixCollapserProperties properties;

  9.    private final HystrixConcurrencyStrategy concurrencyStrategy;

  10.    RequestCollapser(HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> commandCollapser, HystrixCollapserProperties properties, CollapserTimer timer, HystrixConcurrencyStrategy concurrencyStrategy) {

  11.        this.commandCollapser = commandCollapser; // the command with implementation of abstract methods we need

  12.        this.concurrencyStrategy = concurrencyStrategy;

  13.        this.properties = properties;

  14.        this.timer = timer;

  15.        batch.set(new RequestBatch<BatchReturnType, ResponseType, RequestArgumentType>(properties, commandCollapser, properties.maxRequestsInBatch().get()));

  16.    }

  17. }

  • commandCollapser 属性,命令合并器包装器。

  • batch 属性,RequestBatch,即是本文一直说的请求队列。在 「4.2 RequestBatch」 也会详细解析。

  • timerListenerReference 属性,注册在命令合并器的定时器的监听器。每个 RequestCollapser 独有一个监听器。该监听器( 实际上会使用该监听器创建定时任务 )固定周期从请求队列获取多个命令执行,提交 RequestCollapser 合并执行。在 「5. CollapserTimer」 也会详细解析。

  • timerListenerRegistered 属性, timerListenerReference 是否已经注册。

  • timer 属性,命令合并器的定时器。

  • properties 属性,命令合并器属性配置。

  • concurrencyStrategy 属性,并发策略。

4.2 RequestBatch

com.netflix.hystrix.collapser.RequestBatch ,命令请求队列。提供如下功能 :

  • 命令请求的添加

  • 命令请求的移除

  • 命令请求的批量执行。笔者把 RequestBatch 解释成 "命令请求队列",主要方便大家理解。

    • 那可能有胖友有疑问,为啥该功能不在 RequestCollapser 直接实现,这样 RequestBatch 成为纯粹的队列呢?在 「4.4 #createNewBatchAndExecutePreviousIfNeeded(previousBatch)」 详细解析。

RequestBatch 构造方法,代码如下 :

  1. public class RequestBatch<BatchReturnType, ResponseType, RequestArgumentType> {

  2.    private final HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> commandCollapser;

  3.    private final int maxBatchSize;

  4.    private final AtomicBoolean batchStarted = new AtomicBoolean();

  5.    private final ConcurrentMap<RequestArgumentType, CollapsedRequest<ResponseType, RequestArgumentType>> argumentMap =

  6.            new ConcurrentHashMap<RequestArgumentType, CollapsedRequest<ResponseType, RequestArgumentType>>();

  7.    private final HystrixCollapserProperties properties;

  8.    private ReentrantReadWriteLock batchLock = new ReentrantReadWriteLock();

  9.    public RequestBatch(HystrixCollapserProperties properties, HystrixCollapserBridge<BatchReturnType, ResponseType, RequestArgumentType> commandCollapser, int maxBatchSize) {

  10.        this.properties = properties;

  11.        this.commandCollapser = commandCollapser;

  12.        this.maxBatchSize = maxBatchSize;

  13.    }

  14. }

  • commandCollapser 属性,命令合并器包装器。

  • maxBatchSize 属性,队列最大长度。

  • batchStarted 属性,执行是否开始。

  • argumentMap 属性,命令请求参数映射( 队列 )。

  • properties 属性,命令合并器属性配置。

  • batchLock 属性, argumentMap 操作的读写锁。

RequestBatch 实现队列具体的操作方法,在 「4.3 #submitRequest(arg)」/「4.4 #createNewBatchAndExecutePreviousIfNeeded(previousBatch)」 一起解析。

4.3 #submitRequest(arg)

在 #toObservable() 方法里,调用 #submitRequest(arg) 方法,提交单个命令请求到 RequestBatch 。代码如下 :

  1.  1: public Observable<ResponseType> submitRequest(final RequestArgumentType arg) {

  2.  2:     /*

  3.  3:      * We only want the timer ticking if there are actually things to do so we register it the first time something is added.

  4.  4:      */

  5.  5:     if (!timerListenerRegistered.get() && timerListenerRegistered.compareAndSet(false, true)) {

  6.  6:         /* schedule the collapsing task to be executed every x milliseconds (x defined inside CollapsedTask) */

  7.  7:         timerListenerReference.set(timer.addListener(new CollapsedTask()));

  8.  8:     }

  9.  9:

  10. 10:     // loop until succeed (compare-and-set spin-loop)

  11. 11:     while (true) {

  12. 12:         // 获得 RequestBatch

  13. 13:         final RequestBatch<BatchReturnType, ResponseType, RequestArgumentType> b = batch.get();

  14. 14:         if (b == null) {

  15. 15:             return Observable.error(new IllegalStateException("Submitting requests after collapser is shutdown"));

  16. 16:         }

  17. 17:

  18. 18:         // 添加到 RequestBatch

  19. 19:         final Observable<ResponseType> response;

  20. 20:         if (arg != null) {

  21. 21:             response = b.offer(arg);

  22. 22:         } else {

  23. 23:             response = b.offer( (RequestArgumentType) NULL_SENTINEL);

  24. 24:         }

  25. 25:

  26. 26:         // 添加成功,返回 Observable

  27. 27:         // it will always get an Observable unless we hit the max batch size

  28. 28:         if (response != null) {

  29. 29:             return response;

  30. 30:         } else {

  31. 31:             // 添加失败,执行 RequestBatch ,并创建新的 RequestBatch

  32. 32:             // this batch can't accept requests so create a new one and set it if another thread doesn't beat us

  33. 33:             createNewBatchAndExecutePreviousIfNeeded(b);

  34. 34:         }

  35. 35:     }

  36. 36: }

  • 第 5 至 8 行 :当 RequestCollapser 的监听任务( CollapsedTask )还未创建,进行初始化。

  • 第 11 至 35 行 :死循环,直到提交单个命令请求到 RequestBatch 成功。

    • 第 13 至 16 行 :获得 RequestBatch 。从目前代码看下来,除非 RequestCollapser 被 #shutdown() 后才会出现为 null 的情况。

    • 第 19 至 24 行 :调动 RequestBatch#offer(...) 方法,提交单个命令请求到 RequestBatch ,并获得 Observable 。这里对 arg==null 做了特殊处理,因为 RequestBatch.argumentMap 是 ConcurrentHashMap ,不允许值为 null 。另外, RequestBatch#offer(...) 方法的实现代码,在结束了当前方法,详细解析。

    • 第 28 至 29 行 :添加成功,返回 Observable 。

    • 第 30 至 34 行 :添加失败,执行当前 RequestBatch 的多个命令合并执行,并创建新的 RequestBatch 。在 「4.4 #createNewBatchAndExecutePreviousIfNeeded(previousBatch)」 详细解析。


RequestBatch#offer(...) 方法,代码如下 :

  1.  1: public Observable<ResponseType>  offer(RequestArgumentType arg) {

  2.  2:     // 执行已经开始,添加失败

  3.  3:     /* short-cut - if the batch is started we reject the offer */

  4.  4:     if (batchStarted.get()) {

  5.  5:         return null;

  6.  6:     }

  7.  7:

  8.  8:     /*

  9.  9:      * The 'read' just means non-exclusive even though we are writing.

  10. 10:      */

  11. 11:     if (batchLock.readLock().tryLock()) {

  12. 12:         try {

  13. 13:             // 执行已经开始,添加失败

  14. 14:             /* double-check now that we have the lock - if the batch is started we reject the offer */

  15. 15:             if (batchStarted.get()) {

  16. 16:                 return null;

  17. 17:             }

  18. 18:

  19. 19:             // 超过队列最大长度,添加失败

  20. 20:             if (argumentMap.size() >= maxBatchSize) {

  21. 21:                 return null;

  22. 22:             } else {

  23. 23:                 // 创建 CollapsedRequestSubject ,并添加到队列

  24. 24:                 CollapsedRequestSubject<ResponseType, RequestArgumentType> collapsedRequest = new CollapsedRequestSubject<ResponseType, RequestArgumentType>(arg, this);

  25. 25:                 final CollapsedRequestSubject<ResponseType, RequestArgumentType> existing = (CollapsedRequestSubject<ResponseType, RequestArgumentType>) argumentMap.putIfAbsent(arg, collapsedRequest);

  26. 26:                 /**

  27. 27:                  * If the argument already exists in the batch, then there are 2 options:

  28. 28:                  * A) If request caching is ON (the default): only keep 1 argument in the batch and let all responses

  29. 29:                  * be hooked up to that argument

  30. 30:                  * B) If request caching is OFF: return an error to all duplicate argument requests

  31. 31:                  *

  32. 32:                  * This maintains the invariant that each batch has no duplicate arguments.  This prevents the impossible

  33. 33:                  * logic (in a user-provided mapResponseToRequests for HystrixCollapser and the internals of HystrixObservableCollapser)

  34. 34:                  * of trying to figure out which argument of a set of duplicates should get attached to a response.

  35. 35:                  *

  36. 36:                  * See https://github.com/Netflix/Hystrix/pull/1176 for further discussion.

  37. 37:                  */

  38. 38:                 if (existing != null) {

  39. 39:                     boolean requestCachingEnabled = properties.requestCacheEnabled().get();

  40. 40:                     if (requestCachingEnabled) {

  41. 41:                         return existing.toObservable();

  42. 42:                     } else {

  43. 43:                         return Observable.error(new IllegalArgumentException("Duplicate argument in collapser batch : [" + arg + "]  This is not supported.  Please turn request-caching on for HystrixCollapser:" + commandCollapser.getCollapserKey().name() + " or prevent duplicates from making it into the batch!"));

  44. 44:                     }

  45. 45:                 } else {

  46. 46:                     return collapsedRequest.toObservable();

  47. 47:                 }

  48. 48:

  49. 49:             }

  50. 50:         } finally {

  51. 51:             batchLock.readLock().unlock();

  52. 52:         }

  53. 53:     } else {

  54. 54:         return null;

  55. 55:     }

  56. 56: }

  • 第 4 至 6 行 :执行已经开始,添加失败。在 RequestBatch#executeBatchIfNotAlreadyStarted(...) 方法的开头,优先 CAS 使 batchStarted=true 。

  • 第 11 行 :获得读锁。 The'read'just means non-exclusive even though we are writing. ,即使该方法实际在做"写操作",不排他,线程安全,所以可以使用读锁。

  • 第 15 至 17 行 : double-check,执行已经开始,添加失败。在 RequestBatch#executeBatchIfNotAlreadyStarted(...) 方法,优先 CAS 使 batchStarted=true,再获取写锁,所以会出现该情况。

  • 第 20 至 21 行 :超过队列最大长度,添加失败。

  • 第 24 至 25 行 :创建 com.netflix.hystrix.collapser.CollapsedRequestSubject ,并将它添加到队列( argumentMap ) 。

    • argument 属性,单个命令请求参数。

    • valueSet 属性,结果( Response ) 是否设置,通过 #setResponse()/ #emitResponse() 方法设置。

    • subject 属性,可回放执行结果的 Subject 。此处使用 ReplaySubject 的主要目的,当 HystrixCollapser 开启缓存功能时,通过回放执行结果,在 《Hystrix 源码解析 —— 执行结果缓存》「5. HystrixCachedObservable」 也有相同的实现。另外,这里有一点要注意下,ReplaySubject 并没有向任何 Observable 订阅结果,而是通过 #setResponse()/ #emitResponse() 方法设置结果。

    • outstandingSubscriptions 属性,订阅数量。

    • subjectWithAccounting 属性,带订阅数量的 ReplaySubject 。当取消订阅时,调用 RequestBatch#remove(arg) 方法,移除单个命令请求。

    • CollapsedRequestSubject 实现 com.netflix.hystrix.HystrixCollapser.CollapsedRequest 接口,定义了批量命令执行的请求,不仅限于获得请求参数( #getArgument() 方法 ),也包括对批量命令执行结束后,每个请求的结果设置( #setResponse(...)/ #emitResponse(...)/ #setException(...)/ #setComplete() 方法 ),点击 链接 查看该接口的代码。

    • CollapsedRequestSubject 构造方法,代码如下:

  1. /* package */class CollapsedRequestSubject<T, R> implements CollapsedRequest<T, R> {

    /**
  2.  * 参数
  3.  */
  4. private final R argument;
  5. /**
  6.  * 结果( response ) 是否设置
  7.  */
  8. private AtomicBoolean valueSet = new AtomicBoolean(false);
  9. /**
  10.  * 可回放的 ReplaySubject
  11.  */
  12. private final ReplaySubject&lt;T&gt; subject = ReplaySubject.create();
  13. /**
  14.  * 带订阅数量的 ReplaySubject
  15.  */
  16. private final Observable&lt;T&gt; subjectWithAccounting;
  17. /**
  18.  * 订阅数量
  19.  */
  20. private volatile int outstandingSubscriptions = 0;
  21. public CollapsedRequestSubject(final R arg, final RequestBatch&lt;?, T, R&gt; containingBatch) {
  22.     // 设置 argument
  23.     if (arg == RequestCollapser.NULL_SENTINEL) {
  24.         this.argument = null;
  25.     } else {
  26.         this.argument = arg;
  27.     }
  28.     // 设置 带订阅数量的 ReplaySubject
  29.     this.subjectWithAccounting = subject
  30.             .doOnSubscribe(new Action0() {
  31.                 @Override
  32.                 public void call() {
  33.                     outstandingSubscriptions++;
  34.                 }
  35.             })
  36.             .doOnUnsubscribe(new Action0() {
  37.                 @Override
  38.                 public void call() {
  39.                     outstandingSubscriptions--;
  40.                     if (outstandingSubscriptions == 0) {
  41.                         containingBatch.remove(arg);
  42.                     }
  43.                 }
  44.             });
  45. }
  46. }

  • 第 38 至 47 行 :返回 Observable 。

    • 当 argumentMap 已经存在 arg 对应的 Observable 时,必须开启缓存 ( HystrixCollapserProperties.requestCachingEnabled=true ) 功能。原因是,如果在相同的 arg ,并且未开启缓存,同时第 43 行实现的是 collapsedRequest.toObservable() ,那么相同的 arg 将有多个 Observable 执行命令,此时 HystrixCollapserBridge#mapResponseToRequests(...) 方法无法将执行( Response )赋值到 arg 对应的命令请求( CollapsedRequestSubject ) 。更多讨论,见 https://github.com/Netflix/Hystrix/pull/1176 。

    • 回过头看 HystrixCollapser#toObservable() 方法的第 32 至 41 行的代码,这里也有对缓存功能,是不是重复了呢? argumentMap 针对的是 RequestBatch 级的缓存,HystrixCollapser : RequestCollapser : RequestBatch 是 1:1:N 的关系,通过 HystrixCollapser#toObservable() 对缓存的处理逻辑,保证 RequestBatch 切换后,依然有缓存。


    RequestBatch#remove() 方法,代码如下 :

    1. /* package-private */ void remove(RequestArgumentType arg) {

    2.    if (batchStarted.get()) {

    3.        //nothing we can do

    4.        return;

    5.    }

    6.    if (batchLock.readLock().tryLock()) {

    7.        try {

    8.            /* double-check now that we have the lock - if the batch is started, deleting is useless */

    9.            if (batchStarted.get()) {

    10.                return;

    11.            }

    12.            argumentMap.remove(arg);

    13.        } finally {

    14.            batchLock.readLock().unlock();

    15.        }

    16.    }

    17. }

    • 当 RequestBatch 开始执行,不允许移除单个命令请求。

    4.4 #createNewBatchAndExecutePreviousIfNeeded(previousBatch)

    本小节建议在 「5. CollapserTimer」 后,再回过头看。

    #createNewBatchAndExecutePreviousIfNeeded(previousBatch) 方法,代码如下 :

    1.  1: private void createNewBatchAndExecutePreviousIfNeeded(RequestBatch<BatchReturnType, ResponseType, RequestArgumentType> previousBatch) {

    2.  2:     if (previousBatch == null) {

    3.  3:         throw new IllegalStateException("Trying to start null batch which means it was shutdown already.");

    4.  4:     }

    5.  5:     if (batch.compareAndSet(previousBatch, new RequestBatch<BatchReturnType, ResponseType, RequestArgumentType>(properties, commandCollapser, properties.maxRequestsInBatch().get()))) {

    6.  6:         // this thread won so trigger the previous batch

    7.  7:         previousBatch.executeBatchIfNotAlreadyStarted();

    8.  8:     }

    9.  9: }

    • 第 5 行 :通过 CAS 修改 batch ,保证并发情况下的线程安全。同时注意,此处也进行了新的 RequestBatch ,切换掉老的 RequestBatch 。

    • 第 6 行 :使用老的 RequestBatch ,调用 RequestBatch#executeBatchIfNotAlreadyStarted() 方法,命令合并执行。


    RequestBatch#executeBatchIfNotAlreadyStarted() 方法,代码如下 :

    1.  1: public void executeBatchIfNotAlreadyStarted() {

    2.  2:     /*

    3.  3:      * - check that we only execute once since there's multiple paths to do so (timer, waiting thread or max batch size hit)

    4.  4:      * - close the gate so 'offer' can no longer be invoked and we turn those threads away so they create a new batch

    5.  5:      */

    6.  6:     // 设置 执行已经开始

    7.  7:     if (batchStarted.compareAndSet(false, true)) {

    8.  8:         // 获得 写锁

    9.  9:         /* wait for 'offer'/'remove' threads to finish before executing the batch so 'requests' is complete */

    10. 10:         batchLock.writeLock().lock();

    11. 11:

    12. 12:         try {

    13. 13:             // 将多个命令请求分片成 N 个【多个命令请求】。

    14. 14:             // shard batches

    15. 15:             Collection<Collection<CollapsedRequest<ResponseType, RequestArgumentType>>> shards = commandCollapser.shardRequests(argumentMap.values());

    16. 16:             // for each shard execute its requests

    17. 17:             for (final Collection<CollapsedRequest<ResponseType, RequestArgumentType>> shardRequests : shards) {

    18. 18:                 try {

    19. 19:                     // 将多个命令请求合并,创建一个 HystrixCommand

    20. 20:                     // create a new command to handle this batch of requests

    21. 21:                     Observable<BatchReturnType> o = commandCollapser.createObservableCommand(shardRequests);

    22. 22:

    23. 23:                     // 将一个 HystrixCommand 的执行结果,映射回对应的命令请求们

    24. 24:                     commandCollapser.mapResponseToRequests(o, shardRequests).doOnError(new Action1<Throwable>() {

    25. 25:

    26. 26:                         /**

    27. 27:                          * This handles failed completions

    28. 28:                          */

    29. 29:                         @Override

    30. 30:                         public void call(Throwable e) {

    31. 31:                             // handle Throwable in case anything is thrown so we don't block Observers waiting for onError/onCompleted

    32. 32:                             Exception ee;

    33. 33:                             if (e instanceof Exception) {

    34. 34:                                 ee = (Exception) e;

    35. 35:                             } else {

    36. 36:                                 ee = new RuntimeException("Throwable caught while executing batch and mapping responses.", e);

    37. 37:                             }

    38. 38:                             logger.debug("Exception mapping responses to requests.", e);

    39. 39:                             // if a failure occurs we want to pass that exception to all of the Futures that we've returned

    40. 40:                             for (CollapsedRequest<ResponseType, RequestArgumentType> request : argumentMap.values()) {

    41. 41:                                 try {

    42. 42:                                     ((CollapsedRequestSubject<ResponseType, RequestArgumentType>) request).setExceptionIfResponseNotReceived(ee);

    43. 43:                                 } catch (IllegalStateException e2) {

    44. 44:                                     // if we have partial responses set in mapResponseToRequests

    45. 45:                                     // then we may get IllegalStateException as we loop over them

    46. 46:                                     // so we'll log but continue to the rest

    47. 47:                                     logger.error("Partial success of 'mapResponseToRequests' resulted in IllegalStateException while setting Exception. Continuing ... ", e2);

    48. 48:                                 }

    49. 49:                             }

    50. 50:                         }

    51. 51:

    52. 52:                     }).doOnCompleted(new Action0() {

    53. 53:

    54. 54:                         /**

    55. 55:                          * This handles successful completions

    56. 56:                          */

    57. 57:                         @Override

    58. 58:                         public void call() {

    59. 59:                             // check that all requests had setResponse or setException invoked in case 'mapResponseToRequests' was implemented poorly

    60. 60:                             Exception e = null;

    61. 61:                             for (CollapsedRequest<ResponseType, RequestArgumentType> request : shardRequests) {

    62. 62:                                 try {

    63. 63:                                    e = ((CollapsedRequestSubject<ResponseType, RequestArgumentType>) request).setExceptionIfResponseNotReceived(e,"No response set by " + commandCollapser.getCollapserKey().name() + " 'mapResponseToRequests' implementation.");

    64. 64:                                 } catch (IllegalStateException e2) {

    65. 65:                                     logger.debug("Partial success of 'mapResponseToRequests' resulted in IllegalStateException while setting 'No response set' Exception. Continuing ... ", e2);

    66. 66:                                 }

    67. 67:                             }

    68. 68:                         }

    69. 69:

    70. 70:                     }).subscribe();

    71. 71:                    

    72. 72:                 } catch (Exception e) {

    73. 73:                     // 异常

    74. 74:                     logger.error("Exception while creating and queueing command with batch.", e);

    75. 75:                     // if a failure occurs we want to pass that exception to all of the Futures that we've returned

    76. 76:                     for (CollapsedRequest<ResponseType, RequestArgumentType> request : shardRequests) {

    77. 77:                         try {

    78. 78:                             request.setException(e);

    79. 79:                         } catch (IllegalStateException e2) {

    80. 80:                             logger.debug("Failed trying to setException on CollapsedRequest", e2);

    81. 81:                         }

    82. 82:                     }

    83. 83:                 }

    84. 84:             }

    85. 85:

    86. 86:         } catch (Exception e) {

    87. 87:             // 异常

    88. 88:             logger.error("Exception while sharding requests.", e);

    89. 89:             // same error handling as we do around the shards, but this is a wider net in case the shardRequest method fails

    90. 90:             for (CollapsedRequest<ResponseType, RequestArgumentType> request : argumentMap.values()) {

    91. 91:                 try {

    92. 92:                     request.setException(e);

    93. 93:                 } catch (IllegalStateException e2) {

    94. 94:                     logger.debug("Failed trying to setException on CollapsedRequest", e2);

    95. 95:                 }

    96. 96:             }

    97. 97:         } finally {

    98. 98:             batchLock.writeLock().unlock();

    99. 99:         }

    100. 100:     }

    101. 101: }

    • 代码看起来是有点长哈,请对照着官方示例 CommandCollapserGetValueForKey 一起看,临门一脚了,胖友!

    • 第 7 行 :通过 CAS 修改 batchStarted ,保证并发情况下的线程安全。

    • 第 10 行 :获得写锁。等待调用 #offer(...)/ #remove(...) 方法的线程执行完成,以保证命令合并执行时,不再有新的请求添加或移除。

    • 第 15 行 :调用 HystrixCollapserBridge#shardRequests(...) 方法,将多个命令请求分片成 N 个【多个命令请求】。默认实现下,不进行分片。点击 链接 查看代码。

    • 第 17 行 :循环 N 个【多个命令请求】。

    • 第 21 行 :调用 HystrixCollapserBridge#createObservableCommand(...) 方法,将多个命令请求合并,创建一个 HystrixCommand 。点击 链接 查看代码。

    • 第 24 行 :调用 HystrixCollapserBridge#mapResponseToRequests(...) 方法,将一个 HystrixCommand 的执行结果,映射回对应的命令请求们。点击 链接 查看代码。

      • Observable#single() 方法,如果 Observable 终止时只发射了一个值,返回那个值,否则抛出异常。在 《ReactiveX文档中文翻译》「single」 有相关分享。

      • Observable#ignoreElements() 方法,抑制原始 Observable 发射的所有数据,只允许它的终止通知( #onError() 或 #onCompleted())通过。在 《ReactiveX文档中文翻译》「IgnoreElements」 有相关分享。也推荐点击 rx.internal.operators.OperatorIgnoreElements 看下源码,可能更加易懂。

      • Observable#cast() 方法,将原始 Observable 发射的每一项数据都强制转换为一个指定的类型,然后再发射数据,它是 map 的一个特殊版本。在 《ReactiveX文档中文翻译》「cast」 有相关分享。也推荐点击 rx.internal.operators.OperatorCast 看下源码,可能更加易懂。

      • 使用 Observable#ignoreElements()/ Observable#cast() 方法,用于将 Observable 变成不再继续向下发射数据项,只给现有方法里 Observable#doNext() 处理数据项,调用 HystrixCollapser#mapResponseToRequests(...) 方法。

      • 点击 链接 ,查看 CollapsedRequestSubject#setResponse(response) 方法的代码。

    • 第 24 至 50 行 :调用 Observable#doError(Action1) 方法,当命令合并执行发生异常时,设置每个CollapsedRequestSubject 的执行结果为异常。

      • 点击 链接,查看 CollapsedRequestSubject#setResponse(response) 方法的代码。

    • 第 52 至 68 行 :调用 Observable#doOnCompleted(Action0) 方法,当命令合并执行完成时,检查每个CollapsedRequestSubject 是否都有返回结果。设置没有返回结果的 CollapsedRequestSubject 的执行结果为异常。一般情况下,是用户实现 HystrixCollapser#mapResponseToRequests(...) 方法存在 BUG 。另外,如果不设置,将导致无结果的单个命令请求无限阻塞。

    • 第 70 行 :调用 Observable#subscribe() 方法,触发 HystrixCommand 执行。

    • 第 72 至 96 行 :发生异常,设置每个 CollapsedRequestSubject 的执行结果为异常。

      • 点击 链接,查看 CollapsedRequestSubject#setException(response) 方法的代码。

    • 第 97 至 99 行 :释放写锁。

    5. CollapserTimer

    com.netflix.hystrix.collapser.CollapserTimer ,命令合并器的定时器接口,定义了提交定时监听器,生成定时任务的接口方法,代码如下 :

    1. public interface CollapserTimer {

    2.    Reference<TimerListener> addListener(TimerListener collapseTask);

    3. }

    5.1 RealCollapserTimer

    com.netflix.hystrix.collapser.RealCollapserTimer ,命令合并器的定时器实现类,代码如下 :

    1. public class RealCollapserTimer implements CollapserTimer {

    2.    /* single global timer that all collapsers will schedule their tasks on */

    3.    private final static HystrixTimer timer = HystrixTimer.getInstance();

    4.    @Override

    5.    public Reference<TimerListener> addListener(TimerListener collapseTask) {

    6.        return timer.addTimerListener(collapseTask);

    7.    }

    8. }

    • 实际上,使用的是 HystrixTimer 提供的单例。在 《Hystrix 源码解析 —— 执行结果缓存》「3. HystrixTimer 」 有详细解析。

    5.2 CollapsedTask

    com.netflix.hystrix.collapser.RequestCollapser.CollapsedTask ,定时任务,固定周期( 可配,默认 HystrixCollapserProperties.timerDelayInMilliseconds=10ms ) 轮询其对应的一个 RequestCollapser 当前RequestBatch 。若有命令需要执行,则提交 RequestCollapser 合并执行。

    代码比较简单,点击 链接 直接看代码。

    666. 彩蛋

    T T 一开始把命令合并执行,理解成类似线程池批量执行任务,怎么看官方示例,怎么奇怪。有一样的同学,一起泪目 + 握爪下。

    本文有点点长,实在不想拆分成多篇。

    恩,另外部分地方写的不够清晰,欢迎一起讨论和优化。

    胖友,分享一波朋友圈可好!

android+音乐节拍检测,科学网—音乐节拍跟踪或音乐节拍检测软件,LilyBeats alpha - 石自强的博文... 我们设计并实现了一种用于实时跟踪音乐节拍的软件。该软件可以用于智能灯方案提供商,为其提供灯光随音乐变化的核心解决方案。首先给出软件的下载链接:或者音乐节拍是乐曲中表示固定单位时值和强弱规律的组织形式,亦称拍子。它有两个特性:周期性和连续性。节拍周期性表现为节拍结构,是乐曲中周期性出现的节奏序列。节拍连续性表现为音乐的平均速度,其单位为bpm(beat per minute每分钟音乐节拍的个数)。音... 阅读详情

相关推荐

闲置服务器之代理服务器篇

这台服务器是在阿里云免费白嫖的,天工开物计划,阿里给的300块无门槛优惠券,买了一台新加坡的境外服务器,288,刚刚抵扣完,因为是境外服务器,能够访问一些国内不能访问的网站,所以本文章仅从技术的角度来搭建代理服务器,使用时,请自觉遵守国家相关规定,如有任何违法行为,与本人无任何关系!!!。本文使用tinyproxy来搭建代理服务器。

m0_50281408的博客 2108

【微服务笔记08】微服务组件之Hystrix实现请求合并功能

微服务和微服务之间的调用,都是需要建立通信连接的,就拿HTTP接口来说,每一次的通信都需要建立和断开连接,如果微服务之间的调用非常频繁,那就会有很多的连接在建立和断开,为了减少请求连接的数量,Hystrix提出了请求合并的功能。请求合并,也就是将多个相同的请求合并成一个请求,然后只调用一次接口将所有的数据获取到,数据拿到之后,还需要将数据一次分发给合并之前的请求,这样每一个请求才能够正常的拿到属于自己的那一个返回结果。

✅ Java 开发工程师 1375

快速理解OSI七层模型(举例理解,数据传输过程,深入理解OSI七层模型)

我们需要了解互联网的本质是一系列的网络协议,这个协议就叫做OSI协议(开放系统互联(Open System Interconnection)),它是由ISO(国际标准化组织)定义的。 需要注意,随着时代的发展,OSI已经被TCP/IP 4层模型淘汰,在当今世界上并没有大规模的使用。 那么对于OSI,人们按照功能不同,分工不同,人为的将OSI的分为七层。实际上这七层是并不存在的,也就是说没有这些概念,而我们今天提到的七层概念,只是人为的划分而已。目的只是为了让大家更好地理解这些都是用来做什么的。

CN_TangZheng的博客 3万+

SpringCloud源码解析之Hystrix

文章目录一、Hystrix介绍一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 一、Hystrix介绍 Hystrix能够在依赖服务失效的情况下,通过隔离系统依赖服务的方式,防止服务级联失败;同时 Hystrix 提供失败回滚机制,使系统能够更快地从异常中恢复 提示:以下是本篇文章正文内容,下面案例可供参考 一、pandas是什么? 示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。 二、使用步骤 1.引入库 代码如下(示例): import nump

酸菜鱼_sky的博客 471

博图软件V15.1安装教程

博途软件安装

m0_46266496的博客 1万+

Mediapipe入门——搭建姿态检测模型并实时输出人体关节点3d坐标(2024.1.4更新)

Mediapipe入门,利用mediapipe搭建人体整体检测模型,并输出右手21个关节点的坐标;mp_holistic.Holistic函数理解,mp_holistic.Holistic.process函数理解;mp.solutions.drawing_utils.draw_landmarks函数理解;Mediapipe实时姿态估计,3d骨架铰接图,matplotlib和Mediapipe绘图

kalakalabala的博客 6万+

Hystrix进阶篇:断路器参数配置详解(二)

此配置项的值需要根据接口的 QPS 进行计算,值太小会有误打开熔断器的可能,值太大超出了时间窗口内的总请求数,则熔断永远也不会被触发。若共用同一个线程池的不同的方法,配置了相同的线程池属性,在第一个方法被执行后线程池的属性就已经被固定,因此会以第一个被执行的方法上的配置为主。当你正确配置它时,HystrixCommand 层的超时应该会很少见,但保护是存在的,以防网络延迟以外的其他因素影响时间,或者在最坏的情况下连接+读取+重试+连接+读取的组合仍然存在超过配置的整体超时时间。

汝果欲成诗,功夫在诗外。 1260

幸狐RV1106开发板运行Yolo8示例

rknn_model_zoo 是瑞芯微官方提供的 RKNPU 支持的各种主流算法的部署示例,目前对 Luckfox Pico 所使用的 RV1103/RV1106 有限支持,最新的示例支持 mobilenet 模型部署和 yolo 模型部署,本章以部署 yolov8修改版本为例介绍 rknn_model_zoo 示例的使用。:可选项,用于指定 RKNN 模型的保存路径,默认保存在 ONNX 模型同一目录下,名称为 ‘yolov8.rknn’例如“rv1106”。

qq_42730522的博客 3227

Hystrix HystricCommand配置参数全解析

https://www.cnblogs.com/zhenbianshu/p/9630167.html https://juejin.im/post/5c4f18cb6fb9a049ff4e8524 https://blog.youkuaiyun.com/tongtong_use/article/details/78611225 前言 不久前在部门周会上分享了 Hystrix 源码解析之后,就无奈地背上...

yb223731的博客 4101

AI 模型训练核心方法论:零基础也能落地的 8 个关键阶段

本文是为 AI 零基础学习者打造的模型训练入门指南,以生活化语言拆解核心概念,规避专业术语壁垒。内容先通俗阐释模型训练、监督学习、数据集划分等基础概念,再按问题定义、数据准备、模型选择、环境搭建、训练、评估、调参、部署 8 个阶段,拆解完整训练流程,搭配 PyTorch 代码示例与实战案例,同时提供资源预算、常见问题解决方案及学习路径。指南强调数据的核心重要性,推荐新手优先使用迁移学习和免费 GPU 资源,兼顾理论与实操,帮助学习者快速上手,避开训练误区,独立完成从模型搭建到部署的全流程。

weixin_52741870的博客 470

【参数配置】OpenClaw 的破解之道(Doom Loop)

本文深入解析了OpenClaw中的Doom Loop(死循环)问题及其应对策略。主要内容包括: Agent Loop的核心概念:基于ReAct范式,OpenClaw通过"思考-行动-观察"循环执行任务,但可能陷入无进展的死循环。 Doom Loop现象:Agent持续执行但任务进度为零,常见于四种工具调用场景:相同调用重复执行、调用不存在工具、轮询工具卡死、操作交替振荡。 OpenClaw的防御机制: 第一道防线:重试端断路器,通过上下文压缩熔断(最多3次)和总重试次数熔断(32-160次)防止无限重试。

海兰 4042

十一、 Hystrix的高阶知识

11.1 request collapser请求合并技术 第六章(Request Cache请求缓存)优化过一个批量查询的接口了,request cache来做优化,相同的请求参数就可以直接取用缓存了的记录。但仍需要一次业务操作接口中需要发送多次网络请求,调用多次接口,才能拿到结果。可以使用HystrixCollapser将多个HystrixCommand合并到一起,多个command放在一个c...

mtsbv110的专栏 3142

电脑硬件:电脑主板上的接口介绍,看完你就懂了

它们主要用于连接老式的键盘和鼠标,虽然速度和兼容性不如USB接口,但在某些特定场合依然发挥着重要作用。PCIe接口是主板上的扩展能力象征,可以连接各种扩展卡,如显卡、声卡、网卡等。电脑主板作为电脑硬件组成关键的部分,今天给大家分享一下电脑主板上有哪些接口,具体每一个接口的特点、使用场景,有需要的朋友可以来了解一下!它们不仅能连接鼠标、键盘、U盘、打印机,还能连接各种外设,如移动硬盘、游戏手柄等。:虽然现在用得越来越少,但在一些特定场合,如工业控制、老旧设备上,仍然需要使用PS/2接口。

IT技术分享社区 2789

SpringCloud系列之Hystrix熔断器【待完善】

目录1. 简介2. 实例2.1 在RestTemplate和Ribbon上使用熔断器2.2 在Feign上使用熔断器2.3 使用Hystrix Dashboard 监控熔断器状态2.4 使用Turbine 聚合监控3. 源码解析 1. 简介 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时,异常等,Hystrix能保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。 “断路器” 本身是一种开关设置,当某个

陆林烽的博客 721

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

从源码看hystrix的工作原理

Hystrix是Netflix开源的一个限流熔断的项目、主要有以下功能: 隔离(线程池隔离和信号量隔离):限制调用分布式服务的资源使用,某一个调用的服务出现问题不会影响其他服务调用。 优雅的降级机制:超时降级、资源不足时(线程或信号量)降级,降级后可以配合降级接口返回托底数据。 融断:当失败率达到阀值自动触发降级(如因网络故障/超时造成的失败率高),熔断器触发的快速失败会进行快速恢复。 缓存:...

莫夜空的博客 7388

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

熔断器 Hystrix 源码解析 —— 调试环境搭建

摘要: 原创出处 http://www.iocoder.cn/Hystrix/build-debugging-environment/ 「芋道源码」欢迎转载,保留摘要,谢谢!本文主要基于 Hystrix 1.5.X 版本 1. 依赖工具 2. 源码拉取 3. 运行示例 4. 彩蛋 ������关注微信公众号:【芋道源码】有福利: 1. RocketMQ / MyCAT / Shardin

芋艿V 1005

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Hystrix核心原理和断路器源码解析

Hystrix的断路器源码,没有任何炫技的设计,只有极致的简洁和严谨基于状态机模式实现3种状态的切换,逻辑清晰;基于原子类保证线程安全,无并发问题;基于量化指标做熔断判断,无主观逻辑;所有核心方法都是「无锁设计」,性能极高。降级是 Hystrix 的兜底能力,通过@HystrixCommand(fallbackMethod = "降级方法名")注解配置,当请求失败时,快速执行预设的降级方法,返回友好结果接口不报错、用户无感知、系统不雪崩。Hystrix通过断路器的状态机熔断。

xiaolyuh的专栏 1246

Spring Cloud Hystrix:服务容错保护

在微服务架构中,服务与服务之间通过远程调用的方式进行通信,一旦某个被调用的服务发生了故障,其依赖服务也会发生故障,此时就会发生故障的蔓延,最终导致系统瘫痪。Hystrix实现了断路器模式,当某个服务发生故障时,通过断路器的监控,给调用方返回一个错误响应,而不是长时间的等待,这样就不会使得调用方由于长时间得不到响应而占用线程,从而防止故障的蔓延。Hystrix具备服务降级、服务熔断、线程隔离、请求缓存、请求合并及服务监控等强大功能。

smart_an的专栏 1105

Spring Cloud Hystrix 源码系列:工作原理

Hystrix 译为 "豪猪",豪猪的棘刺能保护自己不受天敌伤害,代表了强大的防御能力。Hystrix 基于 RxJava 进行实现,RxJava 是一种基于观察者模式的响应式编程框架。Spring Cloud Hystrix 基于 Netflix Hystrix 实现,具备服务降级、服务熔断、线程与信号隔离、请求缓存、请求合并以及服务监控等强大功能。本文基于hystrix-core1.5.18...

布道 2250

hystrix功能汇总

每一次的通信都需要建立和断开连接,如果微服务之间的调用非常频繁,那就会有很多的连接在建立和断开,为了减少请求连接的数量,Hystrix提出了请求合并的功能。就是对某个服务的多次请求合并成一个请求,单个请求的参数合成批量请求的参数,这就需要被调用的服务接口需要支持批量调用。需要用到2个注解:跳转。

weixin_37862824的博客 453

spring-cloud源码解析-hystrix的基本介绍和配置属性说明

Hystrix简单介绍 Hystrix是一个延迟和容错库,目的是用来隔离远程系统、服务和第三方库的调用以及停止故障传播,并在无法完全避免发生故障的复杂的分布式系统中实现弹性 Hystrix主要的作用 隔离(线程隔离、信号量隔离):主要是限制调用分布式服务的资源,避免个别服务出现问...

chyk1414的博客 262

Hystrix分析

简介 Hystrix是NetFlix公司推出的用于微服务软件架构设计的服务保护模块,该模块是目的是为了去解决整体服务其中某一个服务出现宕机而依赖该服务的其余节点进入故障不可用状态出现调用雪崩这类问题。对微服务架构架构的延迟和故障去提供一个很好的容错性,这个模块提供了诸如服务降级,熔断,线程隔离,请求合并以及监控。 源码分析 Hystrix的Work flow 说到熔断呢,这就要说到熔断器模式了,...

Chaos Awakening 625

SpringCloud-Hystrix服务熔断与降级工作原理&源码 | 京东物流技术团队

在生活中,如果电路的负载过高,保险箱会自动跳闸,以保护家里的各种电器,这就是熔断器的一个活生生例子。在Hystrix中也存在这样一个熔断器,当所依赖的服务不稳定时,能够自动熔断,并提供有损服务,保护服务的稳定性。在运行过程中,Hystrix会根据接口的执行状态(成功、失败、超时和拒绝),收集并统计这些数据,根据这些信息来实时决策是否进行熔断。

JDDTechTalk的博客 932

请求合并哪家强——接口请求合并的3种技巧,性能直接爆表

工作中,我们常见的请求模型都是”请求-应答”式,即一次请求中,服务给请求分配一个独立的线程,一块独立的内存空间,所有的操作都是独立的,包括资源和系统运算。我们也知道,在请求中处理一次系统I/O的消耗是非常大的,如果有非常多的请求都进行同一类I/O操作,那么是否可以将这些I/O操作都合并到一起,进行一次I/O操作,是否可以大大降低下游资源服务器的负担呢?https。...

LoveSummer 1万+

Hystrix介绍

开篇  对Hystrix耳闻已久,最近刚好想在项目中使用这个神器就顺带研究了一把,很多细节来不及深入研究只能把宏观上的各个概念讲解一下,这个介绍的素材大都来自github上的Hystrix官网。  所谓一图胜千言,但凡能够用图片来表示而且能够表示清楚的,就不多用文字描述了,看图肯定比看文字要让人来的更爽一些。...

weixin_34419321的博客 121

java接口合并

工作中,我们常见的请求模型都是”请求-应答”式,即一次请求中,服务给请求分配一个独立的线程,一块独立的内存空间,所有的操作都是独立的,包括资源和系统运算。我们也知道,在请求中处理一次系统 I/O 的消耗是非常大的,如果有非常多的请求都进行同一类 I/O 操作,那么是否可以将这些 I/O 操作都合并到一起,进行一次 I/O 操作,是否可以大大降低下游资源服务器的负担呢?

TianZhongFei的博客 525
上一篇: Hystrix 源码解析 —— 命令合并执行
下一篇: Spring Security(四)--核心过滤器源码分析
公众号-芋道源码
博客等级 码龄10年 1640粉丝 377原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值