summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
Diffstat (limited to 'same')
-rw-r--r--same/src/main/java/com/orbekk/paxos/MasterProposer.java14
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosService.java4
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java51
-rw-r--r--same/src/test/java/com/orbekk/paxos/MasterProposerTest.java12
-rw-r--r--same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java4
-rw-r--r--same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java32
6 files changed, 42 insertions, 75 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java
index 3a451d0..912de7e 100644
--- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java
+++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java
@@ -17,11 +17,11 @@ public class MasterProposer {
this.connections = connections;
}
- private boolean internalPropose(int roundId, int proposalNumber) {
+ private boolean internalPropose(int proposalNumber) {
int promises = 0;
for (String url : paxosUrls) {
PaxosService paxos = connections.getPaxos(url);
- boolean success = paxos.propose(myUrl, roundId, proposalNumber);
+ boolean success = paxos.propose(myUrl, proposalNumber);
if (success) {
promises += 1;
}
@@ -29,11 +29,11 @@ public class MasterProposer {
return promises > paxosUrls.size() / 2;
}
- private boolean internalAcceptRequest(int roundId, int proposalNumber) {
+ private boolean internalAcceptRequest(int proposalNumber) {
int accepts = 0;
for (String url : paxosUrls) {
PaxosService paxos = connections.getPaxos(url);
- boolean success = paxos.acceptRequest(myUrl, roundId, proposalNumber);
+ boolean success = paxos.acceptRequest(myUrl, proposalNumber);
if (success) {
accepts += 1;
}
@@ -41,11 +41,11 @@ public class MasterProposer {
return accepts > paxosUrls.size() / 2;
}
- public boolean propose(int roundId, int proposalNumber) {
+ public boolean propose(int proposalNumber) {
boolean success = false;
- success = internalPropose(roundId, proposalNumber);
+ success = internalPropose(proposalNumber);
if (success) {
- success = internalAcceptRequest(roundId, proposalNumber);
+ success = internalAcceptRequest(proposalNumber);
}
if (success) {
return true;
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java
index b650ed5..b515fa9 100644
--- a/same/src/main/java/com/orbekk/paxos/PaxosService.java
+++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java
@@ -1,6 +1,6 @@
package com.orbekk.paxos;
public interface PaxosService {
- boolean propose(String clientUrl, int roundId, int proposalNumber);
- boolean acceptRequest(String clientUrl, int roundId, int proposalNumber);
+ boolean propose(String clientUrl, int proposalNumber);
+ boolean 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 70e1618..1e8387e 100644
--- a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
+++ b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
@@ -8,8 +8,8 @@ import org.slf4j.LoggerFactory;
*/
public class PaxosServiceImpl implements PaxosService {
private Logger logger = LoggerFactory.getLogger(getClass());
- private int roundId = 0;
private int highestPromise = 0;
+ private int highestAcceptedValue = 0;
private String tag = "";
public PaxosServiceImpl(String tag) {
@@ -17,57 +17,36 @@ public class PaxosServiceImpl implements PaxosService {
}
@Override
- public synchronized boolean propose(String clientUrl, int roundId,
+ public synchronized boolean propose(String clientUrl,
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});
+ logger.info(tag + "propose({}, {}) = accepted",
+ new Object[]{clientUrl, proposalNumber});
return true;
} else {
- logger.info(tag + "propose({}, {}, {}) = rejected " +
+ logger.info(tag + "propose({}, {}) = rejected " +
"(promised: {})",
- new Object[]{clientUrl, roundId, proposalNumber,
+ new Object[]{clientUrl, proposalNumber,
highestPromise});
return false;
}
}
@Override
- public synchronized boolean acceptRequest(String clientUrl, int roundId,
+ public synchronized boolean acceptRequest(String clientUrl,
int proposalNumber) {
- if (roundId == this.roundId && proposalNumber == highestPromise) {
- logger.info(tag + "acceptRequest({}, {}, {}) = accepted",
- new Object[]{clientUrl, roundId, proposalNumber});
- finishRound();
+ if (proposalNumber == highestPromise) {
+ logger.info(tag + "acceptRequest({}, {}) = accepted",
+ new Object[]{clientUrl, proposalNumber});
+ highestAcceptedValue = proposalNumber;
return true;
} else {
- logger.info(tag + "acceptRequest({}, {}, {}) = rejected " +
- "(roundId={}, promise={})",
- new Object[]{clientUrl, roundId, proposalNumber,
- this.roundId, highestPromise});
+ logger.info(tag + "acceptRequest({}, {}) = rejected " +
+ "(promise={})",
+ new Object[]{clientUrl, proposalNumber,
+ highestPromise});
return false;
}
}
-
- private synchronized void finishRound() {
- newRound(roundId + 1);
- }
-
- private synchronized void newRound(int roundId) {
- logger.info(tag + "new round: {}", roundId);
- this.roundId = roundId;
- highestPromise = 0;
- }
}
diff --git a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
index 8056ab6..45ee53e 100644
--- a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
+++ b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
@@ -30,25 +30,25 @@ public class MasterProposerTest {
@Test public void successfulProposal() {
connections.paxosMap.put("p1", p1);
- when(p1.propose("client1", 1, 1)).thenReturn(true);
- when(p1.acceptRequest("client1", 1, 1)).thenReturn(true);
+ when(p1.propose("client1", 1)).thenReturn(true);
+ when(p1.acceptRequest("client1", 1)).thenReturn(true);
MasterProposer c1 = new MasterProposer(
"client1",
paxosUrls(),
connections);
- assertTrue(c1.propose(1, 1));
+ assertTrue(c1.propose(1));
}
@Test public void unsucessfulProposal() {
connections.paxosMap.put("p1", p1);
- when(p1.propose("client1", 1, 1)).thenReturn(true);
- when(p1.acceptRequest("client1", 1, 1)).thenReturn(false);
+ when(p1.propose("client1", 1)).thenReturn(true);
+ when(p1.acceptRequest("client1", 1)).thenReturn(false);
MasterProposer c1 = new MasterProposer(
"client1",
paxosUrls(),
connections);
- assertFalse(c1.propose(1, 1));
+ assertFalse(c1.propose(1));
}
}
diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java
index 757d27c..45d6624 100644
--- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java
+++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java
@@ -42,7 +42,7 @@ public class PaxosServiceFunctionalTest {
public void testMasterElection() {
MasterProposer m1 = new MasterProposer("http://client1", paxosUrls,
connections);
- assertTrue(m1.propose(1, 1));
+ assertTrue(m1.propose(1));
}
@Test
@@ -56,7 +56,7 @@ public class PaxosServiceFunctionalTest {
MasterProposer client =
new MasterProposer("http:/client" + j, paxosUrls,
connections);
- if (client.propose(1, 1)) {
+ if (client.propose(1)) {
incrementSuccessfulProposals();
}
}
diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java
index a6a1f0b..f9ee058 100644
--- a/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java
+++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java
@@ -37,35 +37,23 @@ public class PaxosServiceTest {
@Test
public void simpleCase() {
- assertTrue(p1.propose(client, 1, 1));
- assertTrue(p1.acceptRequest(client, 1, 1));
+ assertTrue(p1.propose(client, 1));
+ assertTrue(p1.acceptRequest(client, 1));
}
@Test
public void lowerProposalFails() {
- assertTrue(p1.propose(client1, 5, 10));
- assertFalse(p1.propose(client2, 3, 9));
- assertFalse(p1.propose(client2, 4, 100));
- assertFalse(p1.propose(client2, 5, 9));
- assertFalse(p1.propose(client2, 5, 10));
- assertTrue(p1.propose(client2, 5, 11));
+ assertTrue(p1.propose(client1, 10));
+ assertFalse(p1.propose(client2, 9));
+ assertTrue(p1.propose(client2, 100));
}
@Test
public void testAccept() {
- assertTrue(p1.propose(client1, 2, 3));
- assertTrue(p1.propose(client2, 2, 4));
- assertFalse(p1.acceptRequest(client1, 2, 3));
- assertTrue(p1.acceptRequest(client2, 2, 4));
- }
-
- @Test
- public void testRoundFinished() {
- assertTrue(p1.propose(client1, 4, 5));
- assertTrue(p1.acceptRequest(client1, 4, 5));
- assertFalse(p1.propose(client2, 4, 5));
- assertFalse(p1.acceptRequest(client2, 4, 5));
- assertTrue(p1.propose(client1, 5, 1));
+ assertTrue(p1.propose(client1, 3));
+ assertTrue(p1.propose(client2, 4));
+ assertFalse(p1.acceptRequest(client1, 3));
+ assertTrue(p1.acceptRequest(client2, 4));
}
public List<String> paxosUrls() {
@@ -76,6 +64,6 @@ public class PaxosServiceTest {
public void integrationTest() {
MasterProposer proposer = new MasterProposer("client1", paxosUrls(),
connections);
- assertTrue(proposer.propose(1, 1));
+ assertTrue(proposer.propose(1));
}
}