ResourceMgr.h 2.7 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11

/*******************************************************************************
 * 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 已提交
12 13
#include <mutex>
#include <condition_variable>
W
wxyu 已提交
14

S
starlord 已提交
15
#include "resource/Resource.h"
W
wxyu 已提交
16 17 18 19 20 21 22

namespace zilliz {
namespace milvus {
namespace engine {

class ResourceMgr {
public:
S
starlord 已提交
23
    ResourceMgr();
W
wxyu 已提交
24 25 26 27 28 29 30 31 32

    /******** Management Interface ********/

    /*
     * Add resource into Resource Management;
     * Generate functions on events;
     * Functions only modify bool variable, like event trigger;
     */
    ResourceWPtr
S
starlord 已提交
33
    Add(ResourcePtr &&resource);
W
wxyu 已提交
34 35 36 37 38

    /*
     * Create connection between A and B;
     */
    void
S
starlord 已提交
39
    Connect(ResourceWPtr &res1, ResourceWPtr &res2, Connection &connection);
W
wxyu 已提交
40 41 42 43 44 45

    /*
     * Synchronous start all resource;
     * Last, start event process thread;
     */
    void
S
starlord 已提交
46 47 48 49 50
    Start();

    void
    Stop();

W
wxyu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    // TODO: add stats interface(low)

public:
    /******** Event Register Interface ********/

    /*
     * Register on start up event;
     */
    void
    RegisterOnStartUp(std::function<void(ResourceWPtr)> &func) {
        on_start_up_ = func;
    }

    /*
     * Register on finish one task event;
     */
    void
    RegisterOnFinishTask(std::function<void(ResourceWPtr)> &func) {
        on_finish_task_ = func;
    }

    /*
     * Register on copy task data completed event;
     */
    void
S
starlord 已提交
77 78 79
    RegisterOnCopyCompleted(std::function<void(ResourceWPtr)> &func) {
        on_copy_completed_ = func;
    }
W
wxyu 已提交
80 81 82 83 84

    /*
     * Register on task table updated event;
     */
    void
S
starlord 已提交
85 86 87
    RegisterOnTaskTableUpdated(std::function<void(ResourceWPtr)> &func) {
        on_task_table_updated_ = func;
    }
W
wxyu 已提交
88 89 90 91 92 93 94 95 96

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

    std::string
    Dump();

private:
    void
S
starlord 已提交
97
    EventProcess();
W
wxyu 已提交
98 99 100 101 102

private:
    bool running_;

    std::vector<ResourcePtr> resources_;
S
starlord 已提交
103
    mutable std::mutex resources_mutex_;
W
wxyu 已提交
104 105
    std::thread worker_thread_;

S
starlord 已提交
106
    std::condition_variable event_cv_;
W
wxyu 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    std::vector<bool> start_up_event_;
    std::vector<bool> finish_task_event_;
    std::vector<bool> copy_completed_event_;
    std::vector<bool> task_table_updated_event_;

    std::function<void(ResourceWPtr)> on_start_up_;
    std::function<void(ResourceWPtr)> on_finish_task_;
    std::function<void(ResourceWPtr)> on_copy_completed_;
    std::function<void(ResourceWPtr)> on_task_table_updated_;
};

using ResourceMgrWPtr = std::weak_ptr<ResourceMgr>;

}
}
}