Merge branch 'reginfo'
diff --git a/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java b/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java
index 51f1d32..a68c187 100644
--- a/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java
+++ b/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java
@@ -3,6 +3,10 @@
import java.util.ArrayList;
import java.util.Collection;
+import org.restlet.Response;
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
@@ -27,8 +31,13 @@
if (controllers == null){
controllers = new ArrayList<String>();
+ Response response = getResponse();
+ response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
+ Representation error = new StringRepresentation("Null data returned. Zookeeper connection may be down");
+ response.setEntity(error);
+ return null;
}
-
+
return controllers;
}
diff --git a/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java b/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
index b666db7..acc94f9 100644
--- a/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
+++ b/src/main/java/net/onrc/onos/registry/controller/ZookeeperRegistry.java
@@ -36,6 +36,8 @@
import com.netflix.curator.framework.recipes.leader.LeaderLatchEvent;
import com.netflix.curator.framework.recipes.leader.LeaderLatchListener;
import com.netflix.curator.framework.recipes.leader.Participant;
+import com.netflix.curator.framework.state.ConnectionState;
+import com.netflix.curator.framework.state.ConnectionStateListener;
import com.netflix.curator.retry.ExponentialBackoffRetry;
import com.netflix.curator.utils.ZKPaths;
@@ -71,6 +73,17 @@
protected static final int sessionTimeout = 5000;
protected static final int connectionTimeout = 7000;
+ //We can cache the connection state here whenever we get a connection state event.
+ //Not 100% accurate but it's good enough to decide whether we can return data
+ //via the REST API or not.
+ protected volatile ConnectionState connectionState = ConnectionState.LOST;
+
+ protected ConnectionStateListener connectionStateListener = new ConnectionStateListener(){
+ @Override
+ public void stateChanged(CuratorFramework client, ConnectionState state) {
+ connectionState = state;
+ }
+ };
protected class SwitchLeaderListener implements LeaderLatchListener{
String dpid;
@@ -237,7 +250,23 @@
public Collection<String> getAllControllers() throws RegistryException {
log.debug("Getting all controllers");
+ //Rebuild the cache to make sure we have the latest data
+ //If we don't have ZK connection this will hang until we do. Undesirable.
+ //try {
+ //controllerCache.rebuild();
+ //} catch (Exception e1) {
+ //return null;
+ //}
+ ConnectionState currentConnectionState = connectionState;
+ //if (currentConnectionState != ConnectionState.CONNECTED &&
+ // currentConnectionState != ConnectionState.RECONNECTED){
+ if (currentConnectionState == ConnectionState.LOST ||
+ currentConnectionState == ConnectionState.SUSPENDED){
+ return null;
+ }
+
List<String> controllers = new ArrayList<String>();
+
for (ChildData data : controllerCache.getCurrentData()){
String d = null;
@@ -254,6 +283,7 @@
@Override
public void registerController(String id) throws RegistryException {
+ //TODO this isn't replaced if we lose Zookeeper connection
if (controllerId != null) {
throw new RegistryException(
"Controller already registered with id " + controllerId);
@@ -426,7 +456,7 @@
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
client = CuratorFrameworkFactory.newClient(this.connectionString,
sessionTimeout, connectionTimeout, retryPolicy);
-
+ client.getConnectionStateListenable().addListener(connectionStateListener);
client.start();
client = client.usingNamespace(namespace);