forked from u2mejc/redis-to-cluster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·498 lines (393 loc) · 15.2 KB
/
main.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import re
import sys
import time
import queue
import logging
import argparse
import threading
import redis
import pytool
import rediscluster
def connect_redis(conn):
""" Return a redis client. """
# Don't pass empty password to the client
if not conn.get('password', None):
conn.pop('password', None)
return redis.StrictRedis(**conn)
def connect_redis_cluster(conn):
""" Return a Redis cluster client. """
# Don't pass empty password to the client
if not conn.get('password', None):
conn.pop('password', None)
# Add option to avoid this check
conn['skip_full_coverage_check'] = True
return rediscluster.StrictRedisCluster(**conn)
def get_client(conn):
"""
Return a Redis client object based on the *conn* dictionary.
:param dict conn: Dictionary of connection info as returned by *parse_url*.
"""
# No database indicates a cluster connection
if not conn.get('db', None):
conn.pop('db', None)
return connect_redis_cluster(conn)
# Otherwise it's a regular redis connection
return connect_redis(conn)
def parse_url(url):
"""
Return *url* parsed into a dictionary for consumption by the Redis client
objects.
:param str url: URL connection string
"""
# Expected URL format string (for error messages)
# http://www.iana.org/assignments/uri-schemes/prov/redis
expected = ('<schema>://(:password)@<host>:<port>/(db) (exclude db number '
'for cluster mode)')
# Make sure we can parse the key bits of the URL
try:
schema = re.search('^(.*)://', url).group(1)
host = re.search('://(:.*@)*(.*):', url).group(2)
port = re.search('://(:.*@)*.*:(.*)/', url).group(2)
except Exception:
raise argparse.ArgumentTypeError(f'URL format: {expected}')
# Toggle SSL if we have a secure schema
ssl = (schema == 'rediss')
# Parse the database number from the connection string
db = re.search(r':.*/(\d+$)', url)
if db is None:
Logger().info(f'Using cluster mode for {host}')
else:
db = db.group(1)
# Parse the password from the connection string
password = re.search('://:(.*)@', url)
if password is None:
Logger().info(f'No password set for {host}')
else:
password = password.group(1)
return {'ssl': ssl,
'password': password,
'host': host,
'port': port,
'db': db}
@pytool.lang.singleton
class Logger:
""" Logging wrapper for easy use in threads. """
def __init__(self, level=logging.INFO, filename=None):
# Configure global logging
logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s",
level=level, filename=filename)
# Get a logger for us to use
self.logger = logging.getLogger('redis-migrate')
self.logger.setLevel(level)
# Map logger attributes up
self.info = self.logger.info
self.debug = self.logger.debug
self.error = self.logger.error
self.ttl_log = open('ttl.log', 'a')
self.ttl_lock = threading.Lock()
def ttl(self, key):
with self.ttl_lock:
self.ttl_log.write(str(key) + '\n')
@pytool.lang.singleton
class Metrics:
def __init__(self, prefix, total, frequency=25000):
self.prefix = prefix
self.total = total
self.frequency = frequency
self.lock = threading.Lock()
self.copied = 0
self.errored = 0
self.log = Logger()
self.timer = pytool.time.Timer()
def count(self):
with self.lock:
self.copied += 1
if not self.copied % self.frequency:
self.output_stats()
def error(self):
with self.lock:
self.errored += 1
def output_stats(self):
""" Prints stats to logging. """
elapsed = self.timer.elapsed.total_seconds()
count = self.copied + self.errored
total = self.total
# Time per key in milliseconds
avg = round(elapsed / count * 1000, 3)
# Time remaining in seconds
remaining = 1.0 * elapsed / count * (total - count)
# Time remaining in minutes
remaining = round(remaining / 60.0, 1)
# Time taken in minutes
elapsed = round(elapsed / 60.0, 1)
self.log.info(f"{self.prefix}: {avg}ms avg, {elapsed}min passed, "
f"{remaining}min remaining. ({count:,}/{total:,})")
class Worker(threading.Thread):
def __init__(self, key_queue, src, dest, log):
self.queue = key_queue
self.src = src
self.dest = dest
self.log = log
super().__init__()
def run(self):
""" Consume the *key_queue*, migrating keys. """
metrics = Metrics()
count = 0
while not self.queue.empty():
count += 1
try:
key = self.queue.get(timeout=1)
except queue.Empty:
continue
try:
self.copy_key(key)
metrics.count()
except Exception as err:
self.log.error(f"Error for key '{key}'")
self.log.debug(err, exc_info=True)
metrics.error()
self.log.info(f"Thread completed. {count} keys processed.")
def copy_key(self, key):
""" Copy *key* from the source to the destination. """
# Get the TTL for the key
ttl = self.src.ttl(key)
# -2 means the key doesn't actually exist and is a lie
if ttl == -2:
# self.log.debug(f"TTL -2: {key}")
return
# -1 means the key has no expiration and we set it to 90 days
if ttl == -1:
# self.log.debug(f"TTL -1: {key}")
ttl = 60*60*24*90
self.log.ttl(key)
# restore uses TTL in ms
ttl = ttl * 1000
# Get the original value
value = self.src.dump(key)
# Set the key on our destination
self.dest.restore(key, ttl, value, replace=True)
class Migrate:
# Redis connection objects
_src = None
_dest = None
def __init__(self, prefix, source, destination, workers=10,
overwrite=False):
self.prefix = prefix
self.source_url = source
self.dest_url = destination
self.workers = workers
self.overwrite = overwrite
self.queue = queue.Queue()
self.log = Logger()
self.src_keys = set()
self.dest_keys = set()
def run(self):
self.log.info("Starting migration.")
# Get all the keys and store them
timer = pytool.time.Timer()
# Retrieve source keys in a thread for parallelism
get_src = threading.Thread(target=self.get_src_keys)
get_src.start()
if not self.overwrite:
# Retrieve destination keys in a Thread
get_dest = threading.Thread(target=self.get_dest_keys)
get_dest.start()
get_dest.join()
# Wait for source retrieval to finish
get_src.join()
self.log.info(f"Time to retrieve keys: {timer.elapsed}")
# Find the difference in the destination keys
timer.mark()
self.keys = set(self.src_keys) - set(self.dest_keys)
self.log.debug(f"Time to compute set: {timer.mark()}")
self.log.info(f"Keys to process: {len(self.keys):,}")
self.log.info("Populating queue.")
# Populate the threadsafe queue with keys
for key in self.keys:
self.queue.put(key)
self.log.debug(f"Time to populate queue: {timer.mark()}")
self.log.debug(f"Keys in queue: {self.queue.qsize():,}")
# Create metrics
self.metrics = Metrics(self.prefix, len(self.keys))
# Create worker threadpool
timer.mark()
self.log.debug(f"Creating {self.workers} workers.")
self.pool = [Worker(self.queue, self.src, self.dest, self.log)
for i in range(self.workers)]
self.log.debug(f"Time to create workers: {timer.mark()}")
# Start all the worker threads
self.log.info("Starting workers.")
for worker in self.pool:
worker.start()
self.log.info(f"Startup time: {timer.elapsed}")
# Wait for workers to finish
self.log.info("Processing.")
for worker in self.pool:
worker.join()
self.log.info(f"Copy time: {timer.mark()}")
self.log.info(f"Total time taken: {timer.elapsed}")
self.log.info(f"Total keys migraterd: {len(self.keys)}")
def get_src_keys(self):
""" Populate the src keys from a thread. """
timer = pytool.time.Timer()
self.log.info("Querying source keys.")
self.src_keys = self.src.keys(self.prefix)
self.log.info(f"Retrieve source keys: {timer.elapsed}")
self.log.info(f"Found {len(self.src_keys):,} source keys.")
def get_dest_keys(self):
""" Populate the destination keys from a thread. """
timer = pytool.time.Timer()
self.log.info("Querying destination keys.")
self.dest_keys = self.dest.keys(self.prefix)
self.log.info(f"Retrieve destination keys: {timer.elapsed}")
self.log.info(f"Found {len(self.dest_keys):,} destination keys.")
@property
def src(self):
""" Return a cached client connection object. """
if self._src:
return self._src
# Parse and create a new client
conn = parse_url(self.source_url)
client = get_client(conn)
self._src = client
return self._src
@property
def dest(self):
""" Return a cached client connection object. """
if self._dest:
return self._dest
# Parse and create a new client
conn = parse_url(self.dest_url)
client = get_client(conn)
self._dest = client
return self._dest
class DeleteWorker(threading.Thread):
def __init__(self, key_queue, dest, log):
self.queue = key_queue
self.dest = dest
self.log = log
super().__init__()
def run(self):
""" Consume the *key_queue*, migrating keys. """
metrics = Metrics()
count = 0
while not self.queue.empty():
count += 1
try:
key = self.queue.get(timeout=1)
except queue.Empty:
continue
try:
self.delete_key(key)
metrics.count()
except Exception as err:
self.log.error(f"Error for key '{key}'")
self.log.debug(err, exc_info=True)
metrics.error()
self.log.info(f"Thread completed. {count} keys processed.")
def delete_key(self, key):
""" Delete the key from the destination. """
self.dest.delete(key)
class Delete:
_dest = None
def __init__(self, prefix, destination, workers=10):
self.prefix = prefix
self.dest_url = destination
self.queue = queue.Queue()
self.workers = workers
self.log = Logger()
def run(self):
if not self.prefix or self.prefix == '*':
self.log.error("Cowardly refusing to delete everything.")
sys.exit(1)
timer = pytool.time.Timer()
# Get all our keys
keys = self.dest.keys(self.prefix)
self.log.info(f"Time to retrieve keys: {timer.mark()}")
total = len(keys)
self.log.info(f"Running DELETE against '{self.prefix}' "
f"{self.dest_url}")
self.log.info(f"There are {total:,} keys to delete.")
sample = '\n '.join(str(k) for k in keys[:10])
self.log.info(f"Sample keys:\n {sample}")
self.log.info("Kill this process now if you don't want to proceed.\n"
" ... Sleeping for 10 seconds while you decide.")
time.sleep(10)
timer.mark()
self.log.info("Populating queue.")
# Delete all the keys one by one...
for key in keys:
self.queue.put(key)
self.log.debug(f"Time to populate queue: {timer.mark()}")
self.log.debug(f"Keys in queue: {self.queue.qsize():,}")
# Create metrics
self.metrics = Metrics(self.prefix, total)
# Create worker threadpool
timer.mark()
self.log.debug(f"Creating {self.workers} workers.")
self.pool = [DeleteWorker(self.queue, self.dest, self.log)
for i in range(self.workers)]
self.log.debug(f"Time to create workers: {timer.mark()}")
# Start all the worker threads
self.log.info("Starting workers.")
for worker in self.pool:
worker.start()
self.log.info(f"Startup time: {timer.elapsed}")
# Wait for workers to finish
self.log.info("Processing.")
for worker in self.pool:
worker.join()
self.log.info(f"Copy time: {timer.mark()}")
self.log.info(f"Total time taken: {timer.elapsed}")
self.log.info(f"Total keys deleted: {total}")
self.log.info("Finished.")
@property
def dest(self):
""" Return a cached client connection object. """
if self._dest:
return self._dest
# Parse and create a new client
conn = parse_url(self.dest_url)
client = get_client(conn)
self._dest = client
return self._dest
class Main(pytool.cmd.Command):
def set_opts(self):
self.describe("Simple script to migrate Redis database key data, in a "
"non destructive way.")
self.opt('--source', '-s', required=True, help="source Redis server / "
"cluster")
self.opt('--destination', '-d', required=True, help="destination "
"Redis server / cluster")
self.opt('--workers', '-w', type=int, default=10,
help="Number of workers")
self.opt('--logging', '-l', default='info',
choices=['debug', 'info', 'error'],
help="Set log level")
self.opt('--overwrite', action='store_true',
help="Overwrite keys instead of skipping existing")
self.opt('--delete-dest', action='store_true',
help="Do not migrate, delete destination keys")
self.opt('--logfile', '-f', type=str, help="Log to file")
self.opt('--prefix', '-p', default="*", help="source key prefix ")
def run(self):
self.log = Logger(getattr(logging, self.args.logging.upper()),
self.args.logfile)
self.log.debug("Logging configured successfully.")
self.log.info(f"Using prefix: {self.args.prefix}")
if self.args.delete_dest:
deleter = Delete(self.args.prefix, self.args.destination,
self.args.workers)
deleter.run()
return
migrate = Migrate(self.args.prefix, self.args.source,
self.args.destination, self.args.workers,
self.args.overwrite)
self.log.debug(f"Created migration runner.\n"
f"prefix = {self.args.prefix}\n"
f"workers = {self.args.workers}\n"
f"overwrite = {self.args.overwrite}")
migrate.run()
if __name__ == '__main__':
Main().start(sys.argv[1:])