Resource.cpp 3.2 KB
Newer Older
X
xj.lin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*******************************************************************************
 * 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) {
W
wxyu 已提交
19 20 21 22 23 24
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
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
}

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();
}

50
TaskTableItemPtr Resource::pick_task_load() {
X
xj.lin 已提交
51 52 53 54
    auto indexes = PickToLoad(task_table_, 3);
    for (auto index : indexes) {
        // try to set one task loading, then return
        if (task_table_.Load(index))
55
            return task_table_.Get(index);
X
xj.lin 已提交
56 57 58 59 60
        // else try next
    }
    return nullptr;
}

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

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

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

}
}
}