Mirroring

Mirroring in ProxySQL should be considered experimental:

  • specifications can change at any time.
  • this hasn't been extensively tested
  • upgrades from previous versions that do not support mirroring will lose any previously defined query rules
  • It doesn't support prepared statements

Extensions to mysql_query_rules

Table mysql_query_rules was modified to add 2 more columns:

  • mirror_flagOUT
  • mirror_hostgroup
    Therefore, the new table definition of mysql_query_rules becomes:
  1. mysql> show create table mysql_query_rules\G
  2. *************************** 1. row ***************************
  3. table: mysql_query_rules
  4. Create Table: CREATE TABLE mysql_query_rules (
  5. rule_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  6. active INT CHECK (active IN (0,1)) NOT NULL DEFAULT 0,
  7. username VARCHAR,
  8. schemaname VARCHAR,
  9. flagIN INT NOT NULL DEFAULT 0,
  10. match_digest VARCHAR,
  11. match_pattern VARCHAR,
  12. negate_match_pattern INT CHECK (negate_match_pattern IN (0,1)) NOT NULL DEFAULT 0,
  13. flagOUT INT,
  14. replace_pattern VARCHAR,
  15. destination_hostgroup INT DEFAULT NULL,
  16. cache_ttl INT CHECK(cache_ttl > 0),
  17. reconnect INT CHECK (reconnect IN (0,1)) DEFAULT NULL,
  18. timeout INT UNSIGNED,
  19. delay INT UNSIGNED,
  20. mirror_flagOUT INT UNSIGNED,
  21. mirror_hostgroup INT UNSIGNED,
  22. error_msg VARCHAR,
  23. apply INT CHECK(apply IN (0,1)) NOT NULL DEFAULT 0)
  24. 1 row in set (0.00 sec)

Implementation overview

When either mirror_flagOUT or mirror_hostgroup are set for a matching query, real time query mirroring is automatically enabled.Note that mirroring is enabled for the the final query, if the original was rewritten: if the query was rewritten along the way, the mirroring logic applies to the rewritten query. Although, the mirrored query can be rewritten and modified again. Details below.If a source query is matched against multiple query rules, it is possible that mirror_flagOUT or mirror_hostgroup are changed multiple times.The mirroring logic is the following:

  • if mirror_flagOUT or mirror_hostgroup are set while processing the source query, a new mysql session is created
  • the new mysql session will get all the same properties of the original mysql session : same credentials, schemaname, default hostgroup, etc (note: charset is currently not copied)
  • if mirror_hostgroup was set in the original session, the new session will change its default hostgroup to mirror_hostgroup
  • if mirrorflagOUT is not set, the new session will execute the _original query against the defined mirror_hostgroup
  • if mirror_flagOUT was set in the original session, the new mysql session will try to match the query from the original session against mysql_query_rules starting from a value of FlagIN=mirror_flagOUT : in this way it is possible to modify the query, like rewriting it, or changing again the hostgroup

Examples

Mirror selects to same hostgroup

In this very simple example we will just send all SELECT statements to hostgroup2, both the original and the mirror ones.

  1. Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,mirror_hostgroup,apply) VALUES (5,1,'^SELECT',2,2,1);
  2. Query OK, 1 row affected (0.01 sec)
  3.  
  4. Admin> LOAD MYSQL QUERY RULES TO RUNTIME; Query OK, 0 rows affected (0.01 sec)

From a mysql session we will run some queries:

  1. mysql> use sbtest;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4.  
  5. Database changed
  6. mysql> SHOW TABLES;
  7. +------------------+
  8. | Tables_in_sbtest |
  9. +------------------+
  10. | longtable |
  11. | sbtest1 |
  12. | sbtest2 |
  13. | sbtest3 |
  14. | sbtest4 |
  15. +------------------+
  16. 5 rows in set (0.00 sec)
  17.  
  18. mysql> SELECT id FROM sbtest1 LIMIT 3;
  19. +------+
  20. | id |
  21. +------+
  22. | 6204 |
  23. | 3999 |
  24. | 6650 |
  25. +------+
  26. 3 rows in set (0.02 sec)

Back to the admin interface, we can see that the SELECT statement was executed twice:

  1. Admin> select hostgroup,count_star,schemaname,digest_text from stats_mysql_query_digest ORDER BY digest;
  2. +-----------+------------+--------------------+----------------------------------+
  3. | hostgroup | count_star | schemaname | digest_text |
  4. +-----------+------------+--------------------+----------------------------------+
  5. | 2 | 2 | sbtest | SELECT id FROM sbtest1 LIMIT ? |
  6. | 1 | 1 | information_schema | select @@version_comment limit ? |
  7. | 2 | 2 | information_schema | SELECT DATABASE() |
  8. +-----------+------------+--------------------+----------------------------------+
  9. 3 rows in set (0.00 sec)

