Permalink
Browse files

Bug fix: Handle unclaimed coordinate in method getEndpoints() without…

… throwing exception.
  • Loading branch information...
1 parent ed03c3a commit 4c4910c7b6b51e5d6407a43ac8879bd64e60d255 @dybdahl dybdahl committed May 4, 2012
View
15 cn/src/main/java/org/cloudname/zk/ZkResolver.java
@@ -342,9 +342,18 @@ public void removeResolverListener(ResolverListener listener) {
for (String instance : instances) {
String statusPath = ZkCoordinatePath.getStatusPath(
cell, user, service, Integer.parseInt(instance));
-
- ZkCoordinateData zkCoordinateData = ZkCoordinateData.loadCoordinateData(
- statusPath, getZooKeeper(), null);
+
+ ZkCoordinateData zkCoordinateData = null;
+ try {
+ zkCoordinateData = ZkCoordinateData.loadCoordinateData(
+ statusPath, getZooKeeper(), null);
+ } catch (CloudnameException e) {
+ // This is ok, an unclaimed node will not have status data, we ignore it even
+ // though there might also be other exception (this should be rare).
+ // The advantage is that we don't need to check if the node exists and hence
+ // reduce the load on zookeeper.
+ continue;
+ }
Set<Endpoint> endpoints = zkCoordinateData.snapshot().getEndpoints();
for (Endpoint endpoint : endpoints) {
if (filter.includeEndpointname(endpoint.getName())) {
View
22 cn/src/test/java/org/cloudname/zk/ZkResolverTest.java
@@ -7,8 +7,10 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.cloudname.*;
import org.cloudname.testtools.Net;
@@ -160,6 +162,26 @@ public void testGetCoordinateDataAll() throws Exception {
}
/**
+ * Test an unclaimed coordinate and a path that is not complete.
+ * @throws Exception
+ */
+ @Test
+ public void testGetCoordinateDataAllNoClaimedCoordinate() throws Exception {
+ // Create unclaimned coordinate.
+ Coordinate coordinateNoStatus = Coordinate.parse("4.service.user.cell");
+ cn.createCoordinate(coordinateNoStatus);
+
+ // Throw in a incomplete path.
+ zk.create("/cn/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+
+ Resolver resolver = cn.getResolver();
+
+ Resolver.CoordinateDataFilter filter = new Resolver.CoordinateDataFilter();
+ Set<Endpoint> endpoints = resolver.getEndpoints(filter);
+ assertEquals(4, endpoints.size());
+ }
+
+ /**
* Tests that all methods of the filters are called and some basic filtering are functional.
*/
@Test

0 comments on commit 4c4910c

Please sign in to comment.