ClientProxy.cpp 14.6 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
//
//   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 已提交
18 19 20 21 22 23 24 25
#include "sdk/grpc/ClientProxy.h"
#include "../../../version.h"
#include "grpc/gen-milvus/milvus.grpc.pb.h"

#include <memory>
#include <vector>
#include <string>

K
kun yu 已提交
26
//#define GRPC_MULTIPLE_THREAD;
K
kun yu 已提交
27 28

namespace milvus {
K
kun yu 已提交
29 30
bool
UriCheck(const std::string &uri) {
G
groot 已提交
31
    size_t index = uri.find_first_of(':', 0);
K
kun yu 已提交
32 33 34 35 36 37 38
    if (index == std::string::npos) {
        return false;
    } else {
        return true;
    }
}

K
kun yu 已提交
39 40 41 42 43 44 45
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 已提交
46 47 48 49 50 51
        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 已提交
52 53 54 55 56
    }
}

Status
ClientProxy::Connect(const std::string &uri) {
K
kun yu 已提交
57
    if (!UriCheck(uri)) {
G
groot 已提交
58
        return Status(StatusCode::InvalidAgument, "Invalid uri");
K
kun yu 已提交
59
    }
K
kun yu 已提交
60
    size_t index = uri.find_first_of(':', 0);
K
kun yu 已提交
61 62 63 64 65 66 67 68 69 70 71 72

    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 已提交
73
        return client_ptr_->Cmd(info, "");
K
kun yu 已提交
74 75 76 77 78 79 80
    } catch (std::exception &ex) {
        return Status(StatusCode::NotConnected, "connection lost: " + std::string(ex.what()));
    }
}

Status
ClientProxy::Disconnect() {
K
kun yu 已提交
81
    try {
K
kun yu 已提交
82
        Status status = client_ptr_->Disconnect();
K
kun yu 已提交
83 84
        connected_ = false;
        channel_.reset();
K
kun yu 已提交
85
        return status;
G
groot 已提交
86
    } catch (std::exception &ex) {
K
kun yu 已提交
87 88
        return Status(StatusCode::UnknownError, "failed to disconnect: " + std::string(ex.what()));
    }
K
kun yu 已提交
89 90 91 92
}

std::string
ClientProxy::ClientVersion() const {
K
kun yu 已提交
93
    return MILVUS_VERSION;
K
kun yu 已提交
94 95 96 97 98 99
}

Status
ClientProxy::CreateTable(const TableSchema &param) {
    try {
        ::milvus::grpc::TableSchema schema;
100
        schema.set_table_name(param.table_name);
K
kun yu 已提交
101
        schema.set_dimension(param.dimension);
102
        schema.set_index_file_size(param.index_file_size);
G
groot 已提交
103
        schema.set_metric_type((int32_t) param.metric_type);
K
kun yu 已提交
104

K
kun yu 已提交
105
        return client_ptr_->CreateTable(schema);
K
kun yu 已提交
106 107 108 109 110 111 112
    } 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 已提交
113
    Status status = Status::OK();
K
kun yu 已提交
114 115
    ::milvus::grpc::TableName grpc_table_name;
    grpc_table_name.set_table_name(table_name);
K
kun yu 已提交
116 117
    bool result = client_ptr_->HasTable(grpc_table_name, status);
    return result;
K
kun yu 已提交
118 119 120 121 122 123 124
}

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 已提交
125
        return client_ptr_->DropTable(grpc_table_name);
K
kun yu 已提交
126 127 128 129 130 131
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to drop table: " + std::string(ex.what()));
    }
}

Status
Y
Yu Kun 已提交
132
ClientProxy::CreateIndex(const IndexParam &index_param) {
K
kun yu 已提交
133
    try {
G
groot 已提交
134
        //TODO: add index params
Y
Yu Kun 已提交
135
        ::milvus::grpc::IndexParam grpc_index_param;
136
        grpc_index_param.set_table_name(index_param.table_name);
G
groot 已提交
137
        grpc_index_param.mutable_index()->set_index_type((int32_t) index_param.index_type);
138
        grpc_index_param.mutable_index()->set_nlist(index_param.nlist);
Y
Yu Kun 已提交
139
        return client_ptr_->CreateIndex(grpc_index_param);
K
kun yu 已提交
140 141 142 143 144
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "failed to build index: " + std::string(ex.what()));
    }
}

