Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an ability to set a certain check_type when setting up MySQL replication host groups #20

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
ancestor: null
releases:
1.1.0:
changes:
minor_changes:
- Adds an ability to specify which ProxySQL check type to use when detecting
a MySQL standby node in proxysql_replication_hostgroups module.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do NOT modify changelogs/changelog.yaml directly. Create a changelog fragment.


1.0.0:
changes:
release_summary:
Expand Down
52 changes: 43 additions & 9 deletions plugins/modules/proxysql_replication_hostgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
description:
- Text field that can be used for any purposes defined by the user.
type: str
check_type:
description:
- Which check type to use when detecting that the node is a standby.
type: str
choices: [ "read_only", "innodb_read_only", "super_read_only", "read_only|innodb_read_only", "read_only&innodb_read_only" ]
default: read_only
zentavr marked this conversation as resolved.
Show resolved Hide resolved
version_added: 1.1.0
state:
description:
- When C(present) - adds the replication hostgroup, when C(absent) -
Expand Down Expand Up @@ -64,6 +71,16 @@
state: present
load_to_runtime: False

- name: Add a replication hostgroup with super_read_only check type
community.proxysql.proxysql_replication_hostgroups:
login_user: 'admin'
login_password: 'admin'
writer_hostgroup: 1
reader_hostgroup: 2
check_type: 'super_read_only'
state: present
load_to_runtime: False

# This example removes a replication hostgroup, saves the mysql server config
# to disk, and dynamically loads the mysql server config to runtime. It uses
# credentials in a supplied config file to connect to the proxysql admin
Expand All @@ -88,6 +105,7 @@
"msg": "Added server to mysql_hosts",
"repl_group": {
"comment": "",
"check_type": "read_only",
"reader_hostgroup": "1",
"writer_hostgroup": "2"
},
Expand Down Expand Up @@ -151,17 +169,20 @@ def __init__(self, module):
self.writer_hostgroup = module.params["writer_hostgroup"]
self.reader_hostgroup = module.params["reader_hostgroup"]
self.comment = module.params["comment"]
self.check_type = module.params["check_type"]

def check_repl_group_config(self, cursor, keys):
query_string = \
"""SELECT count(*) AS `repl_groups`
FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

if self.comment and not keys:
query_string += "\n AND comment = %s"
Expand All @@ -176,11 +197,13 @@ def get_repl_group_config(self, cursor):
"""SELECT *
FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
repl_group = cursor.fetchone()
Expand All @@ -191,12 +214,14 @@ def create_repl_group_config(self, cursor):
"""INSERT INTO mysql_replication_hostgroups (
writer_hostgroup,
reader_hostgroup,
check_type,
comment)
VALUES (%s, %s, %s)"""
VALUES (%s, %s, %s, %s)"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup,
self.check_type,
self.comment or '']

cursor.execute(query_string, query_data)
Expand All @@ -207,12 +232,14 @@ def update_repl_group_config(self, cursor):
"""UPDATE mysql_replication_hostgroups
SET comment = %s
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.comment,
self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
return True
Expand All @@ -221,11 +248,13 @@ def delete_repl_group_config(self, cursor):
query_string = \
"""DELETE FROM mysql_replication_hostgroups
WHERE writer_hostgroup = %s
AND reader_hostgroup = %s"""
AND reader_hostgroup = %s
AND check_type = %s"""

query_data = \
[self.writer_hostgroup,
self.reader_hostgroup]
self.reader_hostgroup,
self.check_type]

