flume之退避算法backoff algorithm

什么是退避算法:

In a single channel contention based medium access control (MAC) protocols, whenever more than one station or node tries to access the medium at the same instant of time, it leads to packet collisions. If the collided stations tries to access the channel again, the packets will collide as the nodes are synchrozied in time. So the nodes need to be displaced in time. To displace them temporally, a backoff algorithm is used (example binary exponential backoff (BEB)). For example, in BEB algorithm, whenever a node's transmission is involved in a collision with another node's transmission, both nodes will choose a random waiting time and wait for this amoiunt of time before attempting again. If they are not successful in this attempt, they double their contention window and choose a randoim waiting time before transmitting again. This process will be repeated for certain number of attempts. If the nodes are not successful in their transmission after this limit, the packets will be dropped from their queue.

大致意思是,在一个共享信道的情况下,当网络上的节点在发生冲突时,每个节点节点等待一定的时间后重新发送。在二进制指数退避算法中,等待时间随着以二为底的指数增长。如果重试失败,那么下次的等待时间将会是上次的等待时间二倍。如果重试次数大于最大重试次数,那么包将从包队列中去除。

我们认识了什么是退避算法之后,来看一下flume中对退避算法的应用。从退避算法的概念可知,该算法用在网络错误,重试的情况中,例如打开一个网络链接,向网络中发送数据等。在flume中,insistentAppend和insistentOpen封装器都用到了退避算法来处理网络的发送数据和链接打开过程。我们来通过insistentAppend中的append方法例子,看一下怎么对退避算法进行运用。

Java代码  
  1. public void append(Event evt) throws IOException, InterruptedException {
  2. List<IOException> exns = new ArrayList<IOException>();
  3. int attemptRetries = 0;
  4. appendRequests++;
  5. while (!backoff.isFailed() && isOpen.get()
  6. && !Thread.currentThread().isInterrupted()) {
  7. try {
  8. appendAttempts++;
  9. super.append(evt);
  10. appendSuccesses++;
  11. backoff.reset(); // reset backoff counter;
  12. return;
  13. } catch (InterruptedException ie) {
  14. throw ie;
  15. } catch (IOException e) {
  16. // this is an unexpected exception
  17. long waitTime = backoff.sleepIncrement();
  18. LOG.info("append attempt " + attemptRetries + " failed, backoff ("
  19. + waitTime + "ms): " + e.getMessage());
  20. LOG.debug(e.getMessage(), e);
  21. exns.add((e instanceof IOException) ? (IOException) e
  22. : new IOException(e));
  23. backoff.backoff();
  24. try {
  25. backoff.waitUntilRetryOk();
  26. } catch (InterruptedException e1) {
  27. // got an interrupted signal, bail out!
  28. throw e1;
  29. } finally {
  30. attemptRetries++;
  31. appendRetries++;
  32. }
  33. } catch (RuntimeException e) {
  34. // this is an unexpected exception
  35. LOG.info("Failed due to unexpected runtime exception "
  36. + "during append attempt", e);
  37. appendGiveups++;
  38. throw e;
  39. }
  40. }
  41. appendGiveups++;
  42. // failed to start
  43. IOException ioe = MultipleIOException.createIOException(exns);
  44. if (ioe == null) {
  45. return;
  46. }
  47. throw ioe;
  48. }

通过对以上代码抽象,一般采用以下形式来运用backoff算法。

Java代码  
  1. while (!backoff.isFailed()) {
  2. try {
  3. doSomething(); //do something
  4. backoff.reset(); // reset backoff counter;
  5. return;
  6. } catch (Exception e) {
  7. backoff.backoff();
  8. try {
  9. backoff.waitUntilRetryOk();
  10. } catch (InterruptedException e1) {
  11. }
  12. }
  13. }

目前在flume中主要运用了ExponentialBackoff,CappedExponentialBackoff,CumulativeCappedExponentialBackoff三种退避算法。

ExponentialBackOff是个简单的指数退避算法,仅仅让下次的等待时间是上次等待时间的2倍,当重试次数达到最大重试次数时,该任务将不能重试。

CappedExponentialBackoff对ExponentialBackOff算法作了简单的改造,该算法对每次的等待时间做了个限定,即每次的等待时间不超过某个值sleepCap。但该方法没有限定重试次数。

CumulativeCappedExponentialBackoff算法对CappedExponentialBackoff作了些改造,该算法加入了cumulativeCap变量,用来限制重试次数。在第一次backoff的时候设置failTime值为当前时间+cumulativeCap。是否可以重试由当前时间和failTime决定。当前时间小于failTime则表明还可以重试,否则,不能重试。

通过对以上的分析,可以得到一个Backoff算法必须提供四个接口(isFailed,backOff,waitUntilRetryOk,reset)。其中,isFailed用来判断是否可以重试,backoff用来设置等待时间,waitUntilRetryOk根据backoff设置的等待时间sleep,以便下次重试。reset的接口是在任务成功后,对backoff算法的一些变量重置。详细可以看ExponentialBackoff等源代码。