As an additional test we re-run the same query:

  1. mysql> SELECT id FROM sbtest1 LIMIT 3;
  2. +------+
  3. | id |
  4. +------+
  5. | 6204 |
  6. | 3999 |
  7. | 6650 |
  8. +------+
  9. 3 rows in set (0.00 sec)

On admin interface:

  1. Admin> select hostgroup,count_star,schemaname,digest_text from stats_mysql_query_digest ORDER BY digest;
  2. +-----------+------------+--------------------+----------------------------------+
  3. | hostgroup | count_star | schemaname | digest_text |
  4. +-----------+------------+--------------------+----------------------------------+
  5. | 2 | 4 | sbtest | SELECT id FROM sbtest1 LIMIT ? |
  6. | 1 | 1 | information_schema | select @@version_comment limit ? |
  7. | 2 | 2 | information_schema | SELECT DATABASE() |
  8. +-----------+------------+--------------------+----------------------------------+
  9. 3 rows in set (0.00 sec)

count_star is double the number of times we executed the queries, because it is mirrored.It is important to note that ProxySQL collects metrics both for the original query and the mirrors.

Mirror SELECT to different hostgroup

In this example, we will re-configure proxysql to send all the SELECT statements to hostgroup1, but to mirror them on hostgroup2 :

  1. Admin> DELETE FROM mysql_query_rules;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,mirror_hostgroup,apply) VALUES (5,1,'^SELECT',1,2,1);
  5. Query OK, 1 row affected (0.00 sec)
  6.  
  7. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  8. Query OK, 0 rows affected (0.00 sec)

We also empty stats_mysql_query_digest to have a fresh stats:

  1. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;
  2. +----------+
  3. | COUNT(*) |
  4. +----------+
  5. | 3 |
  6. +----------+
  7. 1 row in set (0.00 sec)
  8.  
  9. Admin> select hostgroup,count_star,schemaname,digest_text from stats_mysql_query_digest ORDER BY digest;
  10. Empty set (0.00 sec)

From the mysql client we can now run some queries (for simplicity, we run the same):

  1. mysql> SELECT id FROM sbtest1 LIMIT 3;
  2. +------+
  3. | id |
  4. +------+
  5. | 6204 |
  6. | 3999 |
  7. | 6650 |
  8. +------+
  9. 3 rows in set (0.00 sec)

In Admin we can now verify what happened:

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY digest;
  2. +-----------+------------+----------+--------------------------------+
  3. | hostgroup | count_star | sum_time | digest_text |
  4. +-----------+------------+----------+--------------------------------+
  5. | 1 | 1 | 2995 | SELECT id FROM sbtest1 LIMIT ? |
  6. | 2 | 1 | 921 | SELECT id FROM sbtest1 LIMIT ? |
  7. +-----------+------------+----------+--------------------------------+
  8. 2 rows in set (0.00 sec)

The same identical query was sent to both hostgroup1 and hostgroup2!

rewrite both source query and mirror

In this example, we will rewrite the original query, and then mirror it:For simplicity, we rewrite sbtest[0-9]+ to sbtest3 :

  1. Admin> DELETE FROM mysql_query_rules;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,replace_pattern,mirror_hostgroup,apply) VALUES (5,1,'sbtest[0-9]+',1,'sbtest3',2,1);
  5. Query OK, 1 row affected (0.00 sec)
  6.  
  7. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  8. Query OK, 0 rows affected (0.00 sec)

Again, we reset stats_mysql_query_digest :

  1. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;

From the mysql client we can run the usual query:

  1. mysql> SELECT id FROM sbtest1 LIMIT 3;
  2. +-------+
  3. | id |
  4. +-------+
  5. | 24878 |
  6. | 8995 |
  7. | 33622 |
  8. +-------+
  9. 3 rows in set (0.03 sec)

As expected, the output is different from previous one because now the original query was rewritten.Let's check stats_mysql_query_digest :

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY digest; +-----------+------------+----------+--------------------------------+
  2. | hostgroup | count_star | sum_time | digest_text |
  3. +-----------+------------+----------+--------------------------------+
  4. | 2 | 1 | 30018 | SELECT id FROM sbtest3 LIMIT ? |
  5. | 1 | 1 | 27227 | SELECT id FROM sbtest3 LIMIT ? |
  6. +-----------+------------+----------+--------------------------------+
  7. 2 rows in set (0.00 sec)

