adapter: reduce Lua list lock contention#384
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 significantly enhances the performance and robustness of Redis list operations executed via Lua scripts. By adopting a lazy loading and delta-based commit approach, it drastically reduces the data processed and locked for list modifications, thereby mitigating contention. Furthermore, the transaction retry logic has been made more intelligent, distinguishing between different types of retryable errors and providing clearer error reporting by normalizing internal keys. Highlights
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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant optimization to reduce lock contention for Lua list operations by implementing a lazy-loading and delta-committing mechanism. Instead of materializing entire lists, operations like LPUSH/RPUSH and LPOP/RPOP now work on deltas, minimizing the transaction's read/write set. The transaction retry logic is also enhanced with different backoff policies for write conflicts versus transaction locks, and error messages are normalized to group retries for the same logical key. The changes are complex but well-implemented and include relevant tests. I've added a couple of suggestions for improving code clarity and robustness.
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce contention in the Redis adapter’s Lua transaction path, primarily by avoiding full list materialization and by committing list changes as deltas where possible. It also refines retry behavior for retryable transaction errors by using different backoff policies and normalizing internal keys in error messages for clearer diagnostics.
Changes:
- Add
kv.ExtractTxnUserKeyto recover logical user keys from!txn|...|internal keys. - Update Redis txn retry handling with per-error retry policies and error-key normalization (list keys, TTL keys, txn-internal keys).
- Rework Lua list handling to lazily load list metadata, materialize only when required, and commit list edits via delta operations; add compatibility tests for
RPOPLPUSHbehavior and TTL preservation.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
kv/txn_keys.go |
Adds helper to extract logical user key from txn-internal key namespaces. |
adapter/redis_retry.go |
Introduces retry policies and normalizes retryable txn error keys to reduce noisy internal key strings. |
adapter/redis_retry_test.go |
Adds unit tests for retry policy selection and error normalization on internal keys. |
adapter/redis_lua_context.go |
Implements lazy list state, list materialization on-demand, and delta-based list commits to reduce write amplification/lock contention. |
adapter/redis_lua_compat_test.go |
Adds integration coverage for RPOPLPUSH list semantics and TTL preservation in Lua flows. |
| } | ||
|
|
||
| func (c *luaScriptContext) listCommitElems(key string) ([]*kv.Elem[kv.OP], error) { | ||
| func (c *luaScriptContext) stringCommitElems(key string) ([]*kv.Elem[kv.OP], error) { | ||
| st, err := c.stringState([]byte(key)) | ||
| if err != nil { |
There was a problem hiding this comment.
listCommitPlan returns preserveExisting: true for the non-materialized/delta path, which skips deleteLogicalKeyElems. If a Lua script calls deleteLogical (e.g., DEL key) and then recreates the same key as a list in the same transaction, listState will not load the persisted list meta (because c.deleted[key]=true makes keyType return redisTypeNone). The delta commit then writes only the new meta/items and leaves all previous list items (and potentially other-type internal keys) orphaned in storage. Suggest tracking an "ever deleted/reset" flag per key and forcing a full rewrite (preserveExisting=false) when a key was deleted/reset earlier in the script, even if later recreated as a list.
|
@copilot open a new pull request to apply changes based on the comments in this thread |
Co-authored-by: bootjp <1306365+bootjp@users.noreply.github.com> Agent-Logs-Url: https://github.com/bootjp/elastickv/sessions/a35c08cd-cb6d-4ea5-9605-cd91074de396
…mbers) Co-authored-by: bootjp <1306365+bootjp@users.noreply.github.com> Agent-Logs-Url: https://github.com/bootjp/elastickv/sessions/c131ce44-56b0-443c-bf86-5921e0684a69
adapter: fix orphaned list items when DEL + recreate in same Lua script
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
No description provided.