diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-20 10:34:25 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-20 10:34:25 +0100 |
commit | f238785c99d2446e75761711b55c39dba79f1f45 (patch) | |
tree | 4e5d820c559bb478f4508873ca86f73e0c71d9d6 /same/src | |
parent | 148d196ef154c420c450f8dcaa5b7d506115421a (diff) |
Client performs master election.
Diffstat (limited to 'same/src')
-rw-r--r-- | same/src/main/java/com/orbekk/same/Client.java | 25 | ||||
-rw-r--r-- | same/src/test/java/com/orbekk/same/FunctionalTest.java | 6 |
2 files changed, 29 insertions, 2 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java index fc4a6af..69d0e33 100644 --- a/same/src/main/java/com/orbekk/same/Client.java +++ b/same/src/main/java/com/orbekk/same/Client.java @@ -4,6 +4,9 @@ import static com.orbekk.same.StackTraceUtil.throwableToString; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,6 +18,7 @@ import com.orbekk.util.DelayedOperation; import com.orbekk.util.WorkQueue; public class Client implements DiscoveryListener { + public static final long MASTER_TAKEOVER_TIMEOUT = 4000l; private Logger logger = LoggerFactory.getLogger(getClass()); /** TODO: Not really useful yet. Remove? */ private ConnectionState connectionState = ConnectionState.DISCONNECTED; @@ -251,12 +255,29 @@ public class Client implements DiscoveryListener { List<String> paxosUrls = getPaxosUrls(); MasterProposer proposer = new MasterProposer(getUrl(), paxosUrls, connections); - // TODO: Run election. if (masterController == null) { logger.warn("Could not become master: No master controller."); return; } - masterController.enableMaster(state); + Runnable sleeperTask = new Runnable() { + @Override public synchronized void run() { + try { + wait(MASTER_TAKEOVER_TIMEOUT); + } catch (InterruptedException e) { + } + } + }; + Future<Integer> proposal = proposer.startProposalTask(1, sleeperTask); + Integer result = null; + try { + result = proposal.get(); + } catch (InterruptedException e) { + } catch (ExecutionException e) { + } catch (CancellationException e) { + } + if (!proposal.isCancelled() && result != null) { + masterController.enableMaster(state); + } } public void startMasterElection() { diff --git a/same/src/test/java/com/orbekk/same/FunctionalTest.java b/same/src/test/java/com/orbekk/same/FunctionalTest.java index 102ae14..9380409 100644 --- a/same/src/test/java/com/orbekk/same/FunctionalTest.java +++ b/same/src/test/java/com/orbekk/same/FunctionalTest.java @@ -12,6 +12,9 @@ import java.util.List; import org.junit.Before; import org.junit.Test; +import com.orbekk.paxos.PaxosService; +import com.orbekk.paxos.PaxosServiceImpl; + /** A functional test that runs with a master and several clients. */ public class FunctionalTest { Master master; @@ -44,6 +47,9 @@ public class FunctionalTest { clientUrl, broadcaster); connections.clientMap.put(clientUrl, client.getService()); clients.add(client); + String paxosUrl = clientUrl.replace("ClientService", "PaxosService"); + PaxosService paxos = new PaxosServiceImpl(paxosUrl); + connections.paxosMap.put(paxosUrl, paxos); return client; } |