Skip to content

Commit

Permalink
[BugFix] fix BE graceful exit and cache limit overflow issue caused b…
Browse files Browse the repository at this point in the history
…y MetadataCache (backport StarRocks#50036) (StarRocks#50041)

Co-authored-by: Yixin Luo <18810541851@163.com>
  • Loading branch information
mergify[bot] and luohaha authored Aug 21, 2024
1 parent 1658760 commit e5fc13b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
8 changes: 8 additions & 0 deletions be/src/storage/rowset/metadata_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@

namespace starrocks {

MetadataCache* MetadataCache::_s_instance = nullptr;

void MetadataCache::create_cache(size_t capacity) {
if (_s_instance == nullptr) {
_s_instance = new MetadataCache(capacity);
}
}

MetadataCache::MetadataCache(size_t capacity) {
_cache.reset(new_lru_cache(capacity));
}
Expand Down
7 changes: 7 additions & 0 deletions be/src/storage/rowset/metadata_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class MetadataCache {

DISALLOW_COPY_AND_MOVE(MetadataCache);

// Create global instance of this class
static void create_cache(size_t capacity);

static MetadataCache* instance() { return _s_instance; }

// will be called after rowset load metadata.
void cache_rowset(Rowset* ptr);

Expand All @@ -48,6 +53,8 @@ class MetadataCache {
void _erase(const std::string& key);
static void _cache_value_deleter(const CacheKey& /*key*/, void* value);

static MetadataCache* _s_instance;

// LRU cache for metadata
std::unique_ptr<Cache> _cache;
};
Expand Down
7 changes: 2 additions & 5 deletions be/src/storage/rowset/rowset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ Rowset::~Rowset() {
if (_keys_type != PRIMARY_KEYS) {
// ONLY support non-pk table now.
// evict rowset before destroy, in case this rowset no close yet.
auto metadata_cache = StorageEngine::instance()->tablet_manager()->metadata_cache();
if (metadata_cache != nullptr) {
metadata_cache->evict_rowset(this);
}
MetadataCache::instance()->evict_rowset(this);
}
#endif
MEM_TRACKER_SAFE_RELEASE(GlobalEnv::GetInstance()->rowset_metadata_mem_tracker(), _mem_usage());
Expand Down Expand Up @@ -188,7 +185,7 @@ Status Rowset::do_load() {
if (config::metadata_cache_memory_limit_percent > 0 && _keys_type != PRIMARY_KEYS) {
// Add rowset to lru metadata cache for memory control.
// ONLY support non-pk table now.
StorageEngine::instance()->tablet_manager()->metadata_cache()->cache_rowset(this);
MetadataCache::instance()->cache_rowset(this);
}
#endif
return Status::OK();
Expand Down
7 changes: 7 additions & 0 deletions be/src/storage/storage_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ StorageEngine::StorageEngine(const EngineOptions& options)
#ifdef USE_STAROS
_local_pk_index_manager = std::make_unique<lake::LocalPkIndexManager>();
#endif
#ifndef BE_TEST
const int64_t process_limit = GlobalEnv::GetInstance()->process_mem_tracker()->limit();
const int64_t lru_cache_limit = process_limit * (int64_t)config::metadata_cache_memory_limit_percent / (int64_t)100;
MetadataCache::create_cache(lru_cache_limit);
REGISTER_GAUGE_STARROCKS_METRIC(metadata_cache_bytes_total,
[&]() { return MetadataCache::instance()->get_memory_usage(); });
#endif
}

StorageEngine::~StorageEngine() {
Expand Down
8 changes: 0 additions & 8 deletions be/src/storage/tablet_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
#include "storage/compaction_manager.h"
#include "storage/data_dir.h"
#include "storage/olap_common.h"
#include "storage/rowset/metadata_cache.h"
#include "storage/rowset/rowset_factory.h"
#include "storage/rowset/rowset_writer.h"
#include "storage/rowset/rowset_writer_context.h"
Expand Down Expand Up @@ -89,13 +88,6 @@ TabletManager::TabletManager(int64_t tablet_map_lock_shard_size)
_last_update_stat_ms(0) {
CHECK_GT(_tablets_shards.size(), 0) << "tablets shard count greater than 0";
CHECK_EQ(_tablets_shards.size() & _tablets_shards_mask, 0) << "tablets shard count must be power of two";
#ifndef BE_TEST
const int64_t process_limit = GlobalEnv::GetInstance()->process_mem_tracker()->limit();
const int32_t lru_cache_limit = process_limit * config::metadata_cache_memory_limit_percent / 100;
_metadata_cache = std::make_unique<MetadataCache>(lru_cache_limit);
REGISTER_GAUGE_STARROCKS_METRIC(metadata_cache_bytes_total,
[this]() { return _metadata_cache->get_memory_usage(); });
#endif
}

Status TabletManager::_add_tablet_unlocked(const TabletSharedPtr& new_tablet, bool update_meta, bool force) {
Expand Down
5 changes: 0 additions & 5 deletions be/src/storage/tablet_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ class TabletManager {

Status generate_pk_dump();

MetadataCache* metadata_cache() const { return _metadata_cache.get(); }

private:
using TabletMap = std::unordered_map<int64_t, TabletSharedPtr>;
using TabletSet = std::unordered_set<int64_t>;
Expand Down Expand Up @@ -278,9 +276,6 @@ class TabletManager {
static Status _remove_tablet_directories(const TabletSharedPtr& tablet);
static Status _move_tablet_directories_to_trash(const TabletSharedPtr& tablet);

// LRU cache for metadata
std::unique_ptr<MetadataCache> _metadata_cache;

std::vector<TabletsShard> _tablets_shards;
const int64_t _tablets_shards_mask;
LockTable _schema_change_lock_tbl;
Expand Down

0 comments on commit e5fc13b

Please sign in to comment.