As expected, the modified query was executed on both hostgroups

rewrite mirror query only

In this example we will rewrite only the mirrored query.This is very useful, if for example, we want to understand the performance of the rewritten query, or if a new index will improve performance.

In this example we will compare the performance of the same queries with and without the use of indexes.We will also send the queries to same hostgroups.

The following creates a rule (rule_id=5) that:

  • matches FROM sbtest1
  • sets destination hostgroup=2
  • sets mirror_flagOUT=100
  • does NOT set a mirror_hostgroup
  1. Admin> DELETE FROM mysql_query_rules;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,mirror_flagOUT,apply) VALUES (5,1,'FROM sbtest1 ',2,100,1);
  5. Query OK, 1 row affected (0.00 sec)

Because mirror_flagOUT is set, a new session will be created to run the same query. However, mirror_hostgroup was not set, so the query will be sent to the default hostgroup for the specific user, according to mysql_users. Instead, we want to send the mirror query to the same hostgroup as the original. We could do this either setting mirror_hostgroup in rule with rule_id=5 , or create a new rule. We will also create a new rule to rewrite the query:

  1. Admin> INSERT INTO mysql_query_rules (rule_id,active,flagIN,match_pattern,destination_hostgroup,replace_pattern,apply) VALUES (10,1,100,'FROM sbtest1 ',2,'FROM sbtest1 IGNORE INDEX(k_1) ',1);
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> SELECT * FROM mysql_query_rules\G
  5. *************************** 1. row ***************************
  6. rule_id: 5
  7. active: 1
  8. username: NULL
  9. schemaname: NULL
  10. flagIN: 0
  11. match_digest: NULL
  12. match_pattern: FROM sbtest1
  13. negate_match_pattern: 0
  14. flagOUT: NULL
  15. replace_pattern: NULL
  16. destination_hostgroup: 2
  17. cache_ttl: NULL
  18. reconnect: NULL
  19. timeout: NULL
  20. delay: NULL
  21. mirror_flagOUT: 100
  22. mirror_hostgroup: NULL
  23. error_msg: NULL
  24. apply: 1
  25. *************************** 2. row ***************************
  26. rule_id: 10
  27. active: 1
  28. username: NULL
  29. schemaname: NULL
  30. flagIN: 100
  31. match_digest: NULL
  32. match_pattern: FROM sbtest1
  33. negate_match_pattern: 0
  34. flagOUT: NULL
  35. replace_pattern: FROM sbtest1 IGNORE INDEX(k_1)
  36. destination_hostgroup: 2
  37. cache_ttl: NULL
  38. reconnect: NULL
  39. timeout: NULL
  40. delay: NULL
  41. mirror_flagOUT: NULL
  42. mirror_hostgroup: NULL
  43. error_msg: NULL
  44. apply: 1
  45. 2 rows in set (0.00 sec)
  46.  
  47. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  48. Query OK, 0 rows affected (0.00 sec)

It is important to note that in the rule with rule_id=10 , the one the mirrored query will match against, we need to set destination_hostgroup and not mirror_hostgroup : mirror_hostgroup should be set only for the original query in order to immediately specify where the mirror query should be sent, without the need of extra rules in mysql_query_rules .

Let's reset stats_mysql_query_digest:

  1. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;

And run some test from mysql client:

  1. mysql> SELECT id FROM sbtest1 ORDER BY k DESC LIMIT 3;
  2. +-------+
  3. | id |
  4. +-------+
  5. | 26372 |
  6. | 81250 |
  7. | 60085 |
  8. +-------+
  9. 3 rows in set (0.01 sec)
  10.  
  11. mysql> SELECT id,k FROM sbtest1 ORDER BY k DESC LIMIT 3;
  12. +-------+-------+
  13. | id | k |
  14. +-------+-------+
  15. | 26372 | 80626 |
  16. | 81250 | 79947 |
  17. | 60085 | 79142 |
  18. +-------+-------+
  19. 3 rows in set (0.01 sec)

