|
| 1 | +import logging |
| 2 | +from typing import ( |
| 3 | + Callable, |
| 4 | + Optional, |
| 5 | + List, |
| 6 | +) |
| 7 | + |
| 8 | +from ...query import base |
| 9 | +from .session import ( |
| 10 | + QuerySessionAsync, |
| 11 | +) |
| 12 | +from ...retries import ( |
| 13 | + RetrySettings, |
| 14 | + retry_operation_async, |
| 15 | +) |
| 16 | +from ... import convert |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class QuerySessionPoolAsync: |
| 22 | + """QuerySessionPoolAsync is an object to simplify operations with sessions of Query Service.""" |
| 23 | + |
| 24 | + def __init__(self, driver: base.SupportedDriverType): |
| 25 | + """ |
| 26 | + :param driver: A driver instance |
| 27 | + """ |
| 28 | + |
| 29 | + logger.warning("QuerySessionPoolAsync is an experimental API, which could be changed.") |
| 30 | + self._driver = driver |
| 31 | + |
| 32 | + def checkout(self) -> "SimpleQuerySessionCheckoutAsync": |
| 33 | + """WARNING: This API is experimental and could be changed. |
| 34 | + Return a Session context manager, that opens session on enter and closes session on exit. |
| 35 | + """ |
| 36 | + |
| 37 | + return SimpleQuerySessionCheckoutAsync(self) |
| 38 | + |
| 39 | + async def retry_operation_async(self, callee: Callable, retry_settings: Optional[RetrySettings] = None, *args, **kwargs): |
| 40 | + """WARNING: This API is experimental and could be changed. |
| 41 | + Special interface to execute a bunch of commands with session in a safe, retriable way. |
| 42 | +
|
| 43 | + :param callee: A function, that works with session. |
| 44 | + :param retry_settings: RetrySettings object. |
| 45 | +
|
| 46 | + :return: Result sets or exception in case of execution errors. |
| 47 | + """ |
| 48 | + |
| 49 | + retry_settings = RetrySettings() if retry_settings is None else retry_settings |
| 50 | + |
| 51 | + async def wrapped_callee(): |
| 52 | + async with self.checkout() as session: |
| 53 | + return await callee(session, *args, **kwargs) |
| 54 | + |
| 55 | + return await retry_operation_async(wrapped_callee, retry_settings) |
| 56 | + |
| 57 | + async def execute_with_retries( |
| 58 | + self, query: str, retry_settings: Optional[RetrySettings] = None, *args, **kwargs |
| 59 | + ) -> List[convert.ResultSet]: |
| 60 | + """WARNING: This API is experimental and could be changed. |
| 61 | + Special interface to execute a one-shot queries in a safe, retriable way. |
| 62 | + Note: this method loads all data from stream before return, do not use this |
| 63 | + method with huge read queries. |
| 64 | +
|
| 65 | + :param query: A query, yql or sql text. |
| 66 | + :param retry_settings: RetrySettings object. |
| 67 | +
|
| 68 | + :return: Result sets or exception in case of execution errors. |
| 69 | + """ |
| 70 | + |
| 71 | + retry_settings = RetrySettings() if retry_settings is None else retry_settings |
| 72 | + |
| 73 | + async def wrapped_callee(): |
| 74 | + async with self.checkout() as session: |
| 75 | + it = await session.execute(query, *args, **kwargs) |
| 76 | + return [result_set async for result_set in it] |
| 77 | + |
| 78 | + return await retry_operation_async(wrapped_callee, retry_settings) |
| 79 | + |
| 80 | + |
| 81 | +class SimpleQuerySessionCheckoutAsync: |
| 82 | + def __init__(self, pool: QuerySessionPoolAsync): |
| 83 | + self._pool = pool |
| 84 | + self._session = QuerySessionAsync(pool._driver) |
| 85 | + |
| 86 | + async def __aenter__(self) -> base.IQuerySession: |
| 87 | + await self._session.create() |
| 88 | + return self._session |
| 89 | + |
| 90 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 91 | + await self._session.delete() |
0 commit comments