Added ConnectionStateListener to cache connection state and return an error to the REST API when there's no connection
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 5da2e7c..a68c187 100644
--- a/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java
+++ b/src/main/java/net/onrc/onos/registry/controller/ControllerRegistryResource.java
@@ -32,7 +32,7 @@
if (controllers == null){
controllers = new ArrayList<String>();
Response response = getResponse();
- response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
+ response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
Representation error = new StringRepresentation("Null data returned. Zookeeper connection may be down");
response.setEntity(error);
return null;
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 58b66f2..eb2afb3 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;
@@ -239,9 +252,16 @@
//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) {
+ //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;
}
@@ -435,7 +455,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);