Let's check stats_mysql_query_digest :

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY sum_time DESC;
  2. +-----------+------------+----------+--------------------------------------------------------------------+
  3. | hostgroup | count_star | sum_time | digest_text |
  4. +-----------+------------+----------+--------------------------------------------------------------------+
  5. | 2 | 1 | 1135673 | SELECT id,k FROM sbtest1 IGNORE INDEX(k_1) ORDER BY k DESC LIMIT ? |
  6. | 2 | 1 | 683906 | SELECT id FROM sbtest1 IGNORE INDEX(k_1) ORDER BY k DESC LIMIT ? |
  7. | 2 | 1 | 7478 | SELECT id,k FROM sbtest1 ORDER BY k DESC LIMIT ? |
  8. | 2 | 1 | 4422 | SELECT id FROM sbtest1 ORDER BY k DESC LIMIT ? |
  9. +-----------+------------+----------+--------------------------------------------------------------------+
  10. 4 rows in set (0.00 sec)

Table stats_mysql_query_digest confirms that:

  • queries were mirrored
  • the original query was not rewritten
  • the mirrored query was rewritten
  • the mirrored query was much slower because ignored the index

Advanced example: use mirroring to test query rewrite

While working on mirroring I was asked a completely different question, related to query rewrite: how one could know if the given regex matches a given query, and verify that the rewrite pattern is correct?To be more specific, the problem is to understand if the rewrite is correct without affecting live traffic.Although mirroring wasn't initially designed for that, it can answer this question.

In this example, we will write a rule to match all the SELECT , "mirror" them , and try to rewrite them .

  1. Admin> DELETE FROM mysql_query_rules;
  2. Query OK, 2 rows affected (0.00 sec)
  3.  
  4. Admin> INSERT INTO mysql_query_rules (rule_id,active,match_pattern,destination_hostgroup,mirror_flagOUT,apply) VALUES (5,1,'^SELECT ',2,100,1);
  5. Query OK, 1 row affected (0.00 sec)
  6.  
  7. Admin> INSERT INTO mysql_query_rules (rule_id,active,flagIN,match_pattern,destination_hostgroup,replace_pattern,apply) VALUES (10,1,100,'^SELECT DISTINCT c FROM sbtest([0-9]{1,2}) WHERE id BETWEEN ([0-9]+) AND ([0-9]+)\+([0-9]+) ORDER BY c$',2,'SELECT DISTINCT c FROM sbtest\1 WHERE id = \3 \+ \4 ORDER BY c',1);
  8. Query OK, 1 row affected (0.00 sec)
  9.  
  10. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  11. Query OK, 0 rows affected (0.00 sec)

The regex above are quite complex, and this is why mirroring helps instead of rewriting live traffic.

Let's reset stats_mysql_query_digest:

  1. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;

And run some tests from the mysql client:

  1. mysql> SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN 10 AND 10+2 ORDER BY c;
  2. +-------------------------------------------------------------------------------------------------------------------------+
  3. | c |
  4. +-------------------------------------------------------------------------------------------------------------------------+
  5. | 41189576069-45553989496-19463022727-28789271530-61175755423-36502565636-61804003878-85903592313-68207739135-17129930980 |
  6. | 48090103407-09222928184-34050945574-85418069333-36966673537-23363106719-15284068881-04674238815-26203696337-24037044694 |
  7. | 74234360637-48574588774-94392661281-55267159983-87261567077-93953988073-73238443191-61462412385-80374300764-69242108888 |
  8. +-------------------------------------------------------------------------------------------------------------------------+
  9. 3 rows in set (0.01 sec)

The query ran successfully. As said, we didn't modify the original traffic.

What about in stats_mysql_query_digest ?

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY digest_text;
  2. +-----------+------------+----------+----------------------------------------------------------------------+
  3. | hostgroup | count_star | sum_time | digest_text |
  4. +-----------+------------+----------+----------------------------------------------------------------------+
  5. | 2 | 2 | 25461 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
  6. +-----------+------------+----------+----------------------------------------------------------------------+
  7. 1 row in set (0.00 sec)

The original query was executed twice, so something didn't run correctly.We can note that both queries were sent to hostgroup2 : we should assume that rule_id=10 was a match, but didn't rewrite the query.Let's verify it was a match:

  1. Admin> SELECT * from stats_mysql_query_rules;
  2. +---------+------+
  3. | rule_id | hits |
  4. +---------+------+
  5. | 5 | 1 |
  6. | 10 | 1 |
  7. +---------+------+
  8. 2 rows in set (0.00 sec)

Rule with rule_id=10 was a match according to hits in stats_mysql_query_rules.Then why wasn't the query rewritten? The error log has this information:

  1. re2/re2.cc:881: invalid rewrite pattern: SELECT DISTINCT c FROM sbtest\1 WHERE id = \3 \+ \4 ORDER BY c

