危险的Hystrix线程池

  • 时间:
  • 浏览:0
  • 来源:大发时时彩_时时彩app网站_大发时时彩app网站

本文介绍Hystrix系统线程池池池的工作原理和参数配置,指出地处的问题图片并提供规避方案,阅读本文需要对Hystrix有一定的了解。

文本讨论的内容,基于hystrix 1.5.18:

    <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-core</artifactId>
      <version>1.5.18</version>
    </dependency>

系统线程池池池和Hystrix Command之间的关系

当hystrix command的隔离策略配置为系统线程池池,也也不execution.isolation.strategy设置为THREAD时,command中的代码会放满系统线程池池池里执行,跟发起command调用的系统线程池池隔选取离开。摘要官方wiki如下:

execution.isolation.strategy

This property indicates which isolation strategy HystrixCommand.run() executes with, one of the following two choices:

THREAD — it executes on a separate thread and concurrent requests are limited by the number of threads in the thread-pool

SEMAPHORE — it executes on the calling thread and concurrent requests are limited by the semaphore count

原来线上的服务,往往会有所以hystrix command分别用来管理不同的实物依赖。 要怎样让几只hystrix系统线程池池池地处呢,什么command跟系统线程池池池的对应关系又是要怎样的呢,是一对一吗?

答案是不一定,command跟系统线程池池池都还能能做到一对一,但通常全版都是,受到HystrixThreadPoolKey和HystrixCommandGroupKey这两项配置的影响。

优先采用HystrixThreadPoolKey来标识系统线程池池池,不可能 这麼 配置HystrixThreadPoolKey这麼 就使用HystrixCommandGroupKey来标识。command跟系统线程池池池的对应关系,想看 HystrixCommandKey、HystrixThreadPoolKey、HystrixCommandGroupKey这原来参数的配置。

获取系统线程池池池标识的代码如下,都还能能想看 跟我的描述是一致的:

    /*
     * ThreadPoolKey
     *
     * This defines which thread-pool this command should run on.
     *
     * It uses the HystrixThreadPoolKey if provided, then defaults to use HystrixCommandGroup.
     *
     * It can then be overridden by a property if defined so it can be changed at runtime.
     */
    private static HystrixThreadPoolKey initThreadPoolKey(HystrixThreadPoolKey threadPoolKey, HystrixCommandGroupKey groupKey, String threadPoolKeyOverride) {
        if (threadPoolKeyOverride == null) {
            // we don't have a property overriding the value so use either HystrixThreadPoolKey or HystrixCommandGroup
            if (threadPoolKey == null) {
                /* use HystrixCommandGroup if HystrixThreadPoolKey is null */
                return HystrixThreadPoolKey.Factory.asKey(groupKey.name());
            } else {
                return threadPoolKey;
            }
        } else {
            // we have a property defining the thread-pool so use it instead
            return HystrixThreadPoolKey.Factory.asKey(threadPoolKeyOverride);
        }
    }

Hystrix会保证同原来系统线程池池池标识只会创建原来系统线程池池池:

    /*
     * Use the String from HystrixThreadPoolKey.name() instead of the HystrixThreadPoolKey instance as it's just an interface and we can't ensure the object
     * we receive implements hashcode/equals correctly and do not want the default hashcode/equals which would create a new threadpool for every object we get even if the name is the same
     */
    /* package */final static ConcurrentHashMap<String, HystrixThreadPool> threadPools = new ConcurrentHashMap<String, HystrixThreadPool>();

    /**
     * Get the {@link HystrixThreadPool} instance for a given {@link HystrixThreadPoolKey}.
     * <p>
     * This is thread-safe and ensures only 1 {@link HystrixThreadPool} per {@link HystrixThreadPoolKey}.
     *
     * @return {@link HystrixThreadPool} instance
     */
    /* package */static HystrixThreadPool getInstance(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesBuilder) {
        // get the key to use instead of using the object itself so that if people forget to implement equals/hashcode things will still work
        String key = threadPoolKey.name();

        // this should find it for all but the first time
        HystrixThreadPool previouslyCached = threadPools.get(key);
        if (previouslyCached != null) {
            return previouslyCached;
        }

        // if we get here this is the first time so we need to initialize
        synchronized (HystrixThreadPool.class) {
            if (!threadPools.containsKey(key)) {
                threadPools.put(key, new HystrixThreadPoolDefault(threadPoolKey, propertiesBuilder));
            }
        }
        return threadPools.get(key);
    }

