Store shared resources in Redis.#814
Merged
Merged
Conversation
This moves all the resource locking logic into a Lua script executed atomically in Redis.
Contributor
Reviewer's GuideCentralizes task resource locking in a single atomic Redis Lua script that handles both exclusive and shared resources, updates worker lock acquisition/release flows to use it, and removes previous Python-side shared-resource coordination logic. Sequence diagram for atomic Redis Lua-based task lock acquisitionsequenceDiagram
actor Worker
participant NewWorker
participant Redis
participant LuaScript as AcquireLocksLua
Worker->>NewWorker: fetch_task()
loop Iterate_candidate_tasks
NewWorker->>Redis: SET task:task_id worker_name NX EX 86400
alt task_lock_acquired
NewWorker->>LuaScript: acquire_locks(redis_conn, worker_name, exclusive_resources, shared_resources)
LuaScript->>Redis: Atomic lock operations
Redis-->>LuaScript: blocked_resource_list
LuaScript-->>NewWorker: blocked_resource_list
alt All_locks_acquired (blocked_resource_list empty)
NewWorker->>NewWorker: task._locked_resources = exclusive_resources
NewWorker->>NewWorker: task._locked_shared_resources = shared_resources
NewWorker-->>Worker: return task
else Some_resources_blocked
NewWorker->>Redis: DEL task:task_id
NewWorker->>NewWorker: blocked_resources.update(blocked_resource_list)
NewWorker->>NewWorker: continue to next task
end
else Task_lock_not_acquired
NewWorker->>NewWorker: skip task and continue
end
end
Sequence diagram for releasing exclusive and shared resource lockssequenceDiagram
actor Worker
participant NewWorker
participant Redis
participant LuaRelease as ReleaseLocksLua
Worker->>NewWorker: handle_tasks()
alt Incompatible_task
NewWorker->>NewWorker: exclusive_resources = getattr(task, _locked_resources, [])
NewWorker->>NewWorker: shared_resources = getattr(task, _locked_shared_resources, [])
alt Has_any_locked_resources
NewWorker->>LuaRelease: release_resource_locks(redis_conn, worker_name, exclusive_resources, shared_resources)
LuaRelease->>Redis: Atomic unlock operations
Redis-->>LuaRelease: ok
LuaRelease-->>NewWorker: ok
end
NewWorker->>Redis: DEL task:task_id
else Task_executed
NewWorker->>NewWorker: _execute_task(task)
alt _execute_task did_not_cleanup_locks
NewWorker->>NewWorker: exclusive_resources = getattr(task, _locked_resources, [])
NewWorker->>NewWorker: shared_resources = getattr(task, _locked_shared_resources, [])
alt Has_any_locked_resources
NewWorker->>LuaRelease: release_resource_locks(redis_conn, worker_name, exclusive_resources, shared_resources)
LuaRelease->>Redis: Atomic unlock operations
Redis-->>LuaRelease: ok
LuaRelease-->>NewWorker: ok
end
end
end
Updated class diagram for NewWorker and Redis lock helpersclassDiagram
class NewWorker {
+redis_conn
+name
+fetch_task()
+handle_tasks()
-_try_acquire_resource_locks(resources)
-_release_resource_locks(resources, shared_resources)
}
class AcquireLocksLua {
+acquire_locks(redis_conn, worker_name, exclusive_resources, shared_resources) blocked_resources
}
class ReleaseLocksLua {
+release_resource_locks(redis_conn, worker_name, resources, shared_resources)
}
NewWorker --> AcquireLocksLua : uses
NewWorker --> ReleaseLocksLua : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The release path is now split across multiple branches (incompatible-task handling and the
finallyblock) with slightly different ways of derivingexclusive_resourcesandshared_resources; consider centralizing this into a single helper to avoid divergence in future changes. - When calling
acquire_locksand_release_resource_locks, it may be safer to normalizeexclusive_resourcesandshared_resourcesto empty lists at the call site, so the Lua/Redis helpers never have to deal withNoneand you avoid subtle type-handling edge cases.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The release path is now split across multiple branches (incompatible-task handling and the `finally` block) with slightly different ways of deriving `exclusive_resources` and `shared_resources`; consider centralizing this into a single helper to avoid divergence in future changes.
- When calling `acquire_locks` and `_release_resource_locks`, it may be safer to normalize `exclusive_resources` and `shared_resources` to empty lists at the call site, so the Lua/Redis helpers never have to deal with `None` and you avoid subtle type-handling edge cases.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Member
|
/retest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This moves all the resource locking logic into a Lua script executed atomically in Redis.
Summary by Sourcery
Centralize resource locking for workers by delegating both exclusive and shared lock management to a Redis Lua script and wiring it into task acquisition and release.
Enhancements: