From 052647191c34126613a810306478220cf4e66815 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Wed, 4 Dec 2024 10:56:35 +0700 Subject: [PATCH 1/6] feat: prioritize GPUs --- engine/controllers/hardware.cc | 2 +- engine/services/hardware_service.cc | 31 +++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/engine/controllers/hardware.cc b/engine/controllers/hardware.cc index 4f5cc2879..3583995cc 100644 --- a/engine/controllers/hardware.cc +++ b/engine/controllers/hardware.cc @@ -40,7 +40,7 @@ void Hardware::Activate( ahc.gpus.push_back(g.asInt()); } } - std::sort(ahc.gpus.begin(), ahc.gpus.end()); + if (!hw_svc_->IsValidConfig(ahc)) { Json::Value ret; ret["message"] = "Invalid GPU index provided."; diff --git a/engine/services/hardware_service.cc b/engine/services/hardware_service.cc index 681ca7578..5574472bd 100644 --- a/engine/services/hardware_service.cc +++ b/engine/services/hardware_service.cc @@ -191,13 +191,30 @@ bool HardwareService::Restart(const std::string& host, int port) { return true; } +// GPU identifiers are given as integer indices or as UUID strings. GPU UUID strings +// should follow the same format as given by nvidia-smi, such as GPU-8932f937-d72c-4106-c12f-20bd9faed9f6. +// However, for convenience, abbreviated forms are allowed; simply specify enough digits +// from the beginning of the GPU UUID to uniquely identify that GPU in the target system. +// For example, CUDA_VISIBLE_DEVICES=GPU-8932f937 may be a valid way to refer to the above GPU UUID, +// assuming no other GPU in the system shares this prefix. Only the devices whose index +// is present in the sequence are visible to CUDA applications and they are enumerated +// in the order of the sequence. If one of the indices is invalid, only the devices whose +// index precedes the invalid index are visible to CUDA applications. For example, setting +// CUDA_VISIBLE_DEVICES to 2,1 causes device 0 to be invisible and device 2 to be enumerated +// before device 1. Setting CUDA_VISIBLE_DEVICES to 0,2,-1,1 causes devices 0 and 2 to be +// visible and device 1 to be invisible. MIG format starts with MIG keyword and GPU UUID +// should follow the same format as given by nvidia-smi. +// For example, MIG-GPU-8932f937-d72c-4106-c12f-20bd9faed9f6/1/2. +// Only single MIG instance enumeration is supported. bool HardwareService::SetActivateHardwareConfig( const cortex::hw::ActivateHardwareConfig& ahc) { // Note: need to map software_id and hardware_id // Update to db cortex::db::Hardwares hw_db; - auto activate = [&ahc](int software_id) { - return std::count(ahc.gpus.begin(), ahc.gpus.end(), software_id) > 0; + // copy all gpu information to new vector + auto ahc_gpus = ahc.gpus; + auto activate = [&ahc_gpus](int software_id) { + return std::count(ahc_gpus.begin(), ahc_gpus.end(), software_id) > 0; }; auto res = hw_db.LoadHardwareList(); if (res.has_value()) { @@ -210,11 +227,12 @@ bool HardwareService::SetActivateHardwareConfig( } } std::sort(activated_ids.begin(), activated_ids.end()); - if (ahc.gpus.size() != activated_ids.size()) { + std::sort(ahc_gpus.begin(), ahc_gpus.end()); + if (ahc_gpus.size() != activated_ids.size()) { need_update = true; } else { - for (size_t i = 0; i < ahc.gpus.size(); i++) { - if (ahc.gpus[i] != activated_ids[i]) + for (size_t i = 0; i < ahc_gpus.size(); i++) { + if (ahc_gpus[i] != activated_ids[i]) need_update = true; } } @@ -291,7 +309,8 @@ void HardwareService::UpdateHardwareInfos() { } #if defined(_WIN32) || defined(_WIN64) || defined(__linux__) - if (!gpus.empty()) { + bool has_deactivated_gpu = a.value().size() != activated_gpu_af.size(); + if (!gpus.empty() && has_deactivated_gpu) { const char* value = std::getenv("CUDA_VISIBLE_DEVICES"); if (value) { LOG_INFO << "CUDA_VISIBLE_DEVICES: " << value; From df6d174df7e63d614e1db6ff05597acf9bb6dd96 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Wed, 4 Dec 2024 13:50:36 +0700 Subject: [PATCH 2/6] fix: migrate db --- engine/migrations/db_helper.h | 31 ++++ engine/migrations/migration_manager.cc | 13 ++ engine/migrations/schema_version.h | 2 +- engine/migrations/v2/migration.h | 204 +++++++++++++++++++++++++ 4 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 engine/migrations/db_helper.h create mode 100644 engine/migrations/v2/migration.h diff --git a/engine/migrations/db_helper.h b/engine/migrations/db_helper.h new file mode 100644 index 000000000..31254eb0f --- /dev/null +++ b/engine/migrations/db_helper.h @@ -0,0 +1,31 @@ +#pragma once +#include + +namespace cortex::mgr { +#include +#include +#include +#include + +inline bool ColumnExists(SQLite::Database& db, const std::string& table_name, + const std::string& column_name) { + try { + SQLite::Statement query( + db, "SELECT " + column_name + " FROM " + table_name + " LIMIT 0"); + return true; + } catch (std::exception&) { + return false; + } +} + +inline void AddColumnIfNotExists(SQLite::Database& db, + const std::string& table_name, + const std::string& column_name, + const std::string& column_type) { + if (!ColumnExists(db, table_name, column_name)) { + std::string sql = "ALTER TABLE " + table_name + " ADD COLUMN " + + column_name + " " + column_type; + db.exec(sql); + } +} +} // namespace cortex::mgr \ No newline at end of file diff --git a/engine/migrations/migration_manager.cc b/engine/migrations/migration_manager.cc index 2c2b6ddfd..da5df3368 100644 --- a/engine/migrations/migration_manager.cc +++ b/engine/migrations/migration_manager.cc @@ -5,6 +5,7 @@ #include "utils/file_manager_utils.h" #include "utils/scope_exit.h" #include "utils/widechar_conv.h" +#include "v2/migration.h" namespace cortex::migr { @@ -140,6 +141,9 @@ cpp::result MigrationManager::DoUpFolderStructure( case 0: return v0::MigrateFolderStructureUp(); break; + case 2: + return v2::MigrateFolderStructureUp(); + break; default: return true; @@ -151,6 +155,9 @@ cpp::result MigrationManager::DoDownFolderStructure( case 0: return v0::MigrateFolderStructureDown(); break; + case 2: + return v2::MigrateFolderStructureDown(); + break; default: return true; @@ -184,6 +191,9 @@ cpp::result MigrationManager::DoUpDB(int version) { case 0: return v0::MigrateDBUp(db_); break; + case 2: + return v2::MigrateDBUp(db_); + break; default: return true; @@ -195,6 +205,9 @@ cpp::result MigrationManager::DoDownDB(int version) { case 0: return v0::MigrateDBDown(db_); break; + case 2: + return v2::MigrateDBDown(db_); + break; default: return true; diff --git a/engine/migrations/schema_version.h b/engine/migrations/schema_version.h index 7cfccf27a..6f07aa868 100644 --- a/engine/migrations/schema_version.h +++ b/engine/migrations/schema_version.h @@ -1,4 +1,4 @@ #pragma once //Track the current schema version -#define SCHEMA_VERSION 0 \ No newline at end of file +#define SCHEMA_VERSION 2 \ No newline at end of file diff --git a/engine/migrations/v2/migration.h b/engine/migrations/v2/migration.h new file mode 100644 index 000000000..11bd0298d --- /dev/null +++ b/engine/migrations/v2/migration.h @@ -0,0 +1,204 @@ +#pragma once +#include +#include +#include +#include "migrations/db_helper.h" +#include "utils/file_manager_utils.h" +#include "utils/logging_utils.h" +#include "utils/result.hpp" + +namespace cortex::migr::v2 { +// Data folder +namespace fmu = file_manager_utils; + +// cortexcpp +// |__ models +// | |__ cortex.so +// | |__ tinyllama +// | |__ gguf +// |__ engines +// | |__ cortex.llamacpp +// | |__ deps +// | |__ windows-amd64-avx +// |__ logs +// +inline cpp::result MigrateFolderStructureUp() { + if (!std::filesystem::exists(fmu::GetCortexDataPath() / "models")) { + std::filesystem::create_directory(fmu::GetCortexDataPath() / "models"); + } + + if (!std::filesystem::exists(fmu::GetCortexDataPath() / "engines")) { + std::filesystem::create_directory(fmu::GetCortexDataPath() / "engines"); + } + + if (!std::filesystem::exists(fmu::GetCortexDataPath() / "logs")) { + std::filesystem::create_directory(fmu::GetCortexDataPath() / "logs"); + } + + return true; +} + +inline cpp::result MigrateFolderStructureDown() { + // CTL_INF("Folder structure already up to date!"); + return true; +} + +// Database +inline cpp::result MigrateDBUp(SQLite::Database& db) { + try { + db.exec( + "CREATE TABLE IF NOT EXISTS schema_version ( version INTEGER PRIMARY " + "KEY);"); + + // models + { + // Check if the table exists + SQLite::Statement query(db, + "SELECT name FROM sqlite_master WHERE " + "type='table' AND name='models'"); + auto table_exists = query.executeStep(); + + if (table_exists) { + // Alter existing table + cortex::mgr::AddColumnIfNotExists(db, "models", "model_format", "TEXT"); + cortex::mgr::AddColumnIfNotExists(db, "models", "model_source", "TEXT"); + cortex::mgr::AddColumnIfNotExists(db, "models", "status", "TEXT"); + cortex::mgr::AddColumnIfNotExists(db, "models", "engine", "TEXT"); + } else { + // Create new table + db.exec( + "CREATE TABLE models (" + "model_id TEXT PRIMARY KEY," + "author_repo_id TEXT," + "branch_name TEXT," + "path_to_model_yaml TEXT," + "model_alias TEXT," + "model_format TEXT," + "model_source TEXT," + "status TEXT," + "engine TEXT" + ")"); + } + } + + // Check if the table exists + SQLite::Statement hw_query(db, + "SELECT name FROM sqlite_master WHERE " + "type='table' AND name='hardware'"); + auto hw_table_exists = hw_query.executeStep(); + + if (hw_table_exists) { + // Alter existing table + cortex::mgr::AddColumnIfNotExists(db, "hardware", "priority", "INTEGER"); + } else { + db.exec( + "CREATE TABLE IF NOT EXISTS hardware (" + "uuid TEXT PRIMARY KEY, " + "type TEXT NOT NULL, " + "hardware_id INTEGER NOT NULL, " + "software_id INTEGER NOT NULL, " + "activated INTEGER NOT NULL CHECK (activated IN (0, 1)), " + "priority INTEGER); "); + } + + // engines + db.exec( + "CREATE TABLE IF NOT EXISTS engines (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "engine_name TEXT," + "type TEXT," + "api_key TEXT," + "url TEXT," + "version TEXT," + "variant TEXT," + "status TEXT," + "metadata TEXT," + "date_created TEXT DEFAULT CURRENT_TIMESTAMP," + "date_updated TEXT DEFAULT CURRENT_TIMESTAMP," + "UNIQUE(engine_name, variant));"); + + // CTL_INF("Database migration up completed successfully."); + return true; + } catch (const std::exception& e) { + CTL_WRN("Migration up failed: " << e.what()); + return cpp::fail(e.what()); + } +}; + +inline cpp::result MigrateDBDown(SQLite::Database& db) { + try { + // models + { + SQLite::Statement query(db, + "SELECT name FROM sqlite_master WHERE " + "type='table' AND name='models'"); + auto table_exists = query.executeStep(); + if (table_exists) { + // Create a new table with the old schema + db.exec( + "CREATE TABLE models_old (" + "model_id TEXT PRIMARY KEY," + "author_repo_id TEXT," + "branch_name TEXT," + "path_to_model_yaml TEXT," + "model_alias TEXT" + ")"); + + // Copy data from the current table to the new table + db.exec( + "INSERT INTO models_old (model_id, author_repo_id, branch_name, " + "path_to_model_yaml, model_alias) " + "SELECT model_id, author_repo_id, branch_name, path_to_model_yaml, " + "model_alias FROM models"); + + // Drop the current table + db.exec("DROP TABLE models"); + + // Rename the new table to the original name + db.exec("ALTER TABLE models_old RENAME TO models"); + } + } + + // hardware + { + SQLite::Statement query(db, + "SELECT name FROM sqlite_master WHERE " + "type='table' AND name='hardware'"); + auto table_exists = query.executeStep(); + if (table_exists) { + // Create a new table with the old schema + db.exec( + "CREATE TABLE hardware_old (" + "uuid TEXT PRIMARY KEY, " + "type TEXT NOT NULL, " + "hardware_id INTEGER NOT NULL, " + "software_id INTEGER NOT NULL, " + "activated INTEGER NOT NULL CHECK (activated IN (0, 1))" + ")"); + + // Copy data from the current table to the new table + db.exec( + "INSERT INTO hardware_old (uuid, type, hardware_id, " + "software_id, activated) " + "SELECT uuid, type, hardware_id, software_id, " + "activated FROM hardware"); + + // Drop the current table + db.exec("DROP TABLE hardware"); + + // Rename the new table to the original name + db.exec("ALTER TABLE hardware_old RENAME TO hardware"); + } + } + + // engines + db.exec("DROP TABLE IF EXISTS engines;"); + // CTL_INF("Migration down completed successfully."); + return true; + } catch (const std::exception& e) { + CTL_WRN("Migration down failed: " << e.what()); + return cpp::fail(e.what()); + } +} + +}; // namespace cortex::migr::v2 \ No newline at end of file From 80bd6b4b6f778d324740b6f82b34077bb683cb30 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Wed, 4 Dec 2024 13:51:20 +0700 Subject: [PATCH 3/6] fix: add priority --- engine/database/hardware.cc | 36 +++++++++++----------- engine/database/hardware.h | 19 ++++++------ engine/services/hardware_service.cc | 47 ++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/engine/database/hardware.cc b/engine/database/hardware.cc index ee68749d5..a1a230ed5 100644 --- a/engine/database/hardware.cc +++ b/engine/database/hardware.cc @@ -4,16 +4,14 @@ namespace cortex::db { -Hardwares::Hardwares() : db_(cortex::db::Database::GetInstance().db()) { -} +Hardware::Hardware() : db_(cortex::db::Database::GetInstance().db()) {} -Hardwares::Hardwares(SQLite::Database& db) : db_(db) { -} +Hardware::Hardware(SQLite::Database& db) : db_(db) {} -Hardwares::~Hardwares() {} +Hardware::~Hardware() {} cpp::result, std::string> -Hardwares::LoadHardwareList() const { +Hardware::LoadHardwareList() const { try { db_.exec("BEGIN TRANSACTION;"); cortex::utils::ScopeExit se([this] { db_.exec("COMMIT;"); }); @@ -21,7 +19,7 @@ Hardwares::LoadHardwareList() const { SQLite::Statement query( db_, "SELECT uuid, type, " - "hardware_id, software_id, activated FROM hardware"); + "hardware_id, software_id, activated, priority FROM hardware"); while (query.executeStep()) { HardwareEntry entry; @@ -30,6 +28,7 @@ Hardwares::LoadHardwareList() const { entry.hardware_id = query.getColumn(2).getInt(); entry.software_id = query.getColumn(3).getInt(); entry.activated = query.getColumn(4).getInt(); + entry.priority = query.getColumn(5).getInt(); entries.push_back(entry); } return entries; @@ -38,19 +37,20 @@ Hardwares::LoadHardwareList() const { return cpp::fail(e.what()); } } -cpp::result Hardwares::AddHardwareEntry( +cpp::result Hardware::AddHardwareEntry( const HardwareEntry& new_entry) { try { SQLite::Statement insert( db_, "INSERT INTO hardware (uuid, type, " - "hardware_id, software_id, activated) VALUES (?, ?, " - "?, ?, ?)"); + "hardware_id, software_id, activated, priority) VALUES (?, ?, " + "?, ?, ?, ?)"); insert.bind(1, new_entry.uuid); insert.bind(2, new_entry.type); insert.bind(3, new_entry.hardware_id); insert.bind(4, new_entry.software_id); insert.bind(5, new_entry.activated); + insert.bind(6, new_entry.priority); insert.exec(); CTL_INF("Inserted: " << new_entry.ToJsonString()); return true; @@ -59,17 +59,19 @@ cpp::result Hardwares::AddHardwareEntry( return cpp::fail(e.what()); } } -cpp::result Hardwares::UpdateHardwareEntry( +cpp::result Hardware::UpdateHardwareEntry( const std::string& id, const HardwareEntry& updated_entry) { try { - SQLite::Statement upd(db_, - "UPDATE hardware " - "SET hardware_id = ?, software_id = ?, activated = ? " - "WHERE uuid = ?"); + SQLite::Statement upd( + db_, + "UPDATE hardware " + "SET hardware_id = ?, software_id = ?, activated = ?, priority = ? " + "WHERE uuid = ?"); upd.bind(1, updated_entry.hardware_id); upd.bind(2, updated_entry.software_id); upd.bind(3, updated_entry.activated); - upd.bind(4, id); + upd.bind(4, updated_entry.priority); + upd.bind(5, id); if (upd.exec() == 1) { CTL_INF("Updated: " << updated_entry.ToJsonString()); return true; @@ -80,7 +82,7 @@ cpp::result Hardwares::UpdateHardwareEntry( } } -cpp::result Hardwares::DeleteHardwareEntry( +cpp::result Hardware::DeleteHardwareEntry( const std::string& id) { try { SQLite::Statement del(db_, "DELETE from hardware WHERE uuid = ?"); diff --git a/engine/database/hardware.h b/engine/database/hardware.h index 0966d58a3..04d0bbda1 100644 --- a/engine/database/hardware.h +++ b/engine/database/hardware.h @@ -4,8 +4,8 @@ #include #include #include -#include "utils/result.hpp" #include "utils/json_helper.h" +#include "utils/result.hpp" namespace cortex::db { struct HardwareEntry { @@ -14,6 +14,7 @@ struct HardwareEntry { int hardware_id; int software_id; bool activated; + int priority; std::string ToJsonString() const { Json::Value root; root["uuid"] = uuid; @@ -21,26 +22,26 @@ struct HardwareEntry { root["hardware_id"] = hardware_id; root["software_id"] = software_id; root["activated"] = activated; + root["priority"] = priority; return json_helper::DumpJsonString(root); } }; -class Hardwares { +class Hardware { private: SQLite::Database& db_; - public: - Hardwares(); - Hardwares(SQLite::Database& db); - ~Hardwares(); + Hardware(); + Hardware(SQLite::Database& db); + ~Hardware(); cpp::result, std::string> LoadHardwareList() const; - cpp::result AddHardwareEntry(const HardwareEntry& new_entry); + cpp::result AddHardwareEntry( + const HardwareEntry& new_entry); cpp::result UpdateHardwareEntry( const std::string& id, const HardwareEntry& updated_entry); - cpp::result DeleteHardwareEntry( - const std::string& id); + cpp::result DeleteHardwareEntry(const std::string& id); }; } // namespace cortex::db \ No newline at end of file diff --git a/engine/services/hardware_service.cc b/engine/services/hardware_service.cc index 5574472bd..629d98c82 100644 --- a/engine/services/hardware_service.cc +++ b/engine/services/hardware_service.cc @@ -34,7 +34,7 @@ bool TryConnectToServer(const std::string& host, int port) { HardwareInfo HardwareService::GetHardwareInfo() { // append active state - cortex::db::Hardwares hw_db; + cortex::db::Hardware hw_db; auto gpus = cortex::hw::GetGPUInfo(); auto res = hw_db.LoadHardwareList(); if (res.has_value()) { @@ -210,12 +210,21 @@ bool HardwareService::SetActivateHardwareConfig( const cortex::hw::ActivateHardwareConfig& ahc) { // Note: need to map software_id and hardware_id // Update to db - cortex::db::Hardwares hw_db; + cortex::db::Hardware hw_db; // copy all gpu information to new vector auto ahc_gpus = ahc.gpus; - auto activate = [&ahc_gpus](int software_id) { - return std::count(ahc_gpus.begin(), ahc_gpus.end(), software_id) > 0; + auto activate = [&ahc](int software_id) { + return std::count(ahc.gpus.begin(), ahc.gpus.end(), software_id) > 0; }; + auto priority = [&ahc](int software_id) -> int { + for (size_t i = 0; i < ahc.gpus.size(); i++) { + if (ahc.gpus[i] == software_id) + return i; + break; + } + return INT_MAX; + }; + auto res = hw_db.LoadHardwareList(); if (res.has_value()) { bool need_update = false; @@ -245,6 +254,7 @@ bool HardwareService::SetActivateHardwareConfig( // Need to update, proceed for (auto& e : res.value()) { e.activated = activate(e.software_id); + e.priority = priority(e.software_id); auto res = hw_db.UpdateHardwareEntry(e.uuid, e); if (res.has_error()) { CTL_WRN(res.error()); @@ -258,14 +268,14 @@ bool HardwareService::SetActivateHardwareConfig( void HardwareService::UpdateHardwareInfos() { using HwEntry = cortex::db::HardwareEntry; auto gpus = cortex::hw::GetGPUInfo(); - cortex::db::Hardwares hw_db; + cortex::db::Hardware hw_db; auto b = hw_db.LoadHardwareList(); - std::vector activated_gpu_bf; + std::vector> activated_gpu_bf; std::string debug_b; for (auto const& he : b.value()) { if (he.type == "gpu" && he.activated) { debug_b += std::to_string(he.software_id) + " "; - activated_gpu_bf.push_back(he.software_id); + activated_gpu_bf.push_back(std::pair(he.software_id, he.priority)); } } CTL_INF("Activated GPUs before: " << debug_b); @@ -276,7 +286,8 @@ void HardwareService::UpdateHardwareInfos() { .type = "gpu", .hardware_id = std::stoi(gpu.id), .software_id = std::stoi(gpu.id), - .activated = true}); + .activated = true, + .priority = INT_MAX}); if (res.has_error()) { CTL_WRN(res.error()); } @@ -284,24 +295,26 @@ void HardwareService::UpdateHardwareInfos() { auto a = hw_db.LoadHardwareList(); std::vector a_gpu; - std::vector activated_gpu_af; + std::vector> activated_gpu_af; std::string debug_a; for (auto const& he : a.value()) { if (he.type == "gpu" && he.activated) { debug_a += std::to_string(he.software_id) + " "; - activated_gpu_af.push_back(he.software_id); + activated_gpu_af.push_back(std::pair(he.software_id, he.priority)); } } CTL_INF("Activated GPUs after: " << debug_a); // if hardware list changes, need to restart - std::sort(activated_gpu_bf.begin(), activated_gpu_bf.end()); - std::sort(activated_gpu_af.begin(), activated_gpu_af.end()); + std::sort(activated_gpu_bf.begin(), activated_gpu_bf.end(), + [](auto& p1, auto& p2) { return p1.second < p2.second; }); + std::sort(activated_gpu_af.begin(), activated_gpu_af.end(), + [](auto& p1, auto& p2) { return p1.second < p2.second; }); bool need_restart = false; if (activated_gpu_bf.size() != activated_gpu_af.size()) { need_restart = true; } else { for (size_t i = 0; i < activated_gpu_bf.size(); i++) { - if (activated_gpu_bf[i] != activated_gpu_af[i]) { + if (activated_gpu_bf[i].first != activated_gpu_af[i].first) { need_restart = true; break; } @@ -322,7 +335,11 @@ void HardwareService::UpdateHardwareInfos() { if (need_restart) { CTL_INF("Need restart"); - ahc_ = {.gpus = activated_gpu_af}; + std::vector gpus; + for (auto const& p : activated_gpu_af) { + gpus.push_back(p.first); + } + ahc_ = {.gpus = gpus}; } } @@ -330,7 +347,7 @@ bool HardwareService::IsValidConfig( const cortex::hw::ActivateHardwareConfig& ahc) { if (ahc.gpus.empty()) return true; - cortex::db::Hardwares hw_db; + cortex::db::Hardware hw_db; auto is_valid = [&ahc](int software_id) { return std::count(ahc.gpus.begin(), ahc.gpus.end(), software_id) > 0; }; From 50b774ed1515f7b9ac8c7e486861e438abef580c Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 5 Dec 2024 09:45:59 +0700 Subject: [PATCH 4/6] fix: db --- engine/migrations/v2/migration.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/engine/migrations/v2/migration.h b/engine/migrations/v2/migration.h index 11bd0298d..d28020645 100644 --- a/engine/migrations/v2/migration.h +++ b/engine/migrations/v2/migration.h @@ -192,7 +192,9 @@ inline cpp::result MigrateDBDown(SQLite::Database& db) { } // engines - db.exec("DROP TABLE IF EXISTS engines;"); + { + // do nothing + } // CTL_INF("Migration down completed successfully."); return true; } catch (const std::exception& e) { From fb581dac10008cbb374b4966d43f6f20112f8eb8 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Thu, 5 Dec 2024 10:48:42 +0700 Subject: [PATCH 5/6] fix: more --- engine/services/hardware_service.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/engine/services/hardware_service.cc b/engine/services/hardware_service.cc index 629d98c82..25be78873 100644 --- a/engine/services/hardware_service.cc +++ b/engine/services/hardware_service.cc @@ -224,15 +224,15 @@ bool HardwareService::SetActivateHardwareConfig( } return INT_MAX; }; - + auto res = hw_db.LoadHardwareList(); if (res.has_value()) { bool need_update = false; - std::vector activated_ids; + std::vector> activated_ids; // Check if need to update for (auto const& e : res.value()) { if (e.activated) { - activated_ids.push_back(e.software_id); + activated_ids.push_back(std::pair(e.software_id, e.priority)); } } std::sort(activated_ids.begin(), activated_ids.end()); @@ -241,8 +241,11 @@ bool HardwareService::SetActivateHardwareConfig( need_update = true; } else { for (size_t i = 0; i < ahc_gpus.size(); i++) { - if (ahc_gpus[i] != activated_ids[i]) + // if activated id or priority changes + if (ahc_gpus[i] != activated_ids[i].first || + i != activated_ids[i].second) need_update = true; + break; } } From f9f600248fc761c6ba5bdedbde5de72205c44b76 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Mon, 9 Dec 2024 11:26:47 +0700 Subject: [PATCH 6/6] fix: migration --- engine/migrations/migration_manager.cc | 4 ---- engine/migrations/v2/migration.h | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/engine/migrations/migration_manager.cc b/engine/migrations/migration_manager.cc index 730ecb856..6936f45a0 100644 --- a/engine/migrations/migration_manager.cc +++ b/engine/migrations/migration_manager.cc @@ -141,7 +141,6 @@ cpp::result MigrationManager::DoUpFolderStructure( switch (version) { case 0: return v0::MigrateFolderStructureUp(); - break; case 1: return v1::MigrateFolderStructureUp(); case 2: @@ -158,7 +157,6 @@ cpp::result MigrationManager::DoDownFolderStructure( switch (version) { case 0: return v0::MigrateFolderStructureDown(); - break; case 1: return v1::MigrateFolderStructureDown(); case 2: @@ -196,7 +194,6 @@ cpp::result MigrationManager::DoUpDB(int version) { switch (version) { case 0: return v0::MigrateDBUp(db_); - break; case 1: return v1::MigrateDBUp(db_); case 2: @@ -212,7 +209,6 @@ cpp::result MigrationManager::DoDownDB(int version) { switch (version) { case 0: return v0::MigrateDBDown(db_); - break; case 1: return v1::MigrateDBDown(db_); case 2: diff --git a/engine/migrations/v2/migration.h b/engine/migrations/v2/migration.h index d28020645..54b79f666 100644 --- a/engine/migrations/v2/migration.h +++ b/engine/migrations/v2/migration.h @@ -60,10 +60,7 @@ inline cpp::result MigrateDBUp(SQLite::Database& db) { if (table_exists) { // Alter existing table - cortex::mgr::AddColumnIfNotExists(db, "models", "model_format", "TEXT"); - cortex::mgr::AddColumnIfNotExists(db, "models", "model_source", "TEXT"); - cortex::mgr::AddColumnIfNotExists(db, "models", "status", "TEXT"); - cortex::mgr::AddColumnIfNotExists(db, "models", "engine", "TEXT"); + cortex::mgr::AddColumnIfNotExists(db, "models", "metadata", "TEXT"); } else { // Create new table db.exec( @@ -76,7 +73,8 @@ inline cpp::result MigrateDBUp(SQLite::Database& db) { "model_format TEXT," "model_source TEXT," "status TEXT," - "engine TEXT" + "engine TEXT," + "metadata TEXT" ")"); } } @@ -141,15 +139,21 @@ inline cpp::result MigrateDBDown(SQLite::Database& db) { "author_repo_id TEXT," "branch_name TEXT," "path_to_model_yaml TEXT," - "model_alias TEXT" + "model_alias TEXT," + "model_format TEXT," + "model_source TEXT," + "status TEXT," + "engine TEXT" ")"); // Copy data from the current table to the new table db.exec( "INSERT INTO models_old (model_id, author_repo_id, branch_name, " - "path_to_model_yaml, model_alias) " + "path_to_model_yaml, model_alias, model_format, model_source, " + "status, engine) " "SELECT model_id, author_repo_id, branch_name, path_to_model_yaml, " - "model_alias FROM models"); + "model_alias, model_format, model_source, status, engine FROM " + "models"); // Drop the current table db.exec("DROP TABLE models");