Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect SessionExpiredError when ZooKeeper leader process down #374

Closed
bwtakacy opened this issue Dec 28, 2015 · 7 comments · Fixed by #588
Closed

Incorrect SessionExpiredError when ZooKeeper leader process down #374

bwtakacy opened this issue Dec 28, 2015 · 7 comments · Fixed by #588
Labels

Comments

@bwtakacy
Copy link
Contributor

bwtakacy commented Dec 28, 2015

I encountered SessionExpiredError when ZooKeeper leader process down with below stack trace, but there was no session expiration at that time in ZooKeeper server logs:

Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/home/postgres/zha/zha.py", line 284, in run
    self.in_elector_loop()
  File "/home/postgres/zha/zha.py", line 303, in in_elector_loop
    lock_result = self.lock.acquire(timeout=self.zha.config.get("elector_interval",3))
  File "/usr/lib/python2.6/site-packages/kazoo-2.2.1-py2.6.egg/kazoo/recipe/lock.py", line 174, in acquire
    self._delete_node(self.node)
  File "/usr/lib/python2.6/site-packages/kazoo-2.2.1-py2.6.egg/kazoo/recipe/lock.py", line 266, in _delete_node
    self.client.delete(self.path + "/" + node)
  File "/usr/lib/python2.6/site-packages/kazoo-2.2.1-py2.6.egg/kazoo/client.py", line 1295, in delete
    return self.delete_async(path, version).get()
  File "/usr/lib/python2.6/site-packages/kazoo-2.2.1-py2.6.egg/kazoo/handlers/utils.py", line 72, in get
    raise self._exception
SessionExpiredError

, where zha.py is a tools using kazoo.

After debugging, I have found that _call() of KazooClient raised this error when KeeperState became CONNECTING and this is what happened in my environment.

        elif self._state in (KeeperState.EXPIRED_SESSION,
                             KeeperState.CONNECTING):
            async_object.set_exception(SessionExpiredError())
            return False

Is there some historical reason about this?
Is it OK to change the condition will catch only the KeeperState.EXPIRED_SESSION ?

@harlowja
Copy link
Contributor

harlowja commented Jan 7, 2016

Said change seems ok although I'm not sure on the history of why it checks against both.

@bwtakacy
Copy link
Contributor Author

bwtakacy commented Jan 7, 2016

Thank you for take a look!
I will create a patch and test it.

@bwtakacy
Copy link
Contributor Author

bwtakacy commented Jan 8, 2016

I tried to remove checking KeeperState.CONNECTIONG, but tests failed because the below test in test_client.py assumes that.

 468     def test_create_on_broken_connection(self):
 469         client = self.client
 470         client.start()
 471 
 472         client._state = KeeperState.EXPIRED_SESSION
 473         self.assertRaises(SessionExpiredError, client.create,
 474                           '/closedpath', b'bar')
 475 
 476         client._state = KeeperState.AUTH_FAILED
 477         self.assertRaises(AuthFailedError, client.create,
 478                           '/closedpath', b'bar')
 479 
 480         client._state = KeeperState.CONNECTING
 481         self.assertRaises(SessionExpiredError, client.create,
 482                           '/closedpath', b'bar')

Hmm.

By the way, when I also remove the assertion of case of KeeperState.CONNECTING, the all of rest tests are passed.

@eric-spriggy
Copy link

eric-spriggy commented Apr 12, 2018

I've also experienced the same as @bwtakacy. Any ideas what's the history of checking for CONNECTING and then raising a SessionExpiredError?

It was introduced in this commit: 4eabbd1

@suyogmapara
Copy link

suyogmapara commented May 16, 2018

@bbangert Can you please comment on this?

@bbangert
Copy link
Member

bbangert commented May 19, 2018

Offhand I can't recall why its returning expired when it isn't expired.

@adityagujral
Copy link

adityagujral commented Jun 17, 2018

Was there any consensus about fixing the issue?

