Skip to content

Commit

Permalink
Improve get replica/primary status (#634)
Browse files Browse the repository at this point in the history
* Fix case where a failed fetchone() still return a dict

* Fix test for MariaDB

* fix case where  a failed fetchone() still return a dict for primary

* Add changelog fragment
  • Loading branch information
laurent-indermuehle authored May 2, 2024
1 parent 47710cf commit 6ce2f49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/improve_get_replica_primary_status.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:

- mysql_replication - Improve detection of IsReplica and IsPrimary by inspecting the dictionary returned from the SQL query instead of relying on variable types. This ensures compatibility with changes in the connector or the output of SHOW REPLICA STATUS and SHOW MASTER STATUS, allowing for easier maintenance if these change in the future.
20 changes: 13 additions & 7 deletions plugins/modules/mysql_replication.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,20 +550,26 @@ def main():

if mode == 'getprimary':
status = get_primary_status(cursor)
if not isinstance(status, dict):
status = dict(Is_Primary=False,
msg="Server is not configured as mysql primary")
else:
if status and "File" in status and "Position" in status:
status['Is_Primary'] = True
else:
status = dict(
Is_Primary=False,
msg="Server is not configured as mysql primary. "
"Meaning: Binary logs are disabled")

module.exit_json(queries=executed_queries, **status)

elif mode == "getreplica":
status = get_replica_status(cursor, connection_name, channel, replica_term)
if not isinstance(status, dict):
status = dict(Is_Replica=False, msg="Server is not configured as mysql replica")
else:
# MySQL 8.0 uses Replica_...
# MariaDB 10.6 uses Slave_...
if status and (
"Slave_IO_Running" in status or
"Replica_IO_Running" in status):
status['Is_Replica'] = True
else:
status = dict(Is_Replica=False, msg="Server is not configured as mysql replica")

module.exit_json(queries=executed_queries, **status)

Expand Down

0 comments on commit 6ce2f49

Please sign in to comment.