/
__init__.py
715 lines (539 loc) · 26.3 KB
/
__init__.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#!/usr/bin/env python3
# pylint: disable=missing-docstring,too-many-locals
import collections
import itertools
import copy
import pendulum
import pymysql
import singer
import singer.metrics as metrics
from singer import metadata, Schema, get_logger
from singer.catalog import Catalog, CatalogEntry
import tap_mysql.sync_strategies.binlog as binlog
import tap_mysql.sync_strategies.common as common
import tap_mysql.sync_strategies.full_table as full_table
import tap_mysql.sync_strategies.incremental as incremental
from tap_mysql.connection import connect_with_backoff, MySQLConnection
LOGGER = get_logger('tap_mysql')
Column = collections.namedtuple('Column', [
"table_schema",
"table_name",
"column_name",
"data_type",
"character_maximum_length",
"numeric_precision",
"numeric_scale",
"column_type",
"column_key"])
REQUIRED_CONFIG_KEYS = [
'host',
'port',
'user',
'password'
]
pymysql.converters.conversions[pendulum.Pendulum] = pymysql.converters.escape_datetime
STRING_TYPES = {'char', 'enum', 'longtext', 'mediumtext', 'text', 'varchar'}
BYTES_FOR_INTEGER_TYPE = {
'tinyint': 1,
'smallint': 2,
'mediumint': 3,
'int': 4,
'bigint': 8
}
FLOAT_TYPES = {'float', 'double'}
DATETIME_TYPES = {'datetime', 'timestamp', 'date', 'time'}
BINARY_TYPES = {'binary', 'varbinary'}
def schema_for_column(column):
"""Returns the Schema object for the given Column."""
data_type = column.data_type.lower()
column_type = column.column_type.lower()
inclusion = 'available'
# We want to automatically include all primary key columns
if column.column_key.lower() == 'pri':
inclusion = 'automatic'
result = Schema(inclusion=inclusion)
if data_type == 'bit' or column_type.startswith('tinyint(1)'):
result.type = ['null', 'boolean']
elif data_type in BYTES_FOR_INTEGER_TYPE:
result.type = ['null', 'integer']
bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8
if 'unsigned' in column.column_type:
result.minimum = 0
result.maximum = 2 ** bits - 1
else:
result.minimum = 0 - 2 ** (bits - 1)
result.maximum = 2 ** (bits - 1) - 1
elif data_type in FLOAT_TYPES:
result.type = ['null', 'number']
elif data_type == 'json':
result.type = ['null', 'object']
elif data_type == 'decimal':
result.type = ['null', 'number']
result.multipleOf = 10 ** (0 - column.numeric_scale)
return result
elif data_type in STRING_TYPES:
result.type = ['null', 'string']
result.maxLength = column.character_maximum_length
elif data_type in DATETIME_TYPES:
result.type = ['null', 'string']
result.format = 'date-time'
elif data_type in BINARY_TYPES:
result.type = ['null', 'string']
result.format = 'binary'
else:
result = Schema(None,
inclusion='unsupported',
description=f'Unsupported column type {column_type}')
return result
def create_column_metadata(cols):
mdata = {}
mdata = metadata.write(mdata, (), 'selected-by-default', False)
for col in cols:
schema = schema_for_column(col)
mdata = metadata.write(mdata,
('properties', col.column_name),
'selected-by-default',
schema.inclusion != 'unsupported')
mdata = metadata.write(mdata,
('properties', col.column_name),
'sql-datatype',
col.column_type.lower())
return metadata.to_list(mdata)
def discover_catalog(mysql_conn, config):
"""Returns a Catalog describing the structure of the database."""
filter_dbs_config = config.get('filter_dbs')
if filter_dbs_config:
filter_dbs_clause = ",".join([f"'{db_name}'" for db_name in filter_dbs_config.split(",")])
table_schema_clause = f"WHERE table_schema IN ({filter_dbs_clause})"
else:
table_schema_clause = """
WHERE table_schema NOT IN (
'information_schema',
'performance_schema',
'mysql',
'sys'
)"""
with connect_with_backoff(mysql_conn) as open_conn:
with open_conn.cursor() as cur:
cur.execute(f"""
SELECT table_schema,
table_name,
table_type,
table_rows
FROM information_schema.tables
{table_schema_clause}
""")
table_info = {}
for (db_name, table, table_type, rows) in cur.fetchall():
if db_name not in table_info:
table_info[db_name] = {}
table_info[db_name][table] = {
'row_count': rows,
'is_view': table_type == 'VIEW'
}
cur.execute(f"""
SELECT table_schema,
table_name,
column_name,
data_type,
character_maximum_length,
numeric_precision,
numeric_scale,
column_type,
column_key
FROM information_schema.columns
{table_schema_clause}
ORDER BY table_schema, table_name
""")
columns = []
rec = cur.fetchone()
while rec is not None:
columns.append(Column(*rec))
rec = cur.fetchone()
entries = []
for (k, cols) in itertools.groupby(columns, lambda c: (c.table_schema, c.table_name)):
cols = list(cols)
(table_schema, table_name) = k
schema = Schema(type='object',
properties={c.column_name: schema_for_column(c) for c in cols})
mdata = create_column_metadata(cols)
md_map = metadata.to_map(mdata)
md_map = metadata.write(md_map,
(),
'database-name',
table_schema)
is_view = table_info[table_schema][table_name]['is_view']
if table_schema in table_info and table_name in table_info[table_schema]:
row_count = table_info[table_schema][table_name].get('row_count')
if row_count is not None:
md_map = metadata.write(md_map,
(),
'row-count',
row_count)
md_map = metadata.write(md_map,
(),
'is-view',
is_view)
column_is_key_prop = lambda c, s: (c.column_key == 'PRI' and
s.properties[c.column_name].inclusion != 'unsupported')
key_properties = [c.column_name for c in cols if column_is_key_prop(c, schema)]
if not is_view:
md_map = metadata.write(md_map,
(),
'table-key-properties',
key_properties)
entry = CatalogEntry(
table=table_name,
stream=table_name,
metadata=metadata.to_list(md_map),
tap_stream_id=common.generate_tap_stream_id(table_schema, table_name),
schema=schema)
entries.append(entry)
return Catalog(entries)
def do_discover(mysql_conn, config):
discover_catalog(mysql_conn, config).dump()
def desired_columns(selected, table_schema):
'''Return the set of column names we need to include in the SELECT.
selected - set of column names marked as selected in the input catalog
table_schema - the most recently discovered Schema for the table
'''
all_columns = set()
available = set()
automatic = set()
unsupported = set()
for column, column_schema in table_schema.properties.items():
all_columns.add(column)
inclusion = column_schema.inclusion
if inclusion == 'automatic':
automatic.add(column)
elif inclusion == 'available':
available.add(column)
elif inclusion == 'unsupported':
unsupported.add(column)
else:
raise Exception('Unknown inclusion ' + inclusion)
selected_but_unsupported = selected.intersection(unsupported)
if selected_but_unsupported:
LOGGER.warning(
'Columns %s were selected but are not supported. Skipping them.',
selected_but_unsupported)
selected_but_nonexistent = selected.difference(all_columns)
if selected_but_nonexistent:
LOGGER.warning(
'Columns %s were selected but do not exist.',
selected_but_nonexistent)
not_selected_but_automatic = automatic.difference(selected)
if not_selected_but_automatic:
LOGGER.warning(
'Columns %s are primary keys but were not selected. Adding them.',
not_selected_but_automatic)
return selected.intersection(available).union(automatic)
def log_engine(mysql_conn, catalog_entry):
is_view = common.get_is_view(catalog_entry)
database_name = common.get_database_name(catalog_entry)
if is_view:
LOGGER.info("Beginning sync for view %s.%s", database_name, catalog_entry.table)
else:
with connect_with_backoff(mysql_conn) as open_conn:
with open_conn.cursor() as cur:
cur.execute("""
SELECT engine
FROM information_schema.tables
WHERE table_schema = %s
AND table_name = %s
""", (database_name, catalog_entry.table))
row = cur.fetchone()
if row:
LOGGER.info("Beginning sync for %s table %s.%s",
row[0],
database_name,
catalog_entry.table)
def is_valid_currently_syncing_stream(selected_stream, state):
stream_metadata = metadata.to_map(selected_stream.metadata)
replication_method = stream_metadata.get((), {}).get('replication-method')
if replication_method != 'LOG_BASED':
return True
if replication_method == 'LOG_BASED' and binlog_stream_requires_historical(selected_stream, state):
return True
return False
def binlog_stream_requires_historical(catalog_entry, state):
log_file = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'log_file')
log_pos = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'log_pos')
max_pk_values = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'max_pk_values')
last_pk_fetched = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'last_pk_fetched')
if (log_file and log_pos) and (not max_pk_values and not last_pk_fetched):
return False
return True
def resolve_catalog(discovered_catalog, streams_to_sync):
result = Catalog(streams=[])
# Iterate over the streams in the input catalog and match each one up
# with the same stream in the discovered catalog.
for catalog_entry in streams_to_sync:
catalog_metadata = metadata.to_map(catalog_entry.metadata)
replication_key = catalog_metadata.get((), {}).get('replication-key')
discovered_table = discovered_catalog.get_stream(catalog_entry.tap_stream_id)
database_name = common.get_database_name(catalog_entry)
if not discovered_table:
LOGGER.warning('Database %s table %s was selected but does not exist',
database_name, catalog_entry.table)
continue
selected = {k for k, v in catalog_entry.schema.properties.items()
if common.property_is_selected(catalog_entry, k) or k == replication_key}
# These are the columns we need to select
columns = desired_columns(selected, discovered_table.schema)
result.streams.append(CatalogEntry(
tap_stream_id=catalog_entry.tap_stream_id,
metadata=catalog_entry.metadata,
stream=catalog_entry.tap_stream_id,
table=catalog_entry.table,
schema=Schema(
type='object',
properties={col: discovered_table.schema.properties[col]
for col in columns}
)
))
return result
def get_non_binlog_streams(mysql_conn, catalog, config, state):
'''Returns the Catalog of data we're going to sync for all SELECT-based
streams (i.e. INCREMENTAL, FULL_TABLE, and LOG_BASED that require a historical
sync). LOG_BASED streams that require a historical sync are inferred from lack
of any state.
Using the Catalog provided from the input file, this function will return a
Catalog representing exactly which tables and columns that will be emitted
by SELECT-based syncs. This is achieved by comparing the input Catalog to a
freshly discovered Catalog to determine the resulting Catalog.
The resulting Catalog will include the following any streams marked as
"selected" that currently exist in the database. Columns marked as "selected"
and those labled "automatic" (e.g. primary keys and replication keys) will be
included. Streams will be prioritized in the following order:
1. currently_syncing if it is SELECT-based
2. any streams that do not have state
3. any streams that do not have a replication method of LOG_BASED
'''
discovered = discover_catalog(mysql_conn, config)
# Filter catalog to include only selected streams
selected_streams = list(filter(common.stream_is_selected, catalog.streams))
streams_with_state = []
streams_without_state = []
for stream in selected_streams:
stream_metadata = metadata.to_map(stream.metadata)
replication_method = stream_metadata.get((), {}).get('replication-method')
stream_state = state.get('bookmarks', {}).get(stream.tap_stream_id)
if not stream_state:
if replication_method == 'LOG_BASED':
LOGGER.info("LOG_BASED stream %s requires full historical sync", stream.tap_stream_id)
streams_without_state.append(stream)
elif stream_state and replication_method == 'LOG_BASED' and binlog_stream_requires_historical(stream, state):
is_view = common.get_is_view(stream)
if is_view:
raise Exception(
f"Unable to replicate stream({stream.stream}) with binlog because it is a view.")
LOGGER.info("LOG_BASED stream %s will resume its historical sync", stream.tap_stream_id)
streams_with_state.append(stream)
elif stream_state and replication_method != 'LOG_BASED':
streams_with_state.append(stream)
# If the state says we were in the middle of processing a stream, skip
# to that stream. Then process streams without prior state and finally
# move onto streams with state (i.e. have been synced in the past)
currently_syncing = singer.get_currently_syncing(state)
# prioritize streams that have not been processed
ordered_streams = streams_without_state + streams_with_state
if currently_syncing:
currently_syncing_stream = list(filter(
lambda s: s.tap_stream_id == currently_syncing and is_valid_currently_syncing_stream(s, state),
streams_with_state))
non_currently_syncing_streams = list(filter(lambda s: s.tap_stream_id != currently_syncing, ordered_streams))
streams_to_sync = currently_syncing_stream + non_currently_syncing_streams
else:
# prioritize streams that have not been processed
streams_to_sync = ordered_streams
return resolve_catalog(discovered, streams_to_sync)
def get_binlog_streams(mysql_conn, catalog, config, state):
discovered = discover_catalog(mysql_conn, config)
selected_streams = list(filter(common.stream_is_selected, catalog.streams))
binlog_streams = []
for stream in selected_streams:
stream_metadata = metadata.to_map(stream.metadata)
replication_method = stream_metadata.get((), {}).get('replication-method')
if replication_method == 'LOG_BASED' and not binlog_stream_requires_historical(stream, state):
binlog_streams.append(stream)
return resolve_catalog(discovered, binlog_streams)
def write_schema_message(catalog_entry, bookmark_properties=None):
if bookmark_properties is None:
bookmark_properties = []
key_properties = common.get_key_properties(catalog_entry)
singer.write_message(singer.SchemaMessage(
stream=catalog_entry.stream,
schema=catalog_entry.schema.to_dict(),
key_properties=key_properties,
bookmark_properties=bookmark_properties
))
def do_sync_incremental(mysql_conn, catalog_entry, state, columns):
LOGGER.info("Stream %s is using incremental replication", catalog_entry.stream)
md_map = metadata.to_map(catalog_entry.metadata)
replication_key = md_map.get((), {}).get('replication-key')
if not replication_key:
raise Exception(
f"Cannot use INCREMENTAL replication for table ({catalog_entry.stream}) without a replication key.")
write_schema_message(catalog_entry=catalog_entry,
bookmark_properties=[replication_key])
incremental.sync_table(mysql_conn, catalog_entry, state, columns)
singer.write_message(singer.StateMessage(value=copy.deepcopy(state)))
def do_sync_historical_binlog(mysql_conn, config, catalog_entry, state, columns):
binlog.verify_binlog_config(mysql_conn)
is_view = common.get_is_view(catalog_entry)
if is_view:
raise Exception(f"Unable to replicate stream({catalog_entry.stream}) with binlog because it is a view.")
log_file = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'log_file')
log_pos = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'log_pos')
max_pk_values = singer.get_bookmark(state,
catalog_entry.tap_stream_id,
'max_pk_values')
write_schema_message(catalog_entry)
stream_version = common.get_stream_version(catalog_entry.tap_stream_id, state)
if log_file and log_pos and max_pk_values:
LOGGER.info("Resuming initial full table sync for LOG_BASED stream %s", catalog_entry.tap_stream_id)
full_table.sync_table(mysql_conn, catalog_entry, state, columns, stream_version)
else:
LOGGER.info("Performing initial full table sync for LOG_BASED stream %s", catalog_entry.tap_stream_id)
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'initial_binlog_complete',
False)
current_log_file, current_log_pos = binlog.fetch_current_log_file_and_pos(mysql_conn)
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'version',
stream_version)
if full_table.pks_are_auto_incrementing(mysql_conn, catalog_entry):
# We must save log_file and log_pos across FULL_TABLE syncs when using
# an incrementing PK
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'log_file',
current_log_file)
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'log_pos',
current_log_pos)
full_table.sync_table(mysql_conn, catalog_entry, state, columns, stream_version)
else:
full_table.sync_table(mysql_conn, catalog_entry, state, columns, stream_version)
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'log_file',
current_log_file)
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'log_pos',
current_log_pos)
def do_sync_full_table(mysql_conn, catalog_entry, state, columns):
LOGGER.info("Stream %s is using full table replication", catalog_entry.stream)
write_schema_message(catalog_entry)
stream_version = common.get_stream_version(catalog_entry.tap_stream_id, state)
full_table.sync_table(mysql_conn, catalog_entry, state, columns, stream_version)
# Prefer initial_full_table_complete going forward
singer.clear_bookmark(state, catalog_entry.tap_stream_id, 'version')
state = singer.write_bookmark(state,
catalog_entry.tap_stream_id,
'initial_full_table_complete',
True)
singer.write_message(singer.StateMessage(value=copy.deepcopy(state)))
def sync_non_binlog_streams(mysql_conn, non_binlog_catalog, config, state):
for catalog_entry in non_binlog_catalog.streams:
columns = list(catalog_entry.schema.properties.keys())
if not columns:
LOGGER.warning('There are no columns selected for stream %s, skipping it.', catalog_entry.stream)
continue
state = singer.set_currently_syncing(state, catalog_entry.tap_stream_id)
# Emit a state message to indicate that we've started this stream
singer.write_message(singer.StateMessage(value=copy.deepcopy(state)))
md_map = metadata.to_map(catalog_entry.metadata)
replication_method = md_map.get((), {}).get('replication-method')
database_name = common.get_database_name(catalog_entry)
with metrics.job_timer('sync_table') as timer:
timer.tags['database'] = database_name
timer.tags['table'] = catalog_entry.table
log_engine(mysql_conn, catalog_entry)
if replication_method == 'INCREMENTAL':
do_sync_incremental(mysql_conn, catalog_entry, state, columns)
elif replication_method == 'LOG_BASED':
do_sync_historical_binlog(mysql_conn, config, catalog_entry, state, columns)
elif replication_method == 'FULL_TABLE':
do_sync_full_table(mysql_conn, catalog_entry, state, columns)
else:
raise Exception("only INCREMENTAL, LOG_BASED, and FULL TABLE replication methods are supported")
state = singer.set_currently_syncing(state, None)
singer.write_message(singer.StateMessage(value=copy.deepcopy(state)))
def sync_binlog_streams(mysql_conn, binlog_catalog, config, state):
if binlog_catalog.streams:
for stream in binlog_catalog.streams:
write_schema_message(stream)
with metrics.job_timer('sync_binlog'):
binlog.sync_binlog_stream(mysql_conn, config, binlog_catalog.streams, state)
def do_sync(mysql_conn, config, catalog, state):
non_binlog_catalog = get_non_binlog_streams(mysql_conn, catalog, config, state)
binlog_catalog = get_binlog_streams(mysql_conn, catalog, config, state)
sync_non_binlog_streams(mysql_conn, non_binlog_catalog, config, state)
sync_binlog_streams(mysql_conn, binlog_catalog, config, state)
def log_server_params(mysql_conn):
with connect_with_backoff(mysql_conn) as open_conn:
try:
with open_conn.cursor() as cur:
cur.execute('''
SELECT VERSION() as version,
@@session.wait_timeout as wait_timeout,
@@session.innodb_lock_wait_timeout as innodb_lock_wait_timeout,
@@session.max_allowed_packet as max_allowed_packet,
@@session.interactive_timeout as interactive_timeout''')
row = cur.fetchone()
LOGGER.info('Server Parameters: ' +
'version: %s, ' +
'wait_timeout: %s, ' +
'innodb_lock_wait_timeout: %s, ' +
'max_allowed_packet: %s, ' +
'interactive_timeout: %s',
*row)
with open_conn.cursor() as cur:
cur.execute('''
show session status where Variable_name IN ('Ssl_version', 'Ssl_cipher')''')
rows = cur.fetchall()
mapped_row = {r[0]: r[1] for r in rows}
LOGGER.info(
'Server SSL Parameters(blank means SSL is not active): [ssl_version: %s], [ssl_cipher: %s]',
mapped_row['Ssl_version'], mapped_row['Ssl_cipher'])
except pymysql.err.InternalError as exc:
LOGGER.warning("Encountered error checking server params. Error: (%s) %s", *exc.args)
def main_impl():
args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS)
mysql_conn = MySQLConnection(args.config)
log_server_params(mysql_conn)
if args.discover:
do_discover(mysql_conn, args.config)
elif args.catalog:
state = args.state or {}
do_sync(mysql_conn, args.config, args.catalog, state)
elif args.properties:
catalog = Catalog.from_dict(args.properties)
state = args.state or {}
do_sync(mysql_conn, args.config, catalog, state)
else:
LOGGER.info("No properties were selected")
def main():
try:
main_impl()
except Exception as exc:
LOGGER.critical(exc)
raise exc