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

W
wxyu 已提交
15 16
#include "../event/Event.h"
#include "../event/StartUpEvent.h"
17
#include "../event/LoadCompletedEvent.h"
W
wxyu 已提交
18
#include "../event/FinishTaskEvent.h"
W
wxyu 已提交
19
#include "../event/TaskTableUpdatedEvent.h"
W
wxyu 已提交
20
#include "../TaskTable.h"
21
#include "../task/Task.h"
W
wxyu 已提交
22
#include "Connection.h"
X
xj.lin 已提交
23
#include "Node.h"
W
wxyu 已提交
24 25 26 27 28 29


namespace zilliz {
namespace milvus {
namespace engine {

W
wxyu 已提交
30
// TODO(wxyu): Storage, Route, Executor
W
wxyu 已提交
31 32 33 34 35 36
enum class ResourceType {
    DISK = 0,
    CPU = 1,
    GPU = 2
};

W
wxyu 已提交
37
class Resource : public Node, public std::enable_shared_from_this<Resource> {
Y
Yu Kun 已提交
38
 public:
X
xj.lin 已提交
39
    /*
W
wxyu 已提交
40
     * Start loader and executor if enable;
X
xj.lin 已提交
41
     */
W
wxyu 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    void
    Start();

    /*
     * Stop loader and executor, join it, blocking util thread exited;
     */
    void
    Stop();

    /*
     * wake up loader;
     */
    void
    WakeupLoader();

    /*
     * wake up executor;
     */
    void
    WakeupExecutor();

W
wxyu 已提交
63 64 65 66 67
    inline void
    RegisterSubscriber(std::function<void(EventPtr)> subscriber) {
        subscriber_ = std::move(subscriber);
    }

W
wxyu 已提交
68 69 70 71 72 73
    inline virtual std::string
    Dump() const {
        return "<Resource>";
    }

 public:
74
    inline std::string
W
wxyu 已提交
75
    name() const {
76 77 78
        return name_;
    }

W
wxyu 已提交
79
    inline ResourceType
W
wxyu 已提交
80
    type() const {
W
wxyu 已提交
81 82
        return type_;
    }
X
xj.lin 已提交
83

84
    inline uint64_t
W
wxyu 已提交
85
    device_id() const {
86 87 88
        return device_id_;
    }

W
wxyu 已提交
89 90 91 92 93 94
    TaskTable &
    task_table() {
        return task_table_;
    }

public:
W
wxyu 已提交
95
    inline bool
W
wxyu 已提交
96
    HasLoader() const {
W
wxyu 已提交
97 98
        return enable_loader_;
    }
X
xj.lin 已提交
99

W
wxyu 已提交
100
    inline bool
W
wxyu 已提交
101
    HasExecutor() const {
W
wxyu 已提交
102 103
        return enable_executor_;
    }
W
wxyu 已提交
104

Y
Yu Kun 已提交
105 106
    // TODO: const
    uint64_t
W
wxyu 已提交
107
    NumOfTaskToExec();
Y
Yu Kun 已提交
108 109 110 111 112 113 114

    // TODO: need double ?
    inline uint64_t
    TaskAvgCost() const {
        return total_cost_ / total_task_;
    }

Y
Yu Kun 已提交
115 116 117 118 119
    inline uint64_t
    TotalTasks() const {
        return total_task_;
    }

120 121
    friend std::ostream &operator<<(std::ostream &out, const Resource &resource);

Y
Yu Kun 已提交
122
 protected:
123 124
    Resource(std::string name,
             ResourceType type,
125
             uint64_t device_id,
W
wxyu 已提交
126 127
             bool enable_loader,
             bool enable_executor);
W
wxyu 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    // 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;

Y
Yu Kun 已提交
144
 private:
W
wxyu 已提交
145 146 147 148 149 150 151 152 153
    /*
     * These function should move to cost.h ???
     * COST.H ???
     */

    /*
     * Pick one task to load;
     * Order by start time;
     */
154
    TaskTableItemPtr
X
xj.lin 已提交
155
    pick_task_load();
W
wxyu 已提交
156 157 158 159 160

    /*
     * Pick one task to execute;
     * Pick by start time and priority;
     */
161
    TaskTableItemPtr
X
xj.lin 已提交
162
    pick_task_execute();
W
wxyu 已提交
163

Y
Yu Kun 已提交
164
 private:
W
wxyu 已提交
165 166 167 168
    /*
     * Only called by load thread;
     */
    void
X
xj.lin 已提交
169
    loader_function();
W
wxyu 已提交
170 171 172 173 174

    /*
     * Only called by worker thread;
     */
    void
X
xj.lin 已提交
175
    executor_function();
W
wxyu 已提交
176

Y
Yu Kun 已提交
177
 protected:
178
    uint64_t device_id_;
W
wxyu 已提交
179
    std::string name_;
W
wxyu 已提交
180

Y
Yu Kun 已提交
181
 private:
W
wxyu 已提交
182 183 184 185
    ResourceType type_;

    TaskTable task_table_;

Y
fix bug  
Yu Kun 已提交
186 187
    uint64_t total_cost_ = 0;
    uint64_t total_task_ = 0;
Y
Yu Kun 已提交
188

W
wxyu 已提交
189
    std::function<void(EventPtr)> subscriber_ = nullptr;
W
wxyu 已提交
190

W
wxyu 已提交
191
    bool running_ = false;
192 193
    bool enable_loader_ = true;
    bool enable_executor_ = true;
W
wxyu 已提交
194 195 196
    std::thread loader_thread_;
    std::thread executor_thread_;

W
wxyu 已提交
197 198
    bool load_flag_ = false;
    bool exec_flag_ = false;
W
wxyu 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
    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>;

}
}
}