summaryrefslogtreecommitdiff
path: root/same/src
diff options
context:
space:
mode:
Diffstat (limited to 'same/src')
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosService.java6
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java60
2 files changed, 66 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java
new file mode 100644
index 0000000..3398e81
--- /dev/null
+++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java
@@ -0,0 +1,6 @@
+pakage com.orbekk.paxos;
+
+public interface PaxosService {
+ boolean propose(String clientUrl, int roundId, int proposalNumber);
+ boolean acceptRequest(String clientUrl, int roundId, int proposalNumber);
+}
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
new file mode 100644
index 0000000..a39abbe
--- /dev/null
+++ b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
@@ -0,0 +1,60 @@
+package com.orbekk.paxos;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PaxosServiceImpl implements PaxosService {
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ private int roundId = 0;
+ private int highestPromise = 0;
+ private String tag = "";
+
+ @Override
+ public boolean propose(String clientUrl, int roundId, int proposalNumber) {
+ if (roundId > this.roundId) {
+ newRound(roundId);
+ }
+ if (roundId < this.roundId) {
+ logger.info(tag + "propose({}, {}, {}) = rejected " +
+ "(current round: {})",
+ new Object[]{clientUrl, roundId, proposalNumber,
+ this.roundId});
+ return false;
+ }
+
+ if (proposalNumber > highestPromise) {
+ highestPromise = proposalNumber;
+ logger.info(tag + "propose({}, {}, {}) = accepted",
+ new Object[]{clientUrl, roundId, proposalNumber);
+ return true;
+ } else {
+ logger.info(tag + "propose({}, {}, {}) = rejected " +
+ "(promised: {})",
+ new Object[]{clientUrl, roundId, proposalNumber,
+ highestPromise});
+ return false;
+ }
+ }
+
+ @Override
+ public boolean acceptRequest(String clientUrl, int roundId,
+ int proposalNumber) {
+ if (roundId == this.roundId && proposalNumber == highestPromise) {
+ logger.info(tag + "acceptRequest({}, {}, {}) = accepted",
+ new Object[]{clientUrl, roundId, proposalNumber});
+ return true;
+ } else {
+ logger.info(tag + "acceptRequest({}, {}, {}) = rejected " +
+ "(roundId={}, promise={})",
+ new Object[]{clientUrl, roundId, proposalNumber,
+ this.roundId, highestPromise});
+ return false;
+ }
+ }
+
+ private void newRound(int roundId) {
+ logger.info(tag + "new round: {}", roundId);
+ this.roundId = roundId;
+ highestPromise = 0;
+ }
+}