/******************************************************************************* * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited. * Proprietary and confidential. ******************************************************************************/ #pragma once #include #include #include namespace zilliz { namespace milvus { namespace engine { class ResourceMgr { public: ResourceMgr() : running_(false) {} /******** Management Interface ********/ /* * Add resource into Resource Management; * Generate functions on events; * Functions only modify bool variable, like event trigger; */ ResourceWPtr Add(ResourcePtr &&resource) { ResourceWPtr ret(resource); resources_.emplace_back(resource); // resource->RegisterOnStartUp([] { // start_up_event_[index] = true; // }); // resource.RegisterOnFinishTask([] { // finish_task_event_[index] = true; // }); return ret; } /* * Create connection between A and B; */ void Connect(ResourceWPtr &A, ResourceWPtr &B, Connection &connection) { if (auto observe_a = A.lock()) { if (auto observe_b = B.lock()) { observe_a->AddNeighbour(std::static_pointer_cast(observe_b), connection); } } } /* * Synchronous start all resource; * Last, start event process thread; */ void StartAll() { for (auto &resource : resources_) { resource->Start(); } worker_thread_ = std::thread(&ResourceMgr::EventProcess, this); } // TODO: add stats interface(low) public: /******** Event Register Interface ********/ /* * Register on start up event; */ void RegisterOnStartUp(std::function &func) { on_start_up_ = func; } /* * Register on finish one task event; */ void RegisterOnFinishTask(std::function &func) { on_finish_task_ = func; } /* * Register on copy task data completed event; */ void RegisterOnCopyCompleted(std::function &func); /* * Register on task table updated event; */ void RegisterOnTaskTableUpdated(std::function &func); public: /******** Utlitity Functions ********/ std::string Dump(); private: void EventProcess() { while (running_) { for (uint64_t i = 0; i < resources_.size(); ++i) { if (start_up_event_[i]) { on_start_up_(resources_[i]); } } } } private: bool running_; std::vector resources_; std::thread worker_thread_; std::vector start_up_event_; std::vector finish_task_event_; std::vector copy_completed_event_; std::vector task_table_updated_event_; std::function on_start_up_; std::function on_finish_task_; std::function on_copy_completed_; std::function on_task_table_updated_; }; using ResourceMgrWPtr = std::weak_ptr; } } }