PackagePaxos;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collection;Importjava.util.Collections;Importjava.util.List;ImportJava.util.Random;Importorg.apache.commons.lang3.StringUtils;Importcom.google.common.base.Charsets;Importcom.google.common.hash.HashFunction;Importcom.google.common.hash.Hashing;/*** *〈 A word function brief 〉<br> *〈 function detailed description 〉paxos algorithm implementation *@authorShichang.liu * @date April 6, 2017 morning 10:25:34 *@see[Related Classes/methods] (optional) *@since[Product/module version] (optional)*/ Public Final classPaxosdemo {Private Static FinalHashfunction hash_function =hashing.murmur3_32 (); Private Static FinalRandom random =NewRandom (); Private Static FinalString[] proposals = {"ProjectA", "PROJECTB", "PROJECTC" }; Public Static voidMain (string[] args) {List<Acceptor> acceptors =NewArraylist<acceptor>(); Arrays.aslist ("A", "B", "C", "D", "E"). ForEach (Name-Acceptors.add (Newacceptor (name))); Proposer.vote (NewProposal (1L,NULL), acceptors); } Private Static voidPrintinfo (string subject, string operation, string result) {System.out.println (subject+ ":" + operation + "<" + result + ">"); } /*** For the constraints of the proposal, the third constraint requires: If the maxvote does not exist, then there is no limit, the next vote can use any proposal; otherwise, the next vote will follow Maxvote's proposal * *@paramCurrentvotenumber *@paramProposals *@return */ Private StaticProposal Nextproposal (LongCurrentvotenumber, list<proposal>proposals) { LongVotenumber = Currentvotenumber + 1; if(Proposals.isempty ())return Newproposal (Votenumber, Proposals[random.nextint (proposals.length)]); Collections.sort (proposals); Proposal Maxvote= Proposals.get (Proposals.size ()-1); LongMaxvotenumber =Maxvote.getvotenumber (); String content=maxvote.getcontent (); if(Maxvotenumber >=currentvotenumber)Throw NewIllegalStateException ("Illegal state maxvotenumber"); if(Content! =NULL) return Newproposal (votenumber, content); Else return Newproposal (Votenumber, Proposals[random.nextint (proposals.length)]); } Private Static classProposer {/** * @paramProposal *@paramacceptors*/ Public Static voidVote (proposal proposal, collection<acceptor>acceptors) { intQuorum = Math.floordiv (Acceptors.size (), 2) + 1; intCount = 0; while(true) {Printinfo ("Vote_round", "START", ++count + ""); List<Proposal> proposals =NewArraylist<proposal>(); for(acceptor acceptor:acceptors) {Promise Promise=Acceptor.onprepare (proposal); if(Promise! =NULL&&promise.isack ()) Proposals.add (Promise.getproposal ()); } if(Proposals.size () <quorum) {Printinfo ("proposer[" + proposal + "]", "VOTE", "Not PREPARED"); Proposal=nextproposal (Proposal.getvotenumber (), proposals); Continue; } intAcceptcount = 0; for(acceptor acceptor:acceptors) {if(acceptor.onaccept (proposal)) Acceptcount++; } if(Acceptcount <quorum) {Printinfo ("proposer[" + proposal + "]", "VOTE", "Not ACCEPTED"); Proposal=nextproposal (Proposal.getvotenumber (), proposals); Continue; } Break; } printinfo ("proposer[" + proposal + "]", "VOTE", "SUCCESS"); } } Private Static classAcceptor {//Last voting result PrivateProposal last =Newproposal (); PrivateString name; Publicacceptor (String name) { This. Name =name; } PublicPromise Onprepare (proposal proposal) {//Let's say this process has a 50% chance of failure. if(Math.random ()-0.5 > 0) {Printinfo ("Accepter_" + Name, "PREPARE", "NO RESPONSE"); return NULL; } if(Proposal = =NULL) Throw NewIllegalArgumentException ("Null proposal"); if(Proposal.getvotenumber () >Last.getvotenumber ()) {Promise response=NewPromise (true, last); Last=proposal; Printinfo ("Accepter_" + Name, "PREPARE", "OK"); returnresponse; } Else{printinfo ("Accepter_" + Name, "PREPARE", "Rejected"); return NewPromise (false,NULL); } } Public BooleanOnAccept (proposal proposal) {//Let's say this process has a 50% chance of failure. if(Math.random ()-0.5 > 0) {Printinfo ("Accepter_" + Name, "ACCEPT", "NO RESPONSE"); return false; } printinfo ("Accepter_" + Name, "ACCEPT", "OK"); returnLast.equals (proposal); } } Private Static classPromise {Private Final Booleanack; Private Finalproposal proposal; PublicPromise (BooleanACK, proposal proposal) { This. Ack =ack; This. Proposal =proposal; } Public BooleanIsack () {returnack; } Publicproposal Getproposal () {returnproposal; } } Private Static classProposalImplementsComparable<proposal> { Private Final LongVotenumber; Private FinalString content; PublicProposal (LongVotenumber, String content) { This. Votenumber =Votenumber; This. Content =content; } Publicproposal () { This(0,NULL); } Public LongGetvotenumber () {returnVotenumber; } PublicString getcontent () {returncontent; } @Override Public intCompareTo (Proposal O) {returnLong.compare (Votenumber, O.votenumber); } @Override Public Booleanequals (Object obj) {if(obj = =NULL) return false; if(! (objinstanceofproposal)) return false; Proposal proposal=(proposal) obj; returnVotenumber = = Proposal.votenumber &&stringutils.equals (content, proposal.content); } @Override Public inthashcode () {returnHash_function.newhasher (). Putlong (Votenumber). putstring (content, Charsets.utf_8). HASH (). Asint (); } @Override PublicString toString () {return NewStringBuilder (). Append (Votenumber). Append (': '). Append (content). toString (); } }}
Paxos algorithm Implementation