Resource.cpp 6.9 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
J
jinhai 已提交
5
//
6 7 8 9 10
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "scheduler/resource/Resource.h"
13
#include "scheduler/SchedInst.h"
S
starlord 已提交
14
#include "scheduler/Utils.h"
W
Wang Xiangyu 已提交
15
#include "scheduler/task/FinishedTask.h"
X
xj.lin 已提交
16

S
starlord 已提交
17
#include <iostream>
W
wxyu 已提交
18
#include <limits>
S
starlord 已提交
19
#include <utility>
X
xj.lin 已提交
20 21

namespace milvus {
W
wxyu 已提交
22
namespace scheduler {
X
xj.lin 已提交
23

S
starlord 已提交
24 25
std::ostream&
operator<<(std::ostream& out, const Resource& resource) {
26 27 28 29
    out << resource.Dump();
    return out;
}

W
wxyu 已提交
30 31 32 33 34 35 36 37 38 39 40 41
std::string
ToString(ResourceType type) {
    switch (type) {
        case ResourceType::DISK: {
            return "DISK";
        }
        case ResourceType::CPU: {
            return "CPU";
        }
        case ResourceType::GPU: {
            return "GPU";
        }
W
wxyu 已提交
42
        default: { return "UNKNOWN"; }
W
wxyu 已提交
43 44 45
    }
}

W
Wang XiangYu 已提交
46
Resource::Resource(std::string name, ResourceType type, uint64_t device_id, bool enable_executor)
C
Cai Yudong 已提交
47
    : device_id_(device_id), name_(std::move(name)), type_(type), enable_executor_(enable_executor) {
W
wxyu 已提交
48
    // register subscriber in tasktable
W
wxyu 已提交
49 50 51 52 53 54
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
55 56
}

W
wxyu 已提交
57 58
void
Resource::Start() {
W
wxyu 已提交
59
    running_ = true;
W
Wang XiangYu 已提交
60
    loader_thread_ = std::thread(&Resource::loader_function, this);
61 62 63
    if (enable_executor_) {
        executor_thread_ = std::thread(&Resource::executor_function, this);
    }
X
xj.lin 已提交
64 65
}

W
wxyu 已提交
66 67
void
Resource::Stop() {
X
xj.lin 已提交
68
    running_ = false;
W
Wang XiangYu 已提交
69 70
    WakeupLoader();
    loader_thread_.join();
71 72 73 74
    if (enable_executor_) {
        WakeupExecutor();
        executor_thread_.join();
    }
X
xj.lin 已提交
75 76
}

W
wxyu 已提交
77 78
void
Resource::WakeupLoader() {
79 80 81 82
    {
        std::lock_guard<std::mutex> lock(load_mutex_);
        load_flag_ = true;
    }
X
xj.lin 已提交
83 84 85
    load_cv_.notify_one();
}

W
wxyu 已提交
86 87
void
Resource::WakeupExecutor() {
88 89 90 91
    {
        std::lock_guard<std::mutex> lock(exec_mutex_);
        exec_flag_ = true;
    }
W
wxyu 已提交
92 93 94
    exec_cv_.notify_one();
}

W
wxyu 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
json
Resource::Dump() const {
    json ret{
        {"device_id", device_id_},
        {"name", name_},
        {"type", ToString(type_)},
        {"task_average_cost", TaskAvgCost()},
        {"task_total_cost", total_cost_},
        {"total_tasks", total_task_},
        {"running", running_},
        {"enable_executor", enable_executor_},
    };
    return ret;
}

W
wxyu 已提交
110 111
uint64_t
Resource::NumOfTaskToExec() {
W
wxyu 已提交
112
    return task_table_.TaskToExecute();
W
wxyu 已提交
113 114
}

S
starlord 已提交
115 116
TaskTableItemPtr
Resource::pick_task_load() {
117
    auto indexes = task_table_.PickToLoad(10);
X
xj.lin 已提交
118 119
    for (auto index : indexes) {
        // try to set one task loading, then return
C
Cai Yudong 已提交
120
        if (task_table_.Load(index)) {
121
            return task_table_.at(index);
C
Cai Yudong 已提交
122
        }
X
xj.lin 已提交
123 124 125 126 127
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
128 129
TaskTableItemPtr
Resource::pick_task_execute() {
130
    auto indexes = task_table_.PickToExecute(std::numeric_limits<uint64_t>::max());
X
xj.lin 已提交
131 132
    for (auto index : indexes) {
        // try to set one task executing, then return
W
wxyu 已提交
133
        if (task_table_[index]->task->label()->Type() == TaskLabelType::SPECIFIED_RESOURCE) {
Y
Yu Kun 已提交
134 135
            if (task_table_[index]->task->path().Last() != name()) {
                continue;
136 137
            }
        }
Y
Yu Kun 已提交
138 139

        if (task_table_.Execute(index)) {
140
            return task_table_.at(index);
Y
Yu Kun 已提交
141
        }
Y
Yu Kun 已提交
142 143 144 145 146 147 148 149 150
        //        if (task_table_[index]->task->label()->Type() == TaskLabelType::SPECIFIED_RESOURCE) {
        //            if (task_table_.Get(index)->task->path().Current() == task_table_.Get(index)->task->path().Last()
        //            &&
        //                task_table_.Get(index)->task->path().Last() == name()) {
        //                if (task_table_.Execute(index)) {
        //                    return task_table_.Get(index);
        //                }
        //            }
        //        }
X
xj.lin 已提交
151 152 153 154 155
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
156 157
void
Resource::loader_function() {
158
    SetThreadName("taskloader_th");
X
xj.lin 已提交
159 160
    while (running_) {
        std::unique_lock<std::mutex> lock(load_mutex_);
S
starlord 已提交
161
        load_cv_.wait(lock, [&] { return load_flag_; });
W
wxyu 已提交
162
        load_flag_ = false;
163
        lock.unlock();
164 165 166 167 168
        while (true) {
            auto task_item = pick_task_load();
            if (task_item == nullptr) {
                break;
            }
169 170
            if (task_item->task->Type() == TaskType::BuildIndexTask && name() == "cpu") {
                BuildMgrInst::GetInstance()->Take();
171
                LOG_SERVER_DEBUG_ << name() << " load BuildIndexTask";
172
            }
173
            LoadFile(task_item->task);
174
            task_item->Loaded();
175 176
            if (task_item->from) {
                task_item->from->Moved();
W
Wang Xiangyu 已提交
177
                //                task_item->from->task = FinishedTask::Create();
178 179
                task_item->from = nullptr;
            }
W
wxyu 已提交
180
            if (subscriber_) {
181
                auto event = std::make_shared<LoadCompletedEvent>(shared_from_this(), task_item);
182
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
183
            }
X
xj.lin 已提交
184 185 186 187
        }
    }
}

S
starlord 已提交
188 189
void
Resource::executor_function() {
190
    SetThreadName("taskexecutor_th");
W
wxyu 已提交
191
    if (subscriber_) {
192 193
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
194
    }
X
xj.lin 已提交
195 196
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
S
starlord 已提交
197
        exec_cv_.wait(lock, [&] { return exec_flag_; });
W
wxyu 已提交
198
        exec_flag_ = false;
199
        lock.unlock();
200 201 202 203 204
        while (true) {
            auto task_item = pick_task_execute();
            if (task_item == nullptr) {
                break;
            }
205
            auto start = get_current_timestamp();
206
            Process(task_item->task);
W
Wang Xiangyu 已提交
207
            //            task_item->task = FinishedTask::Create();
208
            auto finish = get_current_timestamp();
Y
Yu Kun 已提交
209 210 211
            ++total_task_;
            total_cost_ += finish - start;

212
            task_item->Executed();
213 214 215 216

            if (task_item->task->Type() == TaskType::BuildIndexTask) {
                BuildMgrInst::GetInstance()->Put();
                ResMgrInst::GetInstance()->GetResource("cpu")->WakeupLoader();
Y
Yu Kun 已提交
217
                ResMgrInst::GetInstance()->GetResource("disk")->WakeupLoader();
218 219
            }

220 221
            task_item->task = FinishedTask::Create(task_item->task);

W
wxyu 已提交
222
            if (subscriber_) {
223 224
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
225
            }
X
xj.lin 已提交
226 227 228 229
        }
    }
}

S
starlord 已提交
230 231
}  // namespace scheduler
}  // namespace milvus