forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_flavor.py
233 lines (180 loc) · 6.98 KB
/
mysql_flavor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Define abstractions for various MySQL flavors."""
import environment
import logging
import os
import subprocess
class MysqlFlavor(object):
"""Base class with default SQL statements."""
def demote_master_commands(self):
"""Returns commands to stop the current master."""
return [
"SET GLOBAL read_only = ON",
"FLUSH TABLES WITH READ LOCK",
"UNLOCK TABLES",
]
def promote_slave_commands(self):
"""Returns commands to convert a slave to a master."""
return [
"STOP SLAVE",
"RESET SLAVE ALL",
"SET GLOBAL read_only = OFF",
]
def reset_replication_commands(self):
"""Returns commands to reset replication settings."""
return [
"STOP SLAVE",
"RESET SLAVE ALL",
"RESET MASTER",
]
def change_master_commands(self, host, port, pos):
raise NotImplementedError()
def set_semi_sync_enabled_commands(self, master=None, slave=None):
"""Returns commands to turn semi-sync on/off."""
cmds = []
if master is not None:
cmds.append("SET GLOBAL rpl_semi_sync_master_enabled = %d" % master)
if slave is not None:
cmds.append("SET GLOBAL rpl_semi_sync_slave_enabled = %d" % slave)
return cmds
def extra_my_cnf(self):
"""Returns the path to an extra my_cnf file, or None."""
return None
def master_position(self, tablet):
"""Returns the position from SHOW MASTER STATUS as a string."""
raise NotImplementedError()
def position_equal(self, a, b):
"""Returns true if position 'a' is equal to 'b'."""
raise NotImplementedError()
def position_at_least(self, a, b):
"""Returns true if position 'a' is at least as far along as 'b'."""
raise NotImplementedError()
def position_after(self, a, b):
"""Returns true if position 'a' is after 'b'."""
return self.position_at_least(a, b) and not self.position_equal(a, b)
def enable_binlog_checksum(self, tablet):
"""Enables binlog_checksum and returns True if the flavor supports it.
Args:
tablet: A tablet.Tablet object.
Returns:
False if the flavor doesn't support binlog_checksum.
"""
tablet.mquery("", "SET @@global.binlog_checksum=1")
return True
def disable_binlog_checksum(self, tablet):
"""Disables binlog_checksum if the flavor supports it."""
tablet.mquery("", "SET @@global.binlog_checksum=0")
class MariaDB(MysqlFlavor):
"""Overrides specific to MariaDB."""
def reset_replication_commands(self):
return [
"STOP SLAVE",
"RESET SLAVE ALL",
"RESET MASTER",
"SET GLOBAL gtid_slave_pos = ''",
]
def extra_my_cnf(self):
return environment.vttop + "/config/mycnf/master_mariadb.cnf"
def master_position(self, tablet):
gtid = tablet.mquery("", "SELECT @@GLOBAL.gtid_binlog_pos")[0][0]
return "MariaDB/" + gtid
def position_equal(self, a, b):
return a == b
def position_at_least(self, a, b):
# positions are MariaDB/A-B-C and we only compare C
return int(a.split("-")[2]) >= int(b.split("-")[2])
def change_master_commands(self, host, port, pos):
gtid = pos.split("/")[1]
return [
"SET GLOBAL gtid_slave_pos = '%s'" % gtid,
"CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%d, "
"MASTER_USER='vt_repl', MASTER_USE_GTID = slave_pos" %
(host, port)]
class MySQL56(MysqlFlavor):
"""Overrides specific to MySQL 5.6."""
def master_position(self, tablet):
gtid = tablet.mquery("", "SELECT @@GLOBAL.gtid_executed")[0][0]
return "MySQL56/" + gtid
def position_equal(self, a, b):
return subprocess.check_output([
"mysqlctl", "position", "equal", a, b,
]).strip() == "true"
def position_at_least(self, a, b):
return subprocess.check_output([
"mysqlctl", "position", "at_least", a, b,
]).strip() == "true"
def extra_my_cnf(self):
return environment.vttop + "/config/mycnf/master_mysql56.cnf"
def change_master_commands(self, host, port, pos):
gtid = pos.split("/")[1]
return [
"RESET MASTER",
"SET GLOBAL gtid_purged = '%s'" % gtid,
"CHANGE MASTER TO MASTER_HOST='%s', MASTER_PORT=%d, "
"MASTER_USER='vt_repl', MASTER_AUTO_POSITION = 1" %
(host, port)]
# Map of registered MysqlFlavor classes (keyed by an identifier).
flavor_map = {}
MYSQL_FLAVOR = None
# mysql_flavor is a function because we need something to import before the
# variable MYSQL_FLAVOR is initialized, since that doesn't happen until after
# the command-line options are parsed. If we make mysql_flavor a variable and
# import it before it's initialized, the module that imported it won't get the
# updated value when it's later initialized.
def mysql_flavor():
return MYSQL_FLAVOR
def set_mysql_flavor(flavor):
"""Set the object that will be returned by mysql_flavor().
If flavor is not specified, set it based on MYSQL_FLAVOR environment variable.
Args:
flavor: String of the MySQL flavor e.g. "MariaDB" or "MySQL56".
"""
global MYSQL_FLAVOR
if not flavor:
flavor = os.environ.get("MYSQL_FLAVOR", "MariaDB")
# The environment variable might be set, but equal to "".
if not flavor:
flavor = "MariaDB"
v = flavor_map.get(flavor, None)
if not v:
logging.error("Unknown MYSQL_FLAVOR '%s'", flavor)
exit(1)
cls = v["cls"]
env = v["env"]
MYSQL_FLAVOR = cls()
# Set the environment variable explicitly in case we're overriding it via
# command-line flag.
os.environ["MYSQL_FLAVOR"] = env
logging.debug("Using MySQL flavor: %s, setting MYSQL_FLAVOR=%s (%s)",
str(flavor), env, cls)
def register_flavor(flavor, cls, env):
"""Register the available MySQL flavors.
Note: We need the 'env' argument because our internal implementation is
similar to 'MariaDB' (and hence requires MYSQL_FLAVOR=MariaDB) but has its own
flavor class.
Args:
flavor: Name of the flavor (must be passed to test flag --mysql-flavor).
cls: Class which inherits MysqlFlavor and provides the implementation.
env: Value which will be used for the environment variable MYSQL_FLAVOR.
"""
if flavor in flavor_map:
old_cls = flavor_map[flavor]["cls"]
old_env = flavor_map[flavor]["env"]
logging.error("Cannot register MySQL flavor %s because class %s (env: %s)"
" is already registered for it.", flavor, old_cls, old_env)
exit(1)
flavor_map[flavor] = {"cls": cls, "env": env}
register_flavor("MariaDB", MariaDB, "MariaDB")
register_flavor("MySQL56", MySQL56, "MySQL56")