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
This commit is contained in:
John Schwarz 2016-07-14 13:39:17 +03:00
parent 324482fa96
commit f296519c27
3 changed files with 46 additions and 2 deletions

View File

@ -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:

44
tooz/tests/test_etcd.py Normal file
View File

@ -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)

View File

@ -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]