Skip to content

Commit 9629516

Browse files
committed
sparse: add some prometheus metrics for sparse inverted index
Add three prometheus metrics for sparse inverted index, including: - sparse dataset nnz histgram - sparse inverted index posting list length histgram - sparse inverted index size Signed-off-by: Shawn Wang <shawn.wang@zilliz.com>
1 parent 8a705a0 commit 9629516

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

include/knowhere/prometheus_client.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ extern const std::unique_ptr<PrometheusClient> prometheusClient;
8686
#define DECLARE_PROMETHEUS_COUNTER(name, module) extern prometheus::Counter& CONCATENATE(module, name);
8787
#define DECLARE_PROMETHEUS_HISTOGRAM(name, module) extern prometheus::Histogram& CONCATENATE(module, name);
8888

89+
#define DECLARE_PROMETHEUS_GAUGE_FAMILY(name, module) \
90+
extern prometheus::Family<prometheus::Gauge>& CONCATENATE(name, family);
91+
#define DECLARE_PROMETHEUS_COUNTER_FAMILY(name, module) \
92+
extern prometheus::Family<prometheus::Counter>& CONCATENATE(name, family);
93+
#define DECLARE_PROMETHEUS_HISTOGRAM_FAMILY(name, module) \
94+
extern prometheus::Family<prometheus::Histogram>& CONCATENATE(name, family);
95+
8996
DECLARE_PROMETHEUS_HISTOGRAM(build_latency, PROMETHEUS_LABEL_KNOWHERE);
9097
DECLARE_PROMETHEUS_HISTOGRAM(build_latency, PROMETHEUS_LABEL_CARDINAL);
9198

@@ -131,4 +138,8 @@ DECLARE_PROMETHEUS_HISTOGRAM(hnsw_search_hops, PROMETHEUS_LABEL_KNOWHERE);
131138
DECLARE_PROMETHEUS_HISTOGRAM(diskann_bitset_ratio, PROMETHEUS_LABEL_KNOWHERE);
132139
DECLARE_PROMETHEUS_HISTOGRAM(diskann_search_hops, PROMETHEUS_LABEL_KNOWHERE);
133140
DECLARE_PROMETHEUS_HISTOGRAM(diskann_range_search_iters, PROMETHEUS_LABEL_KNOWHERE);
141+
142+
DECLARE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_dataset_nnz_len, PROMETHEUS_LABEL_KNOWHERE);
143+
DECLARE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_inverted_index_posting_list_len, PROMETHEUS_LABEL_KNOWHERE);
144+
DECLARE_PROMETHEUS_GAUGE_FAMILY(sparse_inverted_index_size, PROMETHEUS_LABEL_KNOWHERE);
134145
} // namespace knowhere

src/common/prometheus_client.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,7 @@ DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(diskann_range_search_iters, "DISKANN range se
122122
DEFINE_PROMETHEUS_HISTOGRAM_WITH_BUCKETS(diskann_range_search_iters, PROMETHEUS_LABEL_KNOWHERE,
123123
diskannRangeSearchIterBuckets)
124124

125+
DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_dataset_nnz_len, "sparse dataset nnz length")
126+
DEFINE_PROMETHEUS_HISTOGRAM_FAMILY(sparse_inverted_index_posting_list_len, "sparse inverted index posting list length")
127+
DEFINE_PROMETHEUS_GAUGE_FAMILY(sparse_inverted_index_size, "sparse inverted index size (MB)")
125128
} // namespace knowhere

src/index/sparse/sparse_inverted_index.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "knowhere/comp/index_param.h"
3131
#include "knowhere/expected.h"
3232
#include "knowhere/log.h"
33+
#include "knowhere/prometheus_client.h"
3334
#include "knowhere/sparse_utils.h"
3435
#include "knowhere/utils.h"
3536

@@ -94,6 +95,18 @@ template <typename DType, typename QType, InvertedIndexAlgo algo, bool mmapped =
9495
class InvertedIndex : public BaseInvertedIndex<DType> {
9596
public:
9697
explicit InvertedIndex(SparseMetricType metric_type) : metric_type_(metric_type) {
98+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
99+
// for now, use timestamp as index_id
100+
index_id_ = std::to_string(
101+
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch())
102+
.count());
103+
index_size_gauge_ =
104+
&sparse_inverted_index_size_family.Add({{"index_id", index_id_}, {"index_type", "inverted"}});
105+
index_dataset_nnz_len_histogram_ =
106+
&sparse_dataset_nnz_len_family.Add({{"index_id", index_id_}, {"index_type", "inverted"}}, defaultBuckets);
107+
index_posting_list_len_histogram_ = &sparse_inverted_index_posting_list_len_family.Add(
108+
{{"index_id", index_id_}, {"index_type", "inverted"}}, defaultBuckets);
109+
#endif
97110
}
98111

99112
~InvertedIndex() override {
@@ -112,6 +125,17 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
112125
map_fd_ = -1;
113126
}
114127
}
128+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
129+
if (index_size_gauge_ != nullptr) {
130+
sparse_inverted_index_size_family.Remove(index_size_gauge_);
131+
}
132+
if (index_dataset_nnz_len_histogram_ != nullptr) {
133+
sparse_dataset_nnz_len_family.Remove(index_dataset_nnz_len_histogram_);
134+
}
135+
if (index_posting_list_len_histogram_ != nullptr) {
136+
sparse_inverted_index_posting_list_len_family.Remove(index_posting_list_len_histogram_);
137+
}
138+
#endif
115139
}
116140

117141
template <typename U>
@@ -248,6 +272,10 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
248272
}
249273

250274
for (int64_t i = 0; i < rows; ++i) {
275+
if (i % (rows / 10) == 0) {
276+
LOG_KNOWHERE_INFO_ << "Loading progress: " << (i * 100 / rows) << "%";
277+
}
278+
251279
size_t count;
252280
readBinaryPOD(reader, count);
253281
SparseRow<DType> raw_row;
@@ -261,10 +289,21 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
261289
}
262290
}
263291
add_row_to_index(raw_row, i);
292+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
293+
index_dataset_nnz_len_histogram_->Observe(count);
294+
#endif
295+
}
296+
297+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
298+
for (size_t i = 0; i < dim_map_.size(); ++i) {
299+
index_posting_list_len_histogram_->Observe(inverted_index_ids_[i].size());
264300
}
301+
index_size_gauge_->Set((double)size() / 1024.0 / 1024.0);
302+
#endif
265303

266304
n_rows_internal_ = rows;
267305

306+
LOG_KNOWHERE_INFO_ << "Loading progress: 100%";
268307
return Status::success;
269308
}
270309

@@ -921,7 +960,18 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
921960
}
922961
inverted_index_ids_[dim_it->second].emplace_back(vec_id);
923962
inverted_index_vals_[dim_it->second].emplace_back(get_quant_val(val));
924-
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
963+
}
964+
// update max_score_in_dim_
965+
if constexpr (algo == InvertedIndexAlgo::DAAT_WAND || algo == InvertedIndexAlgo::DAAT_MAXSCORE) {
966+
for (size_t j = 0; j < row.size(); ++j) {
967+
auto [dim, val] = row[j];
968+
if (val == 0) {
969+
continue;
970+
}
971+
auto dim_it = dim_map_.find(dim);
972+
if (dim_it == dim_map_.cend()) {
973+
throw std::runtime_error("unexpected vector dimension in InvertedIndex");
974+
}
925975
auto score = static_cast<float>(val);
926976
if (metric_type_ == SparseMetricType::METRIC_BM25) {
927977
score = bm25_params_->max_score_computer(val, row_sum);
@@ -984,6 +1034,12 @@ class InvertedIndex : public BaseInvertedIndex<DType> {
9841034

9851035
std::unique_ptr<BM25Params> bm25_params_;
9861036

1037+
#if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
1038+
std::string index_id_{};
1039+
prometheus::Gauge* index_size_gauge_{nullptr};
1040+
prometheus::Histogram* index_dataset_nnz_len_histogram_{nullptr};
1041+
prometheus::Histogram* index_posting_list_len_histogram_{nullptr};
1042+
#endif
9871043
}; // class InvertedIndex
9881044

9891045
} // namespace knowhere::sparse

0 commit comments

Comments
 (0)