分布式算法领导者选举_第1页
分布式算法领导者选举_第2页
分布式算法领导者选举_第3页
分布式算法领导者选举_第4页
分布式算法领导者选举_第5页
已阅读5页,还剩61页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

分布协同计算基础第二章:分布式算法(2)

---领导者选举张锡哲副教授计算机应用技术研究所东北大学信息科学与工程学院选举问题Ifweareusingoneprocessasacoordinatorforasharedresource……howdoweselectthatoneprocess?Often,thereisnoownerormasterthatisautomaticallyconsideredascoordinator选举问题Manydistributedsystemsareclientserverbased,withoneserver/coordinator(leader),andmultipleclientsWhathappensiftheleaderfails?ElectanewoneCannotassumeonlyoneprocessdetectsthecrashTheelectionshouldgetalloftheprocessestoagreethatonenodeisaleader选举谁?Basicidea:Ifeachprocesshasauniqueidentity,andtheidentitiesareorderedElectthenon-crashednodewithminimum/maximumidentity选举算法旳应用whenacentralizedalgorithmistobe

executedinadistributedsystemforinstance,afterasystemcrashwhenitis

unknownwhichnodesareworking什么是选举

“tostartfromaconfigurationwhereall

processesareinthesamestateandarriveat

aconfigurationwhereexactlyoneprocessis

theleader”

Goal:toobtainlowmessagecomplexity选举算法旳定义Formally,analgorithmisanelectionalgorithmiff:EachprocesshasthesamelocalalgorithmThealgorithmisdecentralizedItcanbeinitiatedbyanynumberofprocessesinthesystemItreachesaterminalconfiguration,andineachreachableterminalconfigurationoneprocessisinstateleaderandtherestareinthestatelost假设FullyasynchronoussystemNoglobalclock,transmissiontimesarbitraryEveryprocesshasauniqueidentityIdentitiesaretotallyorderedBecausewehavefinitenumberofprocesses:MaxidentityandMinidentityavailable(extremevalues)RemarkProcessorsareinoneofthreestates:undecided,leader,lostInitiallyeveryprocessisintheundecidedstateWhenleavingtheundecidedstate,aprocessorgoesintoaterminatedstate(leaderorlost)环网上旳选举单向环双向环LeLann77,O(N2)Chang-Roberts79Ω(N2),Θ(NlogN)Hirshbergetal.80Ω(NlogN)Petersen/Dolevetal.82Ω(NlogN)11环网上旳选举ThefirsteverelectionalgorithmwasdoneforringsbyLeLannSimpleidea:LeteveryinitiatorsendatokenwiththeiridentityaroundthewholeringNodesarenotallowedtoinitiateaftertheyreceiveatoken(example…)12ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiator13ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiator14ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiate15ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiatenotallowedtoinitiate16ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiatenotallowedtoinitiatenotallowedtoinitiate17ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiatenotallowedtoinitiateinitiatornotallowedtoinitiate18ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiatenotallowedtoinitiateinitiatornotallowedtoinitiatenotallowedtoinitiate19ExampleNodesarenotallowedtoinitiateamessageaftertheyreceiveatoken(example…)35198267initiatornotallowedtoinitiatenotallowedtoinitiateinitiatornotallowedtoinitiatenotallowedtoinitiatenotallowedtoinitiate20LeLann’sringelectionWheneveranodereceivesbackitsid,ithasseeneveryotherinitiatorsidAssumingFIFOchannelsLeteverynodekeepalistofeveryidentifierseen(listp)Ifnon-initiator,state=lostimmediatelyIfinitiator,whenownidreceived:state=leaderifmin{listp}=pstate=lost

otherwiseLeLann’sAlgorithmNon-initiatorsjustforwardandloseInitiallyonlyknowmyselfSendmyid,andwaitRepeatforwardingandcollectingidsuntilwereceiveouridTermination:didwewinorlose22MessageComplexityWorstcaseiseverynodeisinitiator(N)EveryinitiatorsendsNmessagesGivesatotalofN2messages23时间复杂度AssumelastinitiatorfstartsattimeN-1fterminatesafteritstokenhascirculatedthering,NstepsTimecomplexity2N-135198267initiatorlastinitiator24Chang-Roberts算法ChangandRobertscameupwithasmallimprovementIdea:Whenanodereceivesatokenwithlargeridthanitself,whyshoulditkeepforwardingit?Itisawaste,weknowthatthatidwillneverwin!Letsdroptokenswithlargeridsthanourselves!25HowtomakeitworkButifwedropatokenwithids,nodeswillbewaitingforeveronitstokentocomebackHowdowesolvethat?26HowtomakeitworkIfnodestokenisdropped,itwillanywayreceiveanothertokenwithloweridInthatcase,sshouldsetstate=lostChangandRobertsAlgoNon-initiatorsjustforwardandloseInitiatorsendsitstokenWhilenotleader,keepreceivingtokensIfmytokencomes,thenIwonOtherwise,onlyforwardifit’sloweridthanmeandlose28ChangRoberts-ExampleNodes1,3,6areinitiators6913Non-initiatorLostWon6Initiator132901753264ChangRobertsAnalysisWorstcasecomplexitysameasLeLann’sTimeComplexity:2N-1MessageComplexity:O(N2)ConsideredasortedringwithNinitiators8times7times6times5times4times3times2times1times30Furtherimprovements?Letthenon-initiatorsalsofilterloweridsDonotjustcomparewithyourid,comparewiththelowestidyouhaveseen!HSRingElectionHirschbergSinclairAlgorithm(ringmodification)O(N2)isalotofmessages.HereisamodificationthatisO(NlogN).Assumptions:theringsizecanbeunknown.Thecommunicationsmustbebidirectional.Allnodesstartmoreorlessatthesametime.Eachnodeoperatesinphasesandsendsouttokens.Thetokenscarryhop-countsanddirectionflagsinadditiontotheIDofthesender.3