ztzg added a commit to ztzg/kazoo that referenced this issue Feb 9, 2020
…n-zk#374)

As discussed in python-zk#570 (comment):

  * Requests issued in 'CONNECTING' state get queued, and a "token"
    added to the pipe, as in the 'CONNECTED' state;

  * All test pass;

  * TODO: This patch does not try to limit the maximum queue length.
    How should that be controlled?  A new 'KazooClient' parameter?  If
    so, what should it default to?
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 9, 2020
…n-zk#374)

As discussed in python-zk#570 (comment):

With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a (misleading)
'SessionExpiredError'.

  * [TODO] This patch does not prevent requests which have been queued
    but not emitted from being rejected with 'ConnectionLoss'.  It
    should also get rid of the second part of '_notify_pending',
    shouldn't it?

  * [TODO] This patch does not try to limit the maximum queue length,
    which should probably be controlled via a new 'KazooClient'
    parameter.  (How about 'max_queue_length'?  What should it default
    to?)
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 9, 2020
…n-zk#374)

As discussed in python-zk#570 (comment):

With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a (misleading)
'SessionExpiredError'.

  * [TODO] This patch does not prevent requests which have been queued
    but not emitted from being rejected with 'ConnectionLoss'.  It
    should also get rid of the second part of '_notify_pending',
    shouldn't it?

  * [TODO] This patch does not try to limit the maximum queue length,
    which should probably be controlled via a new 'KazooClient'
    parameter.  (How about 'max_queue_length'?  What should it default
    to?)
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 14, 2020
…#374)

As discussed in python-zk#570 (comment):

With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a (misleading)
'SessionExpiredError'.
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 14, 2020
…thon-zk#374)

If the connection is lost, but the state is 'CONNECTING', the client
is trying to revalidate an existing session.

When that happens, the requests which have been dequeued but are still
pending are interrupted with a 'ConnectionLoss' exception--as we don't
know if the packet reached the server, and the ACKs will never come
back anyway.

Without this patch, the requests which are still queued, and thus
haven't been emitted are also cancelled with the same exception
(see bottom half of '_notify_pending').

It seems that there is no reason to cancel such requests when the new
state is 'CONNECTING', as the client is trying to validate an existing
session; these "pristine" requests could very well be submitted
in-order over the new connection if the session is recovered.

This also seems more consistent with the first patch for issue python-zk#374:
it does not matter whether a request was queued just before or during
the "outage"--as long as it was not emitted.

This patch implements that feature.

ATTN: The patch is marked WIP because it should most probably *NOT* be
merged--as it turns out that both the Java and the C client cancel
such requests when the connection is lost.

Java: ClientCnxn socket error -> cleanAndNotifyState -> cleanup
    -> conLossPacket/remove

C: Socket error -> handle_socket_error_msg -> handle_error -> cleanup
    -> cleanup_bufs -> free_buffers/free_completions
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 17, 2020
…#374)

With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a misleading
'SessionExpiredError'.

This fixes python-zk#374, and brings
Kazoo more in line with the Java and C clients.

See the 'kazoo.client.KazooClient.state' documentation as well as
these discussions for more details:

python-zk#570 (comment)
python-zk#583 (comment)
ztzg added a commit to ztzg/kazoo that referenced this issue Feb 24, 2020
…#374)

With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a misleading
'SessionExpiredError'.

This fixes python-zk#374, and brings
Kazoo more in line with the Java and C clients.

See the 'kazoo.client.KazooClient.state' documentation as well as
these discussions for more details:

python-zk#570 (comment)
python-zk#583 (comment)
StephenSorriaux pushed a commit that referenced this issue Mar 9, 2020
With this patch, requests issued while the client is in the
'CONNECTING' state get queued instead of raising a misleading
'SessionExpiredError'.

This fixes #374, and brings
Kazoo more in line with the Java and C clients.

See the 'kazoo.client.KazooClient.state' documentation as well as
these discussions for more details:

#570 (comment)
#583 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants