diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-22 19:15:24 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-22 19:15:24 +0100 |
commit | 37aa311231b4abd2f9d0a1d2ad6dd3da698862df (patch) | |
tree | aa793acd594392aca4f7a1202a0f0ef7b2f963da /same/src/main/java | |
parent | 39a99670d96d2d8fca324103d9c950dfea3c7214 (diff) |
Unit test of Paxos Service.
Diffstat (limited to 'same/src/main/java')
-rw-r--r-- | same/src/main/java/com/orbekk/paxos/PaxosService.java | 2 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java | 18 |
2 files changed, 15 insertions, 5 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java index 3398e81..b650ed5 100644 --- a/same/src/main/java/com/orbekk/paxos/PaxosService.java +++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java @@ -1,4 +1,4 @@ -pakage com.orbekk.paxos; +package com.orbekk.paxos; public interface PaxosService { boolean propose(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 index a39abbe..50bd88b 100644 --- a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java +++ b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java @@ -9,8 +9,13 @@ public class PaxosServiceImpl implements PaxosService { private int highestPromise = 0; private String tag = ""; + public PaxosServiceImpl(String tag) { + this.tag = tag; + } + @Override - public boolean propose(String clientUrl, int roundId, int proposalNumber) { + public synchronized boolean propose(String clientUrl, int roundId, + int proposalNumber) { if (roundId > this.roundId) { newRound(roundId); } @@ -25,7 +30,7 @@ public class PaxosServiceImpl implements PaxosService { if (proposalNumber > highestPromise) { highestPromise = proposalNumber; logger.info(tag + "propose({}, {}, {}) = accepted", - new Object[]{clientUrl, roundId, proposalNumber); + new Object[]{clientUrl, roundId, proposalNumber}); return true; } else { logger.info(tag + "propose({}, {}, {}) = rejected " + @@ -37,11 +42,12 @@ public class PaxosServiceImpl implements PaxosService { } @Override - public boolean acceptRequest(String clientUrl, int roundId, + public synchronized boolean acceptRequest(String clientUrl, int roundId, int proposalNumber) { if (roundId == this.roundId && proposalNumber == highestPromise) { logger.info(tag + "acceptRequest({}, {}, {}) = accepted", new Object[]{clientUrl, roundId, proposalNumber}); + finishRound(); return true; } else { logger.info(tag + "acceptRequest({}, {}, {}) = rejected " + @@ -52,7 +58,11 @@ public class PaxosServiceImpl implements PaxosService { } } - private void newRound(int roundId) { + private synchronized void finishRound() { + newRound(roundId + 1); + } + + private synchronized void newRound(int roundId) { logger.info(tag + "new round: {}", roundId); this.roundId = roundId; highestPromise = 0; |