ID=3,2hops

clockwise

ID=3,2hops

counterclckwsHSRingElectionPhasesarenumbered0,1,2,3,…Ineachphase,k,nodejsendsouttokensujcontainingitsIDinbothdirections.Thetokenstravel2khopsthenreturntotheiroriginj.Ifbothtokensmakeitback,processjcontinueswiththenextphase(incrementsk).Ifbothtokensdonotmakeitback,processjsimplywaitstobetoldwhotheresultsoftheelection.33xxiPhase0:20Phase1:21Phase2:22TrajectoryofsuccessivetokensoriginatingatprocessiHSRingElection6-34HSRingElectionIfaprocessmreceivesatokenujgoingintheoutbounddirection,itcomparesthetoken’sIDwithitsown.IfithasalargerID,itsimplydiscardsthetoken.IfithasasmallerID,itrelaysthetokenasrequested.IfitisequaltothetokenID,ithasreceiveditsowntokenintheoutbounddirection,sothetokenhasgonecleararoundtheringandtheprocessdeclaresitselfleader.Allprocessesalwaysrelayinboundtokens.4ID=3,2hopsclockwise非形式化描述Eachprocessioperatesinphases0,1,2,....Ineachphasel,processisendsout"tokens"containingitsUIDuiinbothdirections.Theseareintendedtotraveldistance2l,thenreturntotheirorigini.Ifbothtokensmakeitbacksafely,processicontinueswiththefollowingphase.However,thetokensmightnotmakeitbacksafely.Whileauitokenisproceedingintheoutbounddirection,eachotherprocessjonui'spathcomparesuiwithitsownUIDuj.Ifui<uj,thenjsimplydiscardsthetoken,whereasifui>uj,thenjrelaysui.Ifui=uj,thenitmeansthatprocessjhasreceiveditsownUIDbeforethetokenhasturnedaround,soprocessjelectsitselfastheleader.Allprocessalwaysrelayalltokensintheinbounddirection.HSRingElection一种例子Fordemonstrationpurposes,wewillignoretheasynchronousnatureofthesystemandassumethatallnodesoperateatthesamespeedandatthesametime.InitialnetworkconfigurationwithUIDvaluesinblue.015225314456544134Initialconfiguration.BluenumbersareUIDvaluesThealgorithmstartsinphase0,andeachnodewillsendanoutboundmessagetoitsneighbors.Themessageisintendedtotraveladistanceof20

hops.ThenodesthatreceivedaninboundmessagewithitsOWNUIDfrombothneighborsarequalifiedtoproceedtothenextphase.Theyareindicatedingray.015225314456544134Thenodeseligibletocontinuetophase2Nowweadvancetophase1,andeachactivenodewillsendanoutboundmessagetoitsneighbors.Themessagethistimeisintendedtotraveladistanceof21

hops.Thestraightarrowsrepresentsoutboundandthecurvedarrowsrepresentsinbound.

015225314456544134Phase1.Onlynode4receivesaninboundmessagefrombothneighbors34345656Phase2.Onlynode4isactiveinphase2.Itistheonlynodethatcaninitiateamessage.Themessageisintendedtotravel22

hops.Outgoingmessagessentbynode4intheclockwisedirectionareinpink.Outgoingmessagesinthecounter-clockwisedirectionareingreen.Straightlinesareoutgoingmessages,curvedlinesareincoming.015225314456544134Phase2.Node4onceagainreceivesbothinboundmessageswithownUID5656Phase3.Onlynode4isactiveinphase2.Itistheonlynodethatcaninitiateamessage.Itsmessagesareintendedtotravel23

