TaskTable.cpp 9.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.

G
groot 已提交
18
#include "scheduler/TaskTable.h"
W
wxyu 已提交
19
#include "Utils.h"
G
groot 已提交
20
#include "event/TaskTableUpdatedEvent.h"
Y
Yu Kun 已提交
21
#include "scheduler/SchedInst.h"
22
#include "utils/Log.h"
W
wxyu 已提交
23
#include "utils/TimeRecorder.h"
W
wxyu 已提交
24

25
#include <ctime>
G
groot 已提交
26 27
#include <sstream>
#include <vector>
W
wxyu 已提交
28 29

namespace milvus {
W
wxyu 已提交
30
namespace scheduler {
W
wxyu 已提交
31

32 33 34
std::string
ToString(TaskTableItemState state) {
    switch (state) {
G
groot 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        case TaskTableItemState::INVALID:
            return "INVALID";
        case TaskTableItemState::START:
            return "START";
        case TaskTableItemState::LOADING:
            return "LOADING";
        case TaskTableItemState::LOADED:
            return "LOADED";
        case TaskTableItemState::EXECUTING:
            return "EXECUTING";
        case TaskTableItemState::EXECUTED:
            return "EXECUTED";
        case TaskTableItemState::MOVING:
            return "MOVING";
        case TaskTableItemState::MOVED:
            return "MOVED";
        default:
            return "";
53 54 55
    }
}

W
wxyu 已提交
56
json
W
wxyu 已提交
57
TaskTimestamp::Dump() const {
W
wxyu 已提交
58 59 60 61 62
    json ret{
        {"start", start},       {"load", load}, {"loaded", loaded}, {"execute", execute},
        {"executed", executed}, {"move", move}, {"moved", moved},   {"finish", finish},
    };
    return ret;
63 64
}

65 66 67 68 69
bool
TaskTableItem::IsFinish() {
    return state == TaskTableItemState::MOVED || state == TaskTableItemState::EXECUTED;
}

70 71 72 73 74 75
bool
TaskTableItem::Load() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::START) {
        state = TaskTableItemState::LOADING;
        lock.unlock();
W
wxyu 已提交
76
        timestamp.load = get_current_timestamp();
77 78 79 80
        return true;
    }
    return false;
}
G
groot 已提交
81

82 83 84 85 86 87
bool
TaskTableItem::Loaded() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::LOADING) {
        state = TaskTableItemState::LOADED;
        lock.unlock();
W
wxyu 已提交
88
        timestamp.loaded = get_current_timestamp();
89 90 91 92
        return true;
    }
    return false;
}
G
groot 已提交
93

94 95 96 97 98 99
bool
TaskTableItem::Execute() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::LOADED) {
        state = TaskTableItemState::EXECUTING;
        lock.unlock();
W
wxyu 已提交
100
        timestamp.execute = get_current_timestamp();
101 102 103 104
        return true;
    }
    return false;
}
G
groot 已提交
105

106 107 108 109 110 111
bool
TaskTableItem::Executed() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::EXECUTING) {
        state = TaskTableItemState::EXECUTED;
        lock.unlock();
W
wxyu 已提交
112 113
        timestamp.executed = get_current_timestamp();
        timestamp.finish = get_current_timestamp();
114 115 116 117
        return true;
    }
    return false;
}
G
groot 已提交
118

119 120 121 122 123 124
bool
TaskTableItem::Move() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::LOADED) {
        state = TaskTableItemState::MOVING;
        lock.unlock();
W
wxyu 已提交
125
        timestamp.move = get_current_timestamp();
126 127 128 129
        return true;
    }
    return false;
}
G
groot 已提交
130

131 132 133 134 135 136
bool
TaskTableItem::Moved() {
    std::unique_lock<std::mutex> lock(mutex);
    if (state == TaskTableItemState::MOVING) {
        state = TaskTableItemState::MOVED;
        lock.unlock();
W
wxyu 已提交
137 138
        timestamp.moved = get_current_timestamp();
        timestamp.finish = get_current_timestamp();
139 140 141 142 143
        return true;
    }
    return false;
}

W
wxyu 已提交
144
json
W
wxyu 已提交
145
TaskTableItem::Dump() const {
W
wxyu 已提交
146 147 148 149 150 151 152
    json ret{
        {"id", id},
        {"task", (int64_t)task.get()},
        {"state", ToString(state)},
        {"timestamp", timestamp.Dump()},
    };
    return ret;
153
}
W
wxyu 已提交
154

