Mercurial > hg > Database > Christie
changeset 148:301c1633745e
update paxos
author | akahori |
---|---|
date | Tue, 08 Jan 2019 16:24:14 +0900 |
parents | 3ce77273d76b |
children | e60ee525841d |
files | src/main/java/christie/test/Paxos/AcceptCodeGear.java src/main/java/christie/test/Paxos/AcceptorCodeGear.java src/main/java/christie/test/Paxos/LearnerCodeGear.java src/main/java/christie/test/Paxos/PromiseCodeGear.java src/main/java/christie/test/Paxos/Proposal.java src/main/java/christie/test/Paxos/ProposerCodeGear.java src/main/java/christie/test/Paxos/ReceivePromiseCodeGear.java src/main/java/christie/test/Paxos/SendAcceptRequestCodeGear.java src/main/java/christie/test/Paxos/SendPrepareRequestCodeGear.java src/main/java/christie/test/Paxos/StartLocalPaxos.java src/main/java/christie/test/Paxos/StartPaxos.java |
diffstat | 11 files changed, 118 insertions(+), 113 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/christie/test/Paxos/AcceptCodeGear.java Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/AcceptCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -1,13 +1,28 @@ 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 Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/AcceptorCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -7,7 +7,7 @@ public class AcceptorCodeGear extends CodeGear { @Override - protected void run(CodeGearManager cgm) {//できるだけ並列に走らせるためにStartCodeGearには書かない + protected void run(CodeGearManager cgm) { cgm.setup(new PromiseCodeGear()); put("promisedProposal", new Proposal()); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/LearnerCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -0,0 +1,5 @@ +package christie.test.Paxos; + +public class LearnerCodeGear { + +}
--- a/src/main/java/christie/test/Paxos/PromiseCodeGear.java Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/PromiseCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -6,9 +6,6 @@ public class PromiseCodeGear extends CodeGear { - - Proposal acceptedProposal; - @Take Proposal promisedProposal; @@ -21,17 +18,20 @@ @Override protected void run(CodeGearManager cgm) { + String proposerName = prepareProposal.getProposerName(); + if(promisedProposal.getNumber() < prepareProposal.getNumber()) { - String proposerName= prepareProposal.getProposerName(); System.out.println("promised < prepare : " + promisedProposal.getNumber() + " < " + prepareProposal.getNumber()); System.out.println("Acceptor" + cgm.cgmID + " Recive Proposal from : " + proposerName); - getLocalDGM().put("promisedProposal", prepareProposal); - getDGM(proposerName).put("recievePromise", prepareProposal); - //cgm.setup(new AcceptCodeGear()); - }else { - put("promisedProposal", promisedProposal); + promisedProposal = prepareProposal; + + cgm.setup(new AcceptCodeGear()); + }else{ + System.out.println("cannot promiss " + proposerName); } + put("promisedProposal", promisedProposal); + put(proposerName,"receivePromise", promisedProposal); cgm.setup(new PromiseCodeGear()); }
--- a/src/main/java/christie/test/Paxos/Proposal.java Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/Proposal.java Tue Jan 08 16:24:14 2019 +0900 @@ -28,6 +28,8 @@ this.value = value; } + public int getValue(){ return this.value; } + public int getNumber(){ return this.number; }
--- a/src/main/java/christie/test/Paxos/ProposerCodeGear.java Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/ProposerCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -1,6 +1,7 @@ package christie.test.Paxos; +import christie.annotation.Peek; import christie.codegear.CodeGear; import christie.codegear.CodeGearManager; import christie.datagear.DataGearManager; @@ -8,14 +9,22 @@ import java.util.HashMap; public class ProposerCodeGear extends CodeGear { + @Peek + int id; - HashMap<String, DataGearManager> acceptors; + @Peek + String nodeName; + + @Peek + int nodeNum; @Override protected void run(CodeGearManager cgm) { cgm.setup(new SendPrepareRequestCodeGear()); - put("sendProposal", new Proposal()); + 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 Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/ReceivePromiseCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -11,47 +11,36 @@ public class ReceivePromiseCodeGear extends CodeGear{ @Peek - ConcurrentHashMap<String, DataGearManager> acceptors; + int acceptorNodeNum; - @Peek + @Take Proposal sendedProposal; @Take - Proposal recievePromise; + Proposal receivePromise; @Take int promiseCount; - @Take - long startTimeMillis; - - int timeOut_ms = 1 * 1000; - @Override protected void run(CodeGearManager cgm) { - if(promiseCount > acceptors.size()/2){ - System.out.println(cgm.cgmID + " Can send AcceptRequest"); - return; - } - - if(recievePromise.getNumber() == sendedProposal.getNumber()){ - promiseCount = promiseCount + 1; - - } + if(receivePromise.getNumber() == sendedProposal.getNumber()){ + promiseCount++; + if(promiseCount > acceptorNodeNum/2){ + cgm.setup(new SendAcceptRequestCodeGear()); + } + put("sendedProposal", sendedProposal); - long endTimeMillis = System.currentTimeMillis(); - if(endTimeMillis - startTimeMillis < timeOut_ms){ - put("promiseCount", promiseCount); - put("recievePromise", new Proposal()); - put("startTimeMillis", startTimeMillis); - cgm.setup(new ReceivePromiseCodeGear()); - - }else{ - put("sendProposal", sendedProposal.getIncrementedProposal()); + }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 Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/SendAcceptRequestCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -4,19 +4,15 @@ import christie.annotation.Take; import christie.codegear.CodeGear; import christie.codegear.CodeGearManager; -import christie.datagear.DataGearManager; -import java.util.concurrent.ConcurrentHashMap; public class SendAcceptRequestCodeGear extends CodeGear { + @Peek - ConcurrentHashMap<String, DataGearManager> acceptors; - - @Take Proposal sendedProposal; @Override protected void run(CodeGearManager cgm) { - System.out.println("send Accept Request"); + cgm.putAllDGM("acceptProposal", sendedProposal); } }
--- a/src/main/java/christie/test/Paxos/SendPrepareRequestCodeGear.java Tue Jan 08 11:43:13 2019 +0900 +++ b/src/main/java/christie/test/Paxos/SendPrepareRequestCodeGear.java Tue Jan 08 16:24:14 2019 +0900 @@ -10,21 +10,14 @@ import java.util.concurrent.ConcurrentHashMap; public class SendPrepareRequestCodeGear extends CodeGear{ - @Peek - HashMap<String, DataGearManager> acceptors; @Take Proposal sendProposal; + @Override protected void run(CodeGearManager cgm) { - for (String acceptorName : acceptors.keySet()){ - put(acceptorName,"prepareProposal", sendProposal); - } - + cgm.putAllDGM("prepareProposal", sendProposal); put("sendedProposal", sendProposal); - put("promiseCount", 0); - put("startTimeMillis", System.currentTimeMillis()); - cgm.setup(new ReceivePromiseCodeGear()); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Paxos/StartLocalPaxos.java Tue Jan 08 16:24:14 2019 +0900 @@ -0,0 +1,56 @@ +package christie.test.Paxos; + +import christie.codegear.CodeGearManager; + +import christie.codegear.StartCodeGear; + +import java.util.ArrayList; +import java.util.HashMap; + +public class StartLocalPaxos extends StartCodeGear{ + + public StartLocalPaxos(CodeGearManager cgm) { + super(cgm); + + } + + 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; + + ArrayList<CodeGearManager> proposers = new ArrayList<>(); + ArrayList<CodeGearManager> acceptors = new ArrayList<>(); + + + for(int i = 0; i < acceptors_num; i++){ + String nodeName = "acceptor" + i; + CodeGearManager acceptor = createCGM(acceptor_port + i); + acceptor.getLocalDGM().put("nodeName", nodeName); + acceptor.setup(new AcceptorCodeGear()); + acceptors.add(acceptor); + } + + for(int i = 0; i < proposers_num; 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); + 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); + acceptors.get(j).createRemoteDGM("proposer" + i, "localhost", proposers.get(i).localPort); + } + proposers.get(i).setup(new ProposerCodeGear()); + } + + } +} \ No newline at end of file
--- a/src/main/java/christie/test/Paxos/StartPaxos.java Tue Jan 08 11:43:13 2019 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -package christie.test.Paxos; - -import christie.codegear.CodeGearManager; - -import christie.codegear.StartCodeGear; - -import java.util.ArrayList; -import java.util.HashMap; - -public class StartPaxos extends StartCodeGear{ - - public StartPaxos(CodeGearManager cgm) { - super(cgm); - - } - - 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; - - - - HashMap<String, CodeGearManager> proposers = new HashMap<>(); - HashMap<String, CodeGearManager> acceptors = new HashMap<>(); - - - for(int i = 0; i < acceptors_num; i++){ - CodeGearManager acceptor = createCGM(acceptor_port + i); - String nodeName = "acceptor" + i; - acceptor.getLocalDGM().put("nodeName", nodeName); - acceptor.setup(new AcceptorCodeGear()); - acceptors.put(nodeName, acceptor); - } - - for(int i = 0; i < proposers_num; i++){ - String nodeName = "proposer" + i; - CodeGearManager proposer = createCGM(proposer_port + i); - proposer.getLocalDGM().put("nodeName", "proposer" + i); - proposer.getLocalDGM().put("nodeNum", proposers_num + acceptors_num); - proposers.put(nodeName, 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); - acceptors.get(j).createRemoteDGM("proposer" + i, "localhost", proposers.get(i).localPort); - } - proposers.get(i).setup(new ProposerCodeGear()); - } - - } - - - - -} \ No newline at end of file