Bugfix: GET /cluster was showing stale member info in zookeeper (#1573)

Zookpeeper implementation heavily relies on cached version of the cluster view in order to minimize the number of requests. Having stale members information is fine for Patroni workflow because it basically relies only on member names and tags.

The `GET /cluster` is a different case. Being exposed outside it might be used for monitoring purposes and therefore we should show the up-to-date members information.
This commit is contained in:
Alexander Kukushkin
2020-06-05 09:23:54 +02:00
committed by GitHub
parent 6406b39b77
commit c2a78ee652
5 changed files with 13 additions and 5 deletions
+1 -1
View File
@@ -134,7 +134,7 @@ class RestApiHandler(BaseHTTPRequestHandler):
self._write_status_response(200, response)
def do_GET_cluster(self):
cluster = self.server.patroni.dcs.cluster or self.server.patroni.dcs.get_cluster()
cluster = self.server.patroni.dcs.get_cluster(True)
self._write_json_response(200, cluster_as_json(cluster))
def do_GET_history(self):
+6 -1
View File
@@ -642,7 +642,12 @@ class AbstractDCS(object):
If the current node was running as a master and exception raised,
instance would be demoted."""
def get_cluster(self):
def _bypass_caches(self):
"""Used only in zookeeper"""
def get_cluster(self, force=False):
if force:
self._bypass_caches()
try:
cluster = self._load_cluster()
except Exception:
+3
View File
@@ -223,6 +223,9 @@ class ZooKeeper(AbstractDCS):
raise ZooKeeperError('ZooKeeper in not responding properly')
return cluster
def _bypass_caches(self):
self._fetch_cluster = True
def _create(self, path, value, retry=False, ephemeral=False):
try:
if retry:
+2 -2
View File
@@ -203,8 +203,8 @@ class TestRestApiHandler(unittest.TestCase):
@patch.object(MockPatroni, 'dcs')
def test_do_GET_cluster(self, mock_dcs):
mock_dcs.cluster = get_cluster_initialized_without_leader()
mock_dcs.cluster.members[1].data['xlog_location'] = 11
mock_dcs.get_cluster.return_value = get_cluster_initialized_without_leader()
mock_dcs.get_cluster.return_value.members[1].data['xlog_location'] = 11
self.assertIsNotNone(MockRestApiServer(RestApiHandler, 'GET /cluster'))
@patch.object(MockPatroni, 'dcs')
+1 -1
View File
@@ -150,7 +150,7 @@ class TestZooKeeper(unittest.TestCase):
def test_get_cluster(self):
self.assertRaises(ZooKeeperError, self.zk.get_cluster)
cluster = self.zk.get_cluster()
cluster = self.zk.get_cluster(True)
self.assertIsInstance(cluster.leader, Leader)
self.zk.touch_member({'foo': 'foo'})