diff --git a/be/src/block_cache/block_cache_hit_rate_counter.hpp b/be/src/block_cache/block_cache_hit_rate_counter.hpp index d10ba7e93e8ab..4df4bbf56b8db 100644 --- a/be/src/block_cache/block_cache_hit_rate_counter.hpp +++ b/be/src/block_cache/block_cache_hit_rate_counter.hpp @@ -36,6 +36,10 @@ class BlockCacheHitRateCounter { ssize_t get_miss_bytes() const { return _miss_bytes.get_value(); } ssize_t get_hit_bytes_last_minute() const { return _hit_bytes_last_minute.get_value(); } ssize_t get_miss_bytes_last_minute() const { return _miss_bytes_last_minute.get_value(); } + void reset() { + _hit_bytes.reset(); + _miss_bytes.reset(); + } private: static double hit_rate_calculate(ssize_t hit_bytes, ssize_t miss_bytes) { diff --git a/be/src/exec/hdfs_scanner.cpp b/be/src/exec/hdfs_scanner.cpp index 738cdd0960c27..c3c4fea24396b 100644 --- a/be/src/exec/hdfs_scanner.cpp +++ b/be/src/exec/hdfs_scanner.cpp @@ -14,6 +14,7 @@ #include "exec/hdfs_scanner.h" +#include "block_cache/block_cache_hit_rate_counter.hpp" #include "column/column_helper.h" #include "exec/exec_node.h" #include "fs/hdfs/fs_hdfs.h" diff --git a/be/test/CMakeLists.txt b/be/test/CMakeLists.txt index 3593d94b4c809..4b74bf05491ed 100644 --- a/be/test/CMakeLists.txt +++ b/be/test/CMakeLists.txt @@ -459,6 +459,7 @@ set(EXEC_FILES ./storage/lake/replication_txn_manager_test.cpp ./storage/lake/persistent_index_sstable_test.cpp ./block_cache/datacache_utils_test.cpp + ./block_cache/block_cache_hit_rate_counter_test.cpp ./util/thrift_rpc_helper_test.cpp ) diff --git a/be/test/block_cache/block_cache_hit_rate_counter_test.cpp b/be/test/block_cache/block_cache_hit_rate_counter_test.cpp index ff26df81d2508..dc26d68d548a5 100644 --- a/be/test/block_cache/block_cache_hit_rate_counter_test.cpp +++ b/be/test/block_cache/block_cache_hit_rate_counter_test.cpp @@ -29,16 +29,16 @@ class BlockCacheHitRateCounterTest : public ::testing::Test { }; TEST_F(BlockCacheHitRateCounterTest, app_hit_rate) { - BlockCacheHitRateCounter* counter = BlockCacheHitRateCounter::instance(); - EXPECT_EQ(0, counter->hit_rate()); - EXPECT_EQ(0, counter->get_hit_bytes_last_minute()); - EXPECT_EQ(0, counter->get_miss_bytes_last_minute()); - EXPECT_EQ(0, counter->hit_rate_last_minute()); + BlockCacheHitRateCounter counter{}; + EXPECT_EQ(0, counter.hit_rate()); + EXPECT_EQ(0, counter.get_hit_bytes_last_minute()); + EXPECT_EQ(0, counter.get_miss_bytes_last_minute()); + EXPECT_EQ(0, counter.hit_rate_last_minute()); - counter->update(3, 10); + counter.update(3, 10); - EXPECT_EQ(3, counter->get_hit_bytes()); - EXPECT_EQ(10, counter->get_miss_bytes()); - EXPECT_EQ(0.23, counter->hit_rate()); + EXPECT_EQ(3, counter.get_hit_bytes()); + EXPECT_EQ(10, counter.get_miss_bytes()); + EXPECT_EQ(0.23, counter.hit_rate()); } } // namespace starrocks \ No newline at end of file diff --git a/be/test/http/datacache_action_test.cpp b/be/test/http/datacache_action_test.cpp index 3261ca7a742d6..314a9aec9865e 100644 --- a/be/test/http/datacache_action_test.cpp +++ b/be/test/http/datacache_action_test.cpp @@ -20,7 +20,7 @@ #include #include "block_cache/block_cache.h" -#include "gen_cpp/FrontendService_types.h" +#include "block_cache/block_cache_hit_rate_counter.hpp" #include "gen_cpp/HeartbeatService_types.h" #include "http/http_channel.h" #include "http/http_request.h" @@ -101,23 +101,48 @@ TEST_F(DataCacheActionTest, stat_success) { } TEST_F(DataCacheActionTest, app_stat_success) { + BlockCacheHitRateCounter* counter = BlockCacheHitRateCounter::instance(); + counter->reset(); auto cache = BlockCache::instance(); ASSERT_TRUE(init_datacache_instance("starcache", cache).ok()); _env._block_cache = cache; DataCacheAction action(&_env); - HttpRequest request(_evhttp_req); - request._method = HttpMethod::GET; - request._params.emplace("action", "app_stat"); - request.set_handler(&action); - action.on_header(&request); - action.handle(&request); - - rapidjson::Document doc; - doc.Parse(k_response_str.c_str()); - std::cout << doc["hit_bytes"].GetInt64(); + { + HttpRequest request(_evhttp_req); + request._method = HttpMethod::GET; + request._params.emplace("action", "app_stat"); + request.set_handler(&action); + action.on_header(&request); + action.handle(&request); + + rapidjson::Document doc; + doc.Parse(k_response_str.c_str()); + EXPECT_EQ(0, doc["hit_bytes"].GetInt64()); + EXPECT_EQ(0, doc["miss_bytes"].GetInt64()); + EXPECT_EQ(0, doc["hit_rate"].GetDouble()); + EXPECT_EQ(0, doc["hit_bytes_last_minute"].GetInt64()); + EXPECT_EQ(0, doc["miss_bytes_last_minute"].GetInt64()); + EXPECT_EQ(0, doc["hit_rate_last_minute"].GetDouble()); + } + counter->update(3, 10); + + { + HttpRequest request(_evhttp_req); + request._method = HttpMethod::GET; + request._params.emplace("action", "app_stat"); + request.set_handler(&action); + action.on_header(&request); + action.handle(&request); + + rapidjson::Document doc; + doc.Parse(k_response_str.c_str()); + EXPECT_EQ(3, doc["hit_bytes"].GetInt64()); + EXPECT_EQ(10, doc["miss_bytes"].GetInt64()); + EXPECT_EQ(0.23, doc["hit_rate"].GetDouble()); + } _env._block_cache = nullptr; }