cursor.execute(query_string, query_data)
return True
Expand Down Expand Up @@ -299,6 +328,11 @@ def main():
writer_hostgroup=dict(required=True, type='int'),
reader_hostgroup=dict(required=True, type='int'),
comment=dict(type='str'),
check_type=dict(default='read_only', choices=['read_only',
'innodb_read_only',
'super_read_only',
'read_only|innodb_read_only',
'read_only&innodb_read_only']),
state=dict(default='present', choices=['present',
'absent']),
save_to_disk=dict(default=True, type='bool'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
### prepare
- name: "{{ role_name }} | {{ current_test }} | are we performing a delete"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | are we performing a delete"
set_fact:
test_delete: "{{ current_test | regex_search('^test_delete') | ternary(true, false) }}"

- name: "{{ role_name }} | {{ current_test }} | ensure we're in a clean state when we start"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure we're in a clean state when we start"
include_tasks: "{{ test_delete|ternary('setup_test_replication_hostgroups', 'cleanup_test_replication_hostgroups') }}.yml"
when: not test_proxysql_replication_hostgroups_check_idempotence

### when

- name: "{{ role_name }} | {{ current_test }} | {{ test_delete|ternary('delete','create') }} test mysql replication hostgroup"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | {{ test_delete|ternary('delete','create') }} test mysql replication hostgroup"
proxysql_replication_hostgroups:
login_user: admin
login_password: admin
check_type: '{{ test_check_type }}'
writer_hostgroup: '{{ test_writer_hostgroup }}'
reader_hostgroup: '{{ test_reader_hostgroup }}'
state: "{{ test_delete|ternary('absent', 'present') }}"
Expand All @@ -22,10 +23,10 @@
check_mode: "{{ test_proxysql_replication_hostgroups_check_mode }}"
register: status

- name: "{{ role_name }} | {{ current_test }} | persist the changes to disk, and load to runtime"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | persist the changes to disk, and load to runtime"
block:

- name: "{{ role_name }} | {{ current_test }} | save the replication hostgroups config from memory to disk"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | save the replication hostgroups config from memory to disk"
proxysql_manage_config:
login_user: admin
login_password: admin
Expand All @@ -34,7 +35,7 @@
direction: TO
config_layer: DISK

- name: "{{ role_name }} | {{ current_test }} | load the replication hostgroups config from memory to runtime"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | load the replication hostgroups config from memory to runtime"
proxysql_manage_config:
login_user: admin
login_password: admin
Expand All @@ -45,14 +46,14 @@

when: test_proxysql_replication_hostgroups_with_delayed_persist

- name: "{{ role_name }} | {{ current_test }} | check if test replication hostgroups exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}'"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | check if test replication hostgroups exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}' and check_type = '{{ test_check_type }}'"
register: memory_result

- name: "{{ role_name }} | {{ current_test }} | check if test replication hostgroups exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM disk.mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}'"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | check if test replication hostgroups exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM disk.mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}' and check_type = '{{ test_check_type }}'"
register: disk_result

- name: "{{ role_name }} | {{ current_test }} | check if test replication hostgroups exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM runtime_mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}'"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | check if test replication hostgroups exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT writer_hostgroup || ',' || reader_hostgroup FROM runtime_mysql_replication_hostgroups where writer_hostgroup = '{{ test_writer_hostgroup }}' and reader_hostgroup = '{{ test_reader_hostgroup }}' and check_type = '{{ test_check_type }}'"
register: runtime_result
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: "{{ role_name }} | {{ current_test }} | ensure we're in a clean state when we start/finish"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure we're in a clean state when we start/finish"
block:

- name: "{{ role_name }} | {{ current_test }} | ensure no replication hostgroups are created"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure no replication hostgroups are created"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"DELETE FROM mysql_replication_hostgroups"

- name: "{{ role_name }} | {{ current_test }} | ensure no replication hostgroups are saved on disk"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure no replication hostgroups are saved on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SAVE MYSQL SERVERS TO DISK"

- name: "{{ role_name }} | {{ current_test }} | ensure no replication hostgroups are loaded to runtime"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure no replication hostgroups are loaded to runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"LOAD MYSQL SERVERS TO RUNTIME"
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,13 @@

### tests

- name: "{{ role_name }} | test_create_using_check_mode | test create replication hostgroups using check mode"
import_tasks: test_create_using_check_mode.yml
vars:
test_proxysql_replication_hostgroups_check_mode: true

- name: "{{ role_name }} | test_delete_using_check_mode | test delete replication hostgroups using check mode"
import_tasks: test_delete_using_check_mode.yml
vars:
test_proxysql_replication_hostgroups_check_mode: true

