mozilla-services/zktools — Issue #1: Connection close support?
Author: hannosch
Created: 2012-01-27 21:49:20+00:00
State: open

Closing connections seems to be half-supported:

1. Closing a connection leads to Zookeeper server side errors being logged (java.nio.channels.CancelledKeyException)
2. The ZkConnection.connected attribute isn't set to False
3. Closing a closed connection again, opens a new one to just close it right off again

Reconnection support is nice, but I think we ought to support explicitly closing connections.

Here's the details with the log messages from the Zookeeper server console mixed in (not the client messages, so debug=False):

>>> from zktools import connection
>>> conn = connection.ZkConnection()
>>> conn.connected
False
>>> conn.connect()

INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1352107be28000a with negotiated timeout 10000 for client /0:0:0:0:0:0:0:1%0:56972

>>> conn.connected
True
>>> conn.get_children('/')
['zookeeper']
>>> conn.close()
0

INFO [ProcessThread:-1:PrepRequestProcessor@391] - Processed session termination for sessionid: 0x1352107be28000a
WARN [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@634] - EndOfStreamException: Unable to read additional data from client sessionid 0x1352107be28000a, likely client has closed socket
INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1435] - Closed socket connection for client /0:0:0:0:0:0:0:1%0:56972 which had sessionid 0x1352107be28000a
ERROR [SyncThread:0:NIOServerCnxn@445] - Unexpected Exception: java.nio.channels.CancelledKeyException
at sun.nio.ch.SelectionKeyImpl.ensureValid(SelectionKeyImpl.java:55)
at sun.nio.ch.SelectionKeyImpl.interestOps(SelectionKeyImpl.java:59)
at org.apache.zookeeper.server.NIOServerCnxn.sendBuffer(NIOServerCnxn.java:418)
at org.apache.zookeeper.server.NIOServerCnxn.sendResponse(NIOServerCnxn.java:1509)
at org.apache.zookeeper.server.FinalRequestProcessor.processRequest(FinalRequestProcessor.java:367)
at org.apache.zookeeper.server.SyncRequestProcessor.flush(SyncRequestProcessor.java:161)
at org.apache.zookeeper.server.SyncRequestProcessor.run(SyncRequestProcessor.java:98)

>>> conn.connected
True

>>> conn.close()
0

INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn$Factory@251] - Accepted socket connection from /0:0:0:0:0:0:0:1%0:56981
INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@777] - Client attempting to establish new session at /0:0:0:0:0:0:0:1%0:56981
INFO [SyncThread:0:NIOServerCnxn@1580] - Established session 0x1352107be28000b with negotiated timeout 10000 for client /0:0:0:0:0:0:0:1%0:56981
INFO [ProcessThread:-1:PrepRequestProcessor@391] - Processed session termination for sessionid: 0x1352107be28000b
WARN [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@634] - EndOfStreamException: Unable to read additional data from client sessionid 0x1352107be28000b, likely client has closed socket
INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1435] - Closed socket connection for client /0:0:0:0:0:0:0:1%0:56981 which had sessionid 0x1352107be28000b

>>> conn.connected
True