Mercurial > hg > Database > Christie
changeset 157:7a2108775da7
update paxos done
line wrap: on
line diff
--- a/src/main/java/christie/test/Paxos/AcceptCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -package christie.test.Paxos; - -import christie.annotation.Peek; -import christie.annotation.Take; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; - -public class AcceptCodeGear extends CodeGear { - - @Take - Proposal acceptProposal; - - @Take - Proposal promisedProposal; - - @Override - protected void run(CodeGearManager cgm) { - - if(promisedProposal.getNumber() <= acceptProposal.getNumber()){ - System.out.println("accept: " + acceptProposal.getValue()); - put("acceptProposal", acceptProposal); - return; - }else{ - put("promisedProposal", promisedProposal); - } - - } -}
--- a/src/main/java/christie/test/Paxos/AcceptorCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -package christie.test.Paxos; - -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; - - -public class AcceptorCodeGear extends CodeGear { - - @Override - protected void run(CodeGearManager cgm) { - cgm.setup(new PromiseCodeGear()); - put("promisedProposal", new Proposal()); - } -}
--- a/src/main/java/christie/test/Paxos/LearnerCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -package christie.test.Paxos; - -public class LearnerCodeGear { - -}
--- a/src/main/java/christie/test/Paxos/PromiseCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -package christie.test.Paxos; - -import christie.annotation.Take; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; - -public class PromiseCodeGear extends CodeGear { - - @Take - Proposal promisedProposal; - - @Take - Proposal prepareProposal; - - - public PromiseCodeGear(){ } - - @Override - protected void run(CodeGearManager cgm) { - - String proposerName = prepareProposal.getProposerName(); - - if(promisedProposal.getNumber() < prepareProposal.getNumber()) { - System.out.println("promised < prepare : " + promisedProposal.getNumber() + " < " + prepareProposal.getNumber()); - System.out.println("Acceptor" + cgm.cgmID + " Recive Proposal from : " + proposerName); - promisedProposal = prepareProposal; - - cgm.setup(new AcceptCodeGear()); - }else{ - System.out.println("cannot promiss " + proposerName); - } - - put("promisedProposal", promisedProposal); - put(proposerName,"receivePromise", promisedProposal); - cgm.setup(new PromiseCodeGear()); - } - -} \ No newline at end of file
--- a/src/main/java/christie/test/Paxos/Proposal.java Thu Jan 10 18:04:38 2019 +0900 +++ b/src/main/java/christie/test/Paxos/Proposal.java Tue Jan 15 20:38:26 2019 +0900 @@ -6,10 +6,12 @@ @Message public class Proposal { private String proposerName = ""; + private String acceptorName = ""; private int nodeNum = 0; private int number = 0; private int value = 0; private int id = 0; + private boolean accepted = false; public Proposal(){ @@ -30,6 +32,8 @@ public int getValue(){ return this.value; } + public void setNumber(int number) { this.number = number; } + public int getNumber(){ return this.number; } @@ -47,4 +51,31 @@ public String getProposerName() { return proposerName; } + + public boolean isAccepted() { + return accepted; + } + + public void setAccepted(boolean accepted) { + this.accepted = accepted; + } + + public String getAcceptorName() { + return acceptorName; + } + + public void setAcceptorName(String acceptorName) { + this.acceptorName = acceptorName; + } + + public boolean equalValue(Proposal proposal){ + return this.equalValue(proposal.value); + } + + public boolean equalValue(int value){ + if(this.value == value) return true; + return false; + } + + }
--- a/src/main/java/christie/test/Paxos/ProposerCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -package christie.test.Paxos; - - -import christie.annotation.Peek; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; -import christie.datagear.DataGearManager; - -import java.util.HashMap; - -public class ProposerCodeGear extends CodeGear { - @Peek - int id; - - @Peek - String nodeName; - - @Peek - int nodeNum; - - @Override - protected void run(CodeGearManager cgm) { - - cgm.setup(new SendPrepareRequestCodeGear()); - cgm.setup(new ReceivePromiseCodeGear()); - Proposal sendProposal = new Proposal(nodeName, nodeNum, id, id); - put("sendProposal", sendProposal); - put("promiseCount", 0); - - } -} - -
--- a/src/main/java/christie/test/Paxos/ReceivePromiseCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -package christie.test.Paxos; - -import christie.annotation.Peek; -import christie.annotation.Take; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; -import christie.datagear.DataGearManager; - -import java.util.concurrent.ConcurrentHashMap; - - -public class ReceivePromiseCodeGear extends CodeGear{ - @Peek - int acceptorNodeNum; - - @Take - Proposal sendedProposal; - - @Take - Proposal receivePromise; - - @Take - int promiseCount; - - - @Override - protected void run(CodeGearManager cgm) { - - if(receivePromise.getNumber() == sendedProposal.getNumber()){ - promiseCount++; - if(promiseCount > acceptorNodeNum/2){ - cgm.setup(new SendAcceptRequestCodeGear()); - } - put("sendedProposal", sendedProposal); - - }else if(receivePromise.getNumber() > sendedProposal.getNumber()){ - promiseCount = 0; - sendedProposal.incrementNumber(); - put("sendProposal", sendedProposal); - cgm.setup(new SendPrepareRequestCodeGear()); - } - - put("promiseCount", promiseCount); - cgm.setup(new ReceivePromiseCodeGear()); - } -}
--- a/src/main/java/christie/test/Paxos/SendAcceptRequestCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -package christie.test.Paxos; - -import christie.annotation.Peek; -import christie.annotation.Take; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; - - -public class SendAcceptRequestCodeGear extends CodeGear { - - @Peek - Proposal sendedProposal; - - @Override - protected void run(CodeGearManager cgm) { - cgm.putAllDGM("acceptProposal", sendedProposal); - } -}
--- a/src/main/java/christie/test/Paxos/SendPrepareRequestCodeGear.java Thu Jan 10 18:04:38 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -package christie.test.Paxos; - -import christie.annotation.Peek; -import christie.annotation.Take; -import christie.codegear.CodeGear; -import christie.codegear.CodeGearManager; -import christie.datagear.DataGearManager; - -import java.util.HashMap; -import java.util.concurrent.ConcurrentHashMap; - -public class SendPrepareRequestCodeGear extends CodeGear{ - - @Take - Proposal sendProposal; - - - @Override - protected void run(CodeGearManager cgm) { - cgm.putAllDGM("prepareProposal", sendProposal); - put("sendedProposal", sendProposal); - } -}
--- a/src/main/java/christie/test/Paxos/StartLocalPaxos.java Thu Jan 10 18:04:38 2019 +0900 +++ b/src/main/java/christie/test/Paxos/StartLocalPaxos.java Tue Jan 15 20:38:26 2019 +0900 @@ -3,9 +3,12 @@ import christie.codegear.CodeGearManager; import christie.codegear.StartCodeGear; +import christie.test.Paxos.acceptor.AcceptorCodeGear; +import christie.test.Paxos.learner.LearnerCodeGear; +import christie.test.Paxos.proposer.ProposerCodeGear; import java.util.ArrayList; -import java.util.HashMap; +import java.util.List; public class StartLocalPaxos extends StartCodeGear{ @@ -15,42 +18,62 @@ } public static void main(String args[]){ - int proposer_port = 10000; - int proposers_num = 2; - int acceptor_port = proposer_port + proposers_num; - int acceptors_num = 3; + int proposerPort = 10000; + int proposersNum = 2; + int acceptorPort = proposerPort + proposersNum; + int acceptorsNum = 3; + int learnerPort = acceptorPort + acceptorsNum; + int learnersNum = 1; + List<CodeGearManager> proposers = new ArrayList<>(proposersNum); + List<CodeGearManager> acceptors = new ArrayList<>(acceptorsNum); + List<CodeGearManager> learners = new ArrayList<>(learnersNum); + List<String> acceptorsName = new ArrayList<>(acceptorsNum); + List<String> learnersName = new ArrayList<>(learnersNum); - ArrayList<CodeGearManager> proposers = new ArrayList<>(); - ArrayList<CodeGearManager> acceptors = new ArrayList<>(); - + for(int i = 0; i < learnersNum; i++){ + String nodeName = "learner" + i; + learnersName.add(nodeName); + CodeGearManager learner = createCGM(learnerPort + i); + learner.getLocalDGM().put("acceptorsNum", acceptorsNum); + learner.setup(new LearnerCodeGear()); + learners.add(learner); + } - for(int i = 0; i < acceptors_num; i++){ + for(int i = 0; i < acceptorsNum; i++){ String nodeName = "acceptor" + i; - CodeGearManager acceptor = createCGM(acceptor_port + i); - acceptor.getLocalDGM().put("nodeName", nodeName); + acceptorsName.add(nodeName); + CodeGearManager acceptor = createCGM(acceptorPort + i); + acceptor.getLocalDGM().put("learnersName", learnersName); + acceptor.getLocalDGM().put("acceptorName", nodeName); acceptor.setup(new AcceptorCodeGear()); acceptors.add(acceptor); } - for(int i = 0; i < proposers_num; i++){ + for(int i = 0; i < proposersNum; i++){ String nodeName = "proposer" + i; - CodeGearManager proposer = createCGM(proposer_port + i); - proposer.getLocalDGM().put("nodeName", nodeName); - proposer.getLocalDGM().put("nodeNum", proposers_num + acceptors_num); - proposer.getLocalDGM().put("id", i); - proposer.getLocalDGM().put("acceptorNodeNum", acceptors_num); + CodeGearManager proposer = createCGM(proposerPort + i); + proposer.getLocalDGM().put("sendProposal", new Proposal(nodeName, proposersNum + acceptorsNum, i, i)); + proposer.getLocalDGM().put("acceptorsName", acceptorsName); proposers.add(proposer); } - for(int i = 0; i < proposers_num; i++){ + - for(int j = 0; j < acceptors_num; j++){ - proposers.get(i).createRemoteDGM("acceptor" + j, "localhost", acceptors.get(j).localPort); + for(int i = 0; i < proposersNum; i++){ + for(int j = 0; j < acceptorsNum; j++){ + proposers.get(i).createRemoteDGM(acceptorsName.get(j), "localhost", acceptors.get(j).localPort); acceptors.get(j).createRemoteDGM("proposer" + i, "localhost", proposers.get(i).localPort); + } proposers.get(i).setup(new ProposerCodeGear()); } + for(int i = 0; i < acceptorsNum; i++){ + for(int j = 0; j < learnersNum; j++){ + acceptors.get(i).createRemoteDGM(learnersName.get(j), "localhost", learners.get(j).localPort); + learners.get(j).createRemoteDGM(acceptorsName.get(i), "localhost", acceptors.get(i).localPort); + } + } } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/acceptor/AcceptCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,44 @@ +package christie.test.Paxos.acceptor; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.List; + +public class AcceptCodeGear extends CodeGear { + + @Peek + String acceptorName; + + @Peek + List<String> learnersName; + + @Take + Proposal acceptProposal; + + @Take + Proposal promisedProposal; + + @Override + protected void run(CodeGearManager cgm) { + + if(acceptProposal.getNumber() >= promisedProposal.getNumber()){ + acceptProposal.setAccepted(true); + acceptProposal.setAcceptorName(acceptorName); + for(String learnerName : learnersName) + put(learnerName, "acceptedProposal", acceptProposal); + + put("promisedProposal", acceptProposal); + + }else{ + put("promisedProposal", promisedProposal); + + } + + cgm.setup(new AcceptCodeGear()); + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/acceptor/AcceptorCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,20 @@ +package christie.test.Paxos.acceptor; + +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.List; + + +public class AcceptorCodeGear extends CodeGear { + + + @Override + protected void run(CodeGearManager cgm) { + cgm.setup(new PromiseCodeGear()); + cgm.setup(new AcceptCodeGear()); + put("promisedProposal", new Proposal()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/acceptor/PromiseCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,36 @@ +package christie.test.Paxos.acceptor; + +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +public class PromiseCodeGear extends CodeGear { + + @Take + Proposal promisedProposal; + + @Take + Proposal prepareProposal; + + + public PromiseCodeGear(){ } + + @Override + protected void run(CodeGearManager cgm) { + + if(promisedProposal.getNumber() < prepareProposal.getNumber()) { + // System.out.println("promised < prepare : " + promisedProposal.getNumber() + " < " + prepareProposal.getNumber()); + // System.out.println(" Recive Proposal from : " + prepareProposal.getProposerName()); + if(promisedProposal.isAccepted()){ + promisedProposal.setNumber(prepareProposal.getNumber()); + }else{ + promisedProposal = prepareProposal; + } + } + put("promisedProposal", promisedProposal); + put(prepareProposal.getProposerName(),"receivePromise", promisedProposal); + cgm.setup(new PromiseCodeGear()); + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/learner/AggregateProposalCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,35 @@ +package christie.test.Paxos.learner; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.HashMap; + +public class AggregateProposalCodeGear extends CodeGear { + + @Take + HashMap<String, Proposal> acceptedMap; + + @Peek + int acceptorsNum; + + @Override + protected void run(CodeGearManager cgm) { + HashMap<Integer, Integer> proposalValueMap = new HashMap<>(); + for(Proposal proposal: acceptedMap.values()){ + int value = proposal.getValue(); + if(proposalValueMap.containsKey(value)){ + int count = proposalValueMap.get(value).intValue(); + count++; + if(count > acceptorsNum/2) System.out.println(value); + proposalValueMap.replace(value, count); + }else{ + proposalValueMap.put(value, 1); + } + } + put("acceptedMap", acceptedMap); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/learner/LearnerCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,25 @@ +package christie.test.Paxos.learner; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.annotation.TakeFrom; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.codegear.StartCodeGear; +import christie.test.Paxos.Proposal; + +import java.util.ArrayList; +import java.util.HashMap; + +public class LearnerCodeGear extends CodeGear { + + @Peek + int acceptorsNum; + + @Override + protected void run(CodeGearManager cgm) { + + cgm.setup(new RecieveAcceptProposalCodeGear()); + put("acceptedMap", new HashMap<String, Proposal>(acceptorsNum)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/learner/RecieveAcceptProposalCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,32 @@ +package christie.test.Paxos.learner; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.annotation.TakeFrom; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.HashMap; +import java.util.List; + +public class RecieveAcceptProposalCodeGear extends CodeGear{ + + @Take + Proposal acceptedProposal; + + @Take + HashMap<String, Proposal> acceptedMap; + + @Peek + int acceptorsNum; + + @Override + protected void run(CodeGearManager cgm) { + acceptedMap.put(acceptedProposal.getAcceptorName(), + acceptedProposal); + put("acceptedMap", acceptedMap); + if(acceptedMap.size() > acceptorsNum/2) cgm.setup(new AggregateProposalCodeGear()); + cgm.setup(new RecieveAcceptProposalCodeGear()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/proposer/ProposerCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,18 @@ +package christie.test.Paxos.proposer; + + +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +public class ProposerCodeGear extends CodeGear { + + @Override + protected void run(CodeGearManager cgm) { + + cgm.setup(new SendPrepareRequestCodeGear()); + put("promiseCount", 0); + + } +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/proposer/ReceivePromiseCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,61 @@ +package christie.test.Paxos.proposer; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.List; + + +public class ReceivePromiseCodeGear extends CodeGear{ + @Peek + List<String> acceptorsName; + + @Take + Proposal sendedProposal; + + @Take + Proposal receivePromise; + + @Take + int promiseCount; + + + @Override + protected void run(CodeGearManager cgm) { + promiseCount++; + if(receivePromise.isAccepted()){ + if(sendedProposal.isAccepted()){ + if(receivePromise.getNumber() > sendedProposal.getNumber()){ + put("sendedProposal", receivePromise); + }else{ + put("sendedProposal", sendedProposal); + } + }else{ + sendedProposal.setValue(receivePromise.getValue()); + put("sendedProposal", sendedProposal); + } + }else{ + if(receivePromise.getNumber() > sendedProposal.getNumber()){ + promiseCount = 0; + sendedProposal.incrementNumber(); + put("sendProposal", sendedProposal); + cgm.setup(new SendPrepareRequestCodeGear()); + }else{ + put("sendedProposal", sendedProposal); + } + } + + put("promiseCount", promiseCount); + + + if(promiseCount > acceptorsName.size()/2){ + put("acceptProposal", sendedProposal); + cgm.setup(new SendAcceptRequestCodeGear()); + }else { + cgm.setup(new ReceivePromiseCodeGear()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/proposer/RecieveAcceptedCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,26 @@ +package christie.test.Paxos.proposer; + +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +public class RecieveAcceptedCodeGear extends CodeGear { + + @Take + Proposal acceptedProposal; + + @Take + Proposal sendedProposal; + + @Override + protected void run(CodeGearManager cgm) { + if(!acceptedProposal.isAccepted()){ + sendedProposal.incrementNumber(); + put("sendProposal", sendedProposal); + cgm.setup(new SendPrepareRequestCodeGear()); + }else{ + cgm.setup(new RecieveAcceptedCodeGear()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/proposer/SendAcceptRequestCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,28 @@ +package christie.test.Paxos.proposer; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.test.Paxos.Proposal; + +import java.util.List; + + +public class SendAcceptRequestCodeGear extends CodeGear { + + @Take + Proposal acceptProposal; + + @Peek + List<String> acceptorsName; + + @Override + protected void run(CodeGearManager cgm) { + for(String acceptorName: acceptorsName) + put(acceptorName, "acceptProposal", acceptProposal); + + put("sendedProposal", acceptProposal); + cgm.setup(new RecieveAcceptedCodeGear()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/proposer/SendPrepareRequestCodeGear.java Tue Jan 15 20:38:26 2019 +0900 @@ -0,0 +1,34 @@ +package christie.test.Paxos.proposer; + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.datagear.DataGearManager; +import christie.test.Paxos.Proposal; +import org.msgpack.annotation.Message; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +public class SendPrepareRequestCodeGear extends CodeGear{ + + @Take + Proposal sendProposal; + + @Peek + List<String> acceptorsName; + + + @Override + protected void run(CodeGearManager cgm) { + + for(String acceptorName: acceptorsName) + put(acceptorName, "prepareProposal", sendProposal); + + put("sendedProposal", sendProposal); + cgm.setup(new ReceivePromiseCodeGear()); + } +}