-
Notifications
You must be signed in to change notification settings - Fork 453
/
workload.py
228 lines (198 loc) · 6.93 KB
/
workload.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
# 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
import time
from collections.abc import Iterator
from typing import Any
import pg8000
from materialize.data_ingest.data_type import DATA_TYPES_FOR_AVRO, DATA_TYPES_FOR_KEY
from materialize.data_ingest.definition import (
Delete,
Insert,
Keyspace,
Records,
RecordSize,
Upsert,
)
from materialize.data_ingest.executor import PgExecutor, PrintExecutor
from materialize.data_ingest.field import Field
from materialize.data_ingest.transaction import Transaction
from materialize.data_ingest.transaction_def import (
RestartMz,
TransactionDef,
TransactionSize,
)
from materialize.mzcompose.composition import Composition
from materialize.util import all_subclasses
class Workload:
cycle: list[TransactionDef]
def __init__(self, composition: Composition | None) -> None:
raise NotImplementedError
def generate(self, fields: list[Field]) -> Iterator[Transaction]:
while True:
for transaction_def in self.cycle:
for transaction in transaction_def.generate(fields):
if transaction:
yield transaction
class SingleSensorUpdating(Workload):
def __init__(self, composition: Composition | None) -> None:
self.cycle = [
TransactionDef(
[
Upsert(
keyspace=Keyspace.SINGLE_VALUE,
count=Records.ONE,
record_size=RecordSize.SMALL,
)
]
)
]
class SingleSensorUpdatingDisruptions(Workload):
def __init__(self, composition: Composition | None) -> None:
self.cycle = [
TransactionDef(
[
Upsert(
keyspace=Keyspace.SINGLE_VALUE,
count=Records.ONE,
record_size=RecordSize.SMALL,
),
]
),
]
if composition:
self.cycle.append(RestartMz(composition, probability=0.1))
class DeleteDataAtEndOfDay(Workload):
def __init__(self, composition: Composition | None) -> None:
insert = Insert(
count=Records.SOME,
record_size=RecordSize.SMALL,
)
insert_phase = TransactionDef(
size=TransactionSize.HUGE,
operations=[insert],
)
# Delete all records in a single transaction
delete_phase = TransactionDef(
[
Delete(
number_of_records=Records.ALL,
record_size=RecordSize.SMALL,
num=insert.max_key(),
)
]
)
self.cycle = [
insert_phase,
delete_phase,
]
class DeleteDataAtEndOfDayDisruptions(Workload):
def __init__(self, composition: Composition | None) -> None:
insert = Insert(
count=Records.SOME,
record_size=RecordSize.SMALL,
)
insert_phase = TransactionDef(
size=TransactionSize.HUGE,
operations=[insert],
)
# Delete all records in a single transaction
delete_phase = TransactionDef(
[
Delete(
number_of_records=Records.ALL,
record_size=RecordSize.SMALL,
num=insert.max_key(),
)
]
)
self.cycle = [
insert_phase,
delete_phase,
]
if composition:
self.cycle.append(RestartMz(composition, probability=0.1))
# TODO: Implement
# class ProgressivelyEnrichRecords(Workload):
# def __init__(self) -> None:
# self.cycle: list[Definition] = [
# ]
WORKLOADS = all_subclasses(Workload)
def execute_workload(
executor_classes: list[Any],
workload: Workload,
num: int,
ports: dict[str, int],
runtime: int,
verbose: bool,
) -> None:
fields = []
for i in range(random.randint(1, 10)):
fields.append(Field(f"key{i}", random.choice(DATA_TYPES_FOR_KEY), True))
for i in range(random.randint(0, 20)):
fields.append(Field(f"value{i}", random.choice(DATA_TYPES_FOR_AVRO), False))
print(f"With fields: {fields}")
executors = [
executor_class(num, ports, fields, "materialize")
for executor_class in [PgExecutor] + executor_classes
]
pg_executor = executors[0]
start = time.time()
run_executors = ([PrintExecutor(ports)] if verbose else []) + executors
for exe in run_executors:
exe.create()
for i, transaction in enumerate(workload.generate(fields)):
duration = time.time() - start
if duration > runtime:
print(f"Ran {i} transactions in {duration} s")
assert i > 0
break
for executor in run_executors:
executor.run(transaction)
order_str = ", ".join(str(i + 1) for i in range(len(fields)))
with pg_executor.pg_conn.cursor() as cur:
cur.execute(f"SELECT * FROM {pg_executor.table} ORDER BY {order_str}")
expected_result = cur.fetchall()
print(f"Expected (via Postgres): {expected_result}")
# Reconnect as Mz disruptions may have destroyed the previous connection
conn = pg8000.connect(
host="localhost",
port=ports["materialized"],
user="materialize",
database="materialize",
)
for executor in executors:
correct_once = False
sleep_time = 0.1
while sleep_time < 60:
conn.autocommit = True
with conn.cursor() as cur:
try:
cur.execute(f"SELECT * FROM {executor.table} ORDER BY {order_str}")
except:
print(f"Comparing against {type(executor).__name__} failed")
raise
actual_result = cur.fetchall()
conn.autocommit = False
if actual_result == expected_result:
if correct_once:
break
print(
"Results match. Check for correctness again to make sure the result is stable"
)
correct_once = True
time.sleep(sleep_time)
continue
else:
print(f"Unexpected ({type(executor).__name__}): {actual_result}")
print(f"Results don't match, sleeping for {sleep_time}s")
time.sleep(sleep_time)
sleep_time *= 2
else:
raise ValueError(f"Unexpected result {actual_result} != {expected_result}") # type: ignore