Hystrix系统线程池池池参数一览

  • coreSize 核心系统线程池池数量
  • maximumSize 最大系统线程池池数量
  • allowMaximumSizeToDivergeFromCoreSize 允许maximumSize大于coreSize,都还能能配了这一 值coreSize才有意义
  • keepAliveTimeMinutes 超过这一 时间多于coreSize数量的系统线程池池会被回收,都还能能maximumsize大于coreSize,这一 值才有意义
  • maxQueueSize 任务队列的最大大小,当系统线程池池池的系统线程池池系统线程池池全版都是工作,也不能创建新的系统线程池池的后后,新的任务会进到队列里等待歌曲
  • queueSizeRejectionThreshold 任务队列中存储的任务数量超过这一 值,系统线程池池池拒绝新的任务。这跟maxQueueSize原来是一回事,也不受限于hystrix的实现土依据maxQueueSize都还能能动态配置,所以有了这一 配置。

根据给定的系统线程池池池参数猜测系统线程池池池表现

都还能能想看 hystrix的系统线程池池池参数跟JDK系统线程池池池ThreadPoolExecutor参数很像但又不一样,即便是全版地想看 文档,仍然你还能能迷惑。不过无妨,先来猜猜几种配置下的表现。

coreSize = 2; maxQueueSize = 10

系统线程池池池中常驻原来系统线程池池。新任务提交到系统线程池池池,有空闲系统线程池池则直接执行,要怎样让入队等待歌曲歌曲。等待歌曲队列中的任务数=10时,拒绝接受新任务。

coreSize = 2; maximumSize = 5; maxQueueSize = -1

系统线程池池池中常驻原来系统线程池池。新任务提交到系统线程池池池,有空闲系统线程池池则直接执行,这麼 空闲系统线程池池时,不可能 当前系统线程池池数小于5则创建原来新的系统线程池池用来执行任务,要怎样让拒绝任务。

coreSize = 2; maximumSize = 5; maxQueueSize = 10

这一 配置下从官方文档中不可能 看这麼来实际表现会是要怎样的。猜测有如下一种不可能 :

  • 不可能 一。系统线程池池池中常驻原来系统线程池池。新任务提交到系统线程池池池,原来系统线程池池含有空闲则直接执行,要怎样让入队等待歌曲歌曲。当原来系统线程池池全版都是工作且等待歌曲队列中的任务数=10时,开始英语 为新任务创建系统线程池池,直到系统线程池池数量为5,此时开始英语 拒绝新任务。原来励志的话 ,对资源敏感型的任务比较友好,这也是JDK系统线程池池池ThreadPoolExecutor的行为。

  • 不可能 二。系统线程池池池中常驻原来系统线程池池。新任务提交到系统线程池池池,有空闲系统线程池池则直接执行,这麼 空闲系统线程池池时,不可能 当前系统线程池池数小于5则创建原来新的系统线程池池用来执行任务。当系统线程池池数量达到四个且全版都是工作时,任务入队等待歌曲歌曲。等待歌曲队列中的任务数=10时,拒绝接受新任务。原来励志的话 ,对延迟敏感型的任务比较友好。

一种清况 全版都是不可能 ,从文档中无法选取究竟要怎样。

并发清况 下Hystrix系统线程池池池的真正表现

本节中,通过测试来看看系统线程池池池的行为究竟会要怎样。

还是这一 配置:

coreSize = 2; maximumSize = 5; maxQueueSize = 10

亲戚朋友通过不断提交任务到hystrix系统线程池池池,要怎样让在任务的执行代码中使用CountDownLatch占住系统线程池池来模拟测试,代码如下:

public class HystrixThreadPoolTest {

