Skip to content

Commit 183088d

Browse files
paulhendricksJason Zhou
authored andcommitted
refactor: Switch ModelManager locks from std::sync::Mutex to parking_lot::Mutex (#2696)
Signed-off-by: Jason Zhou <jasonzho@jasonzho-mlt.client.nvidia.com>
1 parent 77775bb commit 183088d

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/llm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ hf-hub = { workspace = true }
6464
humantime = { workspace = true } # input/batch
6565
rand = { workspace = true }
6666
oneshot = { workspace = true }
67+
parking_lot = "0.12.4"
6768
prometheus = { workspace = true }
6869
serde = { workspace = true }
6970
serde_json = { workspace = true }

lib/llm/src/discovery/model_manager.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::{
5+
collections::{HashMap, HashSet},
6+
sync::{Arc, RwLock},
7+
};
8+
9+
use parking_lot::Mutex;
10+
411
use dynamo_runtime::component::Component;
512
use dynamo_runtime::prelude::DistributedRuntimeProvider;
613
use dynamo_runtime::slug::Slug;
714

815
use crate::discovery::ModelEntry;
9-
1016
use crate::kv_router::{KvRouterConfig, scheduler::DefaultWorkerSelector};
1117
use crate::{
1218
kv_router::KvRouter,
@@ -15,12 +21,6 @@ use crate::{
1521
completions::OpenAICompletionsStreamingEngine, embeddings::OpenAIEmbeddingsStreamingEngine,
1622
},
1723
};
18-
use std::collections::HashSet;
19-
use std::sync::RwLock;
20-
use std::{
21-
collections::HashMap,
22-
sync::{Arc, Mutex},
23-
};
2424

2525
#[derive(Debug, thiserror::Error)]
2626
pub enum ModelManagerError {
@@ -61,7 +61,7 @@ impl ModelManager {
6161
}
6262

6363
pub fn get_model_entries(&self) -> Vec<ModelEntry> {
64-
self.entries.lock().unwrap().values().cloned().collect()
64+
self.entries.lock().values().cloned().collect()
6565
}
6666

6767
pub fn has_model_any(&self, model: &str) -> bool {
@@ -170,12 +170,12 @@ impl ModelManager {
170170
/// Save a ModelEntry under an instance's etcd `models/` key so we can fetch it later when the key is
171171
/// deleted from etcd.
172172
pub fn save_model_entry(&self, key: &str, entry: ModelEntry) {
173-
self.entries.lock().unwrap().insert(key.to_string(), entry);
173+
self.entries.lock().insert(key.to_string(), entry);
174174
}
175175

176176
/// Remove and return model entry for this instance's etcd key. We do this when the instance stops.
177177
pub fn remove_model_entry(&self, key: &str) -> Option<ModelEntry> {
178-
self.entries.lock().unwrap().remove(key)
178+
self.entries.lock().remove(key)
179179
}
180180

181181
pub async fn kv_chooser_for(
@@ -203,7 +203,7 @@ impl ModelManager {
203203
}
204204

205205
fn get_kv_chooser(&self, model_name: &str) -> Option<Arc<KvRouter>> {
206-
self.kv_choosers.lock().unwrap().get(model_name).cloned()
206+
self.kv_choosers.lock().get(model_name).cloned()
207207
}
208208

209209
/// Create and return a KV chooser for this component and model
@@ -242,21 +242,18 @@ impl ModelManager {
242242
let new_kv_chooser = Arc::new(chooser);
243243
self.kv_choosers
244244
.lock()
245-
.unwrap()
246245
.insert(model_name.to_string(), new_kv_chooser.clone());
247246
Ok(new_kv_chooser)
248247
}
249248

250249
pub fn get_model_tool_call_parser(&self, model: &str) -> Option<String> {
251-
match self.entries.lock() {
252-
Ok(entries) => entries
253-
.values()
254-
.find(|entry| entry.name == model)
255-
.and_then(|entry| entry.runtime_config.as_ref())
256-
.and_then(|config| config.tool_call_parser.clone())
257-
.map(|parser| parser.to_string()),
258-
Err(_) => None,
259-
}
250+
self.entries
251+
.lock()
252+
.values()
253+
.find(|entry| entry.name == model)
254+
.and_then(|entry| entry.runtime_config.as_ref())
255+
.and_then(|config| config.tool_call_parser.clone())
256+
.map(|parser| parser.to_string())
260257
}
261258
}
262259

0 commit comments

Comments
 (0)