Skip to content

Commit

Permalink
[feat][sdk] Impl vector get index metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
wchuande authored and ketor committed Mar 13, 2024
1 parent 0265380 commit cd9353c
Show file tree
Hide file tree
Showing 14 changed files with 428 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/example/sdk_vector_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "sdk/status.h"
#include "sdk/vector.h"
#include "sdk/vector/vector_common.h"
#include "sdk/vector/vector_get_index_metrics_task.h"
#include "sdk/vector/vector_index.h"
#include "sdk/vector/vector_index_cache.h"

Expand Down Expand Up @@ -295,6 +296,25 @@ static void VectorScanQuery(bool use_index_name = false) {
}
}

static void VectorGetIndexMetrics(bool use_index_name = false) {
Status tmp;
dingodb::sdk::IndexMetricsResult result;
if (use_index_name) {
tmp = g_vector_client->GetIndexMetricsByIndexName(g_schema_id, g_index_name, result);
} else {
tmp = g_vector_client->GetIndexMetricsByIndexId(g_index_id, result);
}

DINGO_LOG(INFO) << "vector get index metrics:" << tmp.ToString() << ", result :" << result.ToString();
if (tmp.ok()) {
CHECK_EQ(result.index_type, dingodb::sdk::VectorIndexType::kFlat);
CHECK_EQ(result.count, g_vector_ids.size());
CHECK_EQ(result.deleted_count, 0);
CHECK_EQ(result.max_vector_id, g_vector_ids[g_vector_ids.size() - 1]);
CHECK_EQ(result.min_vector_id, g_vector_ids[0]);
}
}