K
kun yu 已提交
145
Status
Y
Yu Kun 已提交
146
ClientProxy::Insert(const std::string &table_name,
G
groot 已提交
147 148
                    const std::vector<RowRecord> &record_array,
                    std::vector<int64_t> &id_array) {
K
kun yu 已提交
149
    Status status = Status::OK();
K
kun yu 已提交
150 151
    try {
////////////////////////////////////////////////////////////////////////////
K
kun yu 已提交
152
#ifdef GRPC_MULTIPLE_THREAD
K
kun yu 已提交
153 154 155
        std::vector<std::thread> threads;
        int thread_count = 10;

K
kun yu 已提交
156 157 158 159 160 161 162 163
        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 已提交
164 165 166
        int64_t record_count = record_array.size() / thread_count;

        for (size_t i = 0; i < thread_count; i++) {
K
kun yu 已提交
167
            insert_info_array.get()[i].set_table_name(table_name);
K
kun yu 已提交
168
            for (size_t j = i * record_count; j < record_count * (i + 1); j++) {
K
kun yu 已提交
169 170
                ::milvus::grpc::RowRecord *grpc_record =
                    insert_info_array.get()[i].add_row_record_array();
K
kun yu 已提交
171 172 173 174 175 176 177 178 179 180
                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 已提交
181 182 183
                    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 已提交
184 185 186
        }
        std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
        auto finish = std::chrono::high_resolution_clock::now();
G
groot 已提交
187 188 189
        std::cout <<
        "InsertVector cost: " << std::chrono::duration_cast<std::chrono::duration<double>>(finish - start).count()
        << "s\n";
K
kun yu 已提交
190 191 192
        std::cout << "*****************************************************\n";

        for (size_t i = 0; i < thread_count; i++) {
K
kun yu 已提交
193 194
            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 已提交
195 196
            }
        }
K
kun yu 已提交
197
#else
Y
Yu Kun 已提交
198 199
        ::milvus::grpc::InsertParam insert_param;
        insert_param.set_table_name(table_name);
K
kun yu 已提交
200 201

        for (auto &record : record_array) {
Y
Yu Kun 已提交
202
            ::milvus::grpc::RowRecord *grpc_record = insert_param.add_row_record_array();
K
kun yu 已提交
203 204 205 206 207 208
            for (size_t i = 0; i < record.data.size(); i++) {
                grpc_record->add_vector_data(record.data[i]);
            }
        }

        //Single thread
Y
Yu Kun 已提交
209 210 211 212 213 214 215 216 217 218 219
        ::milvus::grpc::VectorIds vector_ids;
        if (!id_array.empty()) {
            for (auto i = 0; i < id_array.size(); i++) {
                insert_param.add_row_id_array(id_array[i]);
            }
            client_ptr_->Insert(vector_ids, insert_param, status);
        } else {
            client_ptr_->Insert(vector_ids, insert_param, status);
            for (size_t i = 0; i < vector_ids.vector_id_array_size(); i++) {
                id_array.push_back(vector_ids.vector_id_array(i));
            }
K
kun yu 已提交
220 221
        }
#endif
K
kun yu 已提交
222
    } catch (std::exception &ex) {
K
kun yu 已提交
223
        return Status(StatusCode::UnknownError, "fail to add vector: " + std::string(ex.what()));
K
kun yu 已提交
224 225
    }

K
kun yu 已提交
226
    return status;
K
kun yu 已提交
227 228 229
}

