From 55beb6df753604996ae61f8a099db40f0906804e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Tue, 24 Jan 2012 11:48:19 +0100 Subject: Basic Paxos working. --- .../main/java/com/orbekk/paxos/MasterProposer.java | 24 ++++++++++++++++++++-- .../java/com/orbekk/paxos/MasterProposerTest.java | 4 ++-- .../orbekk/paxos/PaxosServiceFunctionalTest.java | 6 +++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java index 7bc4350..f216ff5 100644 --- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java +++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java @@ -1,11 +1,13 @@ package com.orbekk.paxos; +import com.orbekk.same.ConnectionManager; import java.util.ArrayList; import java.util.List; - -import com.orbekk.same.ConnectionManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class MasterProposer { + private Logger logger = LoggerFactory.getLogger(getClass()); private String myUrl; private List paxosUrls = new ArrayList(); private ConnectionManager connections; @@ -64,4 +66,22 @@ public class MasterProposer { return false; } } + + public boolean proposeRetry(int proposalNumber) { + int nextProposal = proposalNumber; + int result = 0; + + while (result != nextProposal) { + result = internalPropose(nextProposal); + if (result == nextProposal) { + result = internalAcceptRequest(nextProposal); + } + logger.info("Proposed value {}, result {}", nextProposal, result); + if (result < 0) { + nextProposal = -result + 1; + } + } + + return true; + } } diff --git a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java index dfc0b19..601a357 100644 --- a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java +++ b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java @@ -42,8 +42,8 @@ public class MasterProposerTest { @Test public void unsucessfulProposal() { connections.paxosMap.put("p1", p1); - when(p1.propose("client1", 1)).thenReturn(1); - when(p1.acceptRequest("client1", 1)).thenReturn(1); + when(p1.propose("client1", 1)).thenReturn(-1); + when(p1.acceptRequest("client1", 1)).thenReturn(-1); MasterProposer c1 = new MasterProposer( "client1", diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java index 45d6624..4fe3e27 100644 --- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java +++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java @@ -25,7 +25,7 @@ public class PaxosServiceFunctionalTest { public void setUp() throws Exception { server = TestServer.create(handler); myUrl = "http://localhost:" + server.port; - setupPaxos(5); + setupPaxos(10); } public void setupPaxos(int instances) { @@ -56,7 +56,7 @@ public class PaxosServiceFunctionalTest { MasterProposer client = new MasterProposer("http:/client" + j, paxosUrls, connections); - if (client.propose(1)) { + if (client.proposeRetry(1)) { incrementSuccessfulProposals(); } } @@ -72,7 +72,7 @@ public class PaxosServiceFunctionalTest { // Ignore. } } - assertEquals(1, successfulProposals); + assertEquals(5, successfulProposals); } public synchronized void incrementSuccessfulProposals() { -- cgit v1.2.3