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/main/java/com | |
parent | 148d196ef154c420c450f8dcaa5b7d506115421a (diff) |
Client performs master election.
Diffstat (limited to 'same/src/main/java/com')
-rw-r--r-- | same/src/main/java/com/orbekk/same/Client.java | 25 |
1 files changed, 23 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() { |