-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathexecutor.py
477 lines (399 loc) · 15.6 KB
/
executor.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
__all__ = [
"ApiExecutor",
"DefaultApiExecutor",
"AsyncApiExecutor",
"BatchApiExecutor",
"TransactionApiExecutor",
"OverloadControlApiExecutor",
]
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
from os import cpu_count
from typing import Any, Callable, Optional, Sequence, Tuple, TypeVar, Union
from arango.connection import Connection
from arango.exceptions import (
AsyncExecuteError,
BatchStateError,
OverloadControlExecutorError,
TransactionAbortError,
TransactionCommitError,
TransactionFetchError,
TransactionInitError,
TransactionStatusError,
)
from arango.job import AsyncJob, BatchJob
from arango.request import Request
from arango.response import Response
from arango.typings import Fields, Json
ApiExecutor = Union[
"DefaultApiExecutor",
"AsyncApiExecutor",
"BatchApiExecutor",
"TransactionApiExecutor",
"OverloadControlApiExecutor",
]
T = TypeVar("T")
class DefaultApiExecutor:
"""Default API executor.
:param connection: HTTP connection.
:type connection: arango.connection.BasicConnection |
arango.connection.JwtConnection | arango.connection.JwtSuperuserConnection
"""
def __init__(self, connection: Connection) -> None:
self._conn = connection
@property
def context(self) -> str:
return "default"
def execute(self, request: Request, response_handler: Callable[[Response], T]) -> T:
"""Execute an API request and return the result.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: API execution result.
"""
resp = self._conn.send_request(request)
return response_handler(resp)
class AsyncApiExecutor:
"""Async API Executor.
:param connection: HTTP connection.
:type connection: arango.connection.BasicConnection |
arango.connection.JwtConnection | arango.connection.JwtSuperuserConnection
:param return_result: If set to True, API executions return instances of
:class:`arango.job.AsyncJob` and results can be retrieved from server
once available. If set to False, API executions return None and no
results are stored on server.
:type return_result: bool
"""
def __init__(self, connection: Connection, return_result: bool) -> None:
self._conn = connection
self._return_result = return_result
@property
def context(self) -> str:
return "async"
def execute(
self, request: Request, response_handler: Callable[[Response], T]
) -> Optional[AsyncJob[T]]:
"""Execute an API request asynchronously.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: Async job or None if **return_result** parameter was set to
False during initialization.
:rtype: arango.job.AsyncJob | None
"""
if self._return_result:
request.headers["x-arango-async"] = "store"
else:
request.headers["x-arango-async"] = "true"
resp = self._conn.send_request(request)
if not resp.is_success:
raise AsyncExecuteError(resp, request)
if not self._return_result:
return None
job_id = resp.headers["x-arango-async-id"]
return AsyncJob(self._conn, job_id, response_handler)
class BatchApiExecutor:
"""Batch API executor.
:param connection: HTTP connection.
:param return_result: If set to True, API executions return instances of
:class:`arango.job.BatchJob` that are populated with results on commit.
If set to False, API executions return None and no results are tracked
client-side.
:type return_result: bool
:param max_workers: Use a thread pool of at most `max_workers`. If None,
the default value is the number of CPUs. For backwards compatibility,
the default value is 1, effectively behaving like single-threaded
execution.
:type max_workers: Optional[int]
"""
def __init__(
self,
connection: Connection,
return_result: bool,
max_workers: Optional[int] = 1,
) -> None:
self._conn = connection
self._return_result: bool = return_result
self._queue: OrderedDict[str, Tuple[Request, BatchJob[Any]]] = OrderedDict()
self._committed: bool = False
self._max_workers: int = max_workers or cpu_count() # type: ignore
@property
def context(self) -> str:
return "batch"
@property
def jobs(self) -> Optional[Sequence[BatchJob[Any]]]:
"""Return the queued batch jobs.
:return: Batch jobs or None if **return_result** parameter was set to
False during initialization.
:rtype: [arango.job.BatchJob] | None
"""
if not self._return_result:
return None
return [job for _, job in self._queue.values()]
def execute(
self, request: Request, response_handler: Callable[[Response], T]
) -> Optional[BatchJob[T]]:
"""Place the request in the batch queue.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: Batch job or None if **return_result** parameter was set to
False during initialization.
:rtype: arango.job.BatchJob | None
:raise arango.exceptions.BatchStateError: If batch was already
committed.
"""
if self._committed:
raise BatchStateError("batch already committed")
job = BatchJob(response_handler)
self._queue[job.id] = (request, job)
return job if self._return_result else None
def commit(self) -> Optional[Sequence[BatchJob[Any]]]:
"""Execute the queued requests in a batch of requests.
If **return_result** parameter was set to True during initialization,
:class:`arango.job.BatchJob` instances are populated with results.
:return: Batch jobs or None if **return_result** parameter was set to
False during initialization.
:rtype: [arango.job.BatchJob] | None
:raise arango.exceptions.BatchStateError: If batch state is invalid
(e.g. batch was already committed).
"""
if self._committed:
raise BatchStateError("batch already committed")
else:
self._committed = True
if len(self._queue) == 0:
return self.jobs
with ThreadPoolExecutor(
max_workers=min(self._max_workers, len(self._queue))
) as executor:
for req, job in self._queue.values():
job._future = executor.submit(self._conn.send_request, req)
for _, job in self._queue.values():
job._status = "done"
if not self._return_result:
return None
return self.jobs
class TransactionApiExecutor:
"""Executes transaction API requests.
:param connection: HTTP connection.
:param read: Name(s) of collections read during transaction. Read-only
collections are added lazily but should be declared if possible to
avoid deadlocks.
:type read: str | [str]
:param write: Name(s) of collections written to during transaction with
shared access.
:type write: str | [str]
:param exclusive: Name(s) of collections written to during transaction
with exclusive access.
:type exclusive: str | [str]
:param sync: Block until operation is synchronized to disk.
:type sync: bool | None
:param allow_implicit: Allow reading from undeclared collections.
:type allow_implicit: bool
:param lock_timeout: Timeout for waiting on collection locks. If not given,
a default value is used. Setting it to 0 disables the timeout.
:type lock_timeout: int
:param max_size: Max transaction size in bytes.
:type max_size: int
:param allow_dirty_read: Allow reads from followers in a cluster.
:type allow_dirty_read: bool | None
:param transaction_id: Initialize using an existing transaction instead of starting
a new transaction.
:type transaction_id: str | None
:param skip_fast_lock_round: Whether to disable fast locking for write operations.
:type skip_fast_lock_round: bool | None
"""
def __init__(
self,
connection: Connection,
read: Optional[Fields] = None,
write: Optional[Fields] = None,
exclusive: Optional[Fields] = None,
sync: Optional[bool] = None,
allow_implicit: Optional[bool] = None,
lock_timeout: Optional[int] = None,
max_size: Optional[int] = None,
allow_dirty_read: bool = False,
transaction_id: Optional[str] = None,
skip_fast_lock_round: Optional[bool] = None,
) -> None:
self._conn = connection
collections: Json = {}
if read is not None:
collections["read"] = read
if write is not None:
collections["write"] = write
if exclusive is not None:
collections["exclusive"] = exclusive
data: Json = {"collections": collections}
if sync is not None:
data["waitForSync"] = sync
if allow_implicit is not None:
data["allowImplicit"] = allow_implicit
if lock_timeout is not None:
data["lockTimeout"] = lock_timeout
if max_size is not None:
data["maxTransactionSize"] = max_size
if skip_fast_lock_round is not None:
data["skipFastLockRound"] = skip_fast_lock_round
if transaction_id is None:
request = Request(
method="post",
endpoint="/_api/transaction/begin",
data=data,
headers=(
{"x-arango-allow-dirty-read": "true"} if allow_dirty_read else None
),
)
resp = self._conn.send_request(request)
if not resp.is_success:
raise TransactionInitError(resp, request)
result = resp.body["result"]
self._id: str = result["id"]
else:
self._id = transaction_id
try:
self.status()
except TransactionStatusError as err:
raise TransactionFetchError(err.response, err.request)
@property
def context(self) -> str:
return "transaction"
@property
def id(self) -> str:
"""Return the transaction ID.
:return: Transaction ID.
:rtype: str
"""
return self._id
def execute(
self,
request: Request,
response_handler: Callable[[Response], T],
allow_dirty_read: bool = False,
) -> T:
"""Execute API request in a transaction and return the result.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:param allow_dirty_read: Allow reads from followers in a cluster.
:type allow_dirty_read: bool | None
:return: API execution result.
"""
request.headers["x-arango-trx-id"] = self._id
if allow_dirty_read:
request.headers["x-arango-allow-dirty-read"] = "true"
resp = self._conn.send_request(request)
return response_handler(resp)
def status(self) -> str:
"""Return the transaction status.
:return: Transaction status.
:rtype: str
:raise arango.exceptions.TransactionStatusError: If retrieval fails.
"""
request = Request(
method="get",
endpoint=f"/_api/transaction/{self._id}",
)
resp = self._conn.send_request(request)
if resp.is_success:
return str(resp.body["result"]["status"])
raise TransactionStatusError(resp, request)
def commit(self) -> bool:
"""Commit the transaction.
:return: True if commit was successful.
:rtype: bool
:raise arango.exceptions.TransactionCommitError: If commit fails.
"""
request = Request(
method="put",
endpoint=f"/_api/transaction/{self._id}",
)
resp = self._conn.send_request(request)
if resp.is_success:
return True
raise TransactionCommitError(resp, request)
def abort(self) -> bool:
"""Abort the transaction.
:return: True if the abort operation was successful.
:rtype: bool
:raise arango.exceptions.TransactionAbortError: If abort fails.
"""
request = Request(
method="delete",
endpoint=f"/_api/transaction/{self._id}",
)
resp = self._conn.send_request(request)
if resp.is_success:
return True
raise TransactionAbortError(resp, request)
class OverloadControlApiExecutor:
"""Allows setting the maximum acceptable server-side queuing time
for client requests.
:param connection: HTTP connection.
:type connection: arango.connection.BasicConnection |
arango.connection.JwtConnection | arango.connection.JwtSuperuserConnection
:param max_queue_time_seconds: Maximum server-side queuing time in seconds.
:type max_queue_time_seconds: float
"""
def __init__(
self, connection: Connection, max_queue_time_seconds: Optional[float] = None
) -> None:
self._conn = connection
self._max_queue_time_seconds = max_queue_time_seconds
self._queue_time_seconds = 0.0
@property
def context(self) -> str: # pragma: no cover
return "overload-control"
@property
def queue_time_seconds(self) -> float:
"""Return the most recent request queuing/de-queuing time.
Defaults to 0 if no request has been sent.
:return: Server-side queuing time in seconds.
:rtype: float
"""
return self._queue_time_seconds
@property
def max_queue_time_seconds(self) -> Optional[float]:
"""Return the maximum server-side queuing time.
:return: Maximum server-side queuing time in seconds.
:rtype: Optional[float]
"""
return self._max_queue_time_seconds
@max_queue_time_seconds.setter
def max_queue_time_seconds(self, value: Optional[float]) -> None:
"""Set the maximum server-side queuing time.
Setting it to None disables the feature.
:param value: Maximum server-side queuing time in seconds.
:type value: Optional[float]
"""
self._max_queue_time_seconds = value
def execute(
self,
request: Request,
response_handler: Callable[[Response], T],
) -> T:
"""Execute an API request and return the result.
:param request: HTTP request.
:type request: arango.request.Request
:param response_handler: HTTP response handler.
:type response_handler: callable
:return: API execution result.
"""
if self._max_queue_time_seconds is not None:
request.headers["x-arango-queue-time-seconds"] = str(
self._max_queue_time_seconds
)
resp = self._conn.send_request(request)
if not resp.is_success:
raise OverloadControlExecutorError(resp, request)
if "X-Arango-Queue-Time-Seconds" in resp.headers:
self._queue_time_seconds = float(
resp.headers["X-Arango-Queue-Time-Seconds"]
)
return response_handler(resp)