棋牌游戏评测网首页 | XML地图 | RSS订阅 | 站点导航 欢迎光临 棋牌游戏评测网

当前位置:主页 > 辣妈 >

聊聊rocketmq的LatencyFaultTolerance

时间:2020-06-07 | 来源: | 作者: | 阅读:8718次 |

本文主要研究一下rocketmq的LatencyFaultTolerance

LatencyFaultTolerance

rocketmq-client-4.6.0-sources.jar!/org/apache/rocketmq/client/latency/LatencyFaultTolerance.java

publicinterfaceLatencyFaultTolerance{voidupdateFaultItem;​booleanisAvailable;​voidremove;​TpickOneAtLeast;}LatencyFaultTolerance接口定义了updateFaultItem、isAvailable、remove、pickOneAtLeast方法

LatencyFaultToleranceImpl

rocketmq-client-4.6.0-sources.jar!/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java

publicclassLatencyFaultToleranceImplimplementsLatencyFaultTolerance{privatefinalConcurrentHashMapfaultItemTable=newConcurrentHashMap;​privatefinalThreadLocalIndexwhichItemWorst=newThreadLocalIndex;​@OverridepublicvoidupdateFaultItem{FaultItemold=this.faultItemTable.get;if{finalFaultItemfaultItem=newFaultItem;faultItem.setCurrentLatency;faultIte@AnsonC@SEO@m.setStartTimestamp+notAvailableDuration);​old=this.faultItemTable.putIfAbsent;if{old.setCurrentLatency;old.setStartTimestamp+notAvailableDuration);}}else{old.setCurrentLatency;old.setStartTimestamp+notAvailableDuration);}}​@OverridepublicbooleanisAvailable{finalFaultItemfaultItem=this.faultItemTable.get;if{returnfaultItem.isAvailable;}returntrue;}​@Overridepublicvoidremove{this.faultItemTable.remove;}​@OverridepublicStringpickOneAtLeast{finalEnumerationelements=this.faultItemTable.elements;ListtmpList=newLinkedList;while){finalFaultItemfaultItem=elements.nextElement;tmpList.add;}​if){Collections.shuffle;​Collections.sort;​finalinthalf=tmpList.size/2;if.getName;}else{finalinti=this.whichItemWorst.getAndIncrement%half;returntmpList.get.getName;}}​returnnull;}​@OverridepublicStringtoString{return"LatencyFaultToleranceImpl{"+"faultItemTable="+faultItemTable+",whichItemWorst="+whichItemWorst+'}';}​//......}LatencyFaultToleranceImpl实现了LatencyFaultTolerance接口;它维护了一个faultItemTable,其key为name,value为FaultItem;其updateFaultItem方法会将对应name的currentLatency及notAvailableDuration更新到对应的FaultItem中,没有则创建isAvailable方法则先从faultItemTable获取faultItem,不为null则返回faultItem.isAvailable,为null则返回true;remove方法则执行faultItemTable.removepickOneAtLeast方法首先拷贝一份faultItemTable的FaultItem的列表,若该列表为空则返回null;不为空则对tmpList进行shuffle以及sort,然后取half值/2),若half小于等于0则返回tmpList.get.getName,否则取tmpList.get.getName,其中i由whichItemWorst.getAndIncrement%half计算而来

FaultItem

rocketmq-client-4.6.0-sources.jar!/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java

classFaultItemimplementsComparable{privatefinalStringname;privatevolatilelongcurrentL@AnsonA@SEO@atency;privatevolatilelongstartTimestamp;​publicFaultItem{this.name=name;}​@OverridepublicintcompareTo{if!=other.isAvailable){if)return-1;​if)return1;}​if{return1;}​if{return1;}​return0;}​publicbooleanisAvailable{return-startTimestamp)=0;}​@OverridepublicinthashCode{intresult=getName!=null?getName.hashCode:0;result=31*result+^32));result=31*result+^32));returnresult;}​@Overridepublicbooleanequals{ifreturntrue;if)returnfalse;​finalFaultItemfaultItem=o;​if!=faultItem.getCurrentLatency)returnfalse;if!=faultItem.getStartTimestamp)returnfalse;returngetName!=null?getName.equals):faultItem.getName==null;​}​@OverridepublicStringtoString{return"FaultItem{"+"name='"+name+'\''+",currentLatency="+currentLatency+",startTimestamp="+startTimestamp+'}';}​publicStringgetName{returnname;}​publiclonggetCurrentLatency{@AnsonA@SEO@returncurrentLatency;}​publicvoidsetCurrentLatency{this.currentLatency=currentLatency;}​publiclonggetStartTimestamp{returnstartTimestamp;}​publicvoidsetStartTimestamp{this.startTimestamp=startTimestamp;}​}FaultItem首先了Comparable接口,它定义了name、currentLatency、startTimestamp属性,其isAvailable方法的计算公式为-startTimestamp)=0;其compareTo方法依次根据isAvailable、currentLatency、startTimestamp来排序 (责任编辑:棋牌游戏评测网) 本文地址:/lama/20200607/5819.html