Scheduler.h 2.5 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
#pragma once

W
wxyu 已提交
8
#include <memory>
W
wxyu 已提交
9 10 11 12 13
#include <string>
#include <mutex>
#include <thread>
#include <queue>

W
wxyu 已提交
14 15 16
#include "resource/Resource.h"
#include "ResourceMgr.h"

W
wxyu 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

namespace zilliz {
namespace milvus {
namespace engine {


class Scheduler {
public:
    explicit
    Scheduler(ResourceMgrWPtr res_mgr)
        : running_(false),
          res_mgr_(std::move(res_mgr)) {
//        res_mgr.Register();
//        res_mgr.Register();
//        res_mgr.Register();
//        res_mgr.Register();
    }

    void
W
wxyu 已提交
36 37 38
    Start() {
        worker_thread_ = std::thread(&Scheduler::worker_thread_, this);
    }
W
wxyu 已提交
39

W
wxyu 已提交
40 41 42 43
    std::string
    Dump();

private:
W
wxyu 已提交
44 45 46 47 48
    /******** Events ********/

    /*
     * Process start up events;
     */
W
wxyu 已提交
49 50
    void
    OnStartUp(const EventPtr &event);
W
wxyu 已提交
51 52 53 54

    /*
     * Process finish task events;
     */
W
wxyu 已提交
55 56
    void
    OnFinishTask(const EventPtr &event);
W
wxyu 已提交
57 58 59 60

    /*
     * Process copy completed events;
     */
W
wxyu 已提交
61 62
    void
    OnCopyCompleted(const EventPtr &event);
W
wxyu 已提交
63 64 65 66

    /*
     * Process task table updated events;
     */
W
wxyu 已提交
67 68
    void
    OnTaskTableUpdated(const EventPtr &event);
W
wxyu 已提交
69 70

private:
W
wxyu 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    /*
     * Dispatch event to event handler;
     */
    void
    Process(const EventPtr &event) {
        switch (event->Type()) {
            case EventType::START_UP: {
                OnStartUp(event);
                break;
            }
            case EventType::COPY_COMPLETED: {
                OnCopyCompleted(event);
                break;
            }
            case EventType::FINISH_TASK: {
                OnFinishTask(event);
                break;
            }
            case EventType::TASK_TABLE_UPDATED: {
                OnTaskTableUpdated(event);
                break;
            }
            default: {
                break;
            }
        }
    }

W
wxyu 已提交
99 100 101
    /*
     * Called by worker_thread_;
     */
W
wxyu 已提交
102
    void
W
wxyu 已提交
103 104 105
    worker_function() {
        while (running_) {
            auto event = event_queue_.front();
W
wxyu 已提交
106
            Process(event);
W
wxyu 已提交
107 108
        }
    }
W
wxyu 已提交
109 110 111 112 113 114 115 116 117

private:
    bool running_;

    ResourceMgrWPtr res_mgr_;
    std::queue<EventPtr> event_queue_;
    std::thread worker_thread_;
};

W
wxyu 已提交
118 119
using SchedulerPtr = std::shared_ptr<Scheduler>;

W
wxyu 已提交
120 121 122 123
}
}
}