Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
remove inactive replication slots from known governor nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Claus Riegg authored and crigertg committed Aug 31, 2020
1 parent a223493 commit 5c93812
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 8 additions & 3 deletions governor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,17 @@ def run(config):
# create replication slots
if postgresql.is_leader():
logging.info("Governor Running: I am the Leader")
for node in etcd.get_client_path("/members?recursive=true")["node"]["nodes"]:
member = node["key"].split('/')[-1]
if member != postgresql.name:
for node in etcd.members():
member = node["hostname"]
if member != postgresql.name:
if postgresql.is_leader():
postgresql.ensure_replication_slot(
postgresql.replication_slot_name(member)
)
else:
postgresql.drop_replication_slot(
postgresql.replication_slot_name(member)
)
etcd.touch_member(postgresql.name, postgresql.connection_string)

time.sleep(config["loop_wait"])
Expand Down
11 changes: 11 additions & 0 deletions helpers/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ def ensure_replication_slot(self, slot_name):
"END IF; "
"END$$;" % {"slot": slot_name})

def drop_replication_slot(self, slot_name):
self.query(
"DO LANGUAGE plpgsql $$DECLARE somevar VARCHAR; "
"BEGIN "
"SELECT slot_name INTO somevar FROM pg_replication_slots "
"WHERE slot_name = '%(slot)s' AND active = 'f' LIMIT 1; "
"IF FOUND THEN PERFORM "
"pg_drop_replication_slot('%(slot)s'); "
"END IF;"
"END$$;" % {"slot": slot_name})

def write_pg_hba(self):
f = open("%s/pg_hba.conf" % self.data_dir, "a")
f.write("host replication %(username)s %(network)s md5" %
Expand Down

0 comments on commit 5c93812

Please sign in to comment.