Status
Y
Yu Kun 已提交
230
ClientProxy::Search(const std::string &table_name,
G
groot 已提交
231 232 233 234 235
                    const std::vector<RowRecord> &query_record_array,
                    const std::vector<Range> &query_range_array,
                    int64_t topk,
                    int64_t nprobe,
                    std::vector<TopKQueryResult> &topk_query_result_array) {
K
kun yu 已提交
236 237
    try {
        //step 1: convert vectors data
Y
Yu Kun 已提交
238 239 240
        ::milvus::grpc::SearchParam search_param;
        search_param.set_table_name(table_name);
        search_param.set_topk(topk);
Y
Yu Kun 已提交
241
        search_param.set_nprobe(nprobe);
K
kun yu 已提交
242
        for (auto &record : query_record_array) {
Y
Yu Kun 已提交
243
            ::milvus::grpc::RowRecord *row_record = search_param.add_query_record_array();
K
kun yu 已提交
244 245
            for (auto &rec : record.data) {
                row_record->add_vector_data(rec);
K
kun yu 已提交
246 247 248 249 250
            }
        }

        //step 2: convert range array
        for (auto &range : query_range_array) {
Y
Yu Kun 已提交
251
            ::milvus::grpc::Range *grpc_range = search_param.add_query_range_array();
K
kun yu 已提交
252 253 254 255 256
            grpc_range->set_start_value(range.start_value);
            grpc_range->set_end_value(range.end_value);
        }

        //step 3: search vectors
257 258
        ::milvus::grpc::TopKQueryResultList topk_query_result_list;
        Status status = client_ptr_->Search(topk_query_result_list, search_param);
K
kun yu 已提交
259 260

        //step 4: convert result array
261
        for (uint64_t i = 0; i < topk_query_result_list.topk_query_result_size(); ++i) {
K
kun yu 已提交
262
            TopKQueryResult result;
263
            for (uint64_t j = 0; j < topk_query_result_list.topk_query_result(i).query_result_arrays_size(); ++j) {
K
kun yu 已提交
264
                QueryResult query_result;
265 266
                query_result.id = topk_query_result_list.topk_query_result(i).query_result_arrays(j).id();
                query_result.distance = topk_query_result_list.topk_query_result(i).query_result_arrays(j).distance();
K
kun yu 已提交
267 268 269 270 271
                result.query_result_arrays.emplace_back(query_result);
            }

            topk_query_result_array.emplace_back(result);
        }
K
kun yu 已提交
272
        return status;
K
kun yu 已提交
273
    } catch (std::exception &ex) {
K
kun yu 已提交
274
        return Status(StatusCode::UnknownError, "fail to search vectors: " + std::string(ex.what()));
K
kun yu 已提交
275 276 277 278 279 280 281 282
    }
}

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

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

285
        table_schema.table_name = grpc_schema.table_name();
K
kun yu 已提交
286
        table_schema.dimension = grpc_schema.dimension();
287
        table_schema.index_file_size = grpc_schema.index_file_size();
G
groot 已提交
288
        table_schema.metric_type = (MetricType) grpc_schema.metric_type();
K
kun yu 已提交
289 290

        return status;
K
kun yu 已提交
291
    } catch (std::exception &ex) {
K
kun yu 已提交
292
        return Status(StatusCode::UnknownError, "fail to describe table: " + std::string(ex.what()));
K
kun yu 已提交
293 294 295 296
    }
}

Status
Y
Yu Kun 已提交
297
ClientProxy::CountTable(const std::string &table_name, int64_t &row_count) {
K
kun yu 已提交
298
    try {
K
kun yu 已提交
299
        Status status;
Y
Yu Kun 已提交
300
        row_count = client_ptr_->CountTable(table_name, status);
K
kun yu 已提交
301
        return status;
K
kun yu 已提交
302
    } catch (std::exception &ex) {
K
kun yu 已提交
303
        return Status(StatusCode::UnknownError, "fail to show tables: " + std::string(ex.what()));
K
kun yu 已提交
304 305 306 307 308 309
    }
}

Status
ClientProxy::ShowTables(std::vector<std::string> &table_array) {
    try {
310 311 312 313 314 315 316 317 318
        Status status;
        milvus::grpc::TableNameList table_name_list;
        status = client_ptr_->ShowTables(table_name_list);

        table_array.resize(table_name_list.table_names_size());
        for (uint64_t i = 0; i < table_name_list.table_names_size(); ++i) {
            table_array[i] = table_name_list.table_names(i);
        }
        return status;
K
kun yu 已提交
319
    } catch (std::exception &ex) {
K
kun yu 已提交
320
        return Status(StatusCode::UnknownError, "fail to show tables: " + std::string(ex.what()));
K
kun yu 已提交
321 322 323 324 325
    }
}

