TaskTable.h 2.8 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*******************************************************************************
 * 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>

#include "Task.h"


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)
    : id(src.id), state(src.state), mutex(), priority(src.priority) {}

    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;
};

class TaskTable {
public:
    TaskTable() = default;

    explicit
W
wxyu 已提交
50
    TaskTable(std::vector<TaskPtr> &&tasks);
W
wxyu 已提交
51 52 53 54 55

    /*
     * Put one task;
     */
    void
W
wxyu 已提交
56
    Put(TaskPtr task);
W
wxyu 已提交
57 58 59 60 61 62

    /*
     * Put tasks back of task table;
     * Called by DBImpl;
     */
    void
W
wxyu 已提交
63
    Put(std::vector<TaskPtr> &tasks);
W
wxyu 已提交
64 65 66 67 68

    /*
     * Return task table item reference;
     */
    TaskTableItem &
W
wxyu 已提交
69
    Get(uint64_t index);
W
wxyu 已提交
70 71 72 73 74 75 76

    /*
     * TODO
     * Remove sequence task which is DONE or MOVED from front;
     * Called by ?
     */
    void
W
wxyu 已提交
77
    Clear();
W
wxyu 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90


public:

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

    // TODO: bool to Status
    bool
W
wxyu 已提交
91
    Move(uint64_t index);
W
wxyu 已提交
92 93 94 95 96 97 98

    /*
     * Move task finished;
     * Set state moved;
     * Called by scheduler;
     */
    bool
W
wxyu 已提交
99
    Moved(uint64_t index);
W
wxyu 已提交
100 101 102 103 104 105 106

    /*
     * Load a task;
     * Set state loading;
     * Called by loader;
     */
    bool
W
wxyu 已提交
107
    Load(uint64_t index);
W
wxyu 已提交
108 109 110 111 112 113 114

    /*
     * Load task finished;
     * Set state loaded;
     * Called by loader;
     */
    bool
W
wxyu 已提交
115
    Loaded(uint64_t index);
W
wxyu 已提交
116 117 118 119 120 121 122

    /*
     * Execute a task;
     * Set state executing;
     * Called by executor;
     */
    bool
W
wxyu 已提交
123
    Execute(uint64_t index);
W
wxyu 已提交
124 125 126 127 128 129 130

    /*
     * Execute task finished;
     * Set state executed;
     * Called by executor;
     */
    bool
W
wxyu 已提交
131
    Executed(uint64_t index);
W
wxyu 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

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

private:
    // TODO: map better ?
    std::deque<TaskTableItem> table_;
};


}
}
}