Resource.h 2.8 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*******************************************************************************
 * 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>
#include <thread>
#include <functional>
#include <condition_variable>

#include "../TaskTable.h"
16
#include "../task/Task.h"
W
wxyu 已提交
17 18
#include "../Cost.h"
#include "Connection.h"
X
xj.lin 已提交
19 20
#include "Node.h"
#include "RegisterHandler.h"
W
wxyu 已提交
21 22 23 24 25 26 27 28 29 30 31 32


namespace zilliz {
namespace milvus {
namespace engine {

enum class ResourceType {
    DISK = 0,
    CPU = 1,
    GPU = 2
};

X
xj.lin 已提交
33 34 35 36 37 38 39
enum class RegisterType {
    START_UP,
    ON_FINISH_TASK,
    ON_COPY_COMPLETED,
    ON_TASK_TABLE_UPDATED,
};

W
wxyu 已提交
40 41
class Resource : public Node {
public:
X
xj.lin 已提交
42 43 44 45 46 47
    /*
     * Event function MUST be a short function, never blocking;
     */
    template <typename T>
    void Register_T(const RegisterType& type) {
        register_table_.emplace(type, [] { return std::make_shared<T>(); });
W
wxyu 已提交
48 49
    }

X
xj.lin 已提交
50 51 52
    RegisterHandlerPtr
    GetRegisterFunc(const RegisterType& type);

W
wxyu 已提交
53
    void
X
xj.lin 已提交
54 55 56 57
    Start();

    void
    Stop();
W
wxyu 已提交
58 59

    TaskTable &
X
xj.lin 已提交
60
    task_table();
W
wxyu 已提交
61 62 63 64 65 66

public:
    /*
     * wake up executor;
     */
    void
X
xj.lin 已提交
67
    WakeupExecutor();
W
wxyu 已提交
68 69 70 71 72

    /* 
     * wake up loader;
     */
    void
X
xj.lin 已提交
73
    WakeupLoader();
W
wxyu 已提交
74 75

protected:
X
xj.lin 已提交
76
    Resource(std::string name, ResourceType type);
W
wxyu 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    // TODO: SearchContextPtr to TaskPtr
    /*
     * Implementation by inherit class;
     * Blocking function;
     */
    virtual void
    LoadFile(TaskPtr task) = 0;

    /*
     * Implementation by inherit class;
     * Blocking function;
     */
    virtual void
    Process(TaskPtr task) = 0;

private:
    /*
     * These function should move to cost.h ???
     * COST.H ???
     */

    /*
     * Pick one task to load;
     * Order by start time;
     */
    TaskPtr
X
xj.lin 已提交
104
    pick_task_load();
W
wxyu 已提交
105 106 107 108 109 110

    /*
     * Pick one task to execute;
     * Pick by start time and priority;
     */
    TaskPtr
X
xj.lin 已提交
111
    pick_task_execute();
W
wxyu 已提交
112 113 114 115 116 117

private:
    /*
     * Only called by load thread;
     */
    void
X
xj.lin 已提交
118
    loader_function();
W
wxyu 已提交
119 120 121 122 123

    /*
     * Only called by worker thread;
     */
    void
X
xj.lin 已提交
124
    executor_function();
W
wxyu 已提交
125 126 127 128 129 130 131 132


private:
    std::string name_;
    ResourceType type_;

    TaskTable task_table_;

X
xj.lin 已提交
133
    std::map<RegisterType, std::function<RegisterHandlerPtr()>> register_table_;
W
wxyu 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    bool running_;
    std::thread loader_thread_;
    std::thread executor_thread_;

    bool load_flag_;
    bool exec_flag_;
    std::mutex load_mutex_;
    std::mutex exec_mutex_;
    std::condition_variable load_cv_;
    std::condition_variable exec_cv_;
};

using ResourcePtr = std::shared_ptr<Resource>;
using ResourceWPtr = std::weak_ptr<Resource>;

}
}
}