/
mysql_actions.py
233 lines (179 loc) · 7.59 KB
/
mysql_actions.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
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
import random
from textwrap import dedent
from materialize.mzcompose.composition import Composition
from materialize.mzcompose.services.mysql import MySql
from materialize.zippy.balancerd_capabilities import BalancerdIsRunning
from materialize.zippy.framework import Action, Capabilities, Capability
from materialize.zippy.mysql_capabilities import MySqlRunning, MySqlTableExists
from materialize.zippy.mz_capabilities import MzIsRunning
class MySqlStart(Action):
"""Start a MySQL instance."""
def provides(self) -> list[Capability]:
return [MySqlRunning()]
def run(self, c: Composition) -> None:
c.up("mysql")
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
DROP DATABASE IF EXISTS public;
CREATE DATABASE public;
USE public;
"""
)
)
class MySqlStop(Action):
"""Stop the MySQL instance."""
@classmethod
def requires(cls) -> set[type[Capability]]:
return {MySqlRunning}
def withholds(self) -> set[type[Capability]]:
return {MySqlRunning}
def run(self, c: Composition) -> None:
c.kill("mysql")
class MySqlRestart(Action):
"""Restart the MySql instance."""
def run(self, c: Composition) -> None:
c.kill("mysql")
c.up("mysql")
class CreateMySqlTable(Action):
"""Creates a table on the MySql instance. 50% of the tables have a PK."""
@classmethod
def requires(cls) -> set[type[Capability]]:
return {BalancerdIsRunning, MzIsRunning, MySqlRunning}
def __init__(self, capabilities: Capabilities) -> None:
this_mysql_table = MySqlTableExists(name="table" + str(random.randint(1, 10)))
existing_mysql_tables = [
t
for t in capabilities.get(MySqlTableExists)
if t.name == this_mysql_table.name
]
if len(existing_mysql_tables) == 0:
self.new_mysql_table = True
# A PK is now required for Debezium
this_mysql_table.has_pk = True
self.mysql_table = this_mysql_table
elif len(existing_mysql_tables) == 1:
self.new_mysql_table = False
self.mysql_table = existing_mysql_tables[0]
else:
assert False
super().__init__(capabilities)
def run(self, c: Composition) -> None:
if self.new_mysql_table:
primary_key = "PRIMARY KEY" if self.mysql_table.has_pk else ""
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
CREATE TABLE {self.mysql_table.name} (f1 INTEGER {primary_key});
INSERT INTO {self.mysql_table.name} VALUES ({self.mysql_table.watermarks.max});
"""
)
)
def provides(self) -> list[Capability]:
return [self.mysql_table] if self.new_mysql_table else []
class MySqlDML(Action):
"""Performs an INSERT, DELETE or UPDATE against a MySQL table."""
# We use smaller batches in Pg then in Mz because Pg will fill up much faster
MAX_BATCH_SIZE = 10000
@classmethod
def requires(cls) -> set[type[Capability]]:
return {BalancerdIsRunning, MzIsRunning, MySqlRunning, MySqlTableExists}
def __init__(self, capabilities: Capabilities) -> None:
self.mysql_table = random.choice(capabilities.get(MySqlTableExists))
self.delta = random.randint(1, MySqlDML.MAX_BATCH_SIZE)
super().__init__(capabilities)
def __str__(self) -> str:
return f"{Action.__str__(self)} {self.mysql_table.name}"
class MySqlInsert(MySqlDML):
"""Inserts rows into a MySQL table."""
def run(self, c: Composition) -> None:
prev_max = self.mysql_table.watermarks.max
self.mysql_table.watermarks.max = prev_max + self.delta
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
SET @i:={prev_max};
INSERT INTO {self.mysql_table.name} SELECT @i:=@i+1 FROM mysql.time_zone t1, mysql.time_zone t2 LIMIT {self.mysql_table.watermarks.max - prev_max};
"""
)
)
class MySqlShiftForward(MySqlDML):
"""Update all rows from a MySQL table by incrementing their values by a constant (tables without a PK only)"""
def run(self, c: Composition) -> None:
if not self.mysql_table.has_pk:
self.mysql_table.watermarks.shift(self.delta)
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
UPDATE {self.mysql_table.name} SET f1 = f1 + {self.delta};
"""
)
)
class MySqlShiftBackward(MySqlDML):
"""Update all rows from a MySQL table by decrementing their values by a constant (tables without a PK only)"""
def run(self, c: Composition) -> None:
if not self.mysql_table.has_pk:
self.mysql_table.watermarks.shift(-self.delta)
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
UPDATE {self.mysql_table.name} SET f1 = f1 - {self.delta};
"""
)
)
class MySqlDeleteFromHead(MySqlDML):
"""Delete the largest values from a MySQL table"""
def run(self, c: Composition) -> None:
self.mysql_table.watermarks.max = max(
self.mysql_table.watermarks.max - self.delta,
self.mysql_table.watermarks.min,
)
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
DELETE FROM {self.mysql_table.name} WHERE f1 > {self.mysql_table.watermarks.max};
"""
)
)
class MySqlDeleteFromTail(MySqlDML):
"""Delete the smallest values from a MySQL table"""
def run(self, c: Composition) -> None:
self.mysql_table.watermarks.min = min(
self.mysql_table.watermarks.min + self.delta,
self.mysql_table.watermarks.max,
)
c.testdrive(
dedent(
f"""
$ mysql-connect name=mysql url=mysql://root@mysql password={MySql.DEFAULT_ROOT_PASSWORD}
$ mysql-execute name=mysql
USE public;
DELETE FROM {self.mysql_table.name} WHERE f1 < {self.mysql_table.watermarks.min};
"""
)
)