summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--same/src/main/java/com/orbekk/same/Client.java32
-rw-r--r--same/src/main/java/com/orbekk/same/ClientService.java3
-rw-r--r--same/src/test/java/com/orbekk/same/MasterTest.java4
3 files changed, 34 insertions, 5 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java
index 7079016..fc4a6af 100644
--- a/same/src/main/java/com/orbekk/same/Client.java
+++ b/same/src/main/java/com/orbekk/same/Client.java
@@ -123,6 +123,13 @@ public class Client implements DiscoveryListener {
Client.this.masterUrl = masterUrl;
connectionState = ConnectionState.STABLE;
}
+
+ @Override
+ public void masterDown() throws Exception {
+ logger.info("Master is down.");
+ connectionState = ConnectionState.UNSTABLE;
+ tryBecomeMaster();
+ }
};
private WorkQueue<String> discoveryThread = new WorkQueue<String>() {
@@ -240,15 +247,30 @@ public class Client implements DiscoveryListener {
return paxosUrls;
}
- public void startMasterElection() {
- if (masterController == null) {
- logger.warn("Could not become master: No master controller.");
- return;
- }
+ private void tryBecomeMaster() {
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);
}
+
+ public void startMasterElection() {
+ List<String> participants = state.getList(".participants");
+ broadcaster.broadcast(participants, new ServiceOperation() {
+ @Override public void run(String url) {
+ ClientService client = connections.getClient(url);
+ try {
+ client.masterDown();
+ } catch (Exception e) {
+ logger.info("{}.masterDown() did not respond (ignored): " +
+ url, e);
+ }
+ }
+ });
+ }
}
diff --git a/same/src/main/java/com/orbekk/same/ClientService.java b/same/src/main/java/com/orbekk/same/ClientService.java
index 453dfef..dfca2cc 100644
--- a/same/src/main/java/com/orbekk/same/ClientService.java
+++ b/same/src/main/java/com/orbekk/same/ClientService.java
@@ -16,4 +16,7 @@ public interface ClientService {
*/
void masterTakeover(String masterUrl, String networkName,
int masterId) throws Exception;
+
+ /** The master is down, so start a new master election. */
+ void masterDown() throws Exception;
}
diff --git a/same/src/test/java/com/orbekk/same/MasterTest.java b/same/src/test/java/com/orbekk/same/MasterTest.java
index fb5a45c..927b504 100644
--- a/same/src/test/java/com/orbekk/same/MasterTest.java
+++ b/same/src/test/java/com/orbekk/same/MasterTest.java
@@ -38,6 +38,10 @@ public class MasterTest {
public void masterTakeover(String masterUrl, String networkName, int masterId)
throws Exception {
}
+
+ @Override
+ public void masterDown() throws Exception {
+ }
}
@Before