diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-23 20:03:05 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-23 20:03:05 +0100 |
commit | 1ae55b79f849cfd2e8b24311b63caa04baa5d3c2 (patch) | |
tree | 157faa372ad7d74808ea8ae2235b2e46ac1bdcf5 /same/src/main/java/com/orbekk | |
parent | 060274f31ea04b9fe284d91f457f3d82c11dbaed (diff) |
Add MasterProposer.
MasterProposer tries to propose a value to a list of PaxosServices.
Diffstat (limited to 'same/src/main/java/com/orbekk')
-rw-r--r-- | same/src/main/java/com/orbekk/paxos/MasterProposer.java | 43 |
1 files changed, 11 insertions, 32 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java index c33cf68..e7c6e8a 100644 --- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java +++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java @@ -5,39 +5,19 @@ import java.util.List; import com.orbekk.same.ConnectionManager; -public class MasterProposer implements Runnable { +public class MasterProposer { private String myUrl; - private int roundId = 0; - private int proposalNumber = 0; private List<String> paxosUrls = new ArrayList<String>(); - private Runnable roundFailedAction; - private Runnable masterAction; private ConnectionManager connections; - public static Runnable getTimeoutAction(final long milliseconds) { - return new Runnable() { - @Override public void run() { - try { - Thread.sleep(milliseconds); - } catch (InterruptedException e) { - // Ignore interrupts. - } - } - }; - } - - MasterProposer(String clientUrl, List<String> paxosUrls, int roundId, - ConnectionManager connections, Runnable roundFailedAction, - Runnable masterAction) { + MasterProposer(String clientUrl, List<String> paxosUrls, + ConnectionManager connections) { this.myUrl = clientUrl; this.paxosUrls = paxosUrls; - this.roundId = roundId; this.connections = connections; - this.roundFailedAction = roundFailedAction; - this.masterAction = masterAction; } - private boolean propose(int roundId, int proposalNumber) { + private boolean internalPropose(int roundId, int proposalNumber) { int promises = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); @@ -49,7 +29,7 @@ public class MasterProposer implements Runnable { return promises > paxosUrls.size() / 2; } - private boolean acceptRequest(int roundId, int proposalNumber) { + private boolean internalAcceptRequest(int roundId, int proposalNumber) { int accepts = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); @@ -60,18 +40,17 @@ public class MasterProposer implements Runnable { } return accepts > paxosUrls.size() / 2; } - - @Override public void run() { + + public boolean propose(int roundId, int proposalNumber) { boolean success = false; - success = propose(roundId + 1, proposalNumber + 1); + success = internalPropose(roundId, proposalNumber); if (success) { - success = acceptRequest(roundId + 1, proposalNumber + 1); + success = internalAcceptRequest(roundId, proposalNumber); } if (success) { - masterAction.run(); + return true; } else { - roundFailedAction.run(); - // TODO: Next round? + return false; } } } |