  public static void main(String[] args) throws InterruptedException {
    final int coreSize = 2, maximumSize = 5, maxQueueSize = 10;
    final String commandName = "TestThreadPoolCommand";

    final HystrixCommand.Setter commandConfig = HystrixCommand.Setter
        .withGroupKey(HystrixCommandGroupKey.Factory.asKey(commandName))
        .andCommandKey(HystrixCommandKey.Factory.asKey(commandName))
        .andCommandPropertiesDefaults(
            HystrixCommandProperties.Setter()
                .withExecutionTimeoutEnabled(false))
        .andThreadPoolPropertiesDefaults(
            HystrixThreadPoolProperties.Setter()
                .withCoreSize(coreSize)
                .withMaximumSize(maximumSize)
                .withAllowMaximumSizeToDivergeFromCoreSize(true)
                .withMaxQueueSize(maxQueueSize)
                .withQueueSizeRejectionThreshold(maxQueueSize));

    // Run command once, so we can get metrics.
    HystrixCommand<Void> command = new HystrixCommand<Void>(commandConfig) {
      @Override protected Void run() throws Exception {
        return null;
      }
    };
    command.execute();
    Thread.sleep(3000);

    final CountDownLatch stopLatch = new CountDownLatch(1);
    List<Thread> threads = new ArrayList<Thread>();

    for (int i = 0; i < coreSize + maximumSize + maxQueueSize; i++) {
      final int fi = i + 1;

      Thread thread = new Thread(new Runnable() {
        public void run() {
          try {
            HystrixCommand<Void> command = new HystrixCommand<Void>(commandConfig) {
              @Override protected Void run() throws Exception {
                stopLatch.await();
                return null;
              }
            };
            command.execute();
          } catch (HystrixRuntimeException e) {
            System.out.println("Started Jobs: " + fi);
            System.out.println("Job:" + fi + " got rejected.");
            printThreadPoolStatus();
            System.out.println();
          }
        }
      });
      threads.add(thread);
      thread.start();
      Thread.sleep(3000);

      if(fi == coreSize || fi == coreSize + maximumSize || fi == coreSize + maxQueueSize ) {
        System.out.println("Started Jobs: " + fi);
        printThreadPoolStatus();
        System.out.println();
      }
    }

    stopLatch.countDown();

    for (Thread thread : threads) {
      thread.join();
    }

  }

  static void printThreadPoolStatus() {
    for (HystrixThreadPoolMetrics threadPoolMetrics : HystrixThreadPoolMetrics.getInstances()) {
      String name = threadPoolMetrics.getThreadPoolKey().name();
      Number poolSize = threadPoolMetrics.getCurrentPoolSize();
      Number queueSize = threadPoolMetrics.getCurrentQueueSize();
      System.out.println("ThreadPoolKey: " + name + ", PoolSize: " + poolSize + ", QueueSize: " + queueSize);
    }

  }

}

执行代码得到如下输出:

// 任务数 = coreSize。此时coreSize个系统线程池池在工作
Started Jobs: 2
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 0

// 任务数 > coreSize。此时仍然都还能能coreSize个系统线程池池,多于coreSize的任务进入等待歌曲歌曲队列,这麼

创建新的系统线程池池  
Started Jobs: 7
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 5

// 任务数 = coreSize + maxQueueSize。此时仍然都还能能coreSize个系统线程池池,多于coreSize的任务进入等待歌曲歌曲队列,这麼

创建新的系统线程池池  
Started Jobs: 12
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

// 任务数 > coreSize + maxQueueSize。此时仍然都还能能coreSize个系统线程池池,等待歌曲歌曲队列已满,新增任务被拒绝 
Started Jobs: 13
Job:13 got rejected.
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

Started Jobs: 14
Job:14 got rejected.
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

Started Jobs: 15
Job:15 got rejected.
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

Started Jobs: 16
Job:16 got rejected.
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

Started Jobs: 17
Job:17 got rejected.
ThreadPoolKey: TestThreadPoolCommand, PoolSize: 2, QueueSize: 10

全版的测试代码,参见这里

