-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[model-gateway] Use UUIDs for router-managed worker resources #15540
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,6 +121,26 @@ impl WorkerRegistry { | |
| worker_id | ||
| } | ||
|
|
||
| /// Reserve (or retrieve) a stable UUID for a worker URL. | ||
| pub fn reserve_id_for_url(&self, url: &str) -> WorkerId { | ||
| if let Some(existing_id) = self.url_to_id.get(url) { | ||
| return existing_id.clone(); | ||
| } | ||
| let worker_id = WorkerId::new(); | ||
| self.url_to_id.insert(url.to_string(), worker_id.clone()); | ||
| worker_id | ||
| } | ||
|
|
||
| /// Best-effort lookup of the URL for a given worker ID. | ||
| pub fn get_url_by_id(&self, worker_id: &WorkerId) -> Option<String> { | ||
| if let Some(worker) = self.get(worker_id) { | ||
| return Some(worker.url().to_string()); | ||
| } | ||
| self.url_to_id | ||
| .iter() | ||
| .find_map(|entry| (entry.value() == worker_id).then(|| entry.key().clone())) | ||
|
Comment on lines
+139
to
+141
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fallback logic in this function iterates over the For better performance, consider adding a reverse mapping, e.g., |
||
| } | ||
|
|
||
| /// Remove a worker by ID | ||
| pub fn remove(&self, worker_id: &WorkerId) -> Option<Arc<dyn Worker>> { | ||
| if let Some((_, worker)) = self.workers.remove(worker_id) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a race condition in this function. If two threads call
reserve_id_for_urlfor the same new URL concurrently, both can pass theif let Some(...)check, generate a newWorkerId, and insert it into the map. The last one to insert will win, but both threads will return aWorkerId, one of which will be for a mapping that was immediately overwritten. This can lead to inconsistent state.To fix this and make the operation atomic, you can use the
entryAPI ofDashMap.