Indeed, it is invalid syntax , let's fix this:

  1. Admin> UPDATE mysql_query_rules SET replace_pattern='SELECT DISTINCT c FROM sbtest\1 WHERE id = \3 + \4 ORDER BY c' WHERE rule_id=10;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  5. Query OK, 0 rows affected (0.00 sec)
  6.  
  7. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;

Let's re-execute the query from mysql client:

  1. mysql> SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN 10 AND 10+2 ORDER BY c;
  2. +-------------------------------------------------------------------------------------------------------------------------+
  3. | c |
  4. +-------------------------------------------------------------------------------------------------------------------------+
  5. | 41189576069-45553989496-19463022727-28789271530-61175755423-36502565636-61804003878-85903592313-68207739135-17129930980 |
  6. | 48090103407-09222928184-34050945574-85418069333-36966673537-23363106719-15284068881-04674238815-26203696337-24037044694 |
  7. | 74234360637-48574588774-94392661281-55267159983-87261567077-93953988073-73238443191-61462412385-80374300764-69242108888 |
  8. +-------------------------------------------------------------------------------------------------------------------------+
  9. 3 rows in set (0.00 sec)

And now let's verify if the query was rewritten correctly:

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY digest_text; +-----------+------------+----------+----------------------------------------------------------------------+
  2. | hostgroup | count_star | sum_time | digest_text |
  3. +-----------+------------+----------+----------------------------------------------------------------------+
  4. | 2 | 1 | 2756 | SELECT DISTINCT c FROM sbtest1 WHERE id = ? + ? ORDER BY c |
  5. | 2 | 1 | 891 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
  6. +-----------+------------+----------+----------------------------------------------------------------------+

Well, yes, the query was rewritten correctly, and was also executed :-)

Advanced example: use mirroring and firewall to test query rewrite

Taking the previous example/exercise a bit forward: is it possible to know how a query will be rewritten without executing it? YES!To achieve this, we will set error_msg for the mirrored query: in this way ProxySQL will process the query, but it will filter it without sending it to any mysql servers. As stated in the very beginning, the mirrored query can be modified, and firewall it is an example of modifying the mirrored query.

Example:

  1. Admin> UPDATE mysql_query_rules SET error_msg="random error, blah blah" WHERE rule_id=10;
  2. Query OK, 1 row affected (0.00 sec)
  3.  
  4. Admin> LOAD MYSQL QUERY RULES TO RUNTIME;
  5. Query OK, 0 rows affected (0.01 sec)
  6.  
  7. Admin> SELECT COUNT(*) FROM stats_mysql_query_digest_reset;

Let's rerun the query in mysql client:

  1. mysql> SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN 10 AND 10+2 ORDER BY c;
  2. +-------------------------------------------------------------------------------------------------------------------------+
  3. | c |
  4. +-------------------------------------------------------------------------------------------------------------------------+
  5. | 41189576069-45553989496-19463022727-28789271530-61175755423-36502565636-61804003878-85903592313-68207739135-17129930980 |
  6. | 48090103407-09222928184-34050945574-85418069333-36966673537-23363106719-15284068881-04674238815-26203696337-24037044694 |
  7. | 74234360637-48574588774-94392661281-55267159983-87261567077-93953988073-73238443191-61462412385-80374300764-69242108888 |
  8. +-------------------------------------------------------------------------------------------------------------------------+
  9. 3 rows in set (0.00 sec)

And now let's check statistics:

  1. Admin> select hostgroup,count_star,sum_time,digest_text from stats_mysql_query_digest ORDER BY digest_text; +-----------+------------+----------+----------------------------------------------------------------------+
  2. | hostgroup | count_star | sum_time | digest_text |
  3. +-----------+------------+----------+----------------------------------------------------------------------+
  4. | -1 | 1 | 0 | SELECT DISTINCT c FROM sbtest1 WHERE id = ? + ? ORDER BY c |
  5. | 2 | 1 | 3219 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN ? AND ?+? ORDER BY c |
  6. +-----------+------------+----------+----------------------------------------------------------------------+
  7. 2 rows in set (0.00 sec)
  8.  
  9. Admin> SELECT * from stats_mysql_query_rules; +---------+------+
  10. | rule_id | hits |
  11. +---------+------+
  12. | 5 | 1 |
  13. | 10 | 1 |
  14. +---------+------+
  15. 2 rows in set (0.00 sec)

Great! We know that the query was rewritten, but actually not sent anywhere:

  • sum_time=0 because the response was immediate
  • hostgroup=-1 has the special meaning of "not sent anywhere"

原文: https://github.com/sysown/proxysql/wiki/Mirroring