You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by timothyjward <gi...@git.apache.org> on 2018/05/23 08:19:13 UTC
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
GitHub user timothyjward opened a pull request:
https://github.com/apache/zookeeper/pull/529
[ZOOKEEPER-2368] Send a watch event is when a client is closed
Currently, if the client is closed (rather than being remotely disconnected) there is no notification to the watcher. This means that asynchronous clients can end up waiting indefinitely for events that will never come. Watchers need to be aware that the client is closed for good.
Signed-off-by: Tim Ward <ti...@apache.org>
Note that this is a variation on a patch I produced some time ago, which was broadly accepted as a good idea, and didn't cause any problems for Curator, but was deemed by some to be too risky because it reused an existing KeeperState. This patch is therefore updated to use a new `Closed` KeeperState. Fixing this would allow me to avoid maintaining a separate fork of Zookeeper just to support this one feature!
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/timothyjward/zookeeper ZOOKEEPER-2368
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/zookeeper/pull/529.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #529
----
commit 3b76aeaefac2711ba3f9b85c69d6049f0da6ab3f
Author: Tim Ward <ti...@...>
Date: 2015-12-02T17:34:22Z
[ZOOKEEPER-2368] Send a watch event is when a client is closed
Currently, if the client is closed (rather than being remotely disconnected) there is no notification to the watcher. This means that asynchronous clients can end up waiting indefinitely for events that will never come. Watchers need to be aware that the client is closed for good.
Signed-off-by: Tim Ward <ti...@apache.org>
----
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
> Perhaps you could try to re-run the build? You can do this easily with git commit --amend
Done
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
@nkalmar is there anything more I need to do with this before it can be merged?
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
The run went through fine once the Apache infrastructure became less busy!
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/zookeeper/pull/529
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
* All tabs on changed lines are now spaces
* Reordered imports have been moved back
I rebased and squashed the changes so that it would be as easy as possible to merge this PR
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by enixon <gi...@git.apache.org>.
Github user enixon commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r195949390
--- Diff: src/java/main/org/apache/zookeeper/ClientCnxn.java ---
@@ -1260,10 +1260,8 @@ public void run() {
cleanup();
}
clientCnxnSocket.close();
- if (state.isAlive()) {
- eventThread.queueEvent(new WatchedEvent(Event.EventType.None,
- Event.KeeperState.Disconnected, null));
- }
+ eventThread.queueEvent(new WatchedEvent(Event.EventType.None,
+ Event.KeeperState.Closed, null));
--- End diff --
I'd expect the client to always receive a Disconnected state before receiving a Closed state and it looks like that constraint is not enforced.
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by enixon <gi...@git.apache.org>.
Github user enixon commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r195949397
--- Diff: src/java/main/org/apache/zookeeper/ClientCnxn.java ---
@@ -1438,6 +1436,11 @@ public void disconnect() {
}
sendThread.close();
+ try {
+ sendThread.join();
+ } catch (InterruptedException ex) {
+ LOG.warn("Got interrupted while waiting for the sender thread to close", ex);
+ }
eventThread.queueEventOfDeath();
--- End diff --
Any gains/problems with moving the queueEventOfDeath above the join()?
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196731884
--- Diff: src/java/test/org/apache/zookeeper/test/WatcherTest.java ---
@@ -140,6 +142,55 @@ public void processResult(int rc, String path, Object ctx) {
}
}
}
+
+ @Test
+ public void testWatcherDisconnectOnClose()
+ throws IOException, InterruptedException, KeeperException
+ {
+ ZooKeeper zk = null;
+ try {
+ final BlockingQueue<WatchedEvent> queue = new LinkedBlockingQueue<>();
+
+ MyWatcher connWatcher = new MyWatcher();
+
+ Watcher watcher = new Watcher(){
+ @Override
+ public void process(WatchedEvent event) {
+ try {
+ queue.put(event);
+ } catch (InterruptedException e) {
+ // Oh well, never mind
+ }
+ }
+
+ };
+
+ zk = createClient(connWatcher, hostPort);
+
+ StatCallback scb = new StatCallback() {
+ public void processResult(int rc, String path, Object ctx,
+ Stat stat) {
+ // don't do anything
+ }
+ };
+
+ // Register a watch on the node
+ zk.exists("/missing", watcher, scb, null);
+
+ // Close the client without changing the node
+ zk.close();
+
+
+ WatchedEvent event = queue.poll(10, TimeUnit.SECONDS);
+ Assert.assertEquals(Event.EventType.None, event.getType());
--- End diff --
I've pushed updated assertions containing more human readable text.
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by anmolnar <gi...@git.apache.org>.
Github user anmolnar commented on the issue:
https://github.com/apache/zookeeper/pull/529
Committed to `master` and `3.5` branches. Thanks Tim!
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
Retriggered. Overall +1 from Jenkins now.
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by nkalmar <gi...@git.apache.org>.
Github user nkalmar commented on the issue:
https://github.com/apache/zookeeper/pull/529
A commiter needs to review your change and merge it.
Of course anyone can review the pull requests - I did have a look on your PR, looks good to me, but I couldn't review it in detail yet.
But only commiters will be able to merge it. You can try pinging the Apache ZooKeeper Dev list about this PR.
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by enixon <gi...@git.apache.org>.
Github user enixon commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r195949453
--- Diff: src/java/main/org/apache/zookeeper/Watcher.java ---
@@ -84,7 +84,14 @@
* client connection (the session) is no longer valid. You must
* create a new client connection (instantiate a new ZooKeeper
* instance) if you with to access the ensemble. */
- Expired (-112);
+ Expired (-112),
+
+ /**
+ * The client has been closed. This state is never generated by
+ * the server, but is generated locally when a client calls
+ * {@link ZooKeeper#close()} or {@link ZooKeeper#close(int)}
+ */
+ Closed (-2);
--- End diff --
Nit - make the enum value 7?
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by nkalmar <gi...@git.apache.org>.
Github user nkalmar commented on the issue:
https://github.com/apache/zookeeper/pull/529
All I can see is that there was an error in "Adding comment to Jira." step. But I don't think this should be a reason for the check to stuck in pending.
Perhaps you could try to re-run the build? You can do this easily with git commit --amend
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by enixon <gi...@git.apache.org>.
Github user enixon commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196256868
--- Diff: src/java/main/org/apache/zookeeper/ClientCnxn.java ---
@@ -1438,6 +1436,11 @@ public void disconnect() {
}
sendThread.close();
+ try {
+ sendThread.join();
+ } catch (InterruptedException ex) {
+ LOG.warn("Got interrupted while waiting for the sender thread to close", ex);
+ }
eventThread.queueEventOfDeath();
--- End diff --
Sounds good to me - thanks for running the experiment!
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196090467
--- Diff: src/java/main/org/apache/zookeeper/ClientCnxn.java ---
@@ -1260,10 +1260,8 @@ public void run() {
cleanup();
}
clientCnxnSocket.close();
- if (state.isAlive()) {
- eventThread.queueEvent(new WatchedEvent(Event.EventType.None,
- Event.KeeperState.Disconnected, null));
- }
+ eventThread.queueEvent(new WatchedEvent(Event.EventType.None,
+ Event.KeeperState.Closed, null));
--- End diff --
Good catch - in the previous version of this patch the terminal event re-used the disconnected state for closing (hence removing the if check. Now that we have a Closed event the Disconnected event should still be sent if the client is connected at the time of the close.
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
> * why have you created the patch against branch-3.5? jira is targeted to trunk too, don't you want to merge it to that branch?
I work against the 3.5 release branch, which is how I maintain my fork. I created the PR against 3.5 because that's the commit I already had, and the 3.5.x stream is the version I would like to see fixed. I assumed the fix would then be cherry-picked onto trunk (which is what seems to happen with the other PRs).
There's no way that I know of to change the target branch of a PR after it has been created, so unless you need me to close this PR and create a new one I'll continue targeting the 3.5 branch.
> * please fix indentation in both files
Will do - I'll squash the commits too to avoid the risk of it ending up in history
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196119877
--- Diff: src/java/main/org/apache/zookeeper/ClientCnxn.java ---
@@ -1438,6 +1436,11 @@ public void disconnect() {
}
sendThread.close();
+ try {
+ sendThread.join();
+ } catch (InterruptedException ex) {
+ LOG.warn("Got interrupted while waiting for the sender thread to close", ex);
+ }
eventThread.queueEventOfDeath();
--- End diff --
If the `queueEventOfDeath` call moves above the `join` call then some events can be missed.
Specifically closing the connection's eventOfDeath can be processed before completing the send of the session disconnection message triggered in disconnect.
I tried moving the queueEventOfDeath above the join, but then some of the core tests began failing intermittently. This was remedied by moving the eventOfDeath back after the completion of the send thread.
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
Now the build appears to fail without running any tests at all!
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by anmolnar <gi...@git.apache.org>.
Github user anmolnar commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196712402
--- Diff: src/java/test/org/apache/zookeeper/test/WatcherTest.java ---
@@ -140,6 +142,55 @@ public void processResult(int rc, String path, Object ctx) {
}
}
}
+
+ @Test
+ public void testWatcherDisconnectOnClose()
+ throws IOException, InterruptedException, KeeperException
+ {
+ ZooKeeper zk = null;
+ try {
+ final BlockingQueue<WatchedEvent> queue = new LinkedBlockingQueue<>();
+
+ MyWatcher connWatcher = new MyWatcher();
+
+ Watcher watcher = new Watcher(){
+ @Override
+ public void process(WatchedEvent event) {
+ try {
+ queue.put(event);
+ } catch (InterruptedException e) {
+ // Oh well, never mind
+ }
+ }
+
+ };
+
+ zk = createClient(connWatcher, hostPort);
+
+ StatCallback scb = new StatCallback() {
+ public void processResult(int rc, String path, Object ctx,
+ Stat stat) {
+ // don't do anything
+ }
+ };
+
+ // Register a watch on the node
+ zk.exists("/missing", watcher, scb, null);
+
+ // Close the client without changing the node
+ zk.close();
+
+
+ WatchedEvent event = queue.poll(10, TimeUnit.SECONDS);
+ Assert.assertEquals(Event.EventType.None, event.getType());
--- End diff --
When your patch isn't applied, the watcher won't receive any event and timeout occurs. Resulting in `event == null` and test will get NullReferenceException. Would you please assert for null ref and also add some meaningful messages to the assert statements which indicate what exactly went wrong and what should be the expected behaviour.
---
[GitHub] zookeeper pull request #529: [ZOOKEEPER-2368] Send a watch event is when a c...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/529#discussion_r196102971
--- Diff: src/java/main/org/apache/zookeeper/Watcher.java ---
@@ -84,7 +84,14 @@
* client connection (the session) is no longer valid. You must
* create a new client connection (instantiate a new ZooKeeper
* instance) if you with to access the ensemble. */
- Expired (-112);
+ Expired (-112),
+
+ /**
+ * The client has been closed. This state is never generated by
+ * the server, but is generated locally when a client calls
+ * {@link ZooKeeper#close()} or {@link ZooKeeper#close(int)}
+ */
+ Closed (-2);
--- End diff --
My understanding was that a negative value would be preferred for an event which didn't originate from the server, but I don't mind changing it.
---
[GitHub] zookeeper issue #529: [ZOOKEEPER-2368] Send a watch event is when a client i...
Posted by timothyjward <gi...@git.apache.org>.
Github user timothyjward commented on the issue:
https://github.com/apache/zookeeper/pull/529
I'm not sure why the Jenkins build is still marked as pending, it seems to have passed.
---