Timing Wheel 时间轮算法 java实现

来源:互联网 发布:淘宝网迪奥香水 编辑:程序博客网 时间:2024/04/30 03:12

原文地址:http://blog.csdn.net/mindfloating/article/details/8033340

最近自己在写一个网络服务程序时需要管理大量客户端连接的,其中每个客户端连接都需要管理它的 timeout 时间。

通常连接的超时管理一般设置为30~60秒不等,并不需要太精确的时间控制。

另外由于服务端管理着多达数万到数十万不等的连接数,因此我们没法为每个连接使用一个Timer,那样太消耗资源不现实。


最早面临类似问题的应该是在操作系统和网络协议栈的实现中,以TCP协议为例:

其可靠传输依赖超时重传机制,因此每个通过TCP传输的 packet 都需要一个 timer 来调度 timeout 事件。

根据George Varghese 和 Tony Lauck 1996 年的论文<Hashed and Hierarchical Timing Wheels: data structures to efficiently implement a timer facility>(http://cseweb.ucsd.edu/users/varghese/PAPERS/twheel.ps.Z)

提出了一种定时轮的方式来管理和维护大量的 timer 调度,本文主要根据该论文讨论下实现一种定时轮的要点。


定时轮是一种数据结构,其主体是一个循环列表(circular buffer),每个列表中包含一个称之为槽(slot)的结构(附图)。

至于 slot 的具体结构依赖具体应用场景。

以本文开头所述的管理大量连接 timeout 的场景为例,描述一下 timing wheel的具体实现细节。


定时轮的工作原理可以类比于始终,如上图箭头(指针)按某一个方向按固定频率轮动,每一次跳动称为一个 tick。

这样可以看出定时轮由个3个重要的属性参数,ticksPerWheel(一轮的tick数),tickDuration(一个tick的持续时间)

以及 timeUnit(时间单位),例如 当ticksPerWheel=60,tickDuration=1,timeUnit=秒,这就和现实中的始终的秒针走动完全类似了。


这里给出一种简单的实现方式,指针按 tickDuration 的设置进行固定频率的转动,其中的必要约定如下:

  1. 新加入的对象总是保存在当前指针转动方向上一个位置
  2. 相等的对象仅存在于一个 slot 中
  3. 指针转动到当前位置对应的 slot 中保存的对象就意味着 timeout 了

在 Timing Wheel 模型中包含4种操作:

Client invoke:

1. START_TIMER(Interval, Request_ID, Expiry_Action)

2. STOP_TIMER(Request_ID)

Timer tick invoke:

3. PER_TICK_BOOKKEEPING

4. EXPIRY_PROCESSING


Timing Wheel 实现中主要考察的是前3种操作的时间和空间复杂度,而第4种属于超时处理通常实现为回调方法,由调用方的实现决定其效率,下面看一个用 java 实现的 Timing Wheel 的具体例子:

TimingWheel.java

[java] view plaincopy
  1. /** 
  2.  * A timing-wheel optimized for approximated I/O timeout scheduling.<br> 
  3.  * {@link TimingWheel} creates a new thread whenever it is instantiated and started, so don't create many instances. 
  4.  * <p> 
  5.  * <b>The classic usage as follows:</b><br> 
  6.  * <li>using timing-wheel manage any object timeout</li> 
  7.  * <pre> 
  8.  *    // Create a timing-wheel with 60 ticks, and every tick is 1 second. 
  9.  *    private static final TimingWheel<CometChannel> TIMING_WHEEL = new TimingWheel<CometChannel>(1, 60, TimeUnit.SECONDS); 
  10.  *     
  11.  *    // Add expiration listener and start the timing-wheel. 
  12.  *    static { 
  13.  *      TIMING_WHEEL.addExpirationListener(new YourExpirationListener()); 
  14.  *      TIMING_WHEEL.start(); 
  15.  *    } 
  16.  *     
  17.  *    // Add one element to be timeout approximated after 60 seconds 
  18.  *    TIMING_WHEEL.add(e); 
  19.  *     
  20.  *    // Anytime you can cancel count down timer for element e like this 
  21.  *    TIMING_WHEEL.remove(e); 
  22.  * </pre> 
  23.  *  
  24.  * After expiration occurs, the {@link ExpirationListener} interface will be invoked and the expired object will be  
  25.  * the argument for callback method {@link ExpirationListener#expired(Object)} 
  26.  * <p> 
  27.  * {@link TimingWheel} is based on <a href="http://cseweb.ucsd.edu/users/varghese/">George Varghese</a> and Tony Lauck's paper, 
  28.  * <a href="http://cseweb.ucsd.edu/users/varghese/PAPERS/twheel.ps.Z">'Hashed and Hierarchical Timing Wheels: data structures  
  29.  * to efficiently implement a timer facility'</a>.  More comprehensive slides are located <a href="http://www.cse.wustl.edu/~cdgill/courses/cs6874/TimingWheels.ppt">here</a>. 
  30.  *  
  31.  * @author mindwind 
  32.  * @version 1.0, Sep 20, 2012 
  33.  */  
  34. public class TimingWheel<E> {  
  35.       
  36.     private final long tickDuration;  
  37.     private final int ticksPerWheel;  
  38.     private volatile int currentTickIndex = 0;  
  39.       
  40.     private final CopyOnWriteArrayList<ExpirationListener<E>> expirationListeners = new CopyOnWriteArrayList<ExpirationListener<E>>();  
  41.     private final ArrayList<Slot<E>> wheel;  
  42.     private final Map<E, Slot<E>> indicator = new ConcurrentHashMap<E, Slot<E>>();  
  43.       
  44.     private final AtomicBoolean shutdown = new AtomicBoolean(false);  
  45.     private final ReadWriteLock lock = new ReentrantReadWriteLock();  
  46.     private Thread workerThread;  
  47.       
  48.     // ~ -------------------------------------------------------------------------------------------------------------  
  49.   
  50.     /** 
  51.      * Construct a timing wheel. 
  52.      *  
  53.      * @param tickDuration 
  54.      *            tick duration with specified time unit. 
  55.      * @param ticksPerWheel 
  56.      * @param timeUnit 
  57.      */  
  58.     public TimingWheel(int tickDuration, int ticksPerWheel, TimeUnit timeUnit) {  
  59.         if (timeUnit == null) {  
  60.             throw new NullPointerException("unit");  
  61.         }  
  62.         if (tickDuration <= 0) {  
  63.             throw new IllegalArgumentException("tickDuration must be greater than 0: " + tickDuration);  
  64.         }  
  65.         if (ticksPerWheel <= 0) {  
  66.             throw new IllegalArgumentException("ticksPerWheel must be greater than 0: " + ticksPerWheel);  
  67.         }  
  68.           
  69.         this.wheel = new ArrayList<Slot<E>>();  
  70.         this.tickDuration = TimeUnit.MILLISECONDS.convert(tickDuration, timeUnit);  
  71.         this.ticksPerWheel = ticksPerWheel + 1;  
  72.           
  73.         for (int i = 0; i < this.ticksPerWheel; i++) {  
  74.             wheel.add(new Slot<E>(i));  
  75.         }  
  76.         wheel.trimToSize();  
  77.           
  78.         workerThread = new Thread(new TickWorker(), "Timing-Wheel");  
  79.     }  
  80.       
  81.     // ~ -------------------------------------------------------------------------------------------------------------  
  82.       
  83.     public void start() {  
  84.         if (shutdown.get()) {  
  85.             throw new IllegalStateException("Cannot be started once stopped");  
  86.         }  
  87.   
  88.         if (!workerThread.isAlive()) {  
  89.             workerThread.start();  
  90.         }  
  91.     }  
  92.       
  93.     public boolean stop() {  
  94.         if (!shutdown.compareAndSet(falsetrue)) {  
  95.             return false;  
  96.         }  
  97.           
  98.         boolean interrupted = false;  
  99.         while (workerThread.isAlive()) {  
  100.             workerThread.interrupt();  
  101.             try {  
  102.                 workerThread.join(100);  
  103.             } catch (InterruptedException e) {  
  104.                 interrupted = true;  
  105.             }  
  106.         }  
  107.         if (interrupted) {  
  108.             Thread.currentThread().interrupt();  
  109.         }  
  110.           
  111.         return true;  
  112.     }  
  113.       
  114.     public void addExpirationListener(ExpirationListener<E> listener) {  
  115.         expirationListeners.add(listener);  
  116.     }  
  117.       
  118.     public void removeExpirationListener(ExpirationListener<E> listener) {  
  119.         expirationListeners.remove(listener);  
  120.     }  
  121.       
  122.     /** 
  123.      * Add a element to {@link TimingWheel} and start to count down its life-time. 
  124.      *  
  125.      * @param e 
  126.      * @return remain time to be expired in millisecond. 
  127.      */  
  128.     public long add(E e) {  
  129.         synchronized(e) {  
  130.             checkAdd(e);  
  131.               
  132.             int previousTickIndex = getPreviousTickIndex();  
  133.             Slot<E> slot = wheel.get(previousTickIndex);  
  134.             slot.add(e);  
  135.             indicator.put(e, slot);  
  136.               
  137.             return (ticksPerWheel - 1) * tickDuration;  
  138.         }  
  139.     }  
  140.       
  141.     private void checkAdd(E e) {  
  142.         Slot<E> slot = indicator.get(e);  
  143.         if (slot != null) {  
  144.             slot.remove(e);  
  145.         }  
  146.     }  
  147.       
  148.     private int getPreviousTickIndex() {  
  149.         lock.readLock().lock();  
  150.         try {  
  151.             int cti = currentTickIndex;  
  152.             if (cti == 0) {  
  153.                 return ticksPerWheel - 1;  
  154.             }  
  155.               
  156.             return cti - 1;  
  157.         } finally {  
  158.             lock.readLock().unlock();  
  159.         }  
  160.     }  
  161.       
  162.     /** 
  163.      * Removes the specified element from timing wheel. 
  164.      *  
  165.      * @param e 
  166.      * @return <tt>true</tt> if this timing wheel contained the specified 
  167.      *         element 
  168.      */  
  169.     public boolean remove(E e) {  
  170.         synchronized (e) {  
  171.             Slot<E> slot = indicator.get(e);  
  172.             if (slot == null) {  
  173.                 return false;  
  174.             }  
  175.   
  176.             indicator.remove(e);  
  177.             return slot.remove(e) != null;  
  178.         }  
  179.     }  
  180.   
  181.     private void notifyExpired(int idx) {  
  182.         Slot<E> slot = wheel.get(idx);  
  183.         Set<E> elements = slot.elements();  
  184.         for (E e : elements) {  
  185.             slot.remove(e);  
  186.             synchronized (e) {  
  187.                 Slot<E> latestSlot = indicator.get(e);  
  188.                 if (latestSlot.equals(slot)) {  
  189.                     indicator.remove(e);  
  190.                 }  
  191.             }  
  192.             for (ExpirationListener<E> listener : expirationListeners) {  
  193.                 listener.expired(e);  
  194.             }  
  195.         }  
  196.     }  
  197.       
  198.     // ~ -------------------------------------------------------------------------------------------------------------  
  199.        
  200.     private class TickWorker implements Runnable {  
  201.   
  202.         private long startTime;  
  203.         private long tick;  
  204.   
  205.         @Override  
  206.         public void run() {  
  207.             startTime = System.currentTimeMillis();  
  208.             tick = 1;  
  209.   
  210.             for (int i = 0; !shutdown.get(); i++) {  
  211.                 if (i == wheel.size()) {  
  212.                     i = 0;  
  213.                 }  
  214.                 lock.writeLock().lock();  
  215.                 try {  
  216.                     currentTickIndex = i;  
  217.                 } finally {  
  218.                     lock.writeLock().unlock();  
  219.                 }  
  220.                 notifyExpired(currentTickIndex);  
  221.                 waitForNextTick();  
  222.             }  
  223.         }  
  224.   
  225.         private void waitForNextTick() {  
  226.             for (;;) {  
  227.                 long currentTime = System.currentTimeMillis();  
  228.                 long sleepTime = tickDuration * tick - (currentTime - startTime);  
  229.   
  230.                 if (sleepTime <= 0) {  
  231.                     break;  
  232.                 }  
  233.   
  234.                 try {  
  235.                     Thread.sleep(sleepTime);  
  236.                 } catch (InterruptedException e) {  
  237.                     return;  
  238.                 }  
  239.             }  
  240.               
  241.             tick++;  
  242.         }  
  243.     }  
  244.       
  245.     private static class Slot<E> {  
  246.           
  247.         private int id;  
  248.         private Map<E, E> elements = new ConcurrentHashMap<E, E>();  
  249.           
  250.         public Slot(int id) {  
  251.             this.id = id;  
  252.         }  
  253.   
  254.         public void add(E e) {  
  255.             elements.put(e, e);  
  256.         }  
  257.           
  258.         public E remove(E e) {  
  259.             return elements.remove(e);  
  260.         }  
  261.           
  262.         public Set<E> elements() {  
  263.             return elements.keySet();  
  264.         }  
  265.   
  266.         @Override  
  267.         public int hashCode() {  
  268.             final int prime = 31;  
  269.             int result = 1;  
  270.             result = prime * result + id;  
  271.             return result;  
  272.         }  
  273.   
  274.         @Override  
  275.         public boolean equals(Object obj) {  
  276.             if (this == obj)  
  277.                 return true;  
  278.             if (obj == null)  
  279.                 return false;  
  280.             if (getClass() != obj.getClass())  
  281.                 return false;  
  282.             @SuppressWarnings("rawtypes")  
  283.             Slot other = (Slot) obj;  
  284.             if (id != other.id)  
  285.                 return false;  
  286.             return true;  
  287.         }  
  288.   
  289.         @Override  
  290.         public String toString() {  
  291.             return "Slot [id=" + id + ", elements=" + elements + "]";  
  292.         }  
  293.           
  294.     }  
  295.       
  296. }  


ExpirationListener.java

[java] view plaincopy
  1. /** 
  2.  * A listener for expired object events. 
  3.  *  
  4.  * @author mindwind 
  5.  * @version 1.0, Sep 20, 2012 
  6.  * @see TimingWheel 
  7.  */  
  8. public interface ExpirationListener<E> {  
  9.       
  10.     /** 
  11.      * Invoking when a expired event occurs. 
  12.      *  
  13.      * @param expiredObject 
  14.      */  
  15.     void expired(E expiredObject);  
  16.       
  17. }  

我们分析一下这个简化版本  TimingWheel 实现中的 4 个主要操作的实现:


START_TIMER(Interval, Request_ID, Expiry_Action) ,这段伪代码的实现对应于TimingWheel的 add(E e) 方法。

  • 首先检查同样的元素是否已添加到 TimingWheel 中,若已存在则删除旧的引用,重新安置元素在wheel中位置。这个检查是为了满足约束条件2(相等的对象仅存在于一个 slot 中,重新加入相同的元素相当于重置了该元素的 Timer)
  • 获取当前 tick 指针位置的前一个 slot 槽位,放置新加入的元素,并在内部记录下该位置
  • 返回新加入元素的 timeout 时间,以毫秒计算(一般的应用级程序到毫秒这个精度已经足够了)
  • 显然,时间复杂度为O(1)

STOP_TIMER(Request_ID),这段伪代码的实现对应于TimingWheel的 remove(E e) 方法。

  • 获取元素在 TimingWheel 中对应 slot 位置
  • 从中 slot 中删除
  • 显然,时间复杂度也为O(1)

 PER_TICK_BOOKKEEPING,伪代码对应于 TimingWheel 中 TickerWorker 中的  run() 方法。

  • 获取当前 tick 指针的 slot
  • 对当前 slot 的所有元素进行 timeout 处理(notifyExpired())
  • ticker 不需要针对每个元素去判断其 timeout 时间,故时间复杂度也为 O(1)

 EXPIRY_PROCESSING,伪代码对应于TimingWheel 中的 notifyExpired() 方法

  • 实现了对每个 timeout 元素的 Expiry_Action 处理
  • 这里时间复杂度显然 是 O(n)的。

在维护大量连接的例子中:

  • 连接建立时,把一个连接放入 TimingWheel 中进入 timeout 倒计时
  • 每次收到长连接心跳时,重新加入一次TimingWheel 相当于重置了 timer
  • timeout 时间到达时触发 EXPIRY_PROCESSING
  • EXPIRY_PROCESSING 实际就是关闭超时的连接。

这个简化版的 TimingWheel 实现一个实例只能支持一个固定的 timeout 时长调度,不能支持对于每个元素特定的 timeout 时长。

一种改进的做法是设计一个函数,计算每个元素特定的deadline,并根据deadline计算放置在wheel中的特定位置,这个以后再完善。


0 0
原创粉丝点击