diff --git a/cpp/CHANGELOG.md b/cpp/CHANGELOG.md index 87dfa83427f77ab130711256b7cc90d5c734c72e..a7ddee104a3639556a54cd255b58825324d414a1 100644 --- a/cpp/CHANGELOG.md +++ b/cpp/CHANGELOG.md @@ -33,6 +33,7 @@ Please mark all change in change log and use the ticket from JIRA. - MS-379 - Add Dump implementation in Resource - MS-380 - Update resource loader and executor, work util all finished - MS-383 - Modify condition variable usage in scheduler +- MS-384 - Add global instance of ResourceMgr and Scheduler ## New Feature - MS-343 - Implement ResourceMgr diff --git a/cpp/src/scheduler/ResourceMgr.h b/cpp/src/scheduler/ResourceMgr.h index cb2e63193524194971ac9fac197f0b91146fccca..5d273a4f3881bfb11167babad8842c43cead9627 100644 --- a/cpp/src/scheduler/ResourceMgr.h +++ b/cpp/src/scheduler/ResourceMgr.h @@ -1,4 +1,3 @@ - /******************************************************************************* * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited. @@ -61,7 +60,7 @@ public: Stop(); void - PostEvent(const EventPtr& event); + PostEvent(const EventPtr &event); // TODO: add stats interface(low) diff --git a/cpp/src/scheduler/SchedInst.cpp b/cpp/src/scheduler/SchedInst.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d975c164331c6ec4ca8f4da8b877219e4be3c41 --- /dev/null +++ b/cpp/src/scheduler/SchedInst.cpp @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ + +#include "SchedInst.h" + + +namespace zilliz { +namespace milvus { +namespace engine { + +ResourceMgrPtr ResMgrInst::instance = nullptr; +std::mutex ResMgrInst::mutex_; + +SchedulerPtr SchedInst::instance = nullptr; +std::mutex SchedInst::mutex_; + +} +} +} diff --git a/cpp/src/scheduler/SchedInst.h b/cpp/src/scheduler/SchedInst.h new file mode 100644 index 0000000000000000000000000000000000000000..c05f525e3a3d69f5ef07cf2c4a30c8900569b8f9 --- /dev/null +++ b/cpp/src/scheduler/SchedInst.h @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once + +#include "ResourceMgr.h" +#include "Scheduler.h" + +#include +#include + + +namespace zilliz { +namespace milvus { +namespace engine { + +class ResMgrInst { +public: + static ResourceMgrPtr + GetInstance() { + if (instance == nullptr) { + std::lock_guard lock(mutex_); + if (instance == nullptr) { + instance = std::make_shared(); + } + } + return instance; + } + +private: + static ResourceMgrPtr instance; + static std::mutex mutex_; +}; + +class SchedInst { +public: + static SchedulerPtr + GetInstance() { + if (instance == nullptr) { + std::lock_guard lock(mutex_); + if (instance == nullptr) { + instance = std::make_shared(ResMgrInst::GetInstance()); + } + } + return instance; + } + +private: + static SchedulerPtr instance; + static std::mutex mutex_; +}; + +} +} +} diff --git a/cpp/unittest/scheduler/normal_test.cpp b/cpp/unittest/scheduler/normal_test.cpp index d9e4ad218c11c216eb18b1bef9af96d62c9f4f6a..27d16a1b6b8bd7de3719d2a3339aabd761c9a702 100644 --- a/cpp/unittest/scheduler/normal_test.cpp +++ b/cpp/unittest/scheduler/normal_test.cpp @@ -2,6 +2,7 @@ #include "scheduler/ResourceMgr.h" #include "scheduler/Scheduler.h" #include "scheduler/task/TestTask.h" +#include "scheduler/SchedInst.h" #include "utils/Log.h" #include @@ -10,7 +11,8 @@ using namespace zilliz::milvus::engine; TEST(normal_test, test1) { // ResourceMgr only compose resources, provide unified event - auto res_mgr = std::make_shared(); +// auto res_mgr = std::make_shared(); + auto res_mgr = ResMgrInst::GetInstance(); auto disk = res_mgr->Add(ResourceFactory::Create("disk", "ssd")); auto cpu = res_mgr->Add(ResourceFactory::Create("cpu")); auto gpu1 = res_mgr->Add(ResourceFactory::Create("gpu")); @@ -24,10 +26,11 @@ TEST(normal_test, test1) { res_mgr->Start(); - auto scheduler = new Scheduler(res_mgr); +// auto scheduler = new Scheduler(res_mgr); + auto scheduler = SchedInst::GetInstance(); scheduler->Start(); - const uint64_t NUM_TASK = 10; + const uint64_t NUM_TASK = 100; std::vector> tasks; for (uint64_t i = 0; i < NUM_TASK; ++i) { if (auto observe = disk.lock()) { @@ -37,45 +40,8 @@ TEST(normal_test, test1) { } } - if (auto disk_r = disk.lock()) { - if (auto cpu_r = cpu.lock()) { - if (auto gpu1_r = gpu1.lock()) { - if (auto gpu2_r = gpu2.lock()) { - std::cout << "<<<<<<<<<task_table().Dump() << std::endl; - std::cout << "cpu:" << std::endl; - std::cout << cpu_r->task_table().Dump() << std::endl; - std::cout << "gpu1:" << std::endl; - std::cout << gpu1_r->task_table().Dump() << std::endl; - std::cout << "gpu2:" << std::endl; - std::cout << gpu2_r->task_table().Dump() << std::endl; - std::cout << ">>>>>>>>>>before>>>>>>>>>>" << std::endl; - } - } - } - } - sleep(1); - if (auto disk_r = disk.lock()) { - if (auto cpu_r = cpu.lock()) { - if (auto gpu1_r = gpu1.lock()) { - if (auto gpu2_r = gpu2.lock()) { - std::cout << "<<<<<<<<<task_table().Dump() << std::endl; - std::cout << "cpu:" << std::endl; - std::cout << cpu_r->task_table().Dump() << std::endl; - std::cout << "gpu1:" << std::endl; - std::cout << gpu1_r->task_table().Dump() << std::endl; - std::cout << "gpu2:" << std::endl; - std::cout << gpu2_r->task_table().Dump() << std::endl; - std::cout << ">>>>>>>>>>after>>>>>>>>>>" << std::endl; - } - } - } - } scheduler->Stop(); res_mgr->Stop();