BaseRequest.cpp 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
#include "server/delivery/request/BaseRequest.h"
13 14 15 16

#include <map>

#include "server/context/Context.h"
G
groot 已提交
17
#include "utils/CommonUtil.h"
18
#include "utils/Exception.h"
G
groot 已提交
19 20 21 22 23
#include "utils/Log.h"

namespace milvus {
namespace server {

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
static const char* DQL_REQUEST_GROUP = "dql";
static const char* DDL_DML_REQUEST_GROUP = "ddl_dml";
static const char* INFO_REQUEST_GROUP = "info";

namespace {
std::string
RequestGroup(BaseRequest::RequestType type) {
    static std::map<BaseRequest::RequestType, std::string> s_map_type_group = {
        // general operations
        {BaseRequest::kCmd, INFO_REQUEST_GROUP},

        // data operations
        {BaseRequest::kInsert, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kCompact, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kFlush, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kDeleteByID, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kGetVectorByID, INFO_REQUEST_GROUP},
        {BaseRequest::kGetVectorIDs, INFO_REQUEST_GROUP},
42
        {BaseRequest::kInsertEntity, DDL_DML_REQUEST_GROUP},
43

J
Jin Hai 已提交
44
        // collection operations
45 46 47 48 49 50 51 52
        {BaseRequest::kShowCollections, INFO_REQUEST_GROUP},
        {BaseRequest::kCreateCollection, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kHasCollection, INFO_REQUEST_GROUP},
        {BaseRequest::kDescribeCollection, INFO_REQUEST_GROUP},
        {BaseRequest::kCountCollection, INFO_REQUEST_GROUP},
        {BaseRequest::kShowCollectionInfo, INFO_REQUEST_GROUP},
        {BaseRequest::kDropCollection, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kPreloadCollection, DQL_REQUEST_GROUP},
53
        {BaseRequest::kCreateHybridCollection, DDL_DML_REQUEST_GROUP},
54
        {BaseRequest::kDescribeHybridCollection, INFO_REQUEST_GROUP},
55
        {BaseRequest::kReloadSegments, DQL_REQUEST_GROUP},
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

        // partition operations
        {BaseRequest::kCreatePartition, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kShowPartitions, INFO_REQUEST_GROUP},
        {BaseRequest::kDropPartition, DDL_DML_REQUEST_GROUP},

        // index operations
        {BaseRequest::kCreateIndex, DDL_DML_REQUEST_GROUP},
        {BaseRequest::kDescribeIndex, INFO_REQUEST_GROUP},
        {BaseRequest::kDropIndex, DDL_DML_REQUEST_GROUP},

        // search operations
        {BaseRequest::kSearchByID, DQL_REQUEST_GROUP},
        {BaseRequest::kSearch, DQL_REQUEST_GROUP},
        {BaseRequest::kSearchCombine, DQL_REQUEST_GROUP},
71
        {BaseRequest::kHybridSearch, DQL_REQUEST_GROUP},
72 73 74 75
    };

    auto iter = s_map_type_group.find(type);
    if (iter == s_map_type_group.end()) {
76
        LOG_SERVER_ERROR_ << "Unsupported request type: " << type;
77 78 79 80 81 82 83 84 85 86
        throw Exception(SERVER_NOT_IMPLEMENT, "request group undefined");
    }
    return iter->second;
}
}  // namespace

BaseRequest::BaseRequest(const std::shared_ptr<milvus::server::Context>& context, BaseRequest::RequestType type,
                         bool async)
    : context_(context), type_(type), async_(async), done_(false) {
    request_group_ = milvus::server::RequestGroup(type);
87 88 89
    if (nullptr != context_) {
        context_->SetRequestType(type_);
    }
G
groot 已提交
90 91
}

92
BaseRequest::~BaseRequest() {
G
groot 已提交
93 94 95
    WaitToFinish();
}

96 97 98 99 100 101 102 103 104
Status
BaseRequest::PreExecute() {
    status_ = OnPreExecute();
    if (!status_.ok()) {
        Done();
    }
    return status_;
}

G
groot 已提交
105
Status
106
BaseRequest::Execute() {
G
groot 已提交
107 108 109 110 111
    status_ = OnExecute();
    Done();
    return status_;
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
Status
BaseRequest::PostExecute() {
    status_ = OnPostExecute();
    return status_;
}

Status
BaseRequest::OnPreExecute() {
    return Status::OK();
}

Status
BaseRequest::OnPostExecute() {
    return Status::OK();
}

G
groot 已提交
128
void
129
BaseRequest::Done() {
130
    std::unique_lock<std::mutex> lock(finish_mtx_);
G
groot 已提交
131 132 133 134
    done_ = true;
    finish_cond_.notify_all();
}

135 136 137 138
void
BaseRequest::set_status(const Status& status) {
    status_ = status;
    if (!status_.ok()) {
139
        LOG_SERVER_ERROR_ << status_.message();
140
    }
G
groot 已提交
141 142 143
}

std::string
G
groot 已提交
144
BaseRequest::CollectionNotExistMsg(const std::string& collection_name) {
J
Jin Hai 已提交
145
    return "Collection " + collection_name +
146
           " does not exist. Use milvus.has_collection to verify whether the collection exists. "
J
Jin Hai 已提交
147
           "You also can check whether the collection name exists.";
G
groot 已提交
148 149 150
}

Status
151
BaseRequest::WaitToFinish() {
G
groot 已提交
152 153 154 155 156 157 158
    std::unique_lock<std::mutex> lock(finish_mtx_);
    finish_cond_.wait(lock, [this] { return done_; });
    return status_;
}

}  // namespace server
}  // namespace milvus