ClientProxy.cpp 11.1 KB
Newer Older
K
kun yu 已提交
1 2 3 4 5 6
/*******************************************************************************
* Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
******************************************************************************/
#include "ClientProxy.h"
K
kun yu 已提交
7
#include "version.h"
K
kun yu 已提交
8
#include "milvus.grpc.pb.h"
K
kun yu 已提交
9
//#define GRPC_MULTIPLE_THREAD;
K
kun yu 已提交
10 11 12

namespace milvus {

K
kun yu 已提交
13 14 15 16 17 18 19 20 21 22
bool
UriCheck(const std::string &uri) {
     size_t index = uri.find_first_of(':', 0);
    if (index == std::string::npos) {
        return false;
    } else {
        return true;
    }
}

K
kun yu 已提交
23 24 25 26 27 28 29
Status
ClientProxy::Connect(const ConnectParam &param) {
    std::string uri = param.ip_address + ":" + param.port;

    channel_ = ::grpc::CreateChannel(uri, ::grpc::InsecureChannelCredentials());
    if (channel_ != nullptr) {
        connected_ = true;
K
kun yu 已提交
30 31 32 33 34 35
        client_ptr_ = std::make_shared<GrpcClient>(channel_);
        return Status::OK();
    } else {
        std::string reason = "connect failed!";
        connected_ = false;
        return Status(StatusCode::NotConnected, reason);
K
kun yu 已提交
36 37 38 39 40
    }
}

Status
ClientProxy::Connect(const std::string &uri) {
K
kun yu 已提交
41
    if (!UriCheck(uri)) {
K
kun yu 已提交
42 43
        return Status::Invalid("Invalid uri");
    }
K
kun yu 已提交
44
    size_t index = uri.find_first_of(':', 0);
K
kun yu 已提交
45 46 47 48 49 50 51 52 53 54 55 56

    ConnectParam param;
    param.ip_address = uri.substr(0, index);
    param.port = uri.substr(index + 1);

    return Connect(param);
}

Status
ClientProxy::Connected() const {
    try {
        std::string info;
Y
Yu Kun 已提交
57
        return client_ptr_->Cmd(info, "");
K
kun yu 已提交
58 59 60 61 62 63 64
    } catch (std::exception &ex) {
        return Status(StatusCode::NotConnected, "connection lost: " + std::string(ex.what()));
    }
}

Status
ClientProxy::Disconnect() {
K
kun yu 已提交
65
    try {
K
kun yu 已提交
66
        Status status = client_ptr_->Disconnect();
K
kun yu 已提交
67 68
        connected_ = false;
        channel_.reset();
K
kun yu 已提交
69
        return status;
K
kun yu 已提交
70 71 72
    }catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to disconnect: " + std::string(ex.what()));
    }
K
kun yu 已提交
73 74 75 76
}

std::string
ClientProxy::ClientVersion() const {
K
kun yu 已提交
77
    return MILVUS_VERSION;
K
kun yu 已提交
78 79 80 81 82 83
}

Status
ClientProxy::CreateTable(const TableSchema &param) {
    try {
        ::milvus::grpc::TableSchema schema;
K
kun yu 已提交
84
        schema.mutable_table_name()->set_table_name(param.table_name);
K
kun yu 已提交
85 86 87 88
        schema.set_index_type((int) param.index_type);
        schema.set_dimension(param.dimension);
        schema.set_store_raw_vector(param.store_raw_vector);

K
kun yu 已提交
89
        return client_ptr_->CreateTable(schema);
K
kun yu 已提交
90 91 92 93 94 95 96
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to create table: " + std::string(ex.what()));
    }
}

bool
ClientProxy::HasTable(const std::string &table_name) {
K
kun yu 已提交
97
    Status status = Status::OK();
K
kun yu 已提交
98 99
    ::milvus::grpc::TableName grpc_table_name;
    grpc_table_name.set_table_name(table_name);
K
kun yu 已提交
100 101
    bool result = client_ptr_->HasTable(grpc_table_name, status);
    return result;
K
kun yu 已提交
102 103 104 105 106 107 108
}

Status
ClientProxy::DropTable(const std::string &table_name) {
    try {
        ::milvus::grpc::TableName grpc_table_name;
        grpc_table_name.set_table_name(table_name);
K
kun yu 已提交
109
        return client_ptr_->DropTable(grpc_table_name);
K
kun yu 已提交
110 111 112 113 114 115
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to drop table: " + std::string(ex.what()));
    }
}

Status
Y
Yu Kun 已提交
116
ClientProxy::CreateIndex(const IndexParam &index_param) {
K
kun yu 已提交
117
    try {
Y
Yu Kun 已提交
118 119 120 121 122
        //TODO:add index params
        ::milvus::grpc::IndexParam grpc_index_param;
        grpc_index_param.mutable_table_name()->set_table_name(
                index_param.table_name);
        return client_ptr_->CreateIndex(grpc_index_param);
K
kun yu 已提交
123 124 125 126 127 128

    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to build index: " + std::string(ex.what()));
    }
}

