forked from cholcombe973/mysql_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify_mysql_zk.py
executable file
·382 lines (321 loc) · 14.9 KB
/
modify_mysql_zk.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/python
import argparse
import copy
import pprint
import simplejson
from lib import host_utils
from lib import mysql_lib
from lib import environment_specific
log = environment_specific.setup_logging_defaults(__name__)
chat_handler = environment_specific.BufferingChatHandler()
log.addHandler(chat_handler)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('action',
help=("What modification to make. If 'auto', the host "
"replacement log will be used to determine what "
"what role to use. Default is auto."),
choices=['add_slave', 'add_dr_slave',
'swap_master_and_slave',
'swap_slave_and_dr_slave'],
default='auto')
parser.add_argument('instance',
help='What instance to act upon')
parser.add_argument('--dry_run',
help=('Do not actually modify zk, just show '
'what would be modify'),
default=False,
action='store_true')
parser.add_argument('--dangerous',
help=('If you need to swap_master_and_slave in zk'
'outside of the failover script, that is '
'dangerous and you will need this flag.'),
default=False,
action='store_true')
args = parser.parse_args()
action = args.action
instance = host_utils.HostAddr(args.instance)
if args.dry_run:
log.removeHandler(chat_handler)
if action == 'add_slave':
add_replica_to_zk(instance, host_utils.REPLICA_ROLE_SLAVE,
args.dry_run)
elif action == 'add_dr_slave':
add_replica_to_zk(instance, host_utils.REPLICA_ROLE_DR_SLAVE,
args.dry_run)
elif action == 'swap_master_and_slave':
if args.dangerous:
swap_master_and_slave(instance, args.dry_run)
else:
raise Exception('To swap_master_and_slave in zk outside of the '
'failover script is very dangerous and the '
'--dangerous flag was not supplied.')
elif action == 'swap_slave_and_dr_slave':
swap_slave_and_dr_slave(instance, args.dry_run)
else:
raise Exception('Invalid action: {action}'.format(action=action))
def auto_add_instance_to_zk(port, dry_run):
""" Try to do right thing in adding a server to zk
Args:
port - The port of replacement instance on localhost
dry_run - If set, do not modify zk
"""
instance = host_utils.HostAddr(':'.join([host_utils.HOSTNAME, str(port)]))
try:
conn = mysql_lib.get_mysqlops_connections()
log.info('Determining replacement for port {}'.format(port))
instance_id = host_utils.get_local_instance_id()
role = determine_replacement_role(conn, instance_id)
log.info('Adding server as role: {role}'.format(role=role))
except Exception, e:
log.exception(e)
raise
add_replica_to_zk(instance, role, dry_run)
if not dry_run:
log.info('Updating host_replacement_log')
update_host_replacement_log(conn, instance_id)
def determine_replacement_role(conn, instance_id):
""" Try to determine the role an instance should be placed into
Args:
conn - A connection to the reporting server
instance - The replacement instance
Returns:
The replication role which should be either 'slave' or 'dr_slave'
"""
zk = host_utils.MysqlZookeeper()
cursor = conn.cursor()
sql = ("SELECT old_host "
"FROM mysqlops.host_replacement_log "
"WHERE new_instance = %(new_instance)s ")
params = {'new_instance': instance_id}
cursor.execute(sql, params)
log.info(cursor._executed)
result = cursor.fetchone()
if result is None:
raise Exception('Could not determine replacement host')
old_host = host_utils.HostAddr(result['old_host'])
log.info('Host to be replaced is {old_host}'
''.format(old_host=old_host.hostname))
(_, repl_type) = zk.get_replica_set_from_instance(old_host)
if repl_type == host_utils.REPLICA_ROLE_MASTER:
raise Exception('Corwardly refusing to replace a master!')
elif repl_type is None:
raise Exception('Could not determine replacement role')
else:
return repl_type
def get_zk_node_for_replica_set(kazoo_client, replica_set):
""" Figure out what node holds the configuration of a replica set
Args:
kazoo_client - A kazoo_client
replica_set - A name for a replica set
Returns:
zk_node - The node that holds the replica set
parsed_data - The deserialized data from json in the node
"""
for zk_node in [environment_specific.DS_ZK, environment_specific.GEN_ZK]:
znode_data, meta = kazoo_client.get(zk_node)
parsed_data = simplejson.loads(znode_data)
if replica_set in parsed_data:
return (zk_node, parsed_data, meta.version)
raise Exception('Could not find replica_set {replica_set} '
'in zk_nodes'.format(replica_set=replica_set))
def remove_auth(zk_record):
""" Remove passwords from zk records
Args:
zk_record - A dict which may or not have a passwd or userfield.
Returns:
A dict which if a passwd or user field is present will have the
values redacted
"""
ret = copy.deepcopy(zk_record)
if 'passwd' in ret:
ret['passwd'] = 'REDACTED'
if 'user' in ret:
ret['user'] = 'REDACTED'
return ret
def add_replica_to_zk(instance, replica_type, dry_run):
""" Add a replica to zk
Args:
instance - A hostaddr object of the replica to add to zk
replica_type - Either 'slave' or 'dr_slave'.
dry_run - If set, do not modify zk
"""
try:
if replica_type not in [host_utils.REPLICA_ROLE_DR_SLAVE,
host_utils.REPLICA_ROLE_SLAVE]:
raise Exception('Invalid value "{replica_type}" for argument '
"replica_type").format(replica_type=replica_type)
zk_local = host_utils.MysqlZookeeper()
kazoo_client = environment_specific.get_kazoo_client()
if not kazoo_client:
raise Exception('Could not get a zk connection')
log.info('Instance is {inst}'.format(inst=instance))
mysql_lib.assert_replication_sanity(instance)
mysql_lib.assert_replication_unlagged(instance, mysql_lib.REPLICATION_TOLERANCE_NORMAL)
master = mysql_lib.get_master_from_instance(instance)
if master not in zk_local.get_all_mysql_instances_by_type(host_utils.REPLICA_ROLE_MASTER):
raise Exception('Instance {master} is not a master in zk'
''.format(master=master))
log.info('Detected master of {instance} '
'as {master}'.format(instance=instance,
master=master))
(replica_set, _) = zk_local.get_replica_set_from_instance(master)
log.info('Detected replica_set as '
'{replica_set}'.format(replica_set=replica_set))
if replica_type == host_utils.REPLICA_ROLE_SLAVE:
(zk_node, parsed_data, version) = get_zk_node_for_replica_set(kazoo_client,
replica_set)
log.info('Replica set {replica_set} is held in zk_node '
'{zk_node}'.format(zk_node=zk_node,
replica_set=replica_set))
log.info('Existing config:')
log.info(pprint.pformat(remove_auth(parsed_data[replica_set])))
new_data = copy.deepcopy(parsed_data)
new_data[replica_set][host_utils.REPLICA_ROLE_SLAVE]['host'] = \
instance.hostname
new_data[replica_set][host_utils.REPLICA_ROLE_SLAVE]['port'] = \
instance.port
log.info('New config:')
log.info(pprint.pformat(remove_auth(new_data[replica_set])))
if new_data == parsed_data:
raise Exception('No change would be made to zk, '
'will not write new config')
elif dry_run:
log.info('dry_run is set, therefore not modifying zk')
else:
log.info('Pushing new configuration for '
'{replica_set}:'.format(replica_set=replica_set))
kazoo_client.set(zk_node, simplejson.dumps(new_data), version)
elif replica_type == host_utils.REPLICA_ROLE_DR_SLAVE:
znode_data, dr_meta = kazoo_client.get(environment_specific.DR_ZK)
parsed_data = simplejson.loads(znode_data)
new_data = copy.deepcopy(parsed_data)
if replica_set in parsed_data:
log.info('Existing dr config:')
log.info(pprint.pformat(remove_auth(parsed_data[replica_set])))
else:
log.info('Replica set did not previously have a dr slave')
new_data[replica_set] = \
{host_utils.REPLICA_ROLE_DR_SLAVE: {'host': instance.hostname,
'port': instance.port}}
log.info('New dr config:')
log.info(pprint.pformat(remove_auth(new_data[replica_set])))
if new_data == parsed_data:
raise Exception('No change would be made to zk, '
'will not write new config')
elif dry_run:
log.info('dry_run is set, therefore not modifying zk')
else:
log.info('Pushing new dr configuration for '
'{replica_set}:'.format(replica_set=replica_set))
kazoo_client.set(environment_specific.DR_ZK, simplejson.dumps(new_data), dr_meta.version)
else:
# we should raise an exception above rather than getting to here
pass
except Exception, e:
log.exception(e)
raise
def swap_master_and_slave(instance, dry_run):
""" Swap a master and slave in zk. Warning: this does not sanity checks
and does nothing more than update zk. YOU HAVE BEEN WARNED!
Args:
instance - An instance in the replica set. This function will figure
everything else out.
dry_run - If set, do not modify configuration.
"""
zk_local = host_utils.MysqlZookeeper()
kazoo_client = environment_specific.get_kazoo_client()
if not kazoo_client:
raise Exception('Could not get a zk connection')
log.info('Instance is {inst}'.format(inst=instance))
(replica_set, version) = zk_local.get_replica_set_from_instance(instance)
log.info('Detected replica_set as '
'{replica_set}'.format(replica_set=replica_set))
(zk_node,
parsed_data,
version) = get_zk_node_for_replica_set(kazoo_client, replica_set)
log.info('Replica set {replica_set} is held in zk_node '
'{zk_node}'.format(zk_node=zk_node,
replica_set=replica_set))
log.info('Existing config:')
log.info(pprint.pformat(remove_auth(parsed_data[replica_set])))
new_data = copy.deepcopy(parsed_data)
new_data[replica_set][host_utils.REPLICA_ROLE_MASTER] = \
parsed_data[replica_set][host_utils.REPLICA_ROLE_SLAVE]
new_data[replica_set][host_utils.REPLICA_ROLE_SLAVE] = \
parsed_data[replica_set][host_utils.REPLICA_ROLE_MASTER]
log.info('New config:')
log.info(pprint.pformat(remove_auth(new_data[replica_set])))
if new_data == parsed_data:
raise Exception('No change would be made to zk, '
'will not write new config')
elif dry_run:
log.info('dry_run is set, therefore not modifying zk')
else:
log.info('Pushing new configuration for '
'{replica_set}:'.format(replica_set=replica_set))
kazoo_client.set(zk_node, simplejson.dumps(new_data), version)
def swap_slave_and_dr_slave(instance, dry_run):
""" Swap a slave and a dr_slave in zk
Args:
instance - An instance that is either a slave or dr_slave
"""
zk_local = host_utils.MysqlZookeeper()
kazoo_client = environment_specific.get_kazoo_client()
if not kazoo_client:
raise Exception('Could not get a zk connection')
log.info('Instance is {inst}'.format(inst=instance))
(replica_set, _) = zk_local.get_replica_set_from_instance(instance)
log.info('Detected replica_set as '
'{replica_set}'.format(replica_set=replica_set))
(zk_node,
parsed_data,
version) = get_zk_node_for_replica_set(kazoo_client, replica_set)
log.info('Replica set {replica_set} is held in zk_node '
'{zk_node}'.format(zk_node=zk_node,
replica_set=replica_set))
log.info('Existing config:')
log.info(pprint.pformat(remove_auth(parsed_data[replica_set])))
new_data = copy.deepcopy(parsed_data)
dr_znode_data, dr_meta = kazoo_client.get(environment_specific.DR_ZK)
dr_parsed_data = simplejson.loads(dr_znode_data)
new_dr_data = copy.deepcopy(dr_parsed_data)
if replica_set not in parsed_data:
raise Exception('Replica set {replica_set} is not present '
'in dr_node'.format(replica_set=replica_set))
log.info('Existing dr config:')
log.info(pprint.pformat(remove_auth(dr_parsed_data[replica_set])))
new_data[replica_set][host_utils.REPLICA_ROLE_SLAVE] = \
dr_parsed_data[replica_set][host_utils.REPLICA_ROLE_DR_SLAVE]
new_dr_data[replica_set][host_utils.REPLICA_ROLE_DR_SLAVE] = \
parsed_data[replica_set][host_utils.REPLICA_ROLE_SLAVE]
log.info('New config:')
log.info(pprint.pformat(remove_auth(new_data[replica_set])))
log.info('New dr config:')
log.info(pprint.pformat(remove_auth(new_dr_data[replica_set])))
if dry_run:
log.info('dry_run is set, therefore not modifying zk')
else:
log.info('Pushing new configuration for '
'{replica_set}:'.format(replica_set=replica_set))
kazoo_client.set(zk_node, simplejson.dumps(new_data), version)
try:
kazoo_client.set(environment_specific.DR_ZK, simplejson.dumps(new_dr_data), dr_meta.version)
except:
raise Exception('DR node is incorrect due to a different change '
'blocking this change. You need to fix it yourself')
def update_host_replacement_log(conn, instance_id):
""" Mark a replacement as completed
conn - A connection to the reporting server
instance - The replacement instance
"""
cursor = conn.cursor()
sql = ("UPDATE mysqlops.host_replacement_log "
"SET is_completed = 1 "
"WHERE new_instance = %(new_instance)s ")
params = {'new_instance': instance_id}
cursor.execute(sql, params)
log.info(cursor._executed)
conn.commit()
if __name__ == "__main__":
main()