都还能能想看 Hystrix系统线程池池池的实际表现,跟后后的一种猜测全版都是同,跟JDK系统线程池池池的表现不同,跟另一种合理猜测也不通。当maxSize > coreSize && maxQueueSize != -1的后后,maxSize这一 参数根本就不起作用,系统线程池池数量永远不必超过coreSize,对于的任务入队等待歌曲歌曲,队列满了,就直接拒绝新任务。

不得不说,这是一种你还能能疑惑的,非常危险的,容易配置错误的系统线程池池池表现。

JDK系统线程池池池ThreadPoolExecutor

继续分析Hystrix系统线程池池池的原理后后,先来复习一下JDK中的系统线程池池池。

只说跟本文讨论的内容相关的参数:

  • corePoolSize核心系统线程池池数,maximumPoolSize最大系统线程池池数。这一 原来参数跟hystrix系统线程池池池的coreSize和maximumSize含义是一致的。
  • workQueue任务等待歌曲歌曲队列。跟hystrix不同,jdk系统线程池池池的等待歌曲歌曲队列全版都是指定大小,也不需要使用方提供原来BlockingQueue。
  • handler当系统线程池池池无法接受任务时的正确处理器。hystrix是直接拒绝,jdk系统线程池池池都还能能定制。

都还能能想看 ,jdk的系统线程池池池使用起来更加灵活。配置参数的含义也十分清晰,这麼 hystrx系统线程池池池里边allowMaximumSizeToDivergeFromCoreSize、queueSizeRejectionThreshold这一 奇奇怪怪你还能能疑惑的参数。

关于jdk系统线程池池池的参数配置,参加如下jdk源码:


    /**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @param threadFactory the factory to use when the executor
     *        creates a new thread
     * @param handler the handler to use when execution is blocked
     *        because the thread bounds and queue capacities are reached
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue}
     *         or {@code threadFactory} or {@code handler} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

这麼 在跟hystrix系统线程池池池对应的参数配置下,jdk系统线程池池池的表现会要怎样呢?

corePoolSize = 2; maximumPoolSize = 5; workQueue = new ArrayBlockingQueue(10); handler = new ThreadPoolExecutor.DiscardPolicy()

这里不再测试了,直接给出答案。系统线程池池池中常驻原来系统线程池池。新任务提交到系统线程池池池,原来系统线程池池含有空闲则直接执行,要怎样让入队等待歌曲歌曲。当原来系统线程池池全版都是工作且等待歌曲队列中的任务数=10时,开始英语 为新任务创建系统线程池池,直到系统线程池池数量为5,此时开始英语 拒绝新任务。

相关逻辑涉及的源码贴在下面。值得一提的是,jdk系统线程池池池何必 根据等待歌曲歌曲任务的数量来判断等待歌曲歌曲队列不是已满,也不直接调用workQueue的offer土依据,不可能 workQueue接受了那就入队等待歌曲歌曲,要怎样让执行拒绝策略。

    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

都还能能想看 hystrix系统线程池池池的配置参数跟jdk系统线程池池池是非常像的,从名字到含义,都基本一致。

为什么在么在回事 会

事实上hystrix的系统线程池池池,也不在jdk系统线程池池池的基础上实现的。相关代码如下:


    public ThreadPoolExecutor getThreadPool(final HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) {
        final ThreadFactory threadFactory = getThreadFactory(threadPoolKey);

        final boolean allowMaximumSizeToDivergeFromCoreSize = threadPoolProperties.getAllowMaximumSizeToDivergeFromCoreSize().get();
        final int dynamicCoreSize = threadPoolProperties.coreSize().get();
        final int keepAliveTime = threadPoolProperties.keepAliveTimeMinutes().get();
        final int maxQueueSize = threadPoolProperties.maxQueueSize().get();
        final BlockingQueue<Runnable> workQueue = getBlockingQueue(maxQueueSize);

        if (allowMaximumSizeToDivergeFromCoreSize) {
            final int dynamicMaximumSize = threadPoolProperties.maximumSize().get();
            if (dynamicCoreSize > dynamicMaximumSize) {
                logger.error("Hystrix ThreadPool configuration at startup for : " + threadPoolKey.name() + " is trying to set coreSize = " +
                        dynamicCoreSize + " and maximumSize = " + dynamicMaximumSize + ".  Maximum size will be set to " +
                        dynamicCoreSize + ", the coreSize value, since it must be equal to or greater than the coreSize value");
                return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
            } else {
                return new ThreadPoolExecutor(dynamicCoreSize, dynamicMaximumSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
            }
        } else {
            return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
        }
    }

    public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
        /*
         * We are using SynchronousQueue if maxQueueSize <= 0 (meaning a queue is not wanted).
         * <p>
         * SynchronousQueue will do a handoff from calling thread to worker thread and not allow queuing which is what we want.
         * <p>
         * Queuing results in added latency and would only occur when the thread-pool is full at which point there are latency issues
         * and rejecting is the preferred solution.
         */
        if (maxQueueSize <= 0) {
            return new SynchronousQueue<Runnable>();
        } else {
            return new LinkedBlockingQueue<Runnable>(maxQueueSize);
        }
    }

