refactor(firestore) : comprehensive refactoring for FirestoreSessionService#5663
refactor(firestore) : comprehensive refactoring for FirestoreSessionService#5663Dumeng wants to merge 7 commits into
Conversation
|
Hi @Dumeng, Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
|
Hi @xuanyang15 , can you please review this. |
|
@DeanChensj Could you please help review? |
|
@gemini-cli /review |
|
🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
|
🤖 I'm sorry @DeanChensj, but I was unable to process your request. Please see the logs for more details. |
There was a problem hiding this comment.
The changes in this PR significantly improve the efficiency of the FirestoreSessionService by using concurrent fetches (asyncio.gather) and batching user state retrievals (get_all). The refactoring of imports and type hints also makes the code cleaner.
I've reviewed the logic for session creation, retrieval, and event appending, and it looks solid. The use of revisions for optimistic concurrency control is correctly implemented.
Great work!
| ) | ||
| app_doc = await app_ref.get() | ||
|
|
||
| events_docs, app_doc, user_doc = await asyncio.gather( |
There was a problem hiding this comment.
Using asyncio.gather to fetch events, app state, and user state concurrently is a great performance improvement.
| async def _with_session_lock( | ||
| self, *, app_name: str, user_id: str, session_id: str | ||
| ) -> AsyncIterator[None]: | ||
| ) -> AsyncGenerator[None]: |
There was a problem hiding this comment.
Using AsyncGenerator is more appropriate here than AsyncIterator since it's an async context manager that yields. Good catch.
| users_docs = await users_ref.get() | ||
| for u_doc in users_docs: | ||
| user_states_map[u_doc.id] = u_doc.to_dict() | ||
| unique_user_ids = {s["userId"] for s in sessions_data if "userId" in s} |
There was a problem hiding this comment.
The batch fetch using get_all for unique user IDs is much more efficient than fetching all users or fetching them one by one.
| transaction_obj = self.client.transaction() | ||
| new_revision_count = await _append_txn(transaction_obj) | ||
| session._storage_update_marker = str(new_revision_count) | ||
| session.last_update_time = event.timestamp |
There was a problem hiding this comment.
Updating session.last_update_time here ensures the local session object is in sync with the storage update.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
N/A
2. Or, if no issue exists, describe the change:
If applicable, please follow the issue templates to provide as much detail as
possible.
Problem:
This Pull Request introduces comprehensive refactoring, performance optimizations, and robustness improvements to the
FirestoreSessionService. It simplifies the core implementation, aligns state handling logic with other session services, fixes side-effects in transactions, and significantly improves read performance for bulk operations.Solution:
Optimized Batch Retrieval: Updated
list_sessionsto leverage Firestore's nativeclient.get_all(...)method. This fetches multiple user state documents in a single optimized RPC request rather than individual sequential reads, significantly reducing read latency and overhead.Aligned State Handling: Refactored state management logic to match the established, robust patterns found in DatabaseSessionService.
Transaction Safety: Removed unsafe side-effects inside transaction callbacks within
append_eventto ensure idempotency and reliability during automatic Firestore transaction retries.Revision Tracking: Explicitly initialized session revision tracking to 0 upon session creation to guarantee monotonic tracking consistency.
Simplified Implementation: Streamlined internal data extraction and simplified redundant record lookups for improved long-term maintainability.
Import Standards: Resorted and updated import statements to strictly adhere to the ADK project's relative import conventions and code style guide.
Testing Plan
Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.
Unit Tests:
Please include a summary of passed
pytestresults.Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.
Checklist
Additional context
Add any other context or screenshots about the feature request here.