155 156
std::vector<uint64_t>
TaskTable::PickToLoad(uint64_t limit) {
W
wxyu 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#if 1
    TimeRecorder rc("");
    std::vector<uint64_t> indexes;
    bool cross = false;

    uint64_t available_begin = table_.front() + 1;
    for (uint64_t i = 0, loaded_count = 0, pick_count = 0; i < table_.size() && pick_count < limit; ++i) {
        auto index = available_begin + i;
        if (not table_[index])
            break;
        if (index % table_.capacity() == table_.rear())
            break;
        if (not cross && table_[index]->IsFinish()) {
            table_.set_front(index);
        } else if (table_[index]->state == TaskTableItemState::LOADED) {
            cross = true;
            ++loaded_count;
            if (loaded_count > 2)
                return std::vector<uint64_t>();
        } else if (table_[index]->state == TaskTableItemState::START) {
            auto task = table_[index]->task;

            // if task is a build index task, limit it
            if (task->Type() == TaskType::BuildIndexTask && task->path().Current() == "cpu") {
                if (not BuildMgrInst::GetInstance()->Take()) {
                    continue;
                }
            }
            cross = true;
            indexes.push_back(index);
            ++pick_count;
        }
    }
    rc.ElapseFromBegin("PickToLoad ");
    return indexes;
#else
193
    size_t count = 0;
194
    for (uint64_t j = last_finish_ + 1; j < table_.size(); ++j) {
195 196 197
        if (not table_[j]) {
            SERVER_LOG_WARNING << "table[" << j << "] is nullptr";
        }
198 199

        if (table_[j]->task->path().Current() == "cpu") {
Y
Yu Kun 已提交
200
            if (table_[j]->task->Type() == TaskType::BuildIndexTask && BuildMgrInst::GetInstance()->numoftasks() < 1) {
201 202 203 204
                return std::vector<uint64_t>();
            }
        }

205 206
        if (table_[j]->state == TaskTableItemState::LOADED) {
            ++count;
207 208
            if (count > 2)
                return std::vector<uint64_t>();
209 210 211
        }
    }

212 213
    std::vector<uint64_t> indexes;
    bool cross = false;
W
wxyu 已提交
214
    for (uint64_t i = last_finish_ + 1, count = 0; i < table_.size() && count < limit; ++i) {
215 216 217
        if (not cross && table_[i]->IsFinish()) {
            last_finish_ = i;
        } else if (table_[i]->state == TaskTableItemState::START) {
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
            auto task = table_[i]->task;
            if (task->Type() == TaskType::BuildIndexTask && task->path().Current() == "cpu") {
                if (BuildMgrInst::GetInstance()->numoftasks() == 0) {
                    break;
                } else {
                    cross = true;
                    indexes.push_back(i);
                    ++count;
                    BuildMgrInst::GetInstance()->take();
                }
            } else {
                cross = true;
                indexes.push_back(i);
                ++count;
            }
233 234 235
        }
    }
    return indexes;
W
wxyu 已提交
236
#endif
237 238 239 240
}

std::vector<uint64_t>
TaskTable::PickToExecute(uint64_t limit) {
W
wxyu 已提交
241
    TimeRecorder rc("");
242 243
    std::vector<uint64_t> indexes;
    bool cross = false;
W
wxyu 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256
    uint64_t available_begin = table_.front() + 1;
    for (uint64_t i = 0, pick_count = 0; i < table_.size() && pick_count < limit; ++i) {
        uint64_t index = available_begin + i;
        if (not table_[index]) {
            break;
        }
        if (index % table_.capacity() == table_.rear()) {
            break;
        }

        if (not cross && table_[index]->IsFinish()) {
            table_.set_front(index);
        } else if (table_[index]->state == TaskTableItemState::LOADED) {
257
            cross = true;
W
wxyu 已提交
258 259
            indexes.push_back(index);
            ++pick_count;
260 261
        }
    }
W
wxyu 已提交
262
    rc.ElapseFromBegin("PickToExecute ");
263 264 265
    return indexes;
}

W
wxyu 已提交
266 267
void
TaskTable::Put(TaskPtr task) {
W
wxyu 已提交
268
    auto item = std::make_shared<TaskTableItem>();
269
    item->id = id_++;
W
wxyu 已提交
270
    item->task = std::move(task);
W
wxyu 已提交
271
    item->state = TaskTableItemState::START;
W
wxyu 已提交
272
    item->timestamp.start = get_current_timestamp();
W
wxyu 已提交
273
    table_.put(std::move(item));
W
wxyu 已提交
274 275 276
    if (subscriber_) {
        subscriber_();
    }
W
wxyu 已提交
277 278 279
}

void
G
groot 已提交
280 281
TaskTable::Put(std::vector<TaskPtr>& tasks) {
    for (auto& task : tasks) {
W
wxyu 已提交
282
        auto item = std::make_shared<TaskTableItem>();
283
        item->id = id_++;
W
wxyu 已提交
284
        item->task = std::move(task);
W
wxyu 已提交
285
        item->state = TaskTableItemState::START;
W
wxyu 已提交
286
        item->timestamp.start = get_current_timestamp();
W
wxyu 已提交
287
        table_.put(std::move(item));
W
wxyu 已提交
288
    }
W
wxyu 已提交
289 290 291
    if (subscriber_) {
        subscriber_();
    }
W
wxyu 已提交
292 293
}

W
wxyu 已提交
294
TaskTableItemPtr
W
wxyu 已提交
295 296 297 298
TaskTable::Get(uint64_t index) {
    return table_[index];
}

W
wxyu 已提交
299 300 301 302 303 304 305 306 307 308 309 310
size_t
TaskTable::TaskToExecute() {
    size_t count = 0;
    auto begin = table_.front() + 1;
    for (size_t i = 0; i < table_.size(); ++i) {
        auto index = begin + i;
        if (table_[index]->state == TaskTableItemState::LOADED) {
            ++count;
        }
    }
    return count;
}
W
wxyu 已提交
311

W
wxyu 已提交
312
json
W
wxyu 已提交
313
TaskTable::Dump() const {
W
wxyu 已提交
314
    json ret{{"error.message", "not support yet."}};
W
wxyu 已提交
315
    return ret;
W
wxyu 已提交
316 317
}

G
groot 已提交
318 319
}  // namespace scheduler
}  // namespace milvus