curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [7/9] git commit: Fix startup connection problem.
Date Fri, 08 Aug 2014 02:09:14 GMT
Fix startup connection problem.


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/462121f9
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/462121f9
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/462121f9

Branch: refs/heads/master
Commit: 462121f98a4129f9b96ff0a5ebeafe4eac224a38
Parents: be85e96
Author: Scott Blum <scottb@squareup.com>
Authored: Thu Aug 7 15:25:21 2014 -0400
Committer: Scott Blum <scottb@squareup.com>
Committed: Thu Aug 7 19:26:51 2014 -0400

----------------------------------------------------------------------
 .../framework/recipes/cache/TreeCache.java      | 22 ++++++++++++---
 .../recipes/cache/BaseTestTreeCache.java        |  4 +++
 .../framework/recipes/cache/TestTreeCache.java  | 28 ++++++++++++++++++++
 3 files changed, 51 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/462121f9/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
index 1ec0440..617d652 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/cache/TreeCache.java
@@ -304,8 +304,11 @@ public class TreeCache implements Closeable
                 }
                 break;
             default:
-                handleException(new Exception(String.format("Unknown event %s", event)));
-                break;
+                // An unknown event, probably an error of some sort like connection loss.
+                LOG.info(String.format("Unknown event %s", event));
+                // Don't produce an initialized event on error; reconnect can fix this.
+                outstandingOps.decrementAndGet();
+                return;
             }
 
             if ( outstandingOps.decrementAndGet() == 0 )
@@ -424,7 +427,10 @@ public class TreeCache implements Closeable
     {
         Preconditions.checkState(treeState.compareAndSet(TreeState.LATENT, TreeState.STARTED),
"already started");
         client.getConnectionStateListenable().addListener(connectionStateListener);
-        root.wasCreated();
+        if (client.getZookeeperClient().isConnected())
+        {
+            root.wasCreated();
+        }
     }
 
     /**
@@ -595,6 +601,16 @@ public class TreeCache implements Closeable
             break;
 
         case CONNECTED:
+            try
+            {
+                root.wasCreated();
+            }
+            catch ( Exception e )
+            {
+                handleException(e);
+            }
+            break;
+
         case RECONNECTED:
             try
             {

http://git-wip-us.apache.org/repos/asf/curator/blob/462121f9/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
index 5842c21..1daf14d 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/BaseTestTreeCache.java
@@ -85,6 +85,10 @@ public class BaseTestTreeCache extends BaseClassForTests
             }
         };
 
+        initCuratorFramework();
+    }
+
+    void initCuratorFramework() {
         client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
timing.connection(), new RetryOneTime(1));
         client.start();
         client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener()

http://git-wip-us.apache.org/repos/asf/curator/blob/462121f9/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
index 33d08b0..abdbb8b 100644
--- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
+++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestTreeCache.java
@@ -342,4 +342,32 @@ public class TestTreeCache extends BaseTestTreeCache
         client.delete().forPath("/test/one");
         assertNoMoreEvents();
     }
+
+    /**
+     * Make sure TreeCache gets to a sane state when we can't initially connect to server.
+     */
+    @Test
+    public void testServerNotStartedYet() throws Exception
+    {
+        // Stop the existing server.
+        server.stop();
+
+        // Shutdown the existing client and re-create it started.
+        client.close();
+        initCuratorFramework();
+
+        // Start the client disconnected.
+        cache = new MyTreeCache(client, "/test", true);
+        cache.start();
+        assertNoMoreEvents();
+
+        // Now restart the server.
+        server.restart();
+        assertEvent(TreeCacheEvent.Type.INITIALIZED);
+
+        client.create().forPath("/test");
+
+        assertEvent(TreeCacheEvent.Type.NODE_ADDED, "/test");
+        assertNoMoreEvents();
+    }
 }


Mime
View raw message