forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_flavor.py
193 lines (146 loc) · 5.34 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
#!/usr/bin/env python
import environment
import logging
import os
import subprocess
class MysqlFlavor(object):
"""Base class with default SQL statements."""
def promote_slave_commands(self):
"""Returns commands to convert a slave to a master."""
return [
"STOP SLAVE",
"RESET SLAVE",
"RESET MASTER",
]
def reset_replication_commands(self):
"""Returns commands to reset replication settings."""
return [
"STOP SLAVE",
"RESET SLAVE",
"RESET MASTER",
]
def change_master_commands(self, host, port, pos):
raise NotImplementedError()
def extra_my_cnf(self):
"""Returns the path to an extra my_cnf file, or None."""
return None
def bootstrap_archive(self):
"""Returns the name of the bootstrap archive for mysqlctl.
Name is relative to vitess/data/bootstrap/.
"""
return "mysql-db-dir.tbz"
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 position_append(self, pos, gtid):
"""Returns a new position with the given GTID appended."""
raise NotImplementedError()
def enable_binlog_checksum(self, tablet):
"""Enables binlog_checksum and returns True if the flavor supports it.
Arg:
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",
"RESET MASTER",
"SET GLOBAL gtid_slave_pos = ''",
]
def extra_my_cnf(self):
return environment.vttop + "/config/mycnf/master_mariadb.cnf"
def bootstrap_archive(self):
return "mysql-db-dir_10.0.13-MariaDB.tbz"
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 position_append(self, pos, gtid):
if self.position_at_least(pos, gtid):
return pos
else:
return gtid
def change_master_commands(self, host, port, pos):
(_flavor, gtid) = pos.split("/")
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 bootstrap_archive(self):
return "mysql-db-dir_5.6.24.tbz"
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 position_append(self, pos, gtid):
return "MySQL56/" + subprocess.check_output([
"mysqlctl", "position", "append", pos, gtid,
]).strip()
def extra_my_cnf(self):
return environment.vttop + "/config/mycnf/master_mysql56.cnf"
def change_master_commands(self, host, port, pos):
(_flavor, gtid) = pos.split("/")
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)]
__mysql_flavor = None
# mysql_flavor is a function because we need something to import before the
# actual __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):
global __mysql_flavor
if not flavor:
flavor = os.environ.get("MYSQL_FLAVOR", "MariaDB")
# The environment variable might be set, but equal to "".
if flavor == "":
flavor = "MariaDB"
# Set the environment variable explicitly in case we're overriding it via
# command-line flag.
os.environ["MYSQL_FLAVOR"] = flavor
if flavor == "MariaDB":
__mysql_flavor = MariaDB()
elif flavor == "MySQL56":
__mysql_flavor = MySQL56()
else:
logging.error("Unknown MYSQL_FLAVOR '%s'", flavor)
exit(1)
logging.debug("Using MYSQL_FLAVOR=%s", str(flavor))