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

#include <vector>
#include <deque>
#include <mutex>

12
#include "task/SearchTask.h"
W
wxyu 已提交
13
#include "event/Event.h"
W
wxyu 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


namespace zilliz {
namespace milvus {
namespace engine {

enum class TaskTableItemState {
    INVALID,
    START, // idle
    LOADING, // loading data from other resource
    LOADED, // ready to exec or move
    EXECUTING, // executing, locking util executed or failed
    EXECUTED, // executed, termination state
    MOVING, // moving to another resource, locking util executed or failed
    MOVED, // moved, termination state
};

struct TaskTableItem {
    TaskTableItem() : id(0), state(TaskTableItemState::INVALID), mutex(), priority(0) {}

    TaskTableItem(const TaskTableItem &src)
W
wxyu 已提交
35
        : id(src.id), state(src.state), mutex(), priority(src.priority) {}
W
wxyu 已提交
36 37 38 39 40 41 42 43 44 45

    uint64_t id; // auto increment from 0;
    // TODO: add tag into task
    TaskPtr task; // the task;
    TaskTableItemState state; // the state;
    std::mutex mutex;

    uint8_t priority; // just a number, meaningless;
};

W
wxyu 已提交
46 47
using TaskTableItemPtr = std::shared_ptr<TaskTableItem>;

W
wxyu 已提交
48 49 50 51
class TaskTable {
public:
    TaskTable() = default;

52 53 54
    TaskTable(const TaskTable &) = delete;
    TaskTable(TaskTable &&) = delete;

W
wxyu 已提交
55 56 57 58 59
    inline void
    RegisterSubscriber(std::function<void(void)> subscriber) {
        subscriber_ = std::move(subscriber);
    }

W
wxyu 已提交
60 61 62 63
    /*
     * Put one task;
     */
    void
W
wxyu 已提交
64
    Put(TaskPtr task);
W
wxyu 已提交
65 66 67 68 69 70

    /*
     * Put tasks back of task table;
     * Called by DBImpl;
     */
    void
W
wxyu 已提交
71
    Put(std::vector<TaskPtr> &tasks);
W
wxyu 已提交
72 73 74 75

    /*
     * Return task table item reference;
     */
W
wxyu 已提交
76
    TaskTableItemPtr
W
wxyu 已提交
77
    Get(uint64_t index);
W
wxyu 已提交
78 79 80 81 82 83 84

    /*
     * TODO
     * Remove sequence task which is DONE or MOVED from front;
     * Called by ?
     */
    void
W
wxyu 已提交
85
    Clear();
W
wxyu 已提交
86

W
wxyu 已提交
87 88 89 90 91 92 93
    /*
     * Return true if task table empty, otherwise false;
     */
    inline bool
    Empty() {
        return table_.empty();
    }
W
wxyu 已提交
94

W
wxyu 已提交
95 96 97
    /*
     * Return size of task table;
     */
W
wxyu 已提交
98
    inline size_t
W
wxyu 已提交
99 100 101
    Size() {
        return table_.size();
    }
W
wxyu 已提交
102 103 104 105 106 107 108 109
public:
    TaskTableItemPtr &
    operator[](uint64_t index) {
        return table_[index];
    }

    std::deque<TaskTableItemPtr>::iterator begin() { return table_.begin(); }
    std::deque<TaskTableItemPtr>::iterator end() { return table_.end(); }
W
wxyu 已提交
110 111 112 113 114 115 116 117 118 119 120 121

public:

    /******** Action ********/
    /*
     * Move a task;
     * Set state moving;
     * Called by scheduler;
     */

    // TODO: bool to Status
    bool
W
wxyu 已提交
122
    Move(uint64_t index);
W
wxyu 已提交
123 124 125 126 127 128 129

    /*
     * Move task finished;
     * Set state moved;
     * Called by scheduler;
     */
    bool
W
wxyu 已提交
130
    Moved(uint64_t index);
W
wxyu 已提交
131 132 133 134 135 136 137

    /*
     * Load a task;
     * Set state loading;
     * Called by loader;
     */
    bool
W
wxyu 已提交
138
    Load(uint64_t index);
W
wxyu 已提交
139 140 141 142 143 144 145

    /*
     * Load task finished;
     * Set state loaded;
     * Called by loader;
     */
    bool
W
wxyu 已提交
146
    Loaded(uint64_t index);
W
wxyu 已提交
147 148 149 150 151 152 153

    /*
     * Execute a task;
     * Set state executing;
     * Called by executor;
     */
    bool
W
wxyu 已提交
154
    Execute(uint64_t index);
W
wxyu 已提交
155 156 157 158 159 160 161

    /*
     * Execute task finished;
     * Set state executed;
     * Called by executor;
     */
    bool
W
wxyu 已提交
162
    Executed(uint64_t index);
W
wxyu 已提交
163 164 165 166 167 168 169 170 171 172

public:
    /*
     * Dump;
     */
    std::string
    Dump();

private:
    // TODO: map better ?
173 174
    std::uint64_t id_ = 0;
    mutable std::mutex id_mutex_;
W
wxyu 已提交
175
    std::deque<TaskTableItemPtr> table_;
W
wxyu 已提交
176
    std::function<void(void)> subscriber_ = nullptr;
W
wxyu 已提交
177 178 179 180 181 182
};


}
}
}