Skip to content

Query Service AIO #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ydb/aio/query/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ async def __aenter__(self) -> "AsyncResponseContextIterator":
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
# To close stream on YDB it is necessary to scroll through it to the end
async for _ in self:
pass
4 changes: 3 additions & 1 deletion ydb/aio/query/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ async def _check_session_status_loop(self) -> None:
self._state.reset()
self._state._change_state(QuerySessionStateEnum.CLOSED)
except Exception:
pass
if not self._state._already_in(QuerySessionStateEnum.CLOSED):
self._state.reset()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why do you need reset session state before set closed _state._state?
  2. When the session will be really closed (call DeleteSession and detach from attached stream?

self._state._change_state(QuerySessionStateEnum.CLOSED)

async def delete(self) -> None:
"""WARNING: This API is experimental and could be changed.
Expand Down
4 changes: 2 additions & 2 deletions ydb/aio/query/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def __aexit__(self, *args, **kwargs):
Closes a transaction context manager and rollbacks transaction if
it is not finished explicitly
"""
self._ensure_prev_stream_finished()
await self._ensure_prev_stream_finished()
if self._tx_state._state == QueryTxStateEnum.BEGINED:
# It's strictly recommended to close transactions directly
# by using commit_tx=True flag while executing statement or by
Expand All @@ -42,7 +42,7 @@ async def __aexit__(self, *args, **kwargs):

async def _ensure_prev_stream_finished(self) -> None:
if self._prev_stream is not None:
async for _ in self._prev_stream:
async with self._prev_stream:
pass
self._prev_stream = None

Expand Down
1 change: 1 addition & 0 deletions ydb/query/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __enter__(self) -> "SyncResponseContextIterator":
return self

def __exit__(self, exc_type, exc_val, exc_tb):
# To close stream on YDB it is necessary to scroll through it to the end
for _ in self:
pass

Expand Down
4 changes: 3 additions & 1 deletion ydb/query/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ def _check_session_status_loop(self, status_stream: _utilities.SyncResponseItera
self._state.reset()
self._state._change_state(QuerySessionStateEnum.CLOSED)
except Exception:
pass
if not self._state._already_in(QuerySessionStateEnum.CLOSED):
self._state.reset()
self._state._change_state(QuerySessionStateEnum.CLOSED)

def delete(self) -> None:
"""WARNING: This API is experimental and could be changed.
Expand Down
62 changes: 31 additions & 31 deletions ydb/query/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,6 @@ def __init__(self, driver, session_state, session, tx_mode):
self.session = session
self._prev_stream = None

def __enter__(self) -> "BaseQueryTxContext":
"""
Enters a context manager and returns a transaction

:return: A transaction instance
"""
return self

def __exit__(self, *args, **kwargs):
"""
Closes a transaction context manager and rollbacks transaction if
it is not finished explicitly
"""
self._ensure_prev_stream_finished()
if self._tx_state._state == QueryTxStateEnum.BEGINED:
# It's strictly recommended to close transactions directly
# by using commit_tx=True flag while executing statement or by
# .commit() or .rollback() methods, but here we trying to do best
# effort to avoid useless open transactions
logger.warning("Potentially leaked tx: %s", self._tx_state.tx_id)
try:
self.rollback()
except issues.Error:
logger.warning("Failed to rollback leaked tx: %s", self._tx_state.tx_id)

@property
def session_id(self) -> str:
"""
Expand Down Expand Up @@ -304,12 +279,6 @@ def _execute_call(
_apis.QueryService.ExecuteQuery,
)

def _ensure_prev_stream_finished(self) -> None:
if self._prev_stream is not None:
for _ in self._prev_stream:
pass
self._prev_stream = None

def _move_to_beginned(self, tx_id: str) -> None:
if self._tx_state._already_in(QueryTxStateEnum.BEGINED):
return
Expand All @@ -323,6 +292,37 @@ def _move_to_commited(self) -> None:


class QueryTxContextSync(BaseQueryTxContext):
def __enter__(self) -> "BaseQueryTxContext":
"""
Enters a context manager and returns a transaction

:return: A transaction instance
"""
return self

def __exit__(self, *args, **kwargs):
"""
Closes a transaction context manager and rollbacks transaction if
it is not finished explicitly
"""
self._ensure_prev_stream_finished()
if self._tx_state._state == QueryTxStateEnum.BEGINED:
# It's strictly recommended to close transactions directly
# by using commit_tx=True flag while executing statement or by
# .commit() or .rollback() methods, but here we trying to do best
# effort to avoid useless open transactions
logger.warning("Potentially leaked tx: %s", self._tx_state.tx_id)
try:
self.rollback()
except issues.Error:
logger.warning("Failed to rollback leaked tx: %s", self._tx_state.tx_id)

def _ensure_prev_stream_finished(self) -> None:
if self._prev_stream is not None:
with self._prev_stream:
pass
self._prev_stream = None

def begin(self, settings: Optional[base.QueryClientSettings] = None) -> "QueryTxContextSync":
"""WARNING: This API is experimental and could be changed.

Expand Down
Loading