退避算法为我们在解决重试某项任务的时候,提供了一个比较好的等待思想。

flume之退避算法backoff algorithm相关推荐

  1. flume backoff 退避算法

    原文链接: https://qiuqiang1985.iteye.com/blog/1513049 发生冲突时,每个节点等待一定的时间后重新发送,二进制退避算法中,等待时间以以2位底的指数级增长,失败 ...

  2. Exponetial BackOff(指数退避算法)

    一:介绍 指数退避算法的定义和使用可以在网上搜搜.提供一下wiki的介绍部分定义:an algorithm that uses feedback to multiplicatively decreas ...

  3. [Android Traffic] 调整定时更新的频率(C2DM与退避算法)

    转载自: http://blog.csdn.net/kesenhoo/article/details/7395253 Minimizing the Effect of Regular Updates[ ...

  4. exponential backoff algorithm

    在看NDN的默认转发策略BestRoute Strategy中提到了指数退避算法,回忆了一下,即为: 在一个共享信道的情况下,当网络上的节点在发生冲突时,每个节点节点等待一定的时间后重新发送.在二进制 ...

  5. HMM——维特比算法(Viterbi algorithm)

    1. 前言 维特比算法针对HMM第三个问题,即解码或者预测问题,寻找最可能的隐藏状态序列: 对于一个特殊的隐马尔可夫模型(HMM)及一个相应的观察序列,找到生成此序列最可能的隐藏状态序列. 也就是说给 ...

  6. 分类系列之感知器学习算法PLA 和 口袋算法Pocket Algorithm

    我们有一堆数据,默认他们是线性可分的.  定义f为这个数据分割线的最优解,但是我们不知道他的值.  我们仅有一个函数集H,这个函数一般是无穷大的.我们的目的就是从H中找出一条线g来尽可能的接近f.但是 ...

  7. 反向传播算法 Backpropagation Algorithm

    假设我们有一个固定样本集,它包含 个样例.我们可以用批量梯度下降法来求解神经网络.具体来讲,对于单个样例(x,y),其代价函数为:这是一个(二分之一的)方差代价函数.给定一个包含 个样例的数据集,我们 ...

  8. 疯子的算法总结(二) STL Ⅰ 算法 ( algorithm )

    写在前面: 为了能够使后续的代码具有高效简洁的特点,在这里讲一下STL,就不用自己写堆,写队列,但是做为ACMer不用学的很全面,我认为够用就好,我只写我用的比较多的. 什么是STL(STl内容): ...

  9. 语音识别学习日志 2019-7-17 语音识别基础知识准备6 {维特比算法(Viterbi Algorithm)}

    HMM 维特比算法(Viterbi Algorithm)详细解释参考:http://www.52nlp.cn/hmm-learn-best-practices-six-viterbi-algorith ...

最新文章

  1. Spring学习总结一
  2. nc65语义模型设计_文本匹配方法系列––多维度语义交互匹配模型
  3. django之创建第6-1个项目-自定义过滤器
  4. python series用法_如何使用Python中的Series字典创建数据框?
  5. 工作339:pc父组件通过props传值给子组件,如何避免子组件改变props的属性值报错问题
  6. 互联网日报 | 1月22日 星期五 | 春节返乡防疫政策发布;滴滴成立技术委员会;2021全国网上年货节正式启动...
  7. Docker+Nextcloud快速部署个人网盘
  8. ThreadLocal类的实现用法
  9. Shiro学习笔记四(Shiro集成WEB)
  10. 深入浅出Java反射机制
  11. 数据库系统概论第五版(第 1 章 绪论)习题答案
  12. 《网上订餐系统》开发全程回忆
  13. Matlab导入数据(一定有用!!)
  14. 数字信号处理之均值、方差、均方值、均方差计算和它们的物理意义
  15. 易经六十四卦详解白话文解释——易经64卦全解(下)
  16. 斯卡布罗市集 (口哨/宁林 人声/宁林) - 韩乘光
  17. 网马的反挂马检测及精确投放
  18. MybatisPlus乐观锁配置
  19. thzvv.com forum php,为什么Naver账号不能用了?
  20. K_A02_001 基于单片机驱动4位数码管模块(74HC595) 0-3滚动+ 时钟显示

热门文章

  1. ValueError: only single character unicode strings can be converted to Py_UCS4, got length 0
  2. EasyExcel 动态表头 + 数据单元格合并
  3. 台式主机插入耳机没声音
  4. Canal Admin Web-UI 学习
  5. 便宜SSL证书购买攻略:comodo,geotrust,rapidssl证书为例
  6. 免费SSL证书和付费SSL证书的区别在哪儿?
  7. Criteo数据集预处理
  8. 什么样的公司程序员待遇好
  9. Redis-6.2.* 版本配置文件redis.conf详解
  10. Larval 主从读写分离配置