Resource.cpp 6.0 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
//
//   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.

S
starlord 已提交
18
#include "scheduler/resource/Resource.h"
19
#include "scheduler/SchedInst.h"
S
starlord 已提交
20
#include "scheduler/Utils.h"
X
xj.lin 已提交
21

S
starlord 已提交
22
#include <iostream>
W
wxyu 已提交
23
#include <limits>
S
starlord 已提交
24
#include <utility>
X
xj.lin 已提交
25 26

namespace milvus {
W
wxyu 已提交
27
namespace scheduler {
X
xj.lin 已提交
28

S
starlord 已提交
29 30
std::ostream&
operator<<(std::ostream& out, const Resource& resource) {
31 32 33 34
    out << resource.Dump();
    return out;
}

S
starlord 已提交
35
Resource::Resource(std::string name, ResourceType type, uint64_t device_id, bool enable_loader, bool enable_executor)
X
xj.lin 已提交
36 37
    : name_(std::move(name)),
      type_(type),
38
      device_id_(device_id),
39
      enable_loader_(enable_loader),
W
wxyu 已提交
40 41
      enable_executor_(enable_executor) {
    // register subscriber in tasktable
W
wxyu 已提交
42 43 44 45 46 47
    task_table_.RegisterSubscriber([&] {
        if (subscriber_) {
            auto event = std::make_shared<TaskTableUpdatedEvent>(shared_from_this());
            subscriber_(std::static_pointer_cast<Event>(event));
        }
    });
X
xj.lin 已提交
48 49
}

W
wxyu 已提交
50 51
void
Resource::Start() {
W
wxyu 已提交
52
    running_ = true;
53 54 55 56 57 58
    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 已提交
59 60
}

W
wxyu 已提交
61 62
void
Resource::Stop() {
X
xj.lin 已提交
63
    running_ = false;
64 65 66 67 68 69 70 71
    if (enable_loader_) {
        WakeupLoader();
        loader_thread_.join();
    }
    if (enable_executor_) {
        WakeupExecutor();
        executor_thread_.join();
    }
X
xj.lin 已提交
72 73
}

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

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

W
wxyu 已提交
92 93 94
uint64_t
Resource::NumOfTaskToExec() {
    uint64_t count = 0;
S
starlord 已提交
95
    for (auto& task : task_table_) {
S
starlord 已提交
96 97
        if (task->state == TaskTableItemState::LOADED)
            ++count;
W
wxyu 已提交
98 99 100 101
    }
    return count;
}

S
starlord 已提交
102 103
TaskTableItemPtr
Resource::pick_task_load() {
104
    auto indexes = task_table_.PickToLoad(10);
X
xj.lin 已提交
105 106
    for (auto index : indexes) {
        // try to set one task loading, then return
S
starlord 已提交
107 108
        if (task_table_.Load(index))
            return task_table_.Get(index);
X
xj.lin 已提交
109 110 111 112 113
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
114 115
TaskTableItemPtr
Resource::pick_task_execute() {
116
    auto indexes = task_table_.PickToExecute(std::numeric_limits<uint64_t>::max());
X
xj.lin 已提交
117 118
    for (auto index : indexes) {
        // try to set one task executing, then return
W
wxyu 已提交
119
        if (task_table_[index]->task->label()->Type() == TaskLabelType::SPECIFIED_RESOURCE) {
Y
Yu Kun 已提交
120 121
            if (task_table_[index]->task->path().Last() != name()) {
                continue;
122 123
            }
        }
Y
Yu Kun 已提交
124 125 126 127

        if (task_table_.Execute(index)) {
            return task_table_.Get(index);
        }
Y
Yu Kun 已提交
128 129 130 131 132 133 134 135 136
        //        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 已提交
137 138 139 140 141
        // else try next
    }
    return nullptr;
}

S
starlord 已提交
142 143
void
Resource::loader_function() {
X
xj.lin 已提交
144 145
    while (running_) {
        std::unique_lock<std::mutex> lock(load_mutex_);
S
starlord 已提交
146
        load_cv_.wait(lock, [&] { return load_flag_; });
W
wxyu 已提交
147
        load_flag_ = false;
148
        lock.unlock();
149 150 151 152 153
        while (true) {
            auto task_item = pick_task_load();
            if (task_item == nullptr) {
                break;
            }
154
            LoadFile(task_item->task);
155
            task_item->Loaded();
W
wxyu 已提交
156
            if (subscriber_) {
157
                auto event = std::make_shared<LoadCompletedEvent>(shared_from_this(), task_item);
158
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
159
            }
X
xj.lin 已提交
160 161 162 163
        }
    }
}

S
starlord 已提交
164 165
void
Resource::executor_function() {
W
wxyu 已提交
166
    if (subscriber_) {
167 168
        auto event = std::make_shared<StartUpEvent>(shared_from_this());
        subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
169
    }
X
xj.lin 已提交
170 171
    while (running_) {
        std::unique_lock<std::mutex> lock(exec_mutex_);
S
starlord 已提交
172
        exec_cv_.wait(lock, [&] { return exec_flag_; });
W
wxyu 已提交
173
        exec_flag_ = false;
174
        lock.unlock();
175 176 177 178 179
        while (true) {
            auto task_item = pick_task_execute();
            if (task_item == nullptr) {
                break;
            }
Y
Yu Kun 已提交
180

181
            auto start = get_current_timestamp();
182
            Process(task_item->task);
183
            auto finish = get_current_timestamp();
Y
Yu Kun 已提交
184 185 186
            ++total_task_;
            total_cost_ += finish - start;

187
            task_item->Executed();
188 189 190 191

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

W
wxyu 已提交
195
            if (subscriber_) {
196 197
                auto event = std::make_shared<FinishTaskEvent>(shared_from_this(), task_item);
                subscriber_(std::static_pointer_cast<Event>(event));
W
wxyu 已提交
198
            }
X
xj.lin 已提交
199 200 201 202
        }
    }
}

S
starlord 已提交
203 204
}  // namespace scheduler
}  // namespace milvus