From f296519c27f6679d4667100333f099aba0840d54 Mon Sep 17 00:00:00 2001 From: John Schwarz Date: Thu, 14 Jul 2016 13:39:17 +0300 Subject: [PATCH] etcd: properly block when using 'wait' When using the wait functionality, one must use the last index increased by one, as the wait returns once that index has been used. Using the index that is already been returned by the etcd instance will cause 'wait' to return immediately, producing a busy-wait loop while spamming the server with the same requests over and over. Closes-Bug: #1603002 Change-Id: I4f3bee0a52dcf2eb69b96b53a9f777a5e2a7efb1 --- tooz/drivers/etcd.py | 2 +- tooz/tests/test_etcd.py | 44 +++++++++++++++++++++++++++++++++++++++++ tox.ini | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 tooz/tests/test_etcd.py diff --git a/tooz/drivers/etcd.py b/tooz/drivers/etcd.py index 81fb179..4ff93f3 100644 --- a/tooz/drivers/etcd.py +++ b/tooz/drivers/etcd.py @@ -133,7 +133,7 @@ class EtcdLock(locking.Lock): try: reply = self.client.get( self._lock_url + - "?wait=true&waitIndex=%d" % reply['index'], + "?wait=true&waitIndex=%d" % (reply['index'] + 1), make_url=False, timeout=watch.leftover() if watch else None) except requests.exceptions.RequestException: diff --git a/tooz/tests/test_etcd.py b/tooz/tests/test_etcd.py new file mode 100644 index 0000000..bbc99e5 --- /dev/null +++ b/tooz/tests/test_etcd.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2016 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +from testtools import testcase + +import tooz.coordination + + +class TestEtcd(testcase.TestCase): + FAKE_URL = "etcd://mocked-not-really-localhost:2379" + FAKE_MEMBER_ID = "mocked-not-really-member" + + def setUp(self): + super(TestEtcd, self).setUp() + self._coord = tooz.coordination.get_coordinator(self.FAKE_URL, + self.FAKE_MEMBER_ID) + + def test_multiple_locks_etcd_wait_index(self): + lock = self._coord.get_lock('mocked-not-really-random') + + return_values = [ + {'errorCode': {}, 'node': {}, 'index': 10}, + {'errorCode': None, 'node': {}, 'index': 10} + ] + with mock.patch.object(lock.client, 'put', side_effect=return_values): + with mock.patch.object(lock.client, 'get') as mocked_get: + self.assertTrue(lock.acquire()) + mocked_get.assert_called_once() + call = str(mocked_get.call_args) + self.assertIn("waitIndex=11", call) diff --git a/tox.ini b/tox.ini index 6e78135..6490e17 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,7 @@ envlist = py27,py34,py27-zookeeper,py34-zookeeper,py27-redis,py34-redis,py27-sen [testenv] # We need to install a bit more than just `test' because those drivers have # custom tests that we always run -deps = .[test,zake,ipc,memcached,mysql] +deps = .[test,zake,ipc,memcached,mysql,etcd] py{27,34}-zookeeper: .[zookeeper] py{27,34}-redis: .[redis] py{27,34}-sentinel: .[redis]