- name: "{{ role_name }} | test_create_replication_hostgroups | test create replication hostgroups"
import_tasks: test_create_replication_hostgroups.yml
vars:
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_create_replication_hostgroups | test idempotence of create replication hostgroups"
import_tasks: test_create_replication_hostgroups.yml
vars:
test_proxysql_replication_hostgroups_check_idempotence: true

- name: "{{ role_name }} | test_delete_replication_hostgroups | test delete replication hostgroups"
import_tasks: test_delete_replication_hostgroups.yml
vars:
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_delete_replication_hostgroups | test idempotence of delete replication hostgroups"
import_tasks: test_delete_replication_hostgroups.yml
vars:
test_proxysql_replication_hostgroups_check_idempotence: true

- name: "{{ role_name }} | test_create_replication_hostgroups_in_memory_only | test create replication hostgroups in memory"
import_tasks: test_create_replication_hostgroups_in_memory_only.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_create_replication_hostgroups_in_memory_only | test idempotence of create replication hostgroups in memory"
import_tasks: test_create_replication_hostgroups_in_memory_only.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_check_idempotence: true

- name: "{{ role_name }} | test_delete_replication_hostgroups_in_memory_only | test delete replication hostgroups in memory"
import_tasks: test_delete_replication_hostgroups_in_memory_only.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_delete_replication_hostgroups_in_memory_only | test idempotence of delete replication hostgroups in memory"
import_tasks: test_delete_replication_hostgroups_in_memory_only.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_check_idempotence: true

- name: "{{ role_name }} | test_create_replication_hostgroups_with_delayed_persist | test create replication hostgroups with delayed save to disk/load to runtime"
import_tasks: test_create_replication_hostgroups_with_delayed_persist.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_with_delayed_persist: true
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_create_replication_hostgroups_with_delayed_persist | test idempotence of create replication hostgroups with delayed save to disk/load to runtime"
import_tasks: test_create_replication_hostgroups_with_delayed_persist.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_with_delayed_persist: true
test_proxysql_replication_hostgroups_check_idempotence: true

- name: "{{ role_name }} | test_delete_replication_hostgroups_with_delayed_persist | test delete replication hostgroups with delayed save to disk/load to runtime"
import_tasks: test_delete_replication_hostgroups_with_delayed_persist.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_with_delayed_persist: true
test_proxysql_replication_hostgroups_cleanup_after_test: false
- name: "{{ role_name }} | test_delete_replication_hostgroups_with_delayed_persist | test idempotence of delete replication hostgroups with delayed save to disk/load to runtime"
import_tasks: test_delete_replication_hostgroups_with_delayed_persist.yml
vars:
test_proxysql_replication_hostgroups_in_memory_only: true
test_proxysql_replication_hostgroups_with_delayed_persist: true
test_proxysql_replication_hostgroups_check_idempotence: true

### teardown

- name: "{{ role_name }} | teardown | perform teardown"
import_tasks: teardown.yml
- name: "{{ role_name }} | Testing proxysql_replication_hostgroup module using different check_type per host group"
include_tasks: "taskset.yml"
loop:
- "read_only"
- "innodb_read_only"
- "super_read_only"
- "read_only|innodb_read_only"
- "read_only&innodb_read_only"
loop_control:
loop_var: test_check_type
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: "{{ role_name }} | {{ current_test }} | ensure test replication hostgroups is created when we start"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure test replication hostgroups is created when we start"
block:

- name: "{{ role_name }} | {{ current_test }} | ensure test replication hostgroups is created in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup) VALUES ('{{ test_writer_hostgroup }}', '{{ test_reader_hostgroup}}')"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure test replication hostgroups is created in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup, check_type) VALUES ('{{ test_writer_hostgroup }}', '{{ test_reader_hostgroup}}', '{{ test_check_type }}')"

- name: "{{ role_name }} | {{ current_test }} | ensure test replication hostgroups is created on disk"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure test replication hostgroups is created on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SAVE MYSQL SERVERS TO DISK"

- name: "{{ role_name }} | {{ current_test }} | ensure test replication hostgroups is created in runtime"
- name: "{{ role_name }} | {{ current_test }} | {{ test_check_type }} | ensure test replication hostgroups is created in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"LOAD MYSQL SERVERS TO RUNTIME"
Loading