hops.015225314456544134Phase3.Node4changesitsstatustoleader.43AnalysisofHSRingElectionMessageComplexity:Eachmsgbelongstoaparticularphaseandisinitiatedbyaparticularproc.Probedistanceinphaseiis2iNumberofmsgsinitiatedbyaproc.inphaseiisatmost4*2i(probesandrepliesinbothdirections)44AnalysisofHSRingElectionHowmanyprocs.initiateprobesinphasei?Fori=0,everyproc.doesFori>0,everyproc.thatisa"winner"inphasei-1does"winner"meanshaslargestidinits2i-1neighborhood45AnalysisofHSRingElectionMaximumnumberofphasei-1winnersoccurswhentheyarepackedasdenselyaspossible:totalnumberofphasei-1winnersisatmostn/(2i-1+1)46AnalysisofHSRingElectionHowmanyphasesarethere?Ateachphasethenumberof(phase)winnersiscutinhalf(atleast)Soafteratmostlog2

nphases,onlyonewinnerisleft.47AnalysisofHSRingElectionTotalnumberofmessagesissum,overallphases,ofnumberofwinnersatthatphasetimesnumberofmessagesoriginatedbythatwinner:phase0msgsmsgsforphases1tolognterminationmsgsFranklin算法Allactiveidentitiescomparethemselveswiththe

closestactiveneighbortotheleftandtotherightTheidentityremainsactiveifitisthelocalmin.Peterson/Dolev-Klawe-RodehPetersonLeader-ElectionAlgorithmArbitraryelectionofleaderusingcomparisonofUID’susingunidirectionalcommunicationAlgorithmrunsinphasesinwhicheachprocessisassignedtoactiveorpassivemode(allprocessesstartasactive)ThenumberofactiveprocessesisreducedbyafactoroftwoduringeachphaseSummary:AtthebeginningofeachphaseeachactiveprocessisendsitsUIDtwostepsclockwise.ThenprocessicomparesitsownUIDtothetwoUIDsitreceived.Ifui-1<ui-2andui-1<ui,processiremainsactiveadoptingtheUIDofitscounterclockwiseneighborOtherwiseprocessibecomesarelayPetersonLeaderElectionExamplei11UID=9i10UID=4i9UID=5i12UID=7i8UID=11i7UID=12i6UID=3i5UID=2i4UID=6i3UID=1i2UID=10i1UID=8nPhase1Phase2Phase3Phase4187,9---2108,7---3110,8109,12--461,10---526,1610,91012,101212,12632,6---7123,2---81112,3126,10--9511,12---1045,11---1194,5---1279,4912,61210,12-PetersonLeader-ElectionExamplePetersonLeader-ElectionAlgorithmAnyactiveprocesssurvivesaphaseonlyiftheactiveprocesstoitsleftdoesnotsurvivethatphase.Henceofmactiveprocessesduringaphase,atmostm/2willsurviveform>1.TherecanbeatmostlogNphasestoreachoneactiveprocess.Duringaphaseeveryprocesssends(andreceive)twomessages,andonthelastphasetheloneactiveprocesssendsonemessagewhichisrelayedaroundtothemaximumforatotalof2NlogN+N.5455同步环中旳选举Hereisasimplealgorithm.Grouproundsintophases,eachphasecontainingn

roundsInphasei,theprocessorwithidi,ifthereisone,sendsamessagearoundtheringandiselected.56ExampleofSimpleSynchronousAlgorithmn=4,thesmallestidis7.Inphases0through6(correspondingtorounds1through28),nomessageiseversent.Atbeginningofphase7(round29),processorwithid7sendsmessagewhichisforwardedaroundthering.Reliesonsynchronyandknowingn57AnalysisofSimpleAlgorithmCorrectness:Easytosee.Messagecomplexity:

O(n),whichisoptimalTimecomplexity:

O(n*m),wheremisthesmallestidinthering.notboundedbyanyfunctionofn!58AnotherSynchronousAlgorithmWorksinaslightlyweakermodelthantheprevioussynchronousalgorithm:processorsmightnotallstartatsameround;aprocessoreitherwakesupspontaneouslyorwhenitfirstgetsamessageuniform(doesnotrelyonknowingn)59AnotherSynchronousAlgorithmAprocessorthatwakesupspontaneouslyisactive;sendsitsidinafast

message(oneedgeperround)Aprocessorthatwakesupwhenreceivingamsgisrelay;neverinthecompetitionAfastmessagecarryingidmbecomesslow

ifitreachesanactiveprocessor;startstravelingatoneedgeper2mroundsAprocessoronlyforwardsamsgwhoseidissmallerthananyidishaspreviouslysentIfaproc.getsitsownidback,electsself60AnalysisofSynchronousAlgorithmCorrectness:convinceyourselfthattheactiveprocessorwithsmallestidiselected.Messagecomplexity:Winner'smsgisthefastest.Whileittraversesthering,othermsgsareslower,sotheyareovertakenandstoppedbeforetoomanymessagesaresent.61MessageComplexityDividemsgsintothreekinds:fastmsgsslowmsgssentwhiletheleader'smsgisfastslowmsgssentwhiletheleader'smsgisslowNext,countthenumberofeachtypeofmsg.62

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论