flume之退避算法backoff algorithm[通俗易懂]

flume之退避算法backoff algorithm[通俗易懂]flume之退避算法backoffalgorithm什么是退避算法:Inasinglechannelcontentionbasedmediumaccesscontrol(MAC)protocols,whenevermorethanonestationornodetriestoaccessthemediumatthesameinstantof…

大家好,又见面了,我是你们的朋友全栈君。

什么是退避算法:

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等源代码。

 

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149186.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • javah是什么命令_SQL命令

    javah是什么命令_SQL命令今天看了《android框架揭秘》一书中的jni这块,按照书上的写法试了试javah命令,  今天通过javah生成h文件时遇到各种问题。不管是javah-classpath参数啊还是别的什么,一直都无法成功通过class文件生成h文件。  想了想应该是路径问题,现在终于成功编译出来了~  先在cmd命令行里面切换到工程的目录  调用javah命令时,j

    2022年9月24日
    1
  • python如何安装sklearn库

    python如何安装sklearn库1.正常的安装思路是win+Rcmdpipinstall+所要装的库然后就会这样2.其实在我换了3.8版本之后在安装python库的时候,基本上pipinstall+库名80%是会成功的,对于这种失败的,我用了第二种方法进行安装。在官网https://www.lfd.uci.edu/~gohlke/pythonlibs/#找到库对应的文件进行下载下载好了之后可以在浏览器的“下载内容”找到我的建议是点“在文件夹中显示”然后在文件夹中选中复制切回c…

    2022年10月17日
    2
  • Java创建WebService服务及客户端实现

    Java创建WebService服务及客户端实现简介WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto.com/art/200908/147125.htm,不同的语言平台对WebService都有实现,Java的WebService实现,比较流行的有Axis2、Jaxws,…

    2022年7月13日
    21
  • 分子模拟软件amber_[gromacs使用教程] 基于amber力场模拟蛋白小分子复合物

    分子模拟软件amber_[gromacs使用教程] 基于amber力场模拟蛋白小分子复合物祥请参考官网教程,使用其中的mdp参数文件(均100ps),案例只考虑模拟顺利,暂不考虑合理性。平台:windows软件:gaussina16,ambertools,gromacs2019.6,notepad++,spdbv4.10蛋白文件:4w52.pdb(配体选用EPE)小分子amber力场及坐标文件构建参考本公众号的案例蛋白的修复使用Notepad++删除小分子,水,保存文…

    2022年5月9日
    58
  • 用计算机最炫民族风乐谱,最炫民族风乐谱及歌词[通俗易懂]

    用计算机最炫民族风乐谱,最炫民族风乐谱及歌词[通俗易懂]最炫民族风乐谱及歌词《最炫民族风》是凤凰传奇演唱的一首流行歌曲,由张超作词和谱曲,发行于2009年5月27日,是其第三张专辑《最炫民族风》的主打歌。下面由百分网小编为大家介绍《最炫民族风》乐谱,希望能帮到你。《最炫民族风》乐谱【图片来源:中国曲谱网】《最炫民族风》歌词苍茫的天涯是我的爱绵绵的青山脚下花正开什么样的节奏是最呀最摇摆什么样的歌声才是最开怀弯弯的河水从天上来流向那万紫千红一片海火辣辣的歌…

    2022年9月22日
    2
  • python的几个有趣小程序「建议收藏」

    python的几个有趣小程序「建议收藏」最近整理一些python的小程序以及几个第三方库的简单使用,一方面用来熟悉手感,另一方面也用来休闲娱乐。文本进度条的编写:importtimescale=50print(“starting”.center(scale//2,”-“))start=time.perf_counter()foriinrange(scale+1): a=’*’*i b=’.’*(scale-i)…

    2022年6月22日
    144

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号