Commit c5f014c
committed
DISTMYSQL-243 - Orchestrator uses inefficient subquery in REPLACE to update cluster aliases
Problem:
--------
Orchestrator has backend table cluster_alias to store aliases.
At certain intervals, orchestrator will update the aliases or insert new host aliases.
To do this, it uses the below query in UpdateClusterAliases():
```
replace into
cluster_alias (alias, cluster_name, last_registered)
select
suggested_cluster_alias,
cluster_name,
now()
from
database_instance
left join database_instance_downtime using (hostname, port)
where
suggested_cluster_alias!=''
/* exclude newly demoted, downtimed masters */
and ifnull(
database_instance_downtime.downtime_active = 1
and database_instance_downtime.end_timestamp > now()
and database_instance_downtime.reason = ?
, 0) = 0
order by
ifnull(last_checked <= last_seen, 0) asc,
read_only desc,
num_slave_hosts asc
```
The problem with the select query is, it will generate the duplicate records. Note that the "order" of records is important
to determine the active nodes and the alias should only be updated to active nodes.
REPLACE does this operation by doing DELETE+INSERT. REPLACE repeatedly does the same work for all the duplicated records.
This creates un-necessary work for two sub systems in InnoDB
1. Purge
2. SELECTS (ReadView)
All those delete marked records create stress on Purge.
SELECTs, when they have to build a older version of record, they have to build a long chain of old version records (using undo log).
So the un-necessary REPLACE work will create a long chain of records to be built.
Fix:
----
Since the order is important, we cannot use "GROUP BY" to remove duplicates.
Use Insert On Duplicate Key Update (IODKU) instead of REPLACE as it generates less work for InnoDB
IODKU:
------
```
INSERT INTO cluster_alias(alias, cluster_name, last_registered)
SELECT di.suggested_cluster_alias,
di.cluster_name,
now()
FROM database_instance di
LEFT JOIN database_instance_downtime did USING (hostname, port)
WHERE di.suggested_cluster_alias != ''
AND Ifnull(did.downtime_active = 1
AND did.end_timestamp > Now()
AND did.reason = 'lost-in-recovery', 0)
= 0
ORDER BY Ifnull(di.last_checked <= di.last_seen, 0) ASC,
di.read_only DESC,
di.num_slave_hosts ASC
ON DUPLICATE KEY UPDATE
alias = di.suggested_cluster_alias,
cluster_name = di.cluster_name,
last_registered = now();
```
Sample Performance numbers:
----------------------------
REPLACE:
Query OK, 14880 rows affected (16.93 sec)
Records: 7440 Duplicates: 7440 Warnings: 0
IODKU:
Query OK, 468 rows affected (6.17 sec)
Records: 7440 Duplicates: 234 Warnings: 0
Observe the number of records affected,number of duplicates affected and the time. IODKU is way better.1 parent 982c07f commit c5f014c
1 file changed
+60
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
| |||
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
140 | 177 | | |
141 | 178 | | |
142 | 179 | | |
| |||
0 commit comments