Resource.cpp 3.0 KB
Newer Older
X
xj.lin 已提交
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
/*******************************************************************************
 * 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 {

Resource::Resource(std::string name, ResourceType type)
    : name_(std::move(name)),
      type_(type),
      running_(false),
      load_flag_(false),
      exec_flag_(false) {
}

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

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

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

void Resource::WakeupExecutor() {
    exec_cv_.notify_one();
}

void Resource::WakeupLoader() {
    load_cv_.notify_one();
}

44
TaskTableItemPtr Resource::pick_task_load() {
X
xj.lin 已提交
45 46 47 48
    auto indexes = PickToLoad(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task loading, then return
        if (task_table_.Load(index))
49
            return task_table_.Get(index);
X
xj.lin 已提交
50 51 52 53 54
        // else try next
    }
    return nullptr;
}

55
TaskTableItemPtr Resource::pick_task_execute() {
X
xj.lin 已提交
56 57 58 59
    auto indexes = PickToExecute(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task executing, then return
        if (task_table_.Execute(index))
60
            return task_table_.Get(index);
X
xj.lin 已提交
61 62 63 64 65 66 67 68 69
        // 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_; });
70 71 72
        auto task_item = pick_task_load();
        if (task_item) {
            LoadFile(task_item->task);
W
wxyu 已提交
73
            if (subscriber_) {
74 75
                auto event = std::make_shared<CopyCompletedEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
76
            }
X
xj.lin 已提交
77 78 79 80 81 82
        }
    }
}

void Resource::executor_function() {
    GetRegisterFunc(RegisterType::START_UP)->Exec();
W
wxyu 已提交
83
    if (subscriber_) {
84 85
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
86
    }
X
xj.lin 已提交
87 88 89
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
        exec_cv_.wait(lock, [&] { return exec_flag_; });
90 91 92
        auto task_item = pick_task_execute();
        if (task_item) {
            Process(task_item->task);
W
wxyu 已提交
93
            if (subscriber_) {
94 95
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
96
            }
X
xj.lin 已提交
97 98 99 100 101 102 103 104 105 106 107 108
        }
    }
}

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

}
}
}