Skip to content

Commit

Permalink
pglookout: if recovery.conf has no target timeline, change to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Ormod committed Apr 28, 2015
1 parent e823a53 commit 64c78bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pglookout/pglookout.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def get_replication_positions(self, standby_nodes):
known_replication_positions = {}
for hostname, node_state in standby_nodes.items():
now = datetime.datetime.utcnow()
self.log.debug("conn: %r %r", node_state['connection'], now - parse_iso_datetime(node_state['fetch_time']))
if node_state['connection'] and now - parse_iso_datetime(node_state['fetch_time']) < datetime.timedelta(seconds=20) and hostname not in self.never_promote_these_nodes: # pylint: disable=C0301
xlog_pos = convert_xlog_location_to_offset(node_state['pg_last_xlog_receive_location'])
if xlog_pos in known_replication_positions:
Expand Down Expand Up @@ -432,8 +431,11 @@ def modify_recovery_conf_to_point_at_new_master_host(self, new_master_host):
path_to_recovery_conf = os.path.join(self.config.get("pg_data_directory"), "recovery.conf")
with open(path_to_recovery_conf, "r") as fp:
old_recovery_conf_contents = fp.readlines()
has_recovery_target_timeline = False
with open(path_to_recovery_conf + "_temp", "w") as fp:
for line in old_recovery_conf_contents:
if line.startswith("recovery_target_timeline"):
has_recovery_target_timeline = True
if line.startswith("primary_conninfo"):
parts = []
for part in line.split(" "):
Expand All @@ -443,6 +445,10 @@ def modify_recovery_conf_to_point_at_new_master_host(self, new_master_host):
parts.append("host=%s" % new_master_host)
line = ' '.join(parts)
fp.write(line)
# The timeline of the recovery.conf will require a higher timeline target
if not has_recovery_target_timeline:
fp.write("recovery_target_timeline = 'latest'")

os.rename(path_to_recovery_conf + "_temp", path_to_recovery_conf)

def start_following_new_master(self, new_master_host):
Expand Down
2 changes: 1 addition & 1 deletion test/test_lookout.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def test_two_slave_failover_and_autofollow(self):

with open(os.path.join(pg_data_dir, "recovery.conf"), "r") as fp:
content = fp.read()
self.assertEqual(content, "standby_mode = 'on'\nprimary_conninfo = 'user=replication password=vjsh8l7sv4a902y1tsdz host=other port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'\n")
self.assertEqual(content, "standby_mode = 'on'\nprimary_conninfo = 'user=replication password=vjsh8l7sv4a902y1tsdz host=other port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'\nrecovery_target_timeline = 'latest'")

self.assertEqual(self.pglookout.current_master, "other")

Expand Down

0 comments on commit 64c78bf

Please sign in to comment.