Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Smith Cruise <[email protected]>
  • Loading branch information
Smith-Cruise committed Jul 19, 2024
1 parent 9fa768f commit 138eedf
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 80 deletions.
1 change: 1 addition & 0 deletions be/src/block_cache/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
44 changes: 0 additions & 44 deletions be/src/block_cache/block_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions be/src/block_cache/block_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#pragma once

#include <bvar/bvar.h>

#include <atomic>

#include "block_cache/disk_space_monitor.h"
Expand All @@ -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<ssize_t> _hit_bytes;
bvar::Adder<ssize_t> _miss_bytes;
bvar::Window<bvar::Adder<ssize_t>> _hit_bytes_last_minute{&_hit_bytes, 60};
bvar::Window<bvar::Adder<ssize_t>> _miss_bytes_last_minute{&_miss_bytes, 60};
};

class BlockCache {
public:
using CacheKey = std::string;
Expand Down
55 changes: 55 additions & 0 deletions be/src/block_cache/block_cache_hit_rate_counter.hpp
Original file line number Diff line number Diff line change
@@ -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 <bvar/bvar.h>

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<ssize_t> _hit_bytes;
bvar::Adder<ssize_t> _miss_bytes;
bvar::Window<bvar::Adder<ssize_t>> _hit_bytes_last_minute{&_hit_bytes, 60};
bvar::Window<bvar::Adder<ssize_t>> _miss_bytes_last_minute{&_miss_bytes, 60};
};
} // namespace starrocks
1 change: 1 addition & 0 deletions be/src/http/action/datacache_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <string>

#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"
Expand Down
44 changes: 44 additions & 0 deletions be/test/block_cache/block_cache_hit_rate_counter_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

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
14 changes: 0 additions & 14 deletions be/test/block_cache/block_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 138eedf

Please sign in to comment.