static void VectorDelete(bool use_index_name = false) {
Status tmp;
std::vector<dingodb::sdk::DeleteResult> result;
Expand Down Expand Up @@ -343,6 +363,7 @@ int main(int argc, char* argv[]) {
VectorQuey();
VectorGetBorder();
VectorScanQuery();
VectorGetIndexMetrics();
VectorDelete();
VectorSearch();

Expand All @@ -358,6 +379,7 @@ int main(int argc, char* argv[]) {
VectorQuey(true);
VectorGetBorder(true);
VectorScanQuery(true);
VectorGetIndexMetrics(true);
VectorDelete(true);
VectorSearch(true);

Expand Down
14 changes: 12 additions & 2 deletions src/pysdk/dingosdk.swg
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace dingodb {
%feature("docstring") VectorClient::GetBorderByIndexName "return Status, int64_t out_vector_id"
%feature("docstring") VectorClient::ScanQueryByIndexId "return Status, ScanQueryResult out_result"
%feature("docstring") VectorClient::ScanQueryByIndexName "return Status, ScanQueryResult out_result"
%feature("docstring") VectorClient::GetIndexMetricsByIndexId "return Status, IndexMetricsResult out_result"
%feature("docstring") VectorClient::GetIndexMetricsByIndexName "return Status, IndexMetricsResult out_result"

%typemap(in, numinputs=0) Client** (Client* temp){
temp = NULL;
Expand Down Expand Up @@ -162,15 +164,23 @@ namespace dingodb {
$1 = &temp;
}
%typemap(argout) QueryResult& {
PyObject* obj = SWIG_NewPointerObj(new QueryResult(*$1), SWIGTYPE_p_dingodb__sdk__QueryResult, SWIG_POINTER_OWN);
PyObject* obj = SWIG_NewPointerObj(new QueryResult(*$1), $1_descriptor, SWIG_POINTER_OWN);
$result = SWIG_AppendOutput($result, obj);
}

%typemap(in, numinputs=0) ScanQueryResult& (ScanQueryResult temp) {
$1 = &temp;
}
%typemap(argout) ScanQueryResult& {
PyObject* obj = SWIG_NewPointerObj(new ScanQueryResult(*$1), SWIGTYPE_p_dingodb__sdk__ScanQueryResult, SWIG_POINTER_OWN);
PyObject* obj = SWIG_NewPointerObj(new ScanQueryResult(*$1), $1_descriptor, SWIG_POINTER_OWN);
$result = SWIG_AppendOutput($result, obj);
}

%typemap(in, numinputs=0) IndexMetricsResult& (IndexMetricsResult temp) {
$1 = &temp;
}
%typemap(argout) IndexMetricsResult& {
PyObject* obj = SWIG_NewPointerObj(new IndexMetricsResult(*$1), $1_descriptor, SWIG_POINTER_OWN);
$result = SWIG_AppendOutput($result, obj);
}

Expand Down
16 changes: 16 additions & 0 deletions src/pysdk/pysdk_vector_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,20 @@ def vector_scan_query(use_index_name=False):
assert result.vectors[0].id == g_vector_ids[-1]
assert result.vectors[1].id == g_vector_ids[-2]

def vector_get_index_metrics(use_index_name=False):
if use_index_name:
tmp, result = g_vector_client.GetIndexMetricsByIndexName(g_schema_id, g_index_name)
else:
tmp, result = g_vector_client.GetIndexMetricsByIndexId(g_index_id)

print(f"vector get index metrics: {tmp.ToString()}, result : {result.ToString()}")
if tmp.ok():
assert result.index_type == dingosdk.kFlat
assert result.count == len(g_vector_ids)
assert result.deleted_count == 0
assert result.max_vector_id == g_vector_ids[-1]
assert result.min_vector_id == g_vector_ids[0]

def vector_delete(use_index_name=False):
if use_index_name:
tmp, result = g_vector_client.DeleteByIndexName(g_schema_id, g_index_name, g_vector_ids)
Expand All @@ -202,6 +216,7 @@ def vector_delete(use_index_name=False):
vector_query()
vector_get_border()
vector_scan_query()
vector_get_index_metrics()
vector_delete()
post_clean()

Expand All @@ -211,6 +226,7 @@ def vector_delete(use_index_name=False):
vector_query(True)
vector_get_border(True)
vector_scan_query(True)
vector_get_index_metrics(True)
vector_delete(True)
post_clean(True)

1 change: 1 addition & 0 deletions src/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ add_library(sdk
vector/vector_batch_query_task.cc
vector/vector_delete_task.cc
vector/vector_get_border_task.cc
vector/vector_get_index_metrics_task.cc
vector/vector_scan_query_task.cc
vector/vector_search_task.cc
utils/thread_pool_actuator.cc
Expand Down
14 changes: 14 additions & 0 deletions src/sdk/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,17 @@ struct ScanQueryResult {
std::string ToString() const;
};

struct IndexMetricsResult {
VectorIndexType index_type{kNoneIndexType};
int64_t count{0};
int64_t deleted_count{0};
int64_t max_vector_id{0};
int64_t min_vector_id{0};
int64_t memory_bytes{0};

std::string ToString() const;
};

class VectorIndexCreator {
public:
~VectorIndexCreator();
Expand Down Expand Up @@ -496,6 +507,9 @@ class VectorClient {
Status ScanQueryByIndexName(int64_t schema_id, const std::string& index_name, const ScanQueryParam& query_param,
ScanQueryResult& out_result);

Status GetIndexMetricsByIndexId(int64_t index_id, IndexMetricsResult& out_result);
Status GetIndexMetricsByIndexName(int64_t schema_id, const std::string& index_name, IndexMetricsResult& out_result);

private:
friend class Client;

Expand Down
1 change: 1 addition & 0 deletions src/sdk/vector/index_service_rpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ DEFINE_INDEX_SERVICE_RPC(VectorDelete);
DEFINE_INDEX_SERVICE_RPC(VectorBatchQuery);
DEFINE_INDEX_SERVICE_RPC(VectorGetBorderId);
DEFINE_INDEX_SERVICE_RPC(VectorScanQuery);
DEFINE_INDEX_SERVICE_RPC(VectorGetRegionMetrics);

} // namespace sdk
} // namespace dingodb
1 change: 1 addition & 0 deletions src/sdk/vector/index_service_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ DECLARE_INDEX_SERVICE_RPC(VectorDelete);
DECLARE_INDEX_SERVICE_RPC(VectorBatchQuery);
DECLARE_INDEX_SERVICE_RPC(VectorGetBorderId);
DECLARE_INDEX_SERVICE_RPC(VectorScanQuery);
DECLARE_INDEX_SERVICE_RPC(VectorGetRegionMetrics);

} // namespace sdk
} // namespace dingodb
Expand Down
16 changes: 16 additions & 0 deletions src/sdk/vector/vector_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "sdk/vector/vector_batch_query_task.h"
#include "sdk/vector/vector_delete_task.h"
#include "sdk/vector/vector_get_border_task.h"
#include "sdk/vector/vector_get_index_metrics_task.h"
#include "sdk/vector/vector_index_cache.h"
#include "sdk/vector/vector_scan_query_task.h"
#include "sdk/vector/vector_search_task.h"
Expand Down Expand Up @@ -125,6 +126,21 @@ Status VectorClient::ScanQueryByIndexName(int64_t schema_id, const std::string&
VectorScanQueryTask task(stub_, index_id, query_param, out_result);
return task.Run();
}

Status VectorClient::GetIndexMetricsByIndexId(int64_t index_id, IndexMetricsResult& out_result) {
VectorGetIndexMetricsTask task(stub_, index_id, out_result);
return task.Run();
}

Status VectorClient::GetIndexMetricsByIndexName(int64_t schema_id, const std::string& index_name,
IndexMetricsResult& out_result) {
int64_t index_id{0};
DINGO_RETURN_NOT_OK(
stub_.GetVectorIndexCache()->GetIndexIdByKey(EncodeVectorIndexCacheKey(schema_id, index_name), index_id));
CHECK_GT(index_id, 0);
VectorGetIndexMetricsTask task(stub_, index_id, out_result);
return task.Run();
}
} // namespace sdk

} // namespace dingodb
12 changes: 12 additions & 0 deletions src/sdk/vector/vector_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ static VectorWithDistance InternalVectorWithDistance2VectorWithDistance(const pb
return std::move(to_return);
}

static IndexMetricsResult InternalVectorIndexMetrics2IndexMetricsResult(const pb::common::VectorIndexMetrics& pb) {
IndexMetricsResult to_return;
to_return.index_type = InternalVectorIndexTypePB2VectorIndexType(pb.vector_index_type());
to_return.count = pb.current_count();
to_return.deleted_count = pb.deleted_count();
to_return.max_vector_id = pb.max_id();
to_return.min_vector_id = pb.min_id();
to_return.memory_bytes = pb.memory_bytes();

return to_return;
}

static void FillSearchFlatParamPB(pb::common::SearchFlatParam* pb, const SearchParam& parameter) {
if (parameter.extra_params.find(SearchExtraParamType::kParallelOnQueries) != parameter.extra_params.end()) {
pb->set_parallel_on_queries(parameter.extra_params.at(SearchExtraParamType::kParallelOnQueries));
Expand Down
5 changes: 1 addition & 4 deletions src/sdk/vector/vector_get_border_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VectorGetBorderPartTask;
class VectorGetBorderTask : public VectorTask {
public:
VectorGetBorderTask(const ClientStub& stub, int64_t index_id, bool is_max, int64_t& out_vector_id)
: VectorTask(stub), index_id_(index_id), is_max_(is_max), out_vector_id_(out_vector_id), controller_(stub, rpc_) {
: VectorTask(stub), index_id_(index_id), is_max_(is_max), out_vector_id_(out_vector_id) {
target_vector_id_ = is_max_ ? -1 : INT64_MAX;
}

Expand All @@ -50,9 +50,6 @@ class VectorGetBorderTask : public VectorTask {
std::shared_ptr<VectorIndex> vector_index_;
int64_t target_vector_id_;

VectorGetBorderIdRpc rpc_;
StoreRpcController controller_;

std::shared_mutex rw_lock_;
std::set<int64_t> next_part_ids_;
Status status_;
Expand Down
Loading

0 comments on commit cd9353c

Please sign in to comment.