This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathdiff_tables.py
More file actions
227 lines (177 loc) · 8.43 KB
/
diff_tables.py
File metadata and controls
227 lines (177 loc) · 8.43 KB
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
"""Provides classes for performing a table diff
"""
import re
import time
from abc import ABC, abstractmethod
from enum import Enum
from contextlib import contextmanager
from operator import methodcaller
from typing import Tuple, Iterator, Optional
from concurrent.futures import ThreadPoolExecutor, as_completed
from runtype import dataclass
from .utils import run_as_daemon, safezip, getLogger
from .thread_utils import ThreadedYielder
from .table_segment import TableSegment
from .tracking import create_end_event_json, create_start_event_json, send_event_json, is_tracking_enabled
from .sqeleton.databases.database_types import IKey
logger = getLogger(__name__)
class Algorithm(Enum):
AUTO = "auto"
JOINDIFF = "joindiff"
HASHDIFF = "hashdiff"
DiffResult = Iterator[Tuple[str, tuple]] # Iterator[Tuple[Literal["+", "-"], tuple]]
def truncate_error(error: str):
first_line = error.split("\n", 1)[0]
return re.sub("'(.*?)'", "'***'", first_line)
@dataclass
class ThreadBase:
"Provides utility methods for optional threading"
threaded: bool = True
max_threadpool_size: Optional[int] = 1
def _thread_map(self, func, iterable):
if not self.threaded:
return map(func, iterable)
with ThreadPoolExecutor(max_workers=self.max_threadpool_size) as task_pool:
return task_pool.map(func, iterable)
def _threaded_call(self, func, iterable):
"Calls a method for each object in iterable."
return list(self._thread_map(methodcaller(func), iterable))
def _thread_as_completed(self, func, iterable):
if not self.threaded:
yield from map(func, iterable)
return
with ThreadPoolExecutor(max_workers=self.max_threadpool_size) as task_pool:
futures = [task_pool.submit(func, item) for item in iterable]
for future in as_completed(futures):
yield future.result()
def _threaded_call_as_completed(self, func, iterable):
"Calls a method for each object in iterable. Returned in order of completion."
return self._thread_as_completed(methodcaller(func), iterable)
@contextmanager
def _run_in_background(self, *funcs):
with ThreadPoolExecutor(max_workers=self.max_threadpool_size) as task_pool:
futures = [task_pool.submit(f) for f in funcs if f is not None]
yield futures
for f in futures:
f.result()
class TableDiffer(ThreadBase, ABC):
bisection_factor = 32
stats: dict = {}
def diff_tables(self, table1: TableSegment, table2: TableSegment) -> DiffResult:
"""Diff the given tables.
Parameters:
table1 (TableSegment): The "before" table to compare. Or: source table
table2 (TableSegment): The "after" table to compare. Or: target table
Returns:
An iterator that yield pair-tuples, representing the diff. Items can be either -
('-', row) for items in table1 but not in table2.
('+', row) for items in table2 but not in table1.
Where `row` is a tuple of values, corresponding to the diffed columns.
"""
if is_tracking_enabled():
options = dict(self)
options["differ_name"] = type(self).__name__
event_json = create_start_event_json(options)
run_as_daemon(send_event_json, event_json)
self.stats["diff_count"] = 0
start = time.monotonic()
error = None
try:
# Query and validate schema
table1, table2 = self._threaded_call("with_schema", [table1, table2])
self._validate_and_adjust_columns(table1, table2)
yield from self._diff_tables(table1, table2)
except BaseException as e: # Catch KeyboardInterrupt too
error = e
finally:
if is_tracking_enabled():
runtime = time.monotonic() - start
table1_count = self.stats.get("table1_count")
table2_count = self.stats.get("table2_count")
diff_count = self.stats.get("diff_count")
err_message = truncate_error(repr(error))
event_json = create_end_event_json(
error is None,
runtime,
table1.database.name,
table2.database.name,
table1_count,
table2_count,
diff_count,
err_message,
)
send_event_json(event_json)
if error:
raise error
def _validate_and_adjust_columns(self, table1: TableSegment, table2: TableSegment) -> DiffResult:
pass
def _diff_tables(self, table1: TableSegment, table2: TableSegment) -> DiffResult:
return self._bisect_and_diff_tables(table1, table2)
@abstractmethod
def _diff_segments(
self,
ti: ThreadedYielder,
table1: TableSegment,
table2: TableSegment,
max_rows: int,
level=0,
segment_index=None,
segment_count=None,
):
...
def _bisect_and_diff_tables(self, table1, table2):
if len(table1.key_columns) > 1:
raise NotImplementedError("Composite key not supported yet!")
if len(table2.key_columns) > 1:
raise NotImplementedError("Composite key not supported yet!")
(key1,) = table1.key_columns
(key2,) = table2.key_columns
key_type = table1._schema[key1]
key_type2 = table2._schema[key2]
if not isinstance(key_type, IKey):
raise NotImplementedError(f"Cannot use column of type {key_type} as a key")
if not isinstance(key_type2, IKey):
raise NotImplementedError(f"Cannot use column of type {key_type2} as a key")
assert key_type.python_type is key_type2.python_type
# Query min/max values
key_ranges = self._threaded_call_as_completed("query_key_range", [table1, table2])
# Start with the first completed value, so we don't waste time waiting
min_key1, max_key1 = self._parse_key_range_result(key_type, next(key_ranges))
table1, table2 = [t.new(min_key=min_key1, max_key=max_key1) for t in (table1, table2)]
logger.info(
f"Diffing segments at key-range: {table1.min_key}..{table2.max_key}. "
f"size: table1 <= {table1.approximate_size()}, table2 <= {table2.approximate_size()}"
)
ti = ThreadedYielder(self.max_threadpool_size)
# Bisect (split) the table into segments, and diff them recursively.
ti.submit(self._bisect_and_diff_segments, ti, table1, table2)
# Now we check for the second min-max, to diff the portions we "missed".
min_key2, max_key2 = self._parse_key_range_result(key_type, next(key_ranges))
if min_key2 < min_key1:
pre_tables = [t.new(min_key=min_key2, max_key=min_key1) for t in (table1, table2)]
ti.submit(self._bisect_and_diff_segments, ti, *pre_tables)
if max_key2 > max_key1:
post_tables = [t.new(min_key=max_key1, max_key=max_key2) for t in (table1, table2)]
ti.submit(self._bisect_and_diff_segments, ti, *post_tables)
return ti
def _parse_key_range_result(self, key_type, key_range):
mn, mx = key_range
cls = key_type.make_value
# We add 1 because our ranges are exclusive of the end (like in Python)
try:
return cls(mn), cls(mx) + 1
except (TypeError, ValueError) as e:
raise type(e)(f"Cannot apply {key_type} to '{mn}', '{mx}'.") from e
def _bisect_and_diff_segments(
self, ti: ThreadedYielder, table1: TableSegment, table2: TableSegment, level=0, max_rows=None
):
assert table1.is_bounded and table2.is_bounded
# Choose evenly spaced checkpoints (according to min_key and max_key)
biggest_table = max(table1, table2, key=methodcaller("approximate_size"))
checkpoints = biggest_table.choose_checkpoints(self.bisection_factor - 1)
# Create new instances of TableSegment between each checkpoint
segmented1 = table1.segment_by_checkpoints(checkpoints)
segmented2 = table2.segment_by_checkpoints(checkpoints)
# Recursively compare each pair of corresponding segments between table1 and table2
for i, (t1, t2) in enumerate(safezip(segmented1, segmented2)):
ti.submit(self._diff_segments, ti, t1, t2, max_rows, level + 1, i + 1, len(segmented1), priority=level)