fix(sessions): skip unnecessary FOR UPDATE lock on app/user state rows#4656
fix(sessions): skip unnecessary FOR UPDATE lock on app/user state rows#4656giulio-leone wants to merge 2 commits intogoogle:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a performance bottleneck in the session service by optimizing database locking behavior. It ensures that row-level write locks are only acquired when necessary, preventing serialization of concurrent operations that do not modify shared application or user state. This change enhances the scalability and efficiency of event processing without altering existing functional behavior. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Response from ADK Triaging Agent Hello @giulio-leone, thank you for your contribution! It looks like the Contributor License Agreement (CLA) check has failed. Before we can merge this pull request, you'll need to sign the CLA. You can find more information at https://cla.developers.google.com/. Thank you! |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a performance bottleneck by avoiding unnecessary SELECT ... FOR UPDATE locks on the app_states and user_states tables. The approach of pre-analyzing the event's state delta to conditionally acquire locks is sound and should significantly improve concurrency for append_event calls. The refactoring to compute the state deltas only once is also a good efficiency gain. I have one minor suggestion to improve the conciseness of the new logic.
| has_app_delta = False | ||
| has_user_delta = False | ||
| state_deltas = None | ||
| if event.actions and event.actions.state_delta: | ||
| state_deltas = _session_util.extract_state_delta( | ||
| event.actions.state_delta | ||
| ) | ||
| has_app_delta = bool(state_deltas.get("app")) | ||
| has_user_delta = bool(state_deltas.get("user")) |
There was a problem hiding this comment.
The logic for determining if there are app or user state deltas can be made more concise. By using a conditional expression to initialize state_deltas and then deriving the boolean flags directly, you can reduce the number of lines and improve readability without changing the logic.
state_deltas = (
_session_util.extract_state_delta(event.actions.state_delta)
if event.actions and event.actions.state_delta
else None
)
has_app_delta = bool(state_deltas and state_deltas.get("app"))
has_user_delta = bool(state_deltas and state_deltas.get("user"))167a92f to
d198ae5
Compare
DatabaseSessionService.append_event() unconditionally acquires SELECT ... FOR UPDATE on both app_states and user_states tables, even when the event carries no state delta for those scopes. Since app_states is keyed by app_name alone, all concurrent append_event calls within the same app serialize on this single row lock, even when they only carry session-scoped state (the vast majority of events). Fix: pre-analyze the event's state_delta before acquiring locks and only use FOR UPDATE when the corresponding scope actually has changes. This also avoids a redundant call to extract_state_delta later in the method. Fixes google#4655
Addresses review feedback: use a single conditional expression instead of separate variable initializations and if block.
d198ae5 to
2abcbdf
Compare
|
Closing — CLA not yet signed. Will resubmit when ready. |
Summary
Fixes #4655
DatabaseSessionService.append_event()unconditionally acquiresSELECT ... FOR UPDATEon bothapp_statesanduser_statestables, even when the event carries no state delta for those scopes.Problem
Since
app_statesis keyed byapp_namealone, all concurrentappend_eventcalls within the same app serialize on this single row lock, even when they only carry session-scoped state (the vast majority of events). Theuser_stateslock similarly serializes all calls for the same(app_name, user_id)pair.This was partially addressed by #764, which fixed the unnecessary
UPDATE(merge) side. However, theSELECT ... FOR UPDATE(row-level lock acquisition) still happened unconditionally.Fix
Pre-analyze the event's
state_deltabefore entering the transaction to determine which scopes actually need write locks:The state rows are still fetched (needed for the stale-timestamp reload path), but without the
FOR UPDATEclause when there's nothing to write.This also eliminates a redundant call to
extract_state_delta()— the deltas are now computed once and reused.Impact
app:/user:state deltas no longer block on the shared row lockappend_eventthroughput improves significantly for apps with moderate concurrency