summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk
diff options
context:
space:
mode:
Diffstat (limited to 'same/src/main/java/com/orbekk')
-rw-r--r--same/src/main/java/com/orbekk/paxos/MasterProposer.java37
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosService.java10
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java14
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;
}
}
}