std::string
ClientProxy::ServerVersion() const {
K
kun yu 已提交
326
    Status status = Status::OK();
K
kun yu 已提交
327 328
    try {
        std::string version;
Y
Yu Kun 已提交
329
        Status status = client_ptr_->Cmd(version, "version");
K
kun yu 已提交
330 331 332 333 334 335 336 337
        return version;
    } catch (std::exception &ex) {
        return "";
    }
}

std::string
ClientProxy::ServerStatus() const {
K
kun yu 已提交
338
    if (channel_ == nullptr) {
K
kun yu 已提交
339 340 341 342 343
        return "not connected to server";
    }

    try {
        std::string dummy;
Y
Yu Kun 已提交
344
        Status status = client_ptr_->Cmd(dummy, "");
K
kun yu 已提交
345 346 347 348 349 350
        return "server alive";
    } catch (std::exception &ex) {
        return "connection lost";
    }
}

Y
Yu Kun 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
std::string
ClientProxy::DumpTaskTables() const {
    if (channel_ == nullptr) {
        return "not connected to server";
    }

    try {
        std::string dummy;
        Status status = client_ptr_->Cmd(dummy, "tasktable");
        return dummy;
    } catch (std::exception &ex) {
        return "connection lost";
    }
}

Y
Yu Kun 已提交
366 367
Status
ClientProxy::DeleteByRange(milvus::Range &range, const std::string &table_name) {
368 369 370 371 372 373 374 375 376
    try {
        ::milvus::grpc::DeleteByRangeParam delete_by_range_param;
        delete_by_range_param.set_table_name(table_name);
        delete_by_range_param.mutable_range()->set_start_value(range.start_value);
        delete_by_range_param.mutable_range()->set_end_value(range.end_value);
        return client_ptr_->DeleteByRange(delete_by_range_param);
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "fail to delete by range: " + std::string(ex.what()));
    }
Y
Yu Kun 已提交
377 378 379 380
}

Status
ClientProxy::PreloadTable(const std::string &table_name) const {
Y
Yu Kun 已提交
381 382 383 384 385 386
    try {
        ::milvus::grpc::TableName grpc_table_name;
        grpc_table_name.set_table_name(table_name);
        Status status = client_ptr_->PreloadTable(grpc_table_name);
        return status;
    } catch (std::exception &ex) {
387
        return Status(StatusCode::UnknownError, "fail to preload tables: " + std::string(ex.what()));
Y
Yu Kun 已提交
388
    }
Y
Yu Kun 已提交
389 390
}

391 392 393 394 395 396 397
Status
ClientProxy::DescribeIndex(const std::string &table_name, IndexParam &index_param) const {
    try {
        ::milvus::grpc::TableName grpc_table_name;
        grpc_table_name.set_table_name(table_name);
        ::milvus::grpc::IndexParam grpc_index_param;
        Status status = client_ptr_->DescribeIndex(grpc_table_name, grpc_index_param);
G
groot 已提交
398
        index_param.index_type = (IndexType) (grpc_index_param.mutable_index()->index_type());
399 400 401 402 403 404
        index_param.nlist = grpc_index_param.mutable_index()->nlist();

        return status;
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "fail to describe index: " + std::string(ex.what()));
    }
Y
Yu Kun 已提交
405 406 407 408
}

Status
ClientProxy::DropIndex(const std::string &table_name) const {
409 410 411 412 413 414 415 416
    try {
        ::milvus::grpc::TableName grpc_table_name;
        grpc_table_name.set_table_name(table_name);
        Status status = client_ptr_->DropIndex(grpc_table_name);
        return status;
    } catch (std::exception &ex) {
        return Status(StatusCode::UnknownError, "fail to drop index: " + std::string(ex.what()));
    }
Y
Yu Kun 已提交
417 418
}

G
groot 已提交
419
} // namespace milvus