BaseRequest.h 4.9 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
G
groot 已提交
2
//
3 4
// Licensed 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
G
groot 已提交
5
//
6 7 8 9 10
// 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 已提交
11 12 13

#pragma once

14
#include "db/Types.h"
G
groot 已提交
15 16 17 18
#include "db/meta/MetaTypes.h"
#include "grpc/gen-milvus/milvus.grpc.pb.h"
#include "grpc/gen-status/status.grpc.pb.h"
#include "grpc/gen-status/status.pb.h"
Z
Zhiru Zhu 已提交
19
#include "server/context/Context.h"
20
#include "utils/Json.h"
G
groot 已提交
21
#include "utils/Status.h"
22 23

#include <condition_variable>
G
groot 已提交
24 25 26 27
//#include <gperftools/profiler.h>
#include <memory>
#include <string>
#include <thread>
28
#include <utility>
G
groot 已提交
29 30 31 32 33
#include <vector>

namespace milvus {
namespace server {

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
struct TableSchema {
    std::string table_name_;
    int64_t dimension_;
    int64_t index_file_size_;
    int64_t metric_type_;

    TableSchema() {
        dimension_ = 0;
        index_file_size_ = 0;
        metric_type_ = 0;
    }

    TableSchema(const std::string& table_name, int64_t dimension, int64_t index_file_size, int64_t metric_type) {
        table_name_ = table_name;
        dimension_ = dimension;
        index_file_size_ = index_file_size;
        metric_type_ = metric_type;
    }
};

struct TopKQueryResult {
    int64_t row_num_;
    engine::ResultIds id_list_;
    engine::ResultDistances distance_list_;

    TopKQueryResult() {
        row_num_ = 0;
    }

    TopKQueryResult(int64_t row_num, const engine::ResultIds& id_list, const engine::ResultDistances& distance_list) {
        row_num_ = row_num;
        id_list_ = id_list;
        distance_list_ = distance_list;
    }
};

struct IndexParam {
    std::string table_name_;
    int64_t index_type_;
73
    std::string extra_params_;
74 75 76 77 78

    IndexParam() {
        index_type_ = 0;
    }

79
    IndexParam(const std::string& table_name, int64_t index_type) {
80 81 82 83 84 85 86 87 88 89 90
        table_name_ = table_name;
        index_type_ = index_type;
    }
};

struct PartitionParam {
    std::string table_name_;
    std::string tag_;

    PartitionParam() = default;

91
    PartitionParam(const std::string& table_name, const std::string& tag) {
92 93 94 95
        table_name_ = table_name;
        tag_ = tag;
    }
};
G
groot 已提交
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
struct SegmentStat {
    std::string name_;
    int64_t row_num_ = 0;
    std::string index_name_;
    int64_t data_size_ = 0;
};

struct PartitionStat {
    std::string tag_;
    int64_t total_row_num_ = 0;
    std::vector<SegmentStat> segments_stat_;
};

struct TableInfo {
    int64_t total_row_num_ = 0;
    std::vector<PartitionStat> partitions_stat_;
};

115
class BaseRequest {
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
 public:
    enum RequestType {
        // general operations
        kCmd = 100,

        // data operations
        kInsert = 200,
        kCompact,
        kFlush,
        kDeleteByID,
        kGetVectorByID,
        kGetVectorIDs,

        // table operations
        kShowTables = 300,
        kCreateTable,
        kHasTable,
        kDescribeTable,
        kCountTable,
        kShowTableInfo,
        kDropTable,
        kPreloadTable,

        // partition operations
        kCreatePartition = 400,
        kShowPartitions,
        kDropPartition,

        // index operations
        kCreateIndex = 500,
        kDescribeIndex,
        kDropIndex,

        // search operations
        kSearchByID = 600,
        kSearch,
        kSearchCombine,
    };

G
groot 已提交
155
 protected:
156 157
    BaseRequest(const std::shared_ptr<milvus::server::Context>& context, BaseRequest::RequestType type,
                bool async = false);
G
groot 已提交
158

159
    virtual ~BaseRequest();
G
groot 已提交
160 161

 public:
162 163 164
    Status
    PreExecute();

G
groot 已提交
165 166 167
    Status
    Execute();

168 169 170
    Status
    PostExecute();

G
groot 已提交
171 172 173 174 175 176
    void
    Done();

    Status
    WaitToFinish();

177 178 179 180 181
    RequestType
    GetRequestType() const {
        return type_;
    }

G
groot 已提交
182 183 184 185 186 187 188 189 190 191
    std::string
    RequestGroup() const {
        return request_group_;
    }

    const Status&
    status() const {
        return status_;
    }

192 193 194
    void
    set_status(const Status& status);

G
groot 已提交
195 196 197 198 199 200
    bool
    IsAsync() const {
        return async_;
    }

 protected:
201 202 203
    virtual Status
    OnPreExecute();

G
groot 已提交
204 205 206
    virtual Status
    OnExecute() = 0;

207 208 209
    virtual Status
    OnPostExecute();

G
groot 已提交
210 211 212 213
    std::string
    TableNotExistMsg(const std::string& table_name);

 protected:
T
Tinkerrr 已提交
214
    const std::shared_ptr<milvus::server::Context> context_;
Z
Zhiru Zhu 已提交
215

G
groot 已提交
216 217 218
    mutable std::mutex finish_mtx_;
    std::condition_variable finish_cond_;

219
    RequestType type_;
G
groot 已提交
220 221 222 223
    std::string request_group_;
    bool async_;
    bool done_;
    Status status_;
224 225 226 227 228 229

 public:
    const std::shared_ptr<milvus::server::Context>&
    Context() const {
        return context_;
    }
G
groot 已提交
230 231
};

232
using BaseRequestPtr = std::shared_ptr<BaseRequest>;
G
groot 已提交
233 234 235

}  // namespace server
}  // namespace milvus