Skip to content

Commit 663e6ea

Browse files
feat: support get repository list from cortexso HF (#2003)
* feat: support get repository list from cortexso * chore: make db service thread-safe * fix: use map to save data * fix: async for cortexso request --------- Co-authored-by: sangjanai <[email protected]>
1 parent 4a4fff4 commit 663e6ea

File tree

8 files changed

+136
-39
lines changed

8 files changed

+136
-39
lines changed

engine/controllers/models.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,32 @@ void Models::AbortPullModel(
168168
}
169169
}
170170

171-
void Models::ListModel(
172-
const HttpRequestPtr& req,
173-
std::function<void(const HttpResponsePtr&)>&& callback) const {
171+
void Models::ListModel(const HttpRequestPtr& req,
172+
std::function<void(const HttpResponsePtr&)>&& callback,
173+
std::optional<std::string> author) const {
174+
if (author.has_value()) {
175+
auto res = model_src_svc_->GetRepositoryList(author.value());
176+
if (res.has_error()) {
177+
Json::Value ret;
178+
ret["message"] = res.error();
179+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
180+
resp->setStatusCode(k400BadRequest);
181+
callback(resp);
182+
} else {
183+
auto& info = res.value();
184+
Json::Value ret;
185+
Json::Value arr(Json::arrayValue);
186+
for (auto const& i : info) {
187+
arr.append(i);
188+
}
189+
ret["data"] = arr;
190+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
191+
resp->setStatusCode(k200OK);
192+
callback(resp);
193+
}
194+
return;
195+
}
196+
174197
namespace fs = std::filesystem;
175198
namespace fmu = file_manager_utils;
176199
Json::Value ret;

engine/controllers/models.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Models : public drogon::HttpController<Models, false> {
3030

3131
ADD_METHOD_TO(Models::PullModel, "/v1/models/pull", Options, Post);
3232
ADD_METHOD_TO(Models::AbortPullModel, "/v1/models/pull", Options, Delete);
33-
ADD_METHOD_TO(Models::ListModel, "/v1/models", Get);
33+
ADD_METHOD_TO(Models::ListModel, "/v1/models?author={author}", Get);
3434
ADD_METHOD_TO(Models::GetModel, "/v1/models/{1}", Get);
3535
ADD_METHOD_TO(Models::UpdateModel, "/v1/models/{1}", Options, Patch);
3636
ADD_METHOD_TO(Models::ImportModel, "/v1/models/import", Options, Post);
@@ -63,7 +63,8 @@ class Models : public drogon::HttpController<Models, false> {
6363
void AbortPullModel(const HttpRequestPtr& req,
6464
std::function<void(const HttpResponsePtr&)>&& callback);
6565
void ListModel(const HttpRequestPtr& req,
66-
std::function<void(const HttpResponsePtr&)>&& callback) const;
66+
std::function<void(const HttpResponsePtr&)>&& callback,
67+
std::optional<std::string> author) const;
6768
void GetModel(const HttpRequestPtr& req,
6869
std::function<void(const HttpResponsePtr&)>&& callback,
6970
const std::string& model_id) const;

engine/services/database_service.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,125 +6,161 @@ std::optional<EngineEntry> DatabaseService::UpsertEngine(
66
const std::string& api_key, const std::string& url,
77
const std::string& version, const std::string& variant,
88
const std::string& status, const std::string& metadata) {
9+
std::lock_guard<std::mutex> l(mtx_);
910
return cortex::db::Engines().UpsertEngine(engine_name, type, api_key, url,
1011
version, variant, status, metadata);
1112
}
1213

1314
std::optional<std::vector<EngineEntry>> DatabaseService::GetEngines() const {
15+
std::lock_guard<std::mutex> l(mtx_);
1416
return cortex::db::Engines().GetEngines();
1517
}
1618

1719
std::optional<EngineEntry> DatabaseService::GetEngineById(int id) const {
20+
std::lock_guard<std::mutex> l(mtx_);
1821
return cortex::db::Engines().GetEngineById(id);
1922
}
2023

2124
std::optional<EngineEntry> DatabaseService::GetEngineByNameAndVariant(
2225
const std::string& engine_name,
2326
const std::optional<std::string> variant) const {
27+
std::lock_guard<std::mutex> l(mtx_);
2428
return cortex::db::Engines().GetEngineByNameAndVariant(engine_name, variant);
2529
}
2630

2731
std::optional<std::string> DatabaseService::DeleteEngineById(int id) {
32+
std::lock_guard<std::mutex> l(mtx_);
2833
return cortex::db::Engines().DeleteEngineById(id);
2934
}
3035
// end engines
3136

3237
// begin file
3338
cpp::result<std::vector<OpenAi::File>, std::string>
3439
DatabaseService::GetFileList() const {
40+
std::lock_guard<std::mutex> l(mtx_);
3541
return cortex::db::File().GetFileList();
3642
}
3743

3844
cpp::result<OpenAi::File, std::string> DatabaseService::GetFileById(
3945
const std::string& file_id) const {
46+
std::lock_guard<std::mutex> l(mtx_);
4047
return cortex::db::File().GetFileById(file_id);
4148
}
4249

4350
cpp::result<void, std::string> DatabaseService::AddFileEntry(
4451
OpenAi::File& file) {
52+
std::lock_guard<std::mutex> l(mtx_);
4553
return cortex::db::File().AddFileEntry(file);
4654
}
4755

4856
cpp::result<void, std::string> DatabaseService::DeleteFileEntry(
4957
const std::string& file_id) {
58+
std::lock_guard<std::mutex> l(mtx_);
5059
return cortex::db::File().DeleteFileEntry(file_id);
5160
}
5261
// end file
5362

5463
// begin hardware
5564
cpp::result<std::vector<HardwareEntry>, std::string>
5665
DatabaseService::LoadHardwareList() const {
66+
std::lock_guard<std::mutex> l(mtx_);
5767
return cortex::db::Hardware().LoadHardwareList();
5868
}
5969

6070
cpp::result<bool, std::string> DatabaseService::AddHardwareEntry(
6171
const HardwareEntry& new_entry) {
72+
std::lock_guard<std::mutex> l(mtx_);
6273
return cortex::db::Hardware().AddHardwareEntry(new_entry);
6374
}
6475

76+
bool DatabaseService::HasHardwareEntry(const std::string& id) {
77+
std::lock_guard<std::mutex> l(mtx_);
78+
return cortex::db::Hardware().HasHardwareEntry(id);
79+
}
80+
6581
cpp::result<bool, std::string> DatabaseService::UpdateHardwareEntry(
6682
const std::string& id, const HardwareEntry& updated_entry) {
83+
std::lock_guard<std::mutex> l(mtx_);
6784
return cortex::db::Hardware().UpdateHardwareEntry(id, updated_entry);
6885
}
6986

7087
cpp::result<bool, std::string> DatabaseService::DeleteHardwareEntry(
7188
const std::string& id) {
89+
std::lock_guard<std::mutex> l(mtx_);
7290
return cortex::db::Hardware().DeleteHardwareEntry(id);
7391
}
92+
93+
cpp::result<bool, std::string> DatabaseService::UpdateHardwareEntry(
94+
const std::string& id, int hw_id, int sw_id) const {
95+
std::lock_guard<std::mutex> l(mtx_);
96+
return cortex::db::Hardware().UpdateHardwareEntry(id, hw_id, sw_id);
97+
}
7498
// end hardware
7599

76100
// begin models
77101
cpp::result<std::vector<ModelEntry>, std::string>
78102
DatabaseService::LoadModelList() const {
103+
std::lock_guard<std::mutex> l(mtx_);
79104
return cortex::db::Models().LoadModelList();
80105
}
81106

82107
cpp::result<ModelEntry, std::string> DatabaseService::GetModelInfo(
83108
const std::string& identifier) const {
109+
std::lock_guard<std::mutex> l(mtx_);
84110
return cortex::db::Models().GetModelInfo(identifier);
85111
}
86112

87113
cpp::result<bool, std::string> DatabaseService::AddModelEntry(
88114
ModelEntry new_entry) {
115+
std::lock_guard<std::mutex> l(mtx_);
89116
return cortex::db::Models().AddModelEntry(new_entry);
90117
}
91118

92119
cpp::result<bool, std::string> DatabaseService::UpdateModelEntry(
93120
const std::string& identifier, const ModelEntry& updated_entry) {
121+
std::lock_guard<std::mutex> l(mtx_);
94122
return cortex::db::Models().UpdateModelEntry(identifier, updated_entry);
95123
}
96124

97125
cpp::result<bool, std::string> DatabaseService::DeleteModelEntry(
98126
const std::string& identifier) {
127+
std::lock_guard<std::mutex> l(mtx_);
99128
return cortex::db::Models().DeleteModelEntry(identifier);
100129
}
101130

102131
cpp::result<bool, std::string> DatabaseService::DeleteModelEntryWithOrg(
103132
const std::string& src) {
133+
std::lock_guard<std::mutex> l(mtx_);
104134
return cortex::db::Models().DeleteModelEntryWithOrg(src);
105135
}
106136

107137
cpp::result<bool, std::string> DatabaseService::DeleteModelEntryWithRepo(
108138
const std::string& src) {
139+
140+
std::lock_guard<std::mutex> l(mtx_);
109141
return cortex::db::Models().DeleteModelEntryWithRepo(src);
110142
}
111143

112144
cpp::result<std::vector<std::string>, std::string>
113145
DatabaseService::FindRelatedModel(const std::string& identifier) const {
146+
std::lock_guard<std::mutex> l(mtx_);
114147
return cortex::db::Models().FindRelatedModel(identifier);
115148
}
116149

117150
bool DatabaseService::HasModel(const std::string& identifier) const {
151+
std::lock_guard<std::mutex> l(mtx_);
118152
return cortex::db::Models().HasModel(identifier);
119153
}
120154

121155
cpp::result<std::vector<ModelEntry>, std::string> DatabaseService::GetModels(
122156
const std::string& model_src) const {
157+
std::lock_guard<std::mutex> l(mtx_);
123158
return cortex::db::Models().GetModels(model_src);
124159
}
125160

126161
cpp::result<std::vector<ModelEntry>, std::string>
127162
DatabaseService::GetModelSources() const {
163+
std::lock_guard<std::mutex> l(mtx_);
128164
return cortex::db::Models().GetModelSources();
129165
}
130166
// end models

engine/services/database_service.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <mutex>
23
#include "database/engines.h"
34
#include "database/file.h"
45
#include "database/hardware.h"
@@ -39,9 +40,13 @@ class DatabaseService {
3940
cpp::result<std::vector<HardwareEntry>, std::string> LoadHardwareList() const;
4041
cpp::result<bool, std::string> AddHardwareEntry(
4142
const HardwareEntry& new_entry);
43+
bool HasHardwareEntry(const std::string& id);
4244
cpp::result<bool, std::string> UpdateHardwareEntry(
4345
const std::string& id, const HardwareEntry& updated_entry);
4446
cpp::result<bool, std::string> DeleteHardwareEntry(const std::string& id);
47+
cpp::result<bool, std::string> UpdateHardwareEntry(const std::string& id,
48+
int hw_id,
49+
int sw_id) const;
4550

4651
// models
4752
cpp::result<std::vector<ModelEntry>, std::string> LoadModelList() const;
@@ -62,8 +67,8 @@ class DatabaseService {
6267
bool HasModel(const std::string& identifier) const;
6368
cpp::result<std::vector<ModelEntry>, std::string> GetModels(
6469
const std::string& model_src) const;
65-
cpp::result<std::vector<ModelEntry>, std::string> GetModelSources()
66-
const;
70+
cpp::result<std::vector<ModelEntry>, std::string> GetModelSources() const;
6771

6872
private:
73+
mutable std::mutex mtx_;
6974
};

engine/services/hardware_service.cc

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,7 @@ void HardwareService::UpdateHardwareInfos() {
296296
using HwEntry = cortex::db::HardwareEntry;
297297
CheckDependencies();
298298
auto gpus = cortex::hw::GetGPUInfo();
299-
cortex::db::Hardware hw_db;
300-
auto b = hw_db.LoadHardwareList();
299+
auto b = db_service_->LoadHardwareList();
301300
// delete if not exists
302301
auto exists = [&gpus](const std::string& uuid) {
303302
for (auto const& g : gpus) {
@@ -308,12 +307,12 @@ void HardwareService::UpdateHardwareInfos() {
308307
};
309308
for (auto const& he : b.value()) {
310309
if (!exists(he.uuid)) {
311-
hw_db.DeleteHardwareEntry(he.uuid);
310+
db_service_->DeleteHardwareEntry(he.uuid);
312311
}
313312
}
314313

315314
// Get updated list
316-
b = hw_db.LoadHardwareList();
315+
b = db_service_->LoadHardwareList();
317316
std::vector<std::pair<int, int>> activated_gpu_bf;
318317
std::string debug_b;
319318
for (auto const& he : b.value()) {
@@ -326,15 +325,15 @@ void HardwareService::UpdateHardwareInfos() {
326325
for (auto const& gpu : gpus) {
327326
// ignore error
328327
// Note: only support NVIDIA for now, so hardware_id = software_id
329-
if (hw_db.HasHardwareEntry(gpu.uuid)) {
330-
auto res = hw_db.UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
328+
if (db_service_->HasHardwareEntry(gpu.uuid)) {
329+
auto res = db_service_->UpdateHardwareEntry(gpu.uuid, std::stoi(gpu.id),
331330
std::stoi(gpu.id));
332331
if (res.has_error()) {
333332
CTL_WRN(res.error());
334333
}
335334
} else {
336335
auto res =
337-
hw_db.AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
336+
db_service_->AddHardwareEntry(HwEntry{.uuid = gpu.uuid,
338337
.type = "gpu",
339338
.hardware_id = std::stoi(gpu.id),
340339
.software_id = std::stoi(gpu.id),

engine/services/model_service.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,7 @@ cpp::result<StartModelResult, std::string> ModelService::StartModel(
858858
} else {
859859
// only report to user the error
860860
for (auto& depend : depends) {
861-
862-
StopModel(depend);
861+
(void)StopModel(depend);
863862
}
864863
}
865864
CTL_ERR("Model failed to start with status code: " << status);
@@ -869,7 +868,7 @@ cpp::result<StartModelResult, std::string> ModelService::StartModel(
869868

870869
// Running remote model
871870
if (engine_svc_->IsRemoteEngine(mc.engine)) {
872-
engine_svc_->LoadEngine(mc.engine);
871+
(void)engine_svc_->LoadEngine(mc.engine);
873872
config::RemoteModelConfig remote_mc;
874873
remote_mc.LoadFromYamlFile(
875874
fmu::ToAbsoluteCortexDataPath(
@@ -1035,7 +1034,7 @@ cpp::result<bool, std::string> ModelService::StopModel(
10351034
// Stop all depends model
10361035
auto depends = python_model_config.depends;
10371036
for (auto& depend : depends) {
1038-
StopModel(depend);
1037+
(void)StopModel(depend);
10391038
}
10401039
}
10411040

@@ -1233,7 +1232,7 @@ cpp::result<std::optional<std::string>, std::string>
12331232
ModelService::MayFallbackToCpu(const std::string& model_path, int ngl,
12341233
int ctx_len, int n_batch, int n_ubatch,
12351234
const std::string& kv_cache_type) {
1236-
// TODO(sang) temporary disable this function
1235+
// TODO(sang) temporary disable this function
12371236
return std::nullopt;
12381237
assert(hw_service_);
12391238
auto hw_info = hw_service_->GetHardwareInfo();

0 commit comments

Comments
 (0)