既然hystrix系统线程池池池基于jdk系统线程池池池实现,为什么在么在回事 会 在如下原来基本一致的配置上,行为却不一样呢?

//hystrix
coreSize = 2; maximumSize = 5; maxQueueSize = 10

//jdk
corePoolSize = 2; maximumPoolSize = 5; workQueue = new ArrayBlockingQueue(10); handler = new ThreadPoolExecutor.DiscardPolicy()

jdk在队列满了后后会创建系统线程池池执行新任务直到系统线程池池数量达到maximumPoolSize,而hystrix在队列满了后后直接拒绝新任务,maximumSize这项配置成了摆设。

意味就在于hystrix判断队列不是满不是要拒绝新任务,这麼 通过jdk系统线程池池池在判断,也不当时人判断的。参见如下hystrix源码:

    public boolean isQueueSpaceAvailable() {
        if (queueSize <= 0) {
            // we don't have a queue so we won't look for space but instead
            // let the thread-pool reject or not
            return true;
        } else {
            return threadPool.getQueue().size() < properties.queueSizeRejectionThreshold().get();
        }
    }

    public Subscription schedule(Action0 action, long delayTime, TimeUnit unit) {
        if (threadPool != null) {
            if (!threadPool.isQueueSpaceAvailable()) {
                throw new RejectedExecutionException("Rejected command because thread-pool queueSize is at rejection threshold.");
            }
        }
        return worker.schedule(new HystrixContexSchedulerAction(concurrencyStrategy, action), delayTime, unit);
    }

都还能能想看 hystrix在队列大小达到maxQueueSize时,根本不必往底层的ThreadPoolExecutor提交任务。ThreadPoolExecutor也就这麼 不可能 判断workQueue都还能能offer,更都还能能创建新的系统线程池池了。

为什么在么在回事 办

对用惯了jdk的ThreadPoolExecutor的人来说,再用hystrix的确容易出错,笔者就曾在多个重要线上服务的代码里想看 过错误的配置,称一声危险的hystrix系统线程池池池不为过。

那为什么在么在回事 办呢?

配置的后后规避问题图片

一起配置maximumSize > coreSize,maxQueueSize > 0,像下面原来,是不行了。

coreSize = 2; maximumSize = 5; maxQueueSize = 10

妥协一下,不可能 对延迟比较看重,配置maximumSize > coreSize,maxQueueSize = -1。原来在任务多的后后,不必有等待歌曲歌曲队列,直接创建新系统线程池池执行任务。

coreSize = 2; maximumSize = 5; maxQueueSize = -1

不可能 对资源比较看重, 不希望创建太久系统线程池池,配置maximumSize = coreSize,maxQueueSize > 0。原来在任务多的后后,会进等待歌曲歌曲队列,直到有系统线程池池空闲不可能 超时。

coreSize = 2; maximumSize = 2; maxQueueSize = 10

在hystrix上修复这一 问题图片

技术上是可行的,有所以方案都还能能做到。但Netflix不可能 公布不再维护hystrix了,这条路也就不通了,除非维护当时人的hystrix分支版本。

Reference

https://github.com/Netflix/Hystrix/wiki/Configuration

https://github.com/Netflix/Hystrix/issues/1589

https://github.com/Netflix/Hystrix/pull/1670