JobMgr.h 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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.
#pragma once

S
starlord 已提交
19 20
#include <condition_variable>
#include <deque>
21
#include <list>
S
starlord 已提交
22 23
#include <memory>
#include <mutex>
24
#include <queue>
S
starlord 已提交
25
#include <string>
26
#include <thread>
S
starlord 已提交
27 28
#include <unordered_map>
#include <vector>
29

S
starlord 已提交
30
#include "ResourceMgr.h"
W
wxyu 已提交
31
#include "interface/interfaces.h"
32 33 34 35 36 37
#include "job/Job.h"
#include "task/Task.h"

namespace milvus {
namespace scheduler {

W
wxyu 已提交
38
class JobMgr : public interface::dumpable {
S
starlord 已提交
39 40
 public:
    explicit JobMgr(ResourceMgrPtr res_mgr);
41 42 43 44 45 46 47

    void
    Start();

    void
    Stop();

W
wxyu 已提交
48 49 50
    json
    Dump() const override;

S
starlord 已提交
51
 public:
52
    void
S
starlord 已提交
53
    Put(const JobPtr& job);
54

S
starlord 已提交
55
 private:
56 57 58
    void
    worker_function();

W
wxyu 已提交
59
    static std::vector<TaskPtr>
S
starlord 已提交
60
    build_task(const JobPtr& job);
61

F
fishpenguin 已提交
62 63 64
 public:
    static void
    calculate_path(const ResourceMgrPtr& res_mgr, const TaskPtr& task);
W
wxyu 已提交
65

S
starlord 已提交
66
 private:
67 68 69 70 71 72 73 74 75 76 77
    bool running_ = false;
    std::queue<JobPtr> queue_;

    std::thread worker_thread_;

    std::mutex mutex_;
    std::condition_variable cv_;

    ResourceMgrPtr res_mgr_ = nullptr;
};

W
wxyu 已提交
78 79
using JobMgrPtr = std::shared_ptr<JobMgr>;

S
starlord 已提交
80 81
}  // namespace scheduler
}  // namespace milvus