InsertRequest.cpp 8.3 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/InsertRequest.h"
13
#include "db/Utils.h"
G
groot 已提交
14
#include "server/DBWrapper.h"
C
Cai Yudong 已提交
15
#include "utils/CommonUtil.h"
G
groot 已提交
16 17 18 19
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
#include "utils/ValidationUtil.h"

S
shengjh 已提交
20
#include <fiu-local.h>
G
groot 已提交
21 22 23
#include <memory>
#include <string>
#include <vector>
C
Cai Yudong 已提交
24 25 26
#ifdef MILVUS_ENABLE_PROFILING
#include <gperftools/profiler.h>
#endif
G
groot 已提交
27 28 29 30

namespace milvus {
namespace server {

31
InsertRequest::InsertRequest(const std::shared_ptr<Context>& context, const std::string& table_name,
G
groot 已提交
32
                             engine::VectorsData& vectors, const std::string& partition_tag)
33 34
    : BaseRequest(context, DDL_DML_REQUEST_GROUP),
      table_name_(table_name),
G
groot 已提交
35 36
      vectors_data_(vectors),
      partition_tag_(partition_tag) {
G
groot 已提交
37 38 39
}

BaseRequestPtr
G
groot 已提交
40 41 42
InsertRequest::Create(const std::shared_ptr<Context>& context, const std::string& table_name,
                      engine::VectorsData& vectors, const std::string& partition_tag) {
    return std::shared_ptr<BaseRequest>(new InsertRequest(context, table_name, vectors, partition_tag));
G
groot 已提交
43 44 45 46 47
}

Status
InsertRequest::OnExecute() {
    try {
G
groot 已提交
48
        int64_t vector_count = vectors_data_.vector_count_;
S
shengjh 已提交
49
        fiu_do_on("InsertRequest.OnExecute.throw_std_exception", throw std::exception());
G
groot 已提交
50
        std::string hdr = "InsertRequest(table=" + table_name_ + ", n=" + std::to_string(vector_count) +
51
                          ", partition_tag=" + partition_tag_ + ")";
G
groot 已提交
52
        TimeRecorder rc(hdr);
G
groot 已提交
53 54

        // step 1: check arguments
55
        auto status = ValidationUtil::ValidateTableName(table_name_);
G
groot 已提交
56 57 58
        if (!status.ok()) {
            return status;
        }
G
groot 已提交
59
        if (vectors_data_.float_data_.empty() && vectors_data_.binary_data_.empty()) {
G
groot 已提交
60 61 62 63
            return Status(SERVER_INVALID_ROWRECORD_ARRAY,
                          "The vector array is empty. Make sure you have entered vector records.");
        }

S
shengjh 已提交
64
        fiu_do_on("InsertRequest.OnExecute.id_array_error", vectors_data_.id_array_.resize(vector_count + 1));
G
groot 已提交
65 66
        if (!vectors_data_.id_array_.empty()) {
            if (vectors_data_.id_array_.size() != vector_count) {
G
groot 已提交
67 68 69 70 71 72
                return Status(SERVER_ILLEGAL_VECTOR_ID,
                              "The size of vector ID array must be equal to the size of the vector.");
            }
        }

        // step 2: check table existence
73 74 75 76
        // only process root table, ignore partition table
        engine::meta::TableSchema table_schema;
        table_schema.table_id_ = table_name_;
        status = DBWrapper::DB()->DescribeTable(table_schema);
S
shengjh 已提交
77 78
        fiu_do_on("InsertRequest.OnExecute.db_not_found", status = Status(milvus::DB_NOT_FOUND, ""));
        fiu_do_on("InsertRequest.OnExecute.describe_table_fail", status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
G
groot 已提交
79 80
        if (!status.ok()) {
            if (status.code() == DB_NOT_FOUND) {
81
                return Status(SERVER_TABLE_NOT_EXIST, TableNotExistMsg(table_name_));
G
groot 已提交
82 83 84
            } else {
                return status;
            }
85 86 87 88
        } else {
            if (!table_schema.owner_table_.empty()) {
                return Status(SERVER_INVALID_TABLE_NAME, TableNotExistMsg(table_name_));
            }
G
groot 已提交
89 90 91 92
        }

        // step 3: check table flag
        // all user provide id, or all internal id
G
groot 已提交
93
        bool user_provide_ids = !vectors_data_.id_array_.empty();
S
shengjh 已提交
94
        fiu_do_on("InsertRequest.OnExecute.illegal_vector_id", user_provide_ids = false;
95
                  table_schema.flag_ = engine::meta::FLAG_MASK_HAS_USERID);
G
groot 已提交
96
        // user already provided id before, all insert action require user id
97
        if ((table_schema.flag_ & engine::meta::FLAG_MASK_HAS_USERID) != 0 && !user_provide_ids) {
G
groot 已提交
98 99 100 101
            return Status(SERVER_ILLEGAL_VECTOR_ID,
                          "Table vector IDs are user-defined. Please provide IDs for all vectors of this table.");
        }

S
shengjh 已提交
102
        fiu_do_on("InsertRequest.OnExecute.illegal_vector_id2", user_provide_ids = true;
103
                  table_schema.flag_ = engine::meta::FLAG_MASK_NO_USERID);
G
groot 已提交
104
        // user didn't provided id before, no need to provide user id
105
        if ((table_schema.flag_ & engine::meta::FLAG_MASK_NO_USERID) != 0 && user_provide_ids) {
G
groot 已提交
106 107 108 109 110 111 112 113
            return Status(
                SERVER_ILLEGAL_VECTOR_ID,
                "Table vector IDs are auto-generated. All vectors of this table must use auto-generated IDs.");
        }

        rc.RecordSection("check validation");

#ifdef MILVUS_ENABLE_PROFILING
C
Cai Yudong 已提交
114
        std::string fname = "/tmp/insert_" + CommonUtil::GetCurrentTimeStr() + ".profiling";
G
groot 已提交
115 116
        ProfilerStart(fname.c_str());
#endif
G
groot 已提交
117 118
        // step 4: some metric type doesn't support float vectors
        if (!vectors_data_.float_data_.empty()) {  // insert float vectors
119
            if (engine::utils::IsBinaryMetricType(table_schema.metric_type_)) {
G
groot 已提交
120 121
                return Status(SERVER_INVALID_ROWRECORD_ARRAY, "Table metric type doesn't support float vectors.");
            }
G
groot 已提交
122

G
groot 已提交
123 124 125 126 127
            // check prepared float data
            if (vectors_data_.float_data_.size() % vector_count != 0) {
                return Status(SERVER_INVALID_ROWRECORD_ARRAY,
                              "The vector dimension must be equal to the table dimension.");
            }
G
groot 已提交
128

129 130
            fiu_do_on("InsertRequest.OnExecute.invalid_dim", table_schema.dimension_ = -1);
            if (vectors_data_.float_data_.size() / vector_count != table_schema.dimension_) {
G
groot 已提交
131 132 133 134
                return Status(SERVER_INVALID_VECTOR_DIMENSION,
                              "The vector dimension must be equal to the table dimension.");
            }
        } else if (!vectors_data_.binary_data_.empty()) {  // insert binary vectors
135
            if (!engine::utils::IsBinaryMetricType(table_schema.metric_type_)) {
G
groot 已提交
136 137 138 139 140 141 142 143 144
                return Status(SERVER_INVALID_ROWRECORD_ARRAY, "Table metric type doesn't support binary vectors.");
            }

            // check prepared binary data
            if (vectors_data_.binary_data_.size() % vector_count != 0) {
                return Status(SERVER_INVALID_ROWRECORD_ARRAY,
                              "The vector dimension must be equal to the table dimension.");
            }

145
            if (vectors_data_.binary_data_.size() * 8 / vector_count != table_schema.dimension_) {
G
groot 已提交
146 147 148
                return Status(SERVER_INVALID_VECTOR_DIMENSION,
                              "The vector dimension must be equal to the table dimension.");
            }
G
groot 已提交
149 150 151
        }

        // step 5: insert vectors
G
groot 已提交
152
        auto vec_count = static_cast<uint64_t>(vector_count);
G
groot 已提交
153

G
groot 已提交
154
        rc.RecordSection("prepare vectors data");
G
groot 已提交
155
        status = DBWrapper::DB()->InsertVectors(table_name_, partition_tag_, vectors_data_);
S
shengjh 已提交
156
        fiu_do_on("InsertRequest.OnExecute.insert_fail", status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
G
groot 已提交
157 158 159 160
        if (!status.ok()) {
            return status;
        }

G
groot 已提交
161
        auto ids_size = vectors_data_.id_array_.size();
S
shengjh 已提交
162
        fiu_do_on("InsertRequest.OnExecute.invalid_ids_size", ids_size = vec_count - 1);
G
groot 已提交
163 164 165 166 167 168 169
        if (ids_size != vec_count) {
            std::string msg =
                "Add " + std::to_string(vec_count) + " vectors but only return " + std::to_string(ids_size) + " id";
            return Status(SERVER_ILLEGAL_VECTOR_ID, msg);
        }

        // step 6: update table flag
170 171 172
        user_provide_ids ? table_schema.flag_ |= engine::meta::FLAG_MASK_HAS_USERID
                         : table_schema.flag_ |= engine::meta::FLAG_MASK_NO_USERID;
        status = DBWrapper::DB()->UpdateTableFlag(table_name_, table_schema.flag_);
G
groot 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

#ifdef MILVUS_ENABLE_PROFILING
        ProfilerStop();
#endif

        rc.RecordSection("add vectors to engine");
        rc.ElapseFromBegin("total cost");
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus