diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-24 11:27:57 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-24 11:27:57 +0100 |
commit | 5f36d81e47fd142ee7b7c172012917b11411db8d (patch) | |
tree | 74c85ea119184dd17dce7ce7f649433f04b6efb6 /same/src/main/java/com/orbekk | |
parent | 263f3a0b4425287ef234d0dbf039e0027c4ad6c1 (diff) |
Refactor Paxos: Return proposal values.
Diffstat (limited to 'same/src/main/java/com/orbekk')
3 files changed, 39 insertions, 22 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java index 912de7e..7bc4350 100644 --- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java +++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java @@ -17,37 +17,48 @@ public class MasterProposer { this.connections = connections; } - private boolean internalPropose(int proposalNumber) { + private int internalPropose(int proposalNumber) { + int bestPromise = proposalNumber; int promises = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); - boolean success = paxos.propose(myUrl, proposalNumber); - if (success) { + int result = paxos.propose(myUrl, proposalNumber); + if (result == proposalNumber) { promises += 1; } + bestPromise = Math.min(bestPromise, result); + } + if (promises > paxosUrls.size() / 2) { + return proposalNumber; + } else { + return bestPromise; } - return promises > paxosUrls.size() / 2; } - private boolean internalAcceptRequest(int proposalNumber) { + private int internalAcceptRequest(int proposalNumber) { + int bestAccepted = proposalNumber; int accepts = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); - boolean success = paxos.acceptRequest(myUrl, proposalNumber); - if (success) { + int result = paxos.acceptRequest(myUrl, proposalNumber); + if (result == proposalNumber) { accepts += 1; } + bestAccepted = Math.min(bestAccepted, result); + } + if (accepts > paxosUrls.size() / 2) { + return proposalNumber; + } else { + return bestAccepted; } - return accepts > paxosUrls.size() / 2; } public boolean propose(int proposalNumber) { - boolean success = false; - success = internalPropose(proposalNumber); - if (success) { - success = internalAcceptRequest(proposalNumber); + int result = internalPropose(proposalNumber); + if (result == proposalNumber) { + result = internalAcceptRequest(proposalNumber); } - if (success) { + if (result == proposalNumber) { return true; } else { return false; diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java index b515fa9..a92a794 100644 --- a/same/src/main/java/com/orbekk/paxos/PaxosService.java +++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java @@ -1,6 +1,12 @@ package com.orbekk.paxos; public interface PaxosService { - boolean propose(String clientUrl, int proposalNumber); - boolean acceptRequest(String clientUrl, int proposalNumber); + + /** + * @return N == proposalNumber if a promise is made. + * -M if another promise already was made, where M is the promise + * highest proposal number. + */ + int propose(String clientUrl, int proposalNumber); + int acceptRequest(String clientUrl, int proposalNumber); } diff --git a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java index 1e8387e..3ecf523 100644 --- a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java +++ b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java @@ -17,36 +17,36 @@ public class PaxosServiceImpl implements PaxosService { } @Override - public synchronized boolean propose(String clientUrl, + public synchronized int propose(String clientUrl, int proposalNumber) { if (proposalNumber > highestPromise) { - highestPromise = proposalNumber; logger.info(tag + "propose({}, {}) = accepted", new Object[]{clientUrl, proposalNumber}); - return true; + highestPromise = proposalNumber; + return highestPromise; } else { logger.info(tag + "propose({}, {}) = rejected " + "(promised: {})", new Object[]{clientUrl, proposalNumber, highestPromise}); - return false; + return -highestPromise; } } @Override - public synchronized boolean acceptRequest(String clientUrl, + public synchronized int acceptRequest(String clientUrl, int proposalNumber) { if (proposalNumber == highestPromise) { logger.info(tag + "acceptRequest({}, {}) = accepted", new Object[]{clientUrl, proposalNumber}); highestAcceptedValue = proposalNumber; - return true; + return highestAcceptedValue; } else { logger.info(tag + "acceptRequest({}, {}) = rejected " + "(promise={})", new Object[]{clientUrl, proposalNumber, highestPromise}); - return false; + return -highestPromise; } } } |