Resource.h 5.1 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 77
/*******************************************************************************
 * 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"
#include "../Task.h"
#include "../Cost.h"
#include "Node.h"
#include "Connection.h"


namespace zilliz {
namespace milvus {
namespace engine {

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

class Resource : public Node {
public:
    void
    Start() {
        loader_thread_ = std::thread(&Resource::loader_function, this);
        executor_thread_ = std::thread(&Resource::executor_function, this);
    }

    void
    Stop() {
        running_ = false;
        WakeupLoader();
        WakeupExecutor();
    }

    TaskTable &
    task_table() {
        return task_table_;
    }

public:
    /*
     * wake up executor;
     */
    void
    WakeupExecutor() {
        exec_cv_.notify_one();
    }

    /* 
     * wake up loader;
     */
    void
    WakeupLoader() {
        load_cv_.notify_one();
    }

public:
    /*
     * Event function MUST be a short function, never blocking;
     */

    /*
     * Register on start up event;
     */
    void
G
groot 已提交
78 79 80
    RegisterOnStartUp(std::function<void(void)> func) {
        on_start_up_ = func;
    }
W
wxyu 已提交
81 82 83 84 85

    /*
     * Register on finish one task event;
     */
    void
G
groot 已提交
86 87 88
    RegisterOnFinishTask(std::function<void(void)> func) {
        on_finish_task_ = func;
    }
W
wxyu 已提交
89 90 91 92 93

    /*
     * Register on copy task data completed event;
     */
    void
G
groot 已提交
94 95 96
    RegisterOnCopyCompleted(std::function<void(void)> func) {
        on_copy_completed_ = func;
    }
W
wxyu 已提交
97 98 99 100 101

    /*
     * Register on task table updated event;
     */
    void
G
groot 已提交
102 103 104
    RegisterOnTaskTableUpdated(std::function<void(void)> func) {
        on_task_table_updated_ = func;
    }
W
wxyu 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

protected:
    Resource(std::string name, ResourceType type)
        : name_(std::move(name)),
          type_(type),
          on_start_up_(nullptr),
          on_finish_task_(nullptr),
          on_copy_completed_(nullptr),
          on_task_table_updated_(nullptr),
          running_(false),
          load_flag_(false),
          exec_flag_(false) {
    }

    // 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
    pick_task_load() {
        auto tasks = PickToLoad(task_table_, 3);
        for (uint64_t i = 0; i < tasks.size(); ++i) {
            // try to set one task loading, then return
            if (task_table_.Load(i))
                return task_table_.Get(i).task;
            // else try next
        }
        return nullptr;
    }

    /*
     * Pick one task to execute;
     * Pick by start time and priority;
     */
    TaskPtr
    pick_task_execute() {
        auto tasks = PickToExecute(task_table_, 3);
        for (uint64_t i = 0; i < tasks.size(); ++i) {
            // try to set one task executing, then return
            if (task_table_.Execute(i))
                return task_table_.Get(i).task;
            // else try next
        }
        return nullptr;
    }

private:
    /*
     * Only called by load thread;
     */
    void
    loader_function() {
        while (running_) {
            std::unique_lock<std::mutex> lock(load_mutex_);
            load_cv_.wait(lock, [&] { return load_flag_; });
            auto task = pick_task_load();
            if (task) {
                LoadFile(task);
                on_copy_completed_();
            }
        }

    }

    /*
     * Only called by worker thread;
     */
    void
    executor_function() {
        on_start_up_();
        while (running_) {
            std::unique_lock<std::mutex> lock(exec_mutex_);
            exec_cv_.wait(lock, [&] { return exec_flag_; });
            auto task = pick_task_execute();
            if (task) {
                Process(task);
                on_finish_task_();
            }
        }
    }


private:
    std::string name_;
    ResourceType type_;

    TaskTable task_table_;

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

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

}
}
}