-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathworkload.py
More file actions
300 lines (268 loc) · 11.9 KB
/
Copy pathworkload.py
File metadata and controls
300 lines (268 loc) · 11.9 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
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
#!/usr/bin/env python3
# The MIT License (MIT)
# Copyright (c) Microsoft Corporation. All rights reserved.
"""Unified Cosmos DB workload - operations, proxy, and sync/async controlled by env vars.
Environment variables:
WORKLOAD_OPERATIONS comma-separated list of operations (default: read,write,query)
WORKLOAD_USE_PROXY route through Envoy proxy (default: false)
WORKLOAD_USE_SYNC use sync client instead of async (default: false)
WORKLOAD_PARALLEL_OPS run each enabled op-type in its own loop so a stalled op-type
(e.g. writes blocked by a regional outage) does not starve the
others (default: false; opt-in).
"""
import logging
import os
import asyncio
import time
from concurrent.futures import ThreadPoolExecutor
from azure.cosmos.aio import CosmosClient as AsyncClient
from azure.cosmos import CosmosClient as SyncClient, documents
from azure.core.pipeline.transport import AioHttpTransport
from workload_utils import *
from workload_configs import *
async def _op_loop_async(op_name, factory, client_logger):
"""Infinite loop driving a single op-type. Catches per-iteration exceptions so a failing
iteration cannot terminate the task; cancellation is honored to allow clean shutdown."""
while True:
try:
await factory()
except asyncio.CancelledError:
raise
except Exception as e:
client_logger.info("Exception in %s loop", op_name)
client_logger.error(e)
def _op_loop_sync(op_name, factory, client_logger, stop_event):
"""Sync counterpart for ThreadPoolExecutor. Exits when stop_event is set (used on
interpreter shutdown / SIGTERM via finally blocks)."""
while not stop_event.is_set():
try:
factory()
except Exception as e:
client_logger.info("Exception in %s loop", op_name)
client_logger.error(e)
async def run_workload_async(client_id, client_logger, stats=None, reporter=None):
"""Async workload loop - default mode."""
ops = WORKLOAD_OPERATIONS
use_proxy = WORKLOAD_USE_PROXY
owns_reporter = False
if stats is None:
try:
from perf_config import get_perf_config
perf_config = get_perf_config()
if perf_config["enabled"] and perf_config["results_endpoint"]:
from perf_stats import Stats
from perf_reporter import PerfReporter
stats = Stats()
reporter = PerfReporter(stats, perf_config)
reporter.start()
owns_reporter = True
except ImportError as e:
logging.getLogger(__name__).info("Perf reporting disabled: %s", e)
session = None
transport = None
try:
if use_proxy:
session = create_custom_session()
transport = AioHttpTransport(session=session, session_owner=False)
client_kwargs = dict(
preferred_locations=PREFERRED_LOCATIONS,
excluded_locations=CLIENT_EXCLUDED_LOCATIONS,
enable_diagnostics_logging=True,
logger=client_logger,
user_agent=get_user_agent(client_id),
)
if use_proxy and transport:
client_kwargs["transport"] = transport
if USE_MULTIPLE_WRITABLE_LOCATIONS:
client_kwargs["multiple_write_locations"] = True
client = AsyncClient(COSMOS_URI, COSMOS_CREDENTIAL, **client_kwargs)
if not WORKLOAD_SKIP_CLOSE:
await client.__aenter__()
try:
db = client.get_database_client(COSMOS_DATABASE)
cont = db.get_container_client(COSMOS_CONTAINER)
await asyncio.sleep(1)
if WORKLOAD_PARALLEL_OPS:
op_factories = []
if "write" in ops:
op_factories.append(("write", lambda: upsert_item_concurrently(
cont, WRITE_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats)))
if "read" in ops:
op_factories.append(("read", lambda: read_item_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats)))
if "query" in ops:
op_factories.append(("query", lambda: query_items_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_QUERIES, stats)))
if "feedrange_query" in ops:
op_factories.append(("feedrange_query", lambda: query_items_by_feed_ranges_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, stats)))
client_logger.info(
"Running %d op-types in parallel: %s",
len(op_factories), [name for name, _ in op_factories],
)
await asyncio.gather(
*(_op_loop_async(name, factory, client_logger) for name, factory in op_factories)
)
else:
while True:
try:
if "write" in ops:
await upsert_item_concurrently(
cont, WRITE_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats
)
if "read" in ops:
await read_item_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats
)
if "query" in ops:
await query_items_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_QUERIES, stats
)
if "feedrange_query" in ops:
await query_items_by_feed_ranges_concurrently(
cont, REQUEST_EXCLUDED_LOCATIONS, stats
)
except Exception as e:
client_logger.info("Exception in application layer")
client_logger.error(e)
finally:
if not WORKLOAD_SKIP_CLOSE:
await client.__aexit__(None, None, None)
finally:
if reporter and owns_reporter:
try:
reporter.stop()
except Exception:
pass
if session:
await session.close()
def run_workload_sync(client_id, client_logger):
"""Sync workload loop - used when WORKLOAD_USE_SYNC=true."""
if WORKLOAD_USE_PROXY:
raise RuntimeError("Proxy mode is not supported with sync client. "
"Set WORKLOAD_USE_SYNC=false or WORKLOAD_USE_PROXY=false.")
ops = WORKLOAD_OPERATIONS
stats = None
perf_config = None
reporter = None
try:
from perf_config import get_perf_config
perf_config = get_perf_config()
if perf_config["enabled"] and perf_config["results_endpoint"]:
from perf_stats import Stats
from perf_reporter import PerfReporter
stats = Stats()
reporter = PerfReporter(stats, perf_config)
reporter.start()
except ImportError as e:
logging.getLogger(__name__).info("Perf reporting disabled: %s", e)
try:
connection_policy = documents.ConnectionPolicy()
connection_policy.UseMultipleWriteLocations = USE_MULTIPLE_WRITABLE_LOCATIONS
with SyncClient(
COSMOS_URI,
COSMOS_CREDENTIAL,
connection_policy=connection_policy,
preferred_locations=PREFERRED_LOCATIONS,
excluded_locations=CLIENT_EXCLUDED_LOCATIONS,
enable_diagnostics_logging=True,
logger=client_logger,
user_agent=get_user_agent(client_id),
) as client:
db = client.get_database_client(COSMOS_DATABASE)
cont = db.get_container_client(COSMOS_CONTAINER)
time.sleep(1)
if WORKLOAD_PARALLEL_OPS:
import threading
stop_event = threading.Event()
op_factories = []
if "write" in ops:
op_factories.append(("write", lambda: upsert_item(
cont, WRITE_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats)))
if "read" in ops:
op_factories.append(("read", lambda: read_item(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats)))
if "query" in ops:
op_factories.append(("query", lambda: query_items(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_QUERIES, stats)))
if "feedrange_query" in ops:
op_factories.append(("feedrange_query", lambda: query_items_by_feed_ranges(
cont, REQUEST_EXCLUDED_LOCATIONS, stats)))
client_logger.info(
"Running %d op-types in parallel threads: %s",
len(op_factories), [name for name, _ in op_factories],
)
try:
with ThreadPoolExecutor(max_workers=len(op_factories) or 1) as pool:
futures = [
pool.submit(_op_loop_sync, name, factory, client_logger, stop_event)
for name, factory in op_factories
]
for fut in futures:
fut.result()
finally:
stop_event.set()
else:
while True:
try:
if "write" in ops:
upsert_item(
cont, WRITE_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats
)
if "read" in ops:
read_item(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_REQUESTS, stats
)
if "query" in ops:
query_items(
cont, REQUEST_EXCLUDED_LOCATIONS, CONCURRENT_QUERIES, stats
)
if "feedrange_query" in ops:
query_items_by_feed_ranges(
cont, REQUEST_EXCLUDED_LOCATIONS, stats
)
except Exception as e:
client_logger.info("Exception in application layer")
client_logger.error(e)
finally:
if reporter:
try:
reporter.stop()
except Exception:
pass
async def run_multi_client_async(prefix, client_logger):
"""Spawn multiple async clients in a single process with shared metrics."""
stats = None
reporter = None
try:
from perf_config import get_perf_config
perf_config = get_perf_config()
if perf_config["enabled"] and perf_config["results_endpoint"]:
from perf_stats import Stats
from perf_reporter import PerfReporter
stats = Stats()
reporter = PerfReporter(stats, perf_config)
reporter.start()
except ImportError as e:
logging.getLogger(__name__).info("Perf reporting disabled: %s", e)
try:
tasks = []
for i in range(WORKLOAD_NUM_CLIENTS):
client_id = f"{prefix}-c{i}"
tasks.append(run_workload_async(client_id, client_logger, stats=stats, reporter=reporter))
await asyncio.gather(*tasks)
finally:
if reporter:
try:
reporter.stop()
except Exception:
pass
if __name__ == "__main__":
file_name = os.path.basename(__file__)
prefix, logger = create_logger(file_name)
if WORKLOAD_USE_SYNC:
run_workload_sync(prefix, logger)
elif WORKLOAD_NUM_CLIENTS > 1:
asyncio.run(run_multi_client_async(prefix, logger))
else:
asyncio.run(run_workload_async(prefix, logger))