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

Add support for next_query_flagIN #74

Merged
merged 9 commits into from
Sep 22, 2021
2 changes: 2 additions & 0 deletions changelogs/fragments/74-add-support-for-next_query_flagIN.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- proxysql_query_rules - add ``next_query_flagIN`` argument (https://github.com/ansible-collections/community.proxysql/pull/74).
50 changes: 49 additions & 1 deletion plugins/modules/proxysql_query_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
operator in front of the regular expression matching against
match_pattern.
type: bool
re_modifiers:
description:
- Comma separated list of options to modify the behavior of the RE engine.
With C(CASELESS) the match is case insensitive. With C(GLOBAL) the replace
is global (replaces all matches and not just the first).
For backward compatibility, only C(CASELESS) is the enabled by default.
type: str
version_added: "1.3.0"
flagOUT:
description:
- Used in combination with I(flagIN) and apply to create chains of rules.
Expand Down Expand Up @@ -132,6 +140,11 @@
priority to queries over others. This value is added to the
mysql-default_query_delay global variable that applies to all queries.
type: int
next_query_flagIN:
description:
- When is set, its value will become the I(flagIN) value for the next queries.
type: int
version_added: "1.3.0"
mirror_flagOUT:
description:
- Enables query mirroring. If set I(mirror_flagOUT) can be used to
Expand Down Expand Up @@ -251,6 +264,36 @@
match_digest: '^SELECT @@max_allowed_packet'
multiplex: 2

# This example demonstrates how to use next_query_flagIN argument. It allows
# ProxySQL query rules to be chained. The examples shows how you can have SELECTS
# immediately follow INSERT/UPDATE/DELETE statements to query the primary hostgroup
# and avoid replication lag

- name: Add insert query rule
proxysql_query_rules:
match_digest: "^INSERT"
destination_hostgroup: 1,
next_query_flagIN: 1

- name: Add update query rule
proxysql_query_rules:
match_digest: "^UPDATE"
destination_hostgroup: 1,
next_query_flagIN: 1

- name: Add delete query rules
proxysql_query_rules:
match_digest: "^DELETE"
destination_hostgroup: 1,
next_query_flagIN: 1

- name: Add insert query rules
proxysql_query_rules:
match_digest: ".*"
destination_hostgroup: 1,
next_query_flagIN: 1
comment: Match every queries after an INSERT/UPDATE/DELETE query

# This example removes all rules that use the username 'guest_ro', saves the
# mysql query rule config to disk, and dynamically loads the mysql query rule
# config to runtime. It uses credentials in a supplied config file to connect
Expand Down Expand Up @@ -355,6 +398,7 @@ def __init__(self, module):
"match_digest",
"match_pattern",
"negate_match_pattern",
"re_modifiers",
"flagOUT",
"replace_pattern",
"destination_hostgroup",
Expand All @@ -364,10 +408,12 @@ def __init__(self, module):
"timeout",
"retries",
"delay",
"next_query_flagIN",
"mirror_flagOUT",
"mirror_hostgroup",
"OK_msg",
"error_msg",
"OK_msg",
"multiplex",
"log",
"apply",
"comment"]
Expand Down Expand Up @@ -589,6 +635,7 @@ def main():
match_digest=dict(type='str'),
match_pattern=dict(type='str'),
negate_match_pattern=dict(type='bool'),
re_modifiers=dict(type='str'),
flagOUT=dict(type='int'),
replace_pattern=dict(type='str'),
destination_hostgroup=dict(type='int'),
Expand All @@ -598,6 +645,7 @@ def main():
timeout=dict(type='int'),
retries=dict(type='int'),
delay=dict(type='int'),
next_query_flagIN=dict(type='int'),
mirror_flagOUT=dict(type='int'),
mirror_hostgroup=dict(type='int'),
OK_msg=dict(type='str'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test_retries: 3
test_cache_ttl: 20000
test_cache_empty_result: 0
test_multiplex: 0
test_next_query_flagin: 0

test_proxysql_query_rules_check_mode: false
test_proxysql_query_rules_in_memory_only: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
cache_ttl: '{{ test_cache_ttl }}'
cache_empty_result: '{{ test_cache_empty_result }}'
multiplex: '{{ test_multiplex }}'
next_query_flagIN: '{{ test_next_query_flagin }}'
state: "{{ test_delete|ternary('absent', 'present') }}"
save_to_disk: "{{ not test_proxysql_query_rules_in_memory_only }}"
load_to_runtime: "{{ not test_proxysql_query_rules_in_memory_only }}"
Expand Down Expand Up @@ -52,13 +53,13 @@
when: test_proxysql_query_rules_with_delayed_persist

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex FROM mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}'"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: memory_result

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex FROM disk.mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}'"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM disk.mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: disk_result

