ResourceMgr.h 2.2 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
#pragma once

#include <string>
#include <vector>
#include <memory>
S
starlord 已提交
11
#include <mutex>
W
wxyu 已提交
12
#include <queue>
S
starlord 已提交
13
#include <condition_variable>
W
wxyu 已提交
14

S
starlord 已提交
15
#include "resource/Resource.h"
16
#include "utils/Log.h"
W
wxyu 已提交
17

W
wxyu 已提交
18

W
wxyu 已提交
19 20 21 22 23 24
namespace zilliz {
namespace milvus {
namespace engine {

class ResourceMgr {
public:
S
starlord 已提交
25
    ResourceMgr();
W
wxyu 已提交
26 27

    /******** Management Interface ********/
W
wxyu 已提交
28 29 30 31 32 33 34 35 36
    inline void
    RegisterSubscriber(std::function<void(EventPtr)> subscriber) {
        subscriber_ = std::move(subscriber);
    }

    std::vector<ResourceWPtr> &
    GetDiskResources() {
        return disk_resources_;
    }
W
wxyu 已提交
37

W
wxyu 已提交
38 39 40 41 42 43
    /*
     * Return account of resource which enable executor;
     */
    uint64_t
    GetNumOfComputeResource();

W
wxyu 已提交
44 45 46 47 48 49
    /*
     * Add resource into Resource Management;
     * Generate functions on events;
     * Functions only modify bool variable, like event trigger;
     */
    ResourceWPtr
S
starlord 已提交
50
    Add(ResourcePtr &&resource);
W
wxyu 已提交
51 52 53 54 55

    /*
     * Create connection between A and B;
     */
    void
S
starlord 已提交
56
    Connect(ResourceWPtr &res1, ResourceWPtr &res2, Connection &connection);
W
wxyu 已提交
57 58 59 60 61 62

    /*
     * Synchronous start all resource;
     * Last, start event process thread;
     */
    void
S
starlord 已提交
63 64 65 66 67
    Start();

    void
    Stop();

68
    void
69
    PostEvent(const EventPtr &event);
W
wxyu 已提交
70 71 72 73 74 75 76 77 78

    // TODO: add stats interface(low)

public:
    /******** Utlitity Functions ********/

    std::string
    Dump();

79 80 81
    std::string
    DumpTaskTables();

W
wxyu 已提交
82 83
private:
    void
84
    event_process();
W
wxyu 已提交
85 86

private:
W
wxyu 已提交
87 88 89
    std::queue<EventPtr> queue_;
    std::function<void(EventPtr)> subscriber_ = nullptr;

W
wxyu 已提交
90 91
    bool running_;

W
wxyu 已提交
92
    std::vector<ResourceWPtr> disk_resources_;
W
wxyu 已提交
93
    std::vector<ResourcePtr> resources_;
S
starlord 已提交
94
    mutable std::mutex resources_mutex_;
W
wxyu 已提交
95 96
    std::thread worker_thread_;

W
wxyu 已提交
97
    std::mutex event_mutex_;
S
starlord 已提交
98
    std::condition_variable event_cv_;
W
wxyu 已提交
99

W
wxyu 已提交
100 101
};

W
wxyu 已提交
102
using ResourceMgrPtr = std::shared_ptr<ResourceMgr>;
W
wxyu 已提交
103 104 105 106 107 108
using ResourceMgrWPtr = std::weak_ptr<ResourceMgr>;

}
}
}