From b282a0f254d69604f4e66218af10c1968eed9953 Mon Sep 17 00:00:00 2001 From: Dmitry Dolgov <9erthalion6@gmail.com> Date: Mon, 13 Aug 2018 14:02:01 +0200 Subject: [PATCH] Add "cluster_unlocked" field (#764) Add a field to an api to figure out if a master is there from patroni point of view. It can be useful, when you have an alert, based on Auto Scaling Groups, and then ASG decided to shutdown the current master, spin up a new instance but the current master shutdown is stuck. In this situation the current master is no longer a part of ASG, but patroni and Postgres are still alive on the instance, which means a new replica will not be promoted yet - this will lead to a false alert, saying that your cluster doesn't have any master node. --- patroni/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/patroni/api.py b/patroni/api.py index 73cdb14c..56cbcc4f 100644 --- a/patroni/api.py +++ b/patroni/api.py @@ -415,6 +415,8 @@ class RestApiHandler(BaseHTTPRequestHandler): def get_postgresql_status(self, retry=False): try: + cluster = self.server.patroni.dcs.cluster + if self.server.patroni.postgresql.state not in ('running', 'restarting', 'starting'): raise RetryFailedError('') stmt = ("WITH replication_info AS (" @@ -439,6 +441,7 @@ class RestApiHandler(BaseHTTPRequestHandler): 'postmaster_start_time': row[0], 'role': 'replica' if row[1] == 0 else 'master', 'server_version': self.server.patroni.postgresql.server_version, + 'cluster_unlocked': bool(not cluster or cluster.is_unlocked()), 'xlog': ({ 'received_location': row[3], 'replayed_location': row[4], @@ -451,7 +454,6 @@ class RestApiHandler(BaseHTTPRequestHandler): if row[1] > 0: result['timeline'] = row[1] else: - cluster = self.server.patroni.dcs.cluster leader_timeline = None if not cluster or cluster.is_unlocked() else cluster.leader.timeline result['timeline'] = self.server.patroni.postgresql.replica_cached_timeline(leader_timeline)