Resource.cpp 4.1 KB
Newer Older
X
xj.lin 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
#include "Resource.h"


namespace zilliz {
namespace milvus {
namespace engine {

13 14 15 16 17
std::ostream &operator<<(std::ostream &out, const Resource &resource) {
    out << resource.Dump();
    return out;
}

18 19 20 21
Resource::Resource(std::string name,
                   ResourceType type,
                   bool enable_loader,
                   bool enable_executor)
X
xj.lin 已提交
22 23 24
    : name_(std::move(name)),
      type_(type),
      running_(false),
25 26
      enable_loader_(enable_loader),
      enable_executor_(enable_executor),
X
xj.lin 已提交
27 28
      load_flag_(false),
      exec_flag_(false) {
W
wxyu 已提交
29 30 31 32 33 34
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
35 36 37
}

void Resource::Start() {
W
wxyu 已提交
38
    running_ = true;
39 40 41 42 43 44
    if (enable_loader_) {
        loader_thread_ = std::thread(&Resource::loader_function, this);
    }
    if (enable_executor_) {
        executor_thread_ = std::thread(&Resource::executor_function, this);
    }
X
xj.lin 已提交
45 46 47 48
}

void Resource::Stop() {
    running_ = false;
49 50 51 52 53 54 55 56
    if (enable_loader_) {
        WakeupLoader();
        loader_thread_.join();
    }
    if (enable_executor_) {
        WakeupExecutor();
        executor_thread_.join();
    }
X
xj.lin 已提交
57 58 59 60 61 62 63
}

TaskTable &Resource::task_table() {
    return task_table_;
}

void Resource::WakeupLoader() {
W
wxyu 已提交
64 65
    std::lock_guard<std::mutex> lock(load_mutex_);
    load_flag_ = true;
X
xj.lin 已提交
66 67 68
    load_cv_.notify_one();
}

W
wxyu 已提交
69 70 71 72 73 74
void Resource::WakeupExecutor() {
    std::lock_guard<std::mutex> lock(exec_mutex_);
    exec_flag_ = true;
    exec_cv_.notify_one();
}

75
TaskTableItemPtr Resource::pick_task_load() {
X
xj.lin 已提交
76 77 78 79
    auto indexes = PickToLoad(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task loading, then return
        if (task_table_.Load(index))
80
            return task_table_.Get(index);
X
xj.lin 已提交
81 82 83 84 85
        // else try next
    }
    return nullptr;
}

86
TaskTableItemPtr Resource::pick_task_execute() {
X
xj.lin 已提交
87 88 89 90
    auto indexes = PickToExecute(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task executing, then return
        if (task_table_.Execute(index))
91
            return task_table_.Get(index);
X
xj.lin 已提交
92 93 94 95 96 97 98 99 100
        // else try next
    }
    return nullptr;
}

void Resource::loader_function() {
    while (running_) {
        std::unique_lock<std::mutex> lock(load_mutex_);
        load_cv_.wait(lock, [&] { return load_flag_; });
W
wxyu 已提交
101
        load_flag_ = false;
102 103 104
        auto task_item = pick_task_load();
        if (task_item) {
            LoadFile(task_item->task);
W
wxyu 已提交
105 106
            // TODO: wrapper loaded
            task_item->state = TaskTableItemState::LOADED;
W
wxyu 已提交
107
            if (subscriber_) {
108 109
                auto event = std::make_shared<CopyCompletedEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
110
            }
X
xj.lin 已提交
111 112 113 114 115
        }
    }
}

void Resource::executor_function() {
W
wxyu 已提交
116
    if (subscriber_) {
117 118
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
119
    }
X
xj.lin 已提交
120 121 122
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
        exec_cv_.wait(lock, [&] { return exec_flag_; });
W
wxyu 已提交
123
        exec_flag_ = false;
124 125 126
        auto task_item = pick_task_execute();
        if (task_item) {
            Process(task_item->task);
127
            task_item->state = TaskTableItemState::EXECUTED;
W
wxyu 已提交
128
            if (subscriber_) {
129 130
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
131
            }
X
xj.lin 已提交
132 133 134 135 136 137 138 139 140 141 142 143
        }
    }
}

RegisterHandlerPtr Resource::GetRegisterFunc(const RegisterType &type) {
    // construct object each time.
    return register_table_[type]();
}

}
}
}