K
kun yu 已提交
129
Status
Y
Yu Kun 已提交
130
ClientProxy::Insert(const std::string &table_name,
K
kun yu 已提交
131 132
                          const std::vector<RowRecord> &record_array,
                          std::vector<int64_t> &id_array) {
K
kun yu 已提交
133
    Status status = Status::OK();
K
kun yu 已提交
134 135
    try {
////////////////////////////////////////////////////////////////////////////
K
kun yu 已提交
136
#ifdef GRPC_MULTIPLE_THREAD
K
kun yu 已提交
137 138 139
        std::vector<std::thread> threads;
        int thread_count = 10;

K
kun yu 已提交
140 141 142 143 144 145 146 147
        std::shared_ptr<::milvus::grpc::InsertInfos> insert_info_array(
            new ::milvus::grpc::InsertInfos[thread_count],
            std::default_delete<::milvus::grpc::InsertInfos[]>() );

        std::shared_ptr<::milvus::grpc::VectorIds> vector_ids_array(
            new ::milvus::grpc::VectorIds[thread_count],
                std::default_delete<::milvus::grpc::VectorIds[]>() );

K
kun yu 已提交
148 149 150
        int64_t record_count = record_array.size() / thread_count;

        for (size_t i = 0; i < thread_count; i++) {
K
kun yu 已提交
151
            insert_info_array.get()[i].set_table_name(table_name);
K
kun yu 已提交
152
            for (size_t j = i * record_count; j < record_count * (i + 1); j++) {
K
kun yu 已提交
153 154
                ::milvus::grpc::RowRecord *grpc_record =
                    insert_info_array.get()[i].add_row_record_array();
K
kun yu 已提交
155 156 157 158 159 160 161 162 163 164
                for (size_t k = 0; k < record_array[j].data.size(); k++) {
                    grpc_record->add_vector_data(record_array[j].data[k]);
                }
            }
        }

        std::cout << "*****************************************************\n";
        auto start = std::chrono::high_resolution_clock::now();
        for (size_t j = 0; j < thread_count; j++) {
            threads.push_back(
K
kun yu 已提交
165 166 167
                    std::thread(&GrpcClient::InsertVector, client_ptr_,
                        std::ref(vector_ids_array.get()[j]), std::ref(insert_info_array.get()[j]),
                        std::ref(status)));
K
kun yu 已提交
168 169 170 171 172 173 174
        }
        std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
        auto finish = std::chrono::high_resolution_clock::now();
        std::cout << "InsertVector cost: " << std::chrono::duration_cast<std::chrono::duration<double>>(finish - start).count() << "s\n";
        std::cout << "*****************************************************\n";

        for (size_t i = 0; i < thread_count; i++) {
K
kun yu 已提交
175 176
            for (size_t j = 0; j < vector_ids_array.get()[i].vector_id_array_size(); j++) {
                id_array.push_back(vector_ids_array.get()[i].vector_id_array(j));
K
kun yu 已提交
177 178
            }
        }
K
kun yu 已提交
179
#else
Y
Yu Kun 已提交
180 181
        ::milvus::grpc::InsertParam insert_param;
        insert_param.set_table_name(table_name);
K
kun yu 已提交
182 183

        for (auto &record : record_array) {
Y
Yu Kun 已提交
184
            ::milvus::grpc::RowRecord *grpc_record = insert_param.add_row_record_array();
K
kun yu 已提交
185 186 187 188 189 190 191 192
            for (size_t i = 0; i < record.data.size(); i++) {
                grpc_record->add_vector_data(record.data[i]);
            }
        }

        ::milvus::grpc::VectorIds vector_ids;

        //Single thread
Y
Yu Kun 已提交
193
        client_ptr_->Insert(vector_ids, insert_param, status);
K
kun yu 已提交
194 195 196 197 198 199 200
        auto finish = std::chrono::high_resolution_clock::now();

        for (size_t i = 0; i < vector_ids.vector_id_array_size(); i++) {
            id_array.push_back(vector_ids.vector_id_array(i));
        }
#endif

K
kun yu 已提交
201
    } catch (std::exception &ex) {
K
kun yu 已提交
202
        return Status(StatusCode::UnknownError, "fail to add vector: " + std::string(ex.what()));
K
kun yu 已提交
203 204
    }

K
kun yu 已提交
205
    return status;
K
kun yu 已提交
206 207 208
}

