summaryrefslogtreecommitdiff
path: root/same
diff options
context:
space:
mode:
Diffstat (limited to 'same')
-rw-r--r--same/src/main/java/com/orbekk/paxos/MasterProposer.java43
-rw-r--r--same/src/test/java/com/orbekk/paxos/MasterProposerTest.java36
2 files changed, 27 insertions, 52 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java
index c33cf68..e7c6e8a 100644
--- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java
+++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java
@@ -5,39 +5,19 @@ import java.util.List;
import com.orbekk.same.ConnectionManager;
-public class MasterProposer implements Runnable {
+public class MasterProposer {
private String myUrl;
- private int roundId = 0;
- private int proposalNumber = 0;
private List<String> paxosUrls = new ArrayList<String>();
- private Runnable roundFailedAction;
- private Runnable masterAction;
private ConnectionManager connections;
- public static Runnable getTimeoutAction(final long milliseconds) {
- return new Runnable() {
- @Override public void run() {
- try {
- Thread.sleep(milliseconds);
- } catch (InterruptedException e) {
- // Ignore interrupts.
- }
- }
- };
- }
-
- MasterProposer(String clientUrl, List<String> paxosUrls, int roundId,
- ConnectionManager connections, Runnable roundFailedAction,
- Runnable masterAction) {
+ MasterProposer(String clientUrl, List<String> paxosUrls,
+ ConnectionManager connections) {
this.myUrl = clientUrl;
this.paxosUrls = paxosUrls;
- this.roundId = roundId;
this.connections = connections;
- this.roundFailedAction = roundFailedAction;
- this.masterAction = masterAction;
}
- private boolean propose(int roundId, int proposalNumber) {
+ private boolean internalPropose(int roundId, int proposalNumber) {
int promises = 0;
for (String url : paxosUrls) {
PaxosService paxos = connections.getPaxos(url);
@@ -49,7 +29,7 @@ public class MasterProposer implements Runnable {
return promises > paxosUrls.size() / 2;
}
- private boolean acceptRequest(int roundId, int proposalNumber) {
+ private boolean internalAcceptRequest(int roundId, int proposalNumber) {
int accepts = 0;
for (String url : paxosUrls) {
PaxosService paxos = connections.getPaxos(url);
@@ -60,18 +40,17 @@ public class MasterProposer implements Runnable {
}
return accepts > paxosUrls.size() / 2;
}
-
- @Override public void run() {
+
+ public boolean propose(int roundId, int proposalNumber) {
boolean success = false;
- success = propose(roundId + 1, proposalNumber + 1);
+ success = internalPropose(roundId, proposalNumber);
if (success) {
- success = acceptRequest(roundId + 1, proposalNumber + 1);
+ success = internalAcceptRequest(roundId, proposalNumber);
}
if (success) {
- masterAction.run();
+ return true;
} else {
- roundFailedAction.run();
- // TODO: Next round?
+ return false;
}
}
}
diff --git a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
index 74c817f..8056ab6 100644
--- a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
+++ b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java
@@ -8,7 +8,7 @@ import org.junit.Test;
import com.orbekk.same.TestConnectionManager;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class MasterProposerTest {
@@ -18,18 +18,6 @@ public class MasterProposerTest {
PaxosService p3 = mock(PaxosService.class);
PaxosService p4 = mock(PaxosService.class);
PaxosService p5 = mock(PaxosService.class);
- String master = null;
-
- private class TestMasterAction implements Runnable {
- String tag;
- TestMasterAction(String tag) {
- this.tag = tag;
- }
-
- @Override public void run() {
- master = tag;
- }
- }
@Before public void setUp() {
}
@@ -39,7 +27,7 @@ public class MasterProposerTest {
urls.addAll(connections.paxosMap.keySet());
return urls;
}
-
+
@Test public void successfulProposal() {
connections.paxosMap.put("p1", p1);
when(p1.propose("client1", 1, 1)).thenReturn(true);
@@ -48,11 +36,19 @@ public class MasterProposerTest {
MasterProposer c1 = new MasterProposer(
"client1",
paxosUrls(),
- 0,
- connections,
- MasterProposer.getTimeoutAction(0),
- new TestMasterAction("c1"));
- c1.run();
- assertEquals("c1", master);
+ connections);
+ assertTrue(c1.propose(1, 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);
+
+ MasterProposer c1 = new MasterProposer(
+ "client1",
+ paxosUrls(),
+ connections);
+ assertFalse(c1.propose(1, 1));
}
}