- name: "{{ role_name }} | {{ current_test }} | check if test query rule exists in runtime"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex FROM runtime_mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}'"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SELECT username || ',' || match_pattern || ',' || destination_hostgroup || ',' || active || ',' || retries || ',' || cache_ttl || ',' || cache_empty_result || ',' || multiplex || ',' || next_query_flagIN FROM runtime_mysql_query_rules where username = '{{ test_user }}' and match_pattern = '{{ test_match_pattern }}' and destination_hostgroup and '{{ test_destination_hostgroup }}' and active = '{{ test_active }}' and retries = '{{ test_retries }}' and cache_ttl = '{{ test_cache_ttl }}' and cache_empty_result = '{{ test_cache_empty_result }}' and multiplex = '{{ test_multiplex }}' and next_query_flagIN = '{{ test_next_query_flagin }}'"
register: runtime_result
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
block:

- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created in memory"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_query_rules (username, match_pattern, destination_hostgroup, active, retries, cache_ttl, cache_empty_result, multiplex) VALUES ('{{ test_user }}', '{{ test_match_pattern}}', '{{ test_destination_hostgroup }}', '{{ test_active }}', '{{ test_retries }}', '{{ test_cache_ttl }}', '{{ test_cache_empty_result }}', '{{ test_multiplex }}')"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"INSERT OR REPLACE INTO mysql_query_rules (username, match_pattern, destination_hostgroup, active, retries, cache_ttl, cache_empty_result, multiplex, next_query_flagIN) VALUES ('{{ test_user }}', '{{ test_match_pattern}}', '{{ test_destination_hostgroup }}', '{{ test_active }}', '{{ test_retries }}', '{{ test_cache_ttl }}', '{{ test_cache_empty_result }}', '{{ test_multiplex }}', '{{ test_next_query_flagin }}')"

- name: "{{ role_name }} | {{ current_test }} | ensure test query rule is created on disk"
shell: mysql -uadmin -padmin -h127.0.0.1 -P6032 -BNe"SAVE MYSQL QUERY RULES TO DISK"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change in memory"
assert:
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change on disk"
assert:
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change to runtime"
assert:
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

### perform cleanup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change in memory"
assert:
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule didn't make a change on disk"
assert:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change in memory"
assert:
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change on disk"
assert:
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm create query rule did make a change to runtime"
assert:
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

### perform cleanup

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

- name: "{{ role_name }} | {{ current_test }} | confirm delete query rule did make a change on disk"
assert:
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm delete query rule did make a change to runtime"
assert:
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'
### perform cleanup

- name: "{{ role_name }} | {{ current_test }} | ensure we're in a clean state when we finish"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

- name: "{{ role_name }} | {{ current_test }} | confirm delete query rule in check mode didn't make a change in memory"
assert:
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: memory_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm delete query rule in check mode didn't make a change on disk"
assert:
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: disk_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

- name: "{{ role_name }} | {{ current_test }} | confirm delete query rule in check mode didn't make a change to runtime"
assert:
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }}'
that: runtime_result.stdout == '{{ test_user }},{{ test_match_pattern }},{{ test_destination_hostgroup }},{{ test_active }},{{ test_retries }},{{ test_cache_ttl }},{{ test_cache_empty_result }},{{ test_multiplex }},{{ test_next_query_flagin }}'

### perform cleanup

Expand Down