-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
336 lines (276 loc) · 9.67 KB
/
run.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
import json
import os
import time
from pprint import pprint
from time import sleep
from typing import Callable, TypedDict, TypeVar
import pymysql.cursors
from boto3 import client
from lib import query as q
STACK_NAME = os.getenv("STACK_NAME", "")
ENDPOINT_URL = os.getenv("ENDPOINT_URL")
cfn = client("cloudformation", endpoint_url=ENDPOINT_URL)
dms = client("dms", endpoint_url=ENDPOINT_URL)
kinesis = client("kinesis", endpoint_url=ENDPOINT_URL)
secretsmanager = client("secretsmanager", endpoint_url=ENDPOINT_URL)
retries = 100 if not ENDPOINT_URL else 10
retry_sleep = 5 if not ENDPOINT_URL else 1
class CfnOutput(TypedDict):
cdcTaskSecret: str
cdcTask1: str
cdcTask2: str
fullTaskSecret: str
fullTask1: str
fullTask2: str
kinesisStream: str
class Credentials(TypedDict):
host: str
port: int
username: str
password: str
dbname: str
def get_cfn_output():
stacks = cfn.describe_stacks()["Stacks"]
stack = None
for s in stacks:
if s["StackName"] == STACK_NAME:
stack = s
break
if not stack:
raise Exception(f"Stack {STACK_NAME} Not found")
outputs = stack["Outputs"]
cfn_output = CfnOutput()
for output in outputs:
cfn_output[output["OutputKey"]] = output["OutputValue"]
return cfn_output
def get_credentials(secret_arn: str) -> Credentials:
secret_value = secretsmanager.get_secret_value(SecretId=secret_arn)
credentials = Credentials(**json.loads(secret_value["SecretString"]))
if credentials["host"] == "mariadb_server":
credentials["host"] = "localhost"
return credentials
T = TypeVar("T")
def retry(
function: Callable[..., T], retries=retries, sleep=retry_sleep, **kwargs
) -> T:
raise_error = None
retries = int(retries)
for i in range(0, retries + 1):
try:
return function(**kwargs)
except Exception as error:
raise_error = error
time.sleep(sleep)
raise raise_error
def run_queries_on_mysql(
credentials: Credentials,
queries: list[str],
):
cursor = None
cnx = None
try:
cnx = pymysql.connect(
user=credentials["username"],
password=credentials["password"],
host=credentials["host"],
database=credentials["dbname"],
cursorclass=pymysql.cursors.DictCursor,
port=int(credentials["port"]),
)
cursor = cnx.cursor()
for query in queries:
cursor.execute(query)
cnx.commit()
finally:
if cursor:
cursor.close()
if cnx:
cnx.close()
def get_query_result(
credentials: Credentials,
query: str,
):
cursor = None
cnx = None
try:
cnx = pymysql.connect(
user=credentials["username"],
password=credentials["password"],
host=credentials["host"],
database=credentials["dbname"],
cursorclass=pymysql.cursors.DictCursor,
port=int(credentials["port"]),
)
cursor = cnx.cursor()
cursor.execute(query)
return cursor.fetchall()
finally:
if cursor:
cursor.close()
if cnx:
cnx.close()
def start_task(task: str):
response = dms.start_replication_task(
ReplicationTaskArn=task, StartReplicationTaskType="start-replication"
)
status = response["ReplicationTask"].get("Status")
print(f"Replication Task {task} status: {status}")
def stop_task(task: str):
response = dms.stop_replication_task(ReplicationTaskArn=task)
status = response["ReplicationTask"].get("Status")
print(f"\n Replication Task {task} status: {status}")
def wait_for_task_status(task: str, expected_status: str):
print(f"Waiting for task status {expected_status}")
def _wait_for_status():
status = dms.describe_replication_tasks(
Filters=[{"Name": "replication-task-arn", "Values": [task]}],
WithoutSettings=True,
)["ReplicationTasks"][0].get("Status")
print(f"{task=} {status=}")
assert status == expected_status
retry(_wait_for_status)
def wait_for_kinesis(stream: str, expected_count: int, threshold_timestamp: int):
print("\n\tKinesis events\n")
print("fetching Kinesis event")
shard_id = kinesis.describe_stream(StreamARN=stream)["StreamDescription"]["Shards"][
0
]["ShardId"]
shard_iterator = kinesis.get_shard_iterator(
StreamARN=stream,
ShardId=shard_id,
ShardIteratorType="TRIM_HORIZON",
)
shard_iter = shard_iterator["ShardIterator"]
all_records = []
while shard_iter is not None:
res = kinesis.get_records(ShardIterator=shard_iter, Limit=50)
shard_iter = res["NextShardIterator"]
records = res["Records"]
for r in records:
if r["ApproximateArrivalTimestamp"].timestamp() > threshold_timestamp:
all_records.append(r)
if len(all_records) >= expected_count:
break
print(f"found {len(all_records)}, {expected_count=}")
sleep(retry_sleep)
print(f"Received: {len(all_records)} events")
pprint(
[
{**json.loads(record["Data"]), "partition_key": record["PartitionKey"]}
for record in all_records
]
)
def describe_table_statistics(task_arn: str):
res = dms.describe_table_statistics(
ReplicationTaskArn=task_arn,
)
res["TableStatistics"] = sorted(
res["TableStatistics"], key=lambda x: (x["SchemaName"], x["TableName"])
)
return res
def execute_full_load(cfn_output: CfnOutput):
credentials = get_credentials(cfn_output["fullTaskSecret"])
# Full load Flow
threshold_timestamp = int(time.time())
task_1 = cfn_output["fullTask1"]
task_2 = cfn_output["fullTask2"]
stream = cfn_output["kinesisStream"]
print("*" * 12)
print("STARTING FULL LOAD FLOW")
print("*" * 12)
print(f"db endpoint: {credentials['host']}:{credentials['port']}\n")
print("\tCleaning tables")
run_queries_on_mysql(credentials, q.DROP_TABLES)
print("\tCreating tables")
run_queries_on_mysql(credentials, q.CREATE_TABLES)
print("\tInserting data")
run_queries_on_mysql(credentials, q.PRESEED_DATA)
authors = get_query_result(credentials, "SELECT first_name, last_name FROM authors")
accounts = get_query_result(
credentials, "SELECT name, account_balance FROM accounts"
)
novels = get_query_result(credentials, "SELECT title, author_id FROM novels")
print("\n\tAdded the following authors")
pprint(authors)
print("\n\tAdded the following accounts")
pprint(accounts)
print("\n\tAdded the following novels")
pprint(novels)
print("\n****Full Task 1****\n")
print("\n\tStarting Full load task 1 a%")
start_task(task_1)
wait_for_task_status(task_1, "stopped")
# 2 drops, 2 create, 1 authors, 1 accounts = 6
wait_for_kinesis(stream, 6, threshold_timestamp)
print("\n****End of Full Task 1****\n")
sleep(1)
print("\n****Full Task 2****\n")
threshold_timestamp = int(time.time())
print("\tStarting Full load task 2 novels")
start_task(task_2)
wait_for_task_status(task_2, "stopped")
# 1 drop, 1 create, 2 novels = 4
wait_for_kinesis(stream, 4, threshold_timestamp)
print("\n****End of Full Task 2****\n")
print("\n****Table Statistics****\n")
print("\tTable Statistics tasks 1")
pprint(describe_table_statistics(task_1))
print("\n\tTable Statistics tasks 2")
pprint(describe_table_statistics(task_2))
print("\tCleaning tables")
run_queries_on_mysql(credentials, q.DROP_TABLES)
def execute_cdc(cfn_output: CfnOutput):
# CDC Flow
credentials = get_credentials(cfn_output["cdcTaskSecret"])
task_1 = cfn_output["cdcTask1"]
task_2 = cfn_output["cdcTask2"]
stream = cfn_output["kinesisStream"]
print("")
print("*" * 12)
print("STARTING CDC FLOW")
print("*" * 12)
print(f"db endpoint: {credentials['host']}:{credentials['port']}\n")
run_queries_on_mysql(credentials, q.DROP_TABLES)
print("\tCreating tables")
run_queries_on_mysql(credentials, q.CREATE_TABLES)
threshold_timestamp = int(time.time())
print("Starting cdc tasks 1 table a%")
start_task(task_1)
print("Starting cdc tasks 2 table novels")
start_task(task_2)
wait_for_task_status(task_1, "running")
wait_for_task_status(task_2, "running")
print("\n****Create table events****\n")
# 2 create apply_dms_exception, 3 create
wait_for_kinesis(stream, 5, threshold_timestamp)
print("\n****End create table events****\n")
print("\n****INSERT events****\n")
sleep(1)
threshold_timestamp = int(time.time())
sleep(1)
run_queries_on_mysql(credentials, q.PRESEED_DATA)
# 1 authors, 1 accounts, 2 novels
wait_for_kinesis(stream, 4, threshold_timestamp)
print("\n****End of INSERT events****\n")
print("\n****ALTER tables events****\n")
sleep(1)
threshold_timestamp = int(time.time())
sleep(1)
run_queries_on_mysql(credentials, q.ALTER_TABLES)
wait_for_kinesis(stream, 3, threshold_timestamp)
print("\n****End of ALTER tables events****\n")
print("\n****Table Statistics****\n")
print("\tTable Statistics tasks 1")
pprint(describe_table_statistics(task_1))
print("\n\tTable Statistics tasks 2")
pprint(describe_table_statistics(task_2))
stop_task(task_1)
stop_task(task_2)
wait_for_task_status(task_1, "stopped")
wait_for_task_status(task_2, "stopped")
print("\n\tDrop tables")
run_queries_on_mysql(credentials, q.DROP_TABLES)
if __name__ == "__main__":
cfn_output = get_cfn_output()
execute_full_load(cfn_output)
execute_cdc(cfn_output)