From 1d2eca2591c5747c65d52cb96f5246632f06d9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Mon, 6 Feb 2012 15:05:45 +0100 Subject: Fix RpcServlet: Use POST requests. --- .../java/com/orbekk/net/MyJsonRpcHttpClient.java | 5 ++++- .../main/java/com/orbekk/paxos/MasterProposer.java | 21 +++++++++++++++++---- .../main/java/com/orbekk/paxos/PaxosService.java | 4 ++-- .../main/java/com/orbekk/same/http/RpcServlet.java | 2 +- .../java/com/orbekk/paxos/MasterProposerTest.java | 4 ++-- .../orbekk/paxos/PaxosServiceFunctionalTest.java | 18 ++++++++++++++---- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java b/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java index 98de054..237198e 100644 --- a/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java +++ b/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java @@ -17,6 +17,8 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.codehaus.jackson.map.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.googlecode.jsonrpc4j.JsonRpcClient; import com.googlecode.jsonrpc4j.JsonRpcHttpClient; @@ -26,7 +28,8 @@ import com.googlecode.jsonrpc4j.JsonRpcHttpClient; * * We extend JsonRpcHttpClient but try to override everything it does. */ -public class MyJsonRpcHttpClient extends JsonRpcHttpClient { +public class MyJsonRpcHttpClient extends JsonRpcHttpClient { + Logger logger = LoggerFactory.getLogger(getClass()); private URL serviceUrl; private JsonRpcClient rpcClient; private HttpClient httpClient; diff --git a/same/src/main/java/com/orbekk/paxos/MasterProposer.java b/same/src/main/java/com/orbekk/paxos/MasterProposer.java index f216ff5..effe6df 100644 --- a/same/src/main/java/com/orbekk/paxos/MasterProposer.java +++ b/same/src/main/java/com/orbekk/paxos/MasterProposer.java @@ -1,6 +1,7 @@ package com.orbekk.paxos; import com.orbekk.same.ConnectionManager; +import static com.orbekk.same.StackTraceUtil.throwableToString; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; @@ -20,11 +21,17 @@ public class MasterProposer { } private int internalPropose(int proposalNumber) { - int bestPromise = proposalNumber; + int bestPromise = -proposalNumber; int promises = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); - int result = paxos.propose(myUrl, proposalNumber); + int result = 0; + try { + result = paxos.propose(myUrl, proposalNumber); + } catch (Exception e) { + logger.warn("Exception from {}: {}", url, + throwableToString(e)); + } if (result == proposalNumber) { promises += 1; } @@ -38,11 +45,17 @@ public class MasterProposer { } private int internalAcceptRequest(int proposalNumber) { - int bestAccepted = proposalNumber; + int bestAccepted = -proposalNumber; int accepts = 0; for (String url : paxosUrls) { PaxosService paxos = connections.getPaxos(url); - int result = paxos.acceptRequest(myUrl, proposalNumber); + int result = 0; + try { + result = paxos.acceptRequest(myUrl, proposalNumber); + } catch (Exception e) { + logger.warn("Exception from {}: {}", url, + throwableToString(e)); + } if (result == proposalNumber) { accepts += 1; } diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java index a92a794..8de02da 100644 --- a/same/src/main/java/com/orbekk/paxos/PaxosService.java +++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java @@ -7,6 +7,6 @@ public interface PaxosService { * -M if another promise already was made, where M is the promise * highest proposal number. */ - int propose(String clientUrl, int proposalNumber); - int acceptRequest(String clientUrl, int proposalNumber); + int propose(String clientUrl, int proposalNumber) throws Exception; + int acceptRequest(String clientUrl, int proposalNumber) throws Exception; } diff --git a/same/src/main/java/com/orbekk/same/http/RpcServlet.java b/same/src/main/java/com/orbekk/same/http/RpcServlet.java index 6d73759..9b4f82e 100644 --- a/same/src/main/java/com/orbekk/same/http/RpcServlet.java +++ b/same/src/main/java/com/orbekk/same/http/RpcServlet.java @@ -17,7 +17,7 @@ public class RpcServlet extends HttpServlet { } @Override - protected void doGet(HttpServletRequest request, + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { rpcServer.handle(request, response); } diff --git a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java index 601a357..bab2005 100644 --- a/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java +++ b/same/src/test/java/com/orbekk/paxos/MasterProposerTest.java @@ -28,7 +28,7 @@ public class MasterProposerTest { return urls; } - @Test public void successfulProposal() { + @Test public void successfulProposal() throws Exception { connections.paxosMap.put("p1", p1); when(p1.propose("client1", 1)).thenReturn(1); when(p1.acceptRequest("client1", 1)).thenReturn(1); @@ -40,7 +40,7 @@ public class MasterProposerTest { assertTrue(c1.propose(1)); } - @Test public void unsucessfulProposal() { + @Test public void unsucessfulProposal() throws Exception { connections.paxosMap.put("p1", p1); when(p1.propose("client1", 1)).thenReturn(-1); when(p1.acceptRequest("client1", 1)).thenReturn(-1); diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java index 5042168..278f775 100644 --- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java +++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java @@ -20,7 +20,6 @@ import org.junit.Test; public class PaxosServiceFunctionalTest { ConnectionManagerImpl connections = new ConnectionManagerImpl(500, 500); List paxosUrls = new ArrayList(); - // RpcHandler handler = new RpcHandler(null); ServerContainer server; String myUrl; int successfulProposals = 0; @@ -28,9 +27,12 @@ public class PaxosServiceFunctionalTest { @Before public void setUp() throws Exception { ServerBuilder builder = new ServerBuilder(0); - setupPaxos(builder, 10); + List tempUrls = setupPaxos(builder, 10); server = builder.build(); + server.start(); myUrl = "http://localhost:" + server.getPort(); + addUrls(tempUrls); + System.out.println(paxosUrls); } @After @@ -38,13 +40,21 @@ public class PaxosServiceFunctionalTest { server.stop(); } - public void setupPaxos(ServerBuilder builder, int instances) { + public List setupPaxos(ServerBuilder builder, int instances) { + List tempUrls = new ArrayList(); for (int i = 1; i <= instances; i++) { JsonRpcServer jsonServer = new JsonRpcServer( new PaxosServiceImpl("P" + i + ": "), PaxosService.class); String serviceId = "/PaxosService" + i + ".json"; builder.withServlet(new RpcServlet(jsonServer), serviceId); - paxosUrls.add(myUrl + serviceId); + tempUrls.add(serviceId); + } + return tempUrls; + } + + public void addUrls(List services) { + for (String url : services) { + paxosUrls.add(myUrl + url); } } -- cgit v1.2.3