Status
Y
Yu Kun 已提交
209
ClientProxy::Search(const std::string &table_name,
K
kun yu 已提交
210 211 212 213 214 215
                          const std::vector<RowRecord> &query_record_array,
                          const std::vector<Range> &query_range_array,
                          int64_t topk,
                          std::vector<TopKQueryResult> &topk_query_result_array) {
    try {
        //step 1: convert vectors data
Y
Yu Kun 已提交
216 217 218
        ::milvus::grpc::SearchParam search_param;
        search_param.set_table_name(table_name);
        search_param.set_topk(topk);
K
kun yu 已提交
219
        for (auto &record : query_record_array) {
Y
Yu Kun 已提交
220
            ::milvus::grpc::RowRecord *row_record = search_param.add_query_record_array();
K
kun yu 已提交
221 222
            for (auto &rec : record.data) {
                row_record->add_vector_data(rec);
K
kun yu 已提交
223 224 225 226 227
            }
        }

        //step 2: convert range array
        for (auto &range : query_range_array) {
Y
Yu Kun 已提交
228
            ::milvus::grpc::Range *grpc_range = search_param.add_query_range_array();
K
kun yu 已提交
229 230 231 232 233 234
            grpc_range->set_start_value(range.start_value);
            grpc_range->set_end_value(range.end_value);
        }

        //step 3: search vectors
        std::vector<::milvus::grpc::TopKQueryResult> result_array;
Y
Yu Kun 已提交
235
        Status status = client_ptr_->Search(result_array, search_param);
K
kun yu 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248

        //step 4: convert result array
        for (auto &grpc_topk_result : result_array) {
            TopKQueryResult result;
            for (size_t i = 0; i < grpc_topk_result.query_result_arrays_size(); i++) {
                QueryResult query_result;
                query_result.id = grpc_topk_result.query_result_arrays(i).id();
                query_result.distance = grpc_topk_result.query_result_arrays(i).distance();
                result.query_result_arrays.emplace_back(query_result);
            }

            topk_query_result_array.emplace_back(result);
        }
K
kun yu 已提交
249
        return status;
K
kun yu 已提交
250 251

    } catch (std::exception &ex) {
K
kun yu 已提交
252
        return Status(StatusCode::UnknownError, "fail to search vectors: " + std::string(ex.what()));
K
kun yu 已提交
253 254 255 256 257 258 259 260 261
    }

}

Status
ClientProxy::DescribeTable(const std::string &table_name, TableSchema &table_schema) {
    try {
        ::milvus::grpc::TableSchema grpc_schema;

K
kun yu 已提交
262
        Status status = client_ptr_->DescribeTable(grpc_schema, table_name);
K
kun yu 已提交
263

K
kun yu 已提交
264
        table_schema.table_name = grpc_schema.table_name().table_name();
K
kun yu 已提交
265 266 267
        table_schema.index_type = (IndexType) grpc_schema.index_type();
        table_schema.dimension = grpc_schema.dimension();
        table_schema.store_raw_vector = grpc_schema.store_raw_vector();
K
kun yu 已提交
268 269

        return status;
K
kun yu 已提交
270
    } catch (std::exception &ex) {
K
kun yu 已提交
271
        return Status(StatusCode::UnknownError, "fail to describe table: " + std::string(ex.what()));
K
kun yu 已提交
272 273 274 275 276
    }

}

Status
Y
Yu Kun 已提交
277
ClientProxy::CountTable(const std::string &table_name, int64_t &row_count) {
K
kun yu 已提交
278
    try {
K
kun yu 已提交
279
        Status status;
Y
Yu Kun 已提交
280
        row_count = client_ptr_->CountTable(table_name, status);
K
kun yu 已提交
281
        return status;
K
kun yu 已提交
282
    } catch (std::exception &ex) {
K
kun yu 已提交
283
        return Status(StatusCode::UnknownError, "fail to show tables: " + std::string(ex.what()));
K
kun yu 已提交
284 285 286 287 288 289
    }
}

Status
ClientProxy::ShowTables(std::vector<std::string> &table_array) {
    try {
K
kun yu 已提交
290
        return client_ptr_->ShowTables(table_array);
K
kun yu 已提交
291 292

    } catch (std::exception &ex) {
K
kun yu 已提交
293
        return Status(StatusCode::UnknownError, "fail to show tables: " + std::string(ex.what()));
K
kun yu 已提交
294 295 296 297 298
    }
}

std::string
ClientProxy::ServerVersion() const {
K
kun yu 已提交
299
    Status status = Status::OK();
K
kun yu 已提交
300 301
    try {
        std::string version;
Y
Yu Kun 已提交
302
        Status status = client_ptr_->Cmd(version, "version");
K
kun yu 已提交
303 304 305 306 307 308 309 310
        return version;
    } catch (std::exception &ex) {
        return "";
    }
}

std::string
ClientProxy::ServerStatus() const {
K
kun yu 已提交
311
    if (channel_ == nullptr) {
K
kun yu 已提交
312 313 314 315 316
        return "not connected to server";
    }

    try {
        std::string dummy;
Y
Yu Kun 已提交
317
        Status status = client_ptr_->Cmd(dummy, "");
K
kun yu 已提交
318 319 320 321 322 323
        return "server alive";
    } catch (std::exception &ex) {
        return "connection lost";
    }
}

Y
Yu Kun 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
Status
ClientProxy::DeleteByRange(milvus::Range &range, const std::string &table_name) {

}

Status
ClientProxy::PreloadTable(const std::string &table_name) const {

}

IndexParam
ClientProxy::DescribeIndex(const std::string &table_name) const {

}

Status
ClientProxy::DropIndex(const std::string &table_name) const {

}

K
kun yu 已提交
344
}