summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-22 18:55:16 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-22 18:55:16 +0100
commit39a99670d96d2d8fca324103d9c950dfea3c7214 (patch)
tree2e42af3cbc74f80e34c537f371b0452967e8c65a /same
parent7e0e5cdef6d3bc1b1c8b08b0172cfef1a9ca920b (diff)
Start implementing Paxos service.
Diffstat (limited to 'same')
-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;
+ }
+}