Skip to content

Commit

Permalink
mbvar support delete_stats and has_stats interface (#2041)
Browse files Browse the repository at this point in the history
* mbvar support delete_stats and has_stats interface

* Update multi_dimension_inl.h
  • Loading branch information
serverglen authored Dec 6, 2022
1 parent bdbd92a commit a39db73
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/bvar/multi_dimension.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class MultiDimension : public MVariable {
return get_stats_impl(labels_value, READ_OR_INSERT);
}

// Remove stat so those not count and dump
void delete_stats(const key_type& labels_value);

// True if bvar pointer exists
bool has_stats(const key_type& labels_value);

// Get number of stats
size_t count_stats();

Expand All @@ -96,7 +102,7 @@ class MultiDimension : public MVariable {
// Return real bvar pointer if labels_name exist, NULL otherwise.
// CAUTION!!! Just For Debug!!!
T* get_stats_read_only(const key_type& labels_value) {
return get_stats_impl(labels_value, READ_ONLY);
return get_stats_impl(labels_value);
}

// Get real bvar pointer object
Expand All @@ -105,14 +111,12 @@ class MultiDimension : public MVariable {
T* get_stats_read_or_insert(const key_type& labels_value, bool* do_write = NULL) {
return get_stats_impl(labels_value, READ_OR_INSERT, do_write);
}

// Remove all stats so those not count and dump
// CAUTION!!! Just For Debug!!!
void delete_stats(const key_type& labels_value);
#endif

private:
T* get_stats_impl(const key_type& labels_value, STATS_OP stats_op = READ_ONLY, bool* do_write = NULL);
T* get_stats_impl(const key_type& labels_value);

T* get_stats_impl(const key_type& labels_value, STATS_OP stats_op, bool* do_write = NULL);

void make_dump_key(std::ostream& os,
const key_type& labels_value,
Expand All @@ -125,6 +129,7 @@ class MultiDimension : public MVariable {

bool is_valid_lables_value(const key_type& labels_value) const;

// Remove all stats so those not count and dump
void delete_stats();

static size_t init_flatmap(MetricMap& bg);
Expand Down
27 changes: 25 additions & 2 deletions src/bvar/multi_dimension_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ size_t MultiDimension<T>::count_stats() {
return metric_map_ptr->size();
}

#ifdef UNIT_TEST
template <typename T>
inline
void MultiDimension<T>::delete_stats(const key_type& labels_value) {
Expand All @@ -110,7 +109,6 @@ void MultiDimension<T>::delete_stats(const key_type& labels_value) {
}
}
}
#endif // end UNIT_TEST

template <typename T>
inline
Expand Down Expand Up @@ -152,6 +150,25 @@ void MultiDimension<T>::list_stats(std::vector<key_type>* names) {
}
}

template <typename T>
inline
T* MultiDimension<T>::get_stats_impl(const key_type& labels_value) {
if (!is_valid_lables_value(labels_value)) {
return nullptr;
}
MetricMapScopedPtr metric_map_ptr;
if (_metric_map.Read(&metric_map_ptr) != 0) {
LOG(ERROR) << "Fail to read dbd";
return nullptr;
}

auto it = metric_map_ptr->seek(labels_value);
if (it == nullptr) {
return nullptr;
}
return (*it);
}

template <typename T>
inline
T* MultiDimension<T>::get_stats_impl(const key_type& labels_value, STATS_OP stats_op, bool* do_write) {
Expand Down Expand Up @@ -204,6 +221,12 @@ T* MultiDimension<T>::get_stats_impl(const key_type& labels_value, STATS_OP stat
return cache_metric;
}

template <typename T>
inline
bool MultiDimension<T>::has_stats(const key_type& labels_value) {
return get_stats_impl(labels_value) != nullptr;
}

template <typename T>
inline
size_t MultiDimension<T>::dump(Dumper* dumper, const DumpOptions* options) {
Expand Down
26 changes: 26 additions & 0 deletions test/bvar_multi_dimension_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,53 +282,79 @@ TEST_F(MultiDimensionTest, stats) {
std::vector<std::list<std::string> > vec_labels;
std::vector<std::list<std::string> > vec_labels_no_sort;
bvar::MultiDimension<bvar::Adder<int> > my_madder("test_stats", labels);
ASSERT_EQ(0, my_madder.count_stats());
std::list<std::string> labels_value1 = {"tc", "get", "200"};
ASSERT_FALSE(my_madder.has_stats(labels_value1));
vec_labels.push_back(labels_value1);
vec_labels_no_sort.push_back(labels_value1);
bvar::Adder<int>* adder1 = my_madder.get_stats(labels_value1);
ASSERT_TRUE(adder1);
ASSERT_TRUE(my_madder.has_stats(labels_value1));
std::vector<std::list<std::string> > ret_labels;
my_madder.list_stats(&ret_labels);
ASSERT_EQ(vec_labels, ret_labels);
ASSERT_EQ(1, my_madder.count_stats());

std::list<std::string> labels_value2 = {"nj", "get", "200"};
ASSERT_FALSE(my_madder.has_stats(labels_value2));
bvar::Adder<int>* adder2 = my_madder.get_stats(labels_value2);
ASSERT_TRUE(adder2);
ASSERT_TRUE(my_madder.has_stats(labels_value2));
vec_labels.push_back(labels_value2);
vec_labels_no_sort.push_back(labels_value2);
my_madder.list_stats(&ret_labels);
sort(vec_labels.begin(), vec_labels.end());
sort(ret_labels.begin(), ret_labels.end());
ASSERT_EQ(vec_labels, ret_labels);
ASSERT_EQ(2, my_madder.count_stats());

std::list<std::string> labels_value3 = {"hz", "post", "500"};
ASSERT_FALSE(my_madder.has_stats(labels_value3));
bvar::Adder<int>* adder3 = my_madder.get_stats(labels_value3);
ASSERT_TRUE(adder3);
ASSERT_TRUE(my_madder.has_stats(labels_value3));
vec_labels.push_back(labels_value3);
vec_labels_no_sort.push_back(labels_value3);
my_madder.list_stats(&ret_labels);
sort(vec_labels.begin(), vec_labels.end());
sort(ret_labels.begin(), ret_labels.end());
ASSERT_EQ(vec_labels, ret_labels);
ASSERT_EQ(3, my_madder.count_stats());

std::list<std::string> labels_value4 = {"gz", "post", "500"};
ASSERT_FALSE(my_madder.has_stats(labels_value4));
bvar::Adder<int>* adder4 = my_madder.get_stats(labels_value4);
ASSERT_TRUE(adder4);
ASSERT_TRUE(my_madder.has_stats(labels_value4));
ASSERT_EQ(4, my_madder.count_stats());
vec_labels.push_back(labels_value4);
vec_labels_no_sort.push_back(labels_value4);
my_madder.list_stats(&ret_labels);
sort(vec_labels.begin(), vec_labels.end());
sort(ret_labels.begin(), ret_labels.end());
ASSERT_EQ(vec_labels, ret_labels);
ASSERT_EQ(4, my_madder.count_stats());

my_madder.delete_stats(labels_value4);
ASSERT_FALSE(my_madder.has_stats(labels_value4));
ASSERT_EQ(3, my_madder.count_stats());
vec_labels_no_sort.pop_back();
my_madder.list_stats(&ret_labels);
sort(vec_labels_no_sort.begin(), vec_labels_no_sort.end());
sort(ret_labels.begin(), ret_labels.end());
ASSERT_EQ(vec_labels_no_sort, ret_labels);

ASSERT_TRUE(my_madder.has_stats(labels_value1));
ASSERT_TRUE(my_madder.has_stats(labels_value2));
ASSERT_TRUE(my_madder.has_stats(labels_value3));
ASSERT_FALSE(my_madder.has_stats(labels_value4));

my_madder.delete_stats();
ASSERT_EQ(0, my_madder.count_stats());
ASSERT_FALSE(my_madder.has_stats(labels_value1));
ASSERT_FALSE(my_madder.has_stats(labels_value2));
ASSERT_FALSE(my_madder.has_stats(labels_value3));
ASSERT_FALSE(my_madder.has_stats(labels_value4));
}

TEST_F(MultiDimensionTest, get_description) {
Expand Down

0 comments on commit a39db73

Please sign in to comment.