summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShaun Crampton <shaun.crampton@metaswitch.com>2015-05-18 11:52:34 -0700
committerShaun Crampton <shaun.crampton@metaswitch.com>2015-05-18 11:52:34 -0700
commit012534e719290504d51365465179fc41f48ef9e7 (patch)
treede2543d23771363ccfe35dc3d250a661f488fa70 /src
parentfa361efa492afab0f023084ddcfca38500fda8df (diff)
Only raise EtcdClusterIdChanged on first request after change.
Diffstat (limited to 'src')
-rw-r--r--src/etcd/client.py25
-rw-r--r--src/etcd/tests/unit/test_request.py12
2 files changed, 25 insertions, 12 deletions
diff --git a/src/etcd/client.py b/src/etcd/client.py
index 809b4a9..3a7e3ca 100644
--- a/src/etcd/client.py
+++ b/src/etcd/client.py
@@ -624,18 +624,21 @@ class Client(object):
some_request_failed = True
else:
- # Check the cluster ID hasn't changed under us.
- # We need preload_content == False above to ensure we can read
- # the headers here before waiting for the content of a watch
- # below.
+ # Check the cluster ID hasn't changed under us. We use
+ # preload_content=False above so we can read the headers
+ # before we wait for the content of a long poll.
cluster_id = response.getheader("x-etcd-cluster-id")
- if self.expected_cluster_id:
- if self.expected_cluster_id != cluster_id:
- raise etcd.EtcdClusterIdChanged(
- 'The UUID of the cluster changed from {} to '
- '{}.'.format(self.expected_cluster_id, cluster_id))
- else:
- self.expected_cluster_id = cluster_id
+ id_changed = (self.expected_cluster_id and
+ cluster_id != self.expected_cluster_id)
+ # Update the ID so we only raise the exception once.
+ self.expected_cluster_id = cluster_id
+ if id_changed:
+ # Defensive: clear the pool so that we connect afresh next
+ # time.
+ self.http.clear()
+ raise etcd.EtcdClusterIdChanged(
+ 'The UUID of the cluster changed from {} to '
+ '{}.'.format(self.expected_cluster_id, cluster_id))
if some_request_failed:
if not self._use_proxies:
diff --git a/src/etcd/tests/unit/test_request.py b/src/etcd/tests/unit/test_request.py
index 23a5690..2f36168 100644
--- a/src/etcd/tests/unit/test_request.py
+++ b/src/etcd/tests/unit/test_request.py
@@ -513,9 +513,19 @@ class TestClientRequest(TestClientApiInterface):
def test_read_cluster_id_changed(self):
""" Read timeout set to the default """
- self._mock_api(200, {}, cluster_id="notabcd1234")
+ d = {u'action': u'set',
+ u'node': {
+ u'expiration': u'2013-09-14T00:56:59.316195568+02:00',
+ u'modifiedIndex': 6,
+ u'key': u'/testkey',
+ u'ttl': 19,
+ u'value': u'test'
+ }
+ }
+ self._mock_api(200, d, cluster_id="notabcd1234")
self.assertRaises(etcd.EtcdClusterIdChanged,
self.client.read, '/testkey')
+ self.client.read("/testkey")
def test_not_in(self):
pass