From 138eedfcbb2a2827bde76f7618e7123b569da127 Mon Sep 17 00:00:00 2001 From: Smith Cruise Date: Fri, 19 Jul 2024 10:48:08 +0800 Subject: [PATCH] update Signed-off-by: Smith Cruise --- be/src/block_cache/CMakeLists.txt | 1 + be/src/block_cache/block_cache.cpp | 44 --------------- be/src/block_cache/block_cache.h | 22 -------- .../block_cache_hit_rate_counter.hpp | 55 +++++++++++++++++++ be/src/http/action/datacache_action.cpp | 1 + .../block_cache_hit_rate_counter_test.cpp | 44 +++++++++++++++ be/test/block_cache/block_cache_test.cpp | 14 ----- 7 files changed, 101 insertions(+), 80 deletions(-) create mode 100644 be/src/block_cache/block_cache_hit_rate_counter.hpp create mode 100644 be/test/block_cache/block_cache_hit_rate_counter_test.cpp diff --git a/be/src/block_cache/CMakeLists.txt b/be/src/block_cache/CMakeLists.txt index 7283355076a3a..6e8abe031bbbf 100644 --- a/be/src/block_cache/CMakeLists.txt +++ b/be/src/block_cache/CMakeLists.txt @@ -28,6 +28,7 @@ set(CACHE_FILES cache_options.cpp datacache_utils.cpp disk_space_monitor.cpp + block_cache_hit_rate_counter.hpp ) if (${WITH_CACHELIB} STREQUAL "ON") diff --git a/be/src/block_cache/block_cache.cpp b/be/src/block_cache/block_cache.cpp index aaa6f8ea30650..6fcf6c7f2284e 100644 --- a/be/src/block_cache/block_cache.cpp +++ b/be/src/block_cache/block_cache.cpp @@ -33,50 +33,6 @@ namespace starrocks { namespace fs = std::filesystem; -BlockCacheHitRateCounter* BlockCacheHitRateCounter::instance() { - static BlockCacheHitRateCounter counter; - return &counter; -} - -void BlockCacheHitRateCounter::update(uint64_t hit_bytes, uint64_t miss_bytes) { - _hit_bytes << hit_bytes; - _miss_bytes << miss_bytes; -} - -double BlockCacheHitRateCounter::hit_rate() const { - return hit_rate_calculate(_hit_bytes.get_value(), _miss_bytes.get_value()); -} - -double BlockCacheHitRateCounter::hit_rate_last_minute() const { - return hit_rate_calculate(_hit_bytes_last_minute.get_value(), _miss_bytes_last_minute.get_value()); -} - -ssize_t BlockCacheHitRateCounter::get_hit_bytes() const { - return _hit_bytes.get_value(); -} - -ssize_t BlockCacheHitRateCounter::get_miss_bytes() const { - return _miss_bytes.get_value(); -} - -ssize_t BlockCacheHitRateCounter::get_hit_bytes_last_minute() const { - return _hit_bytes_last_minute.get_value(); -} - -ssize_t BlockCacheHitRateCounter::get_miss_bytes_last_minute() const { - return _miss_bytes_last_minute.get_value(); -} - -double BlockCacheHitRateCounter::hit_rate_calculate(ssize_t hit_bytes, ssize_t miss_bytes) { - ssize_t total_bytes = hit_bytes + miss_bytes; - if (total_bytes > 0) { - double hit_rate = std::round(double(hit_bytes) / double(total_bytes) * 100.0) / 100.0; - return hit_rate; - } else { - return 0; - } -} - // The cachelib doesn't support a item (key+valueu+attribute) larger than 4 MB without chain. // So, we check and limit the block_size configured by users to avoid unexpected errors. // For starcache, in theory we doesn't have a hard limitation for block size, but a very large diff --git a/be/src/block_cache/block_cache.h b/be/src/block_cache/block_cache.h index 47b95bd1b841b..bfbd73ca48cbf 100644 --- a/be/src/block_cache/block_cache.h +++ b/be/src/block_cache/block_cache.h @@ -14,8 +14,6 @@ #pragma once -#include - #include #include "block_cache/disk_space_monitor.h" @@ -24,26 +22,6 @@ namespace starrocks { -class BlockCacheHitRateCounter { -public: - static BlockCacheHitRateCounter* instance(); - ~BlockCacheHitRateCounter() = default; - void update(uint64_t hit_bytes, uint64_t miss_bytes); - double hit_rate() const; - double hit_rate_last_minute() const; - ssize_t get_hit_bytes() const; - ssize_t get_miss_bytes() const; - ssize_t get_hit_bytes_last_minute() const; - ssize_t get_miss_bytes_last_minute() const; - -private: - static double hit_rate_calculate(ssize_t hit_bytes, ssize_t miss_bytes); - bvar::Adder _hit_bytes; - bvar::Adder _miss_bytes; - bvar::Window> _hit_bytes_last_minute{&_hit_bytes, 60}; - bvar::Window> _miss_bytes_last_minute{&_miss_bytes, 60}; -}; - class BlockCache { public: using CacheKey = std::string; diff --git a/be/src/block_cache/block_cache_hit_rate_counter.hpp b/be/src/block_cache/block_cache_hit_rate_counter.hpp new file mode 100644 index 0000000000000..d10ba7e93e8ab --- /dev/null +++ b/be/src/block_cache/block_cache_hit_rate_counter.hpp @@ -0,0 +1,55 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. + +#pragma once + +#include + +namespace starrocks { +class BlockCacheHitRateCounter { +public: + static BlockCacheHitRateCounter* instance() { + static BlockCacheHitRateCounter counter; + return &counter; + } + ~BlockCacheHitRateCounter() = default; + void update(uint64_t hit_bytes, uint64_t miss_bytes) { + _hit_bytes << hit_bytes; + _miss_bytes << miss_bytes; + } + double hit_rate() const { return hit_rate_calculate(_hit_bytes.get_value(), _miss_bytes.get_value()); } + double hit_rate_last_minute() const { + return hit_rate_calculate(_hit_bytes_last_minute.get_value(), _miss_bytes_last_minute.get_value()); + } + ssize_t get_hit_bytes() const { return _hit_bytes.get_value(); } + 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(); } + +private: + static double hit_rate_calculate(ssize_t hit_bytes, ssize_t miss_bytes) { + ssize_t total_bytes = hit_bytes + miss_bytes; + if (total_bytes > 0) { + double hit_rate = std::round(double(hit_bytes) / double(total_bytes) * 100.0) / 100.0; + return hit_rate; + } else { + return 0; + } + } + bvar::Adder _hit_bytes; + bvar::Adder _miss_bytes; + bvar::Window> _hit_bytes_last_minute{&_hit_bytes, 60}; + bvar::Window> _miss_bytes_last_minute{&_miss_bytes, 60}; +}; +} // namespace starrocks diff --git a/be/src/http/action/datacache_action.cpp b/be/src/http/action/datacache_action.cpp index a72c83412cab5..84661d7e62889 100644 --- a/be/src/http/action/datacache_action.cpp +++ b/be/src/http/action/datacache_action.cpp @@ -22,6 +22,7 @@ #include #include "block_cache/block_cache.h" +#include "block_cache/block_cache_hit_rate_counter.hpp" #include "http/http_channel.h" #include "http/http_headers.h" #include "http/http_request.h" 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 new file mode 100644 index 0000000000000..ff26df81d2508 --- /dev/null +++ b/be/test/block_cache/block_cache_hit_rate_counter_test.cpp @@ -0,0 +1,44 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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. + +#include "block_cache/block_cache_hit_rate_counter.hpp" + +#include + +namespace starrocks { + +class BlockCacheHitRateCounterTest : public ::testing::Test { +protected: + static void SetUpTestCase() {} + + static void TearDownTestCase() {} + + void SetUp() override {} + void TearDown() override {} +}; + +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()); + + 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()); +} +} // namespace starrocks \ No newline at end of file diff --git a/be/test/block_cache/block_cache_test.cpp b/be/test/block_cache/block_cache_test.cpp index ce736666b27fe..4aac1a455be41 100644 --- a/be/test/block_cache/block_cache_test.cpp +++ b/be/test/block_cache/block_cache_test.cpp @@ -138,20 +138,6 @@ TEST_F(BlockCacheTest, parse_cache_space_paths) { fs::remove_all(cache_dir).ok(); } -TEST_F(BlockCacheTest, 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()); - - 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()); -} - #ifdef WITH_STARCACHE TEST_F(BlockCacheTest, hybrid_cache) { const std::string cache_dir = "./block_disk_cache3";