Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Jun 14, 2024
1 parent 56ac976 commit 1bb4f14
Show file tree
Hide file tree
Showing 25 changed files with 230 additions and 189 deletions.
6 changes: 2 additions & 4 deletions be/src/cloud/cloud_tablet_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class CloudTabletMgr::TabletMap {
CloudTabletMgr::CloudTabletMgr(CloudStorageEngine& engine)
: _engine(engine),
_tablet_map(std::make_unique<TabletMap>()),
_cache(std::make_unique<LRUCachePolicy>(
_cache(std::make_unique<LRUCachePolicyTrackingManual>(
CachePolicy::CacheType::CLOUD_TABLET_CACHE, config::tablet_cache_capacity,
LRUCacheType::NUMBER, 0, config::tablet_cache_shards)) {}

Expand All @@ -148,9 +148,7 @@ Result<std::shared_ptr<CloudTablet>> CloudTabletMgr::get_tablet(int64_t tablet_i
class Value : public LRUCacheValueBase {
public:
Value(const std::shared_ptr<CloudTablet>& tablet, TabletMap& tablet_map)
: LRUCacheValueBase(CachePolicy::CacheType::CLOUD_TABLET_CACHE),
tablet(tablet),
tablet_map(tablet_map) {}
: tablet(tablet), tablet_map(tablet_map) {}
~Value() override { tablet_map.erase(tablet.get()); }

// FIXME(plat1ko): The ownership of tablet seems to belong to 'TabletMap', while `Value`
Expand Down
4 changes: 2 additions & 2 deletions be/src/cloud/cloud_txn_delete_bitmap_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
namespace doris {

CloudTxnDeleteBitmapCache::CloudTxnDeleteBitmapCache(size_t size_in_bytes)
: LRUCachePolicy(CachePolicy::CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE, size_in_bytes,
LRUCacheType::SIZE, 86400, 4),
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE,
size_in_bytes, LRUCacheType::SIZE, 86400, 4),
_stop_latch(1) {}

CloudTxnDeleteBitmapCache::~CloudTxnDeleteBitmapCache() {
Expand Down
6 changes: 2 additions & 4 deletions be/src/cloud/cloud_txn_delete_bitmap_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace doris {

// Record transaction related delete bitmaps using a lru cache.
class CloudTxnDeleteBitmapCache : public LRUCachePolicy {
class CloudTxnDeleteBitmapCache : public LRUCachePolicyTrackingManual {
public:
CloudTxnDeleteBitmapCache(size_t size_in_bytes);

Expand Down Expand Up @@ -63,9 +63,7 @@ class CloudTxnDeleteBitmapCache : public LRUCachePolicy {
RowsetIdUnorderedSet rowset_ids;

DeleteBitmapCacheValue(DeleteBitmapPtr delete_bitmap_, const RowsetIdUnorderedSet& ids_)
: LRUCacheValueBase(CachePolicy::CacheType::CLOUD_TXN_DELETE_BITMAP_CACHE),
delete_bitmap(std::move(delete_bitmap_)),
rowset_ids(ids_) {}
: delete_bitmap(std::move(delete_bitmap_)), rowset_ids(ids_) {}
};

struct TxnKey {
Expand Down
2 changes: 1 addition & 1 deletion be/src/olap/page_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void StoragePageCache::insert(const CacheKey& key, DataPage* data, PageCacheHand
}

auto* cache = _get_page_cache(page_type);
auto* lru_handle = cache->insert_no_tracking(key.encode(), data, data->capacity(), priority);
auto* lru_handle = cache->insert(key.encode(), data, data->capacity(), 0, priority);
*handle = PageCacheHandle(cache, lru_handle);
}

Expand Down
34 changes: 14 additions & 20 deletions be/src/olap/page_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,28 @@ class StoragePageCache {
}
};

class DataPageCache : public LRUCachePolicy {
class DataPageCache : public LRUCachePolicyTrackingAllocator {
public:
DataPageCache(size_t capacity, uint32_t num_shards)
: LRUCachePolicy(CachePolicy::CacheType::DATA_PAGE_CACHE, capacity,
LRUCacheType::SIZE, config::data_page_cache_stale_sweep_time_sec,
num_shards) {
init_mem_tracker_by_allocator(lru_cache_type_string(LRUCacheType::SIZE));
}
: LRUCachePolicyTrackingAllocator(
CachePolicy::CacheType::DATA_PAGE_CACHE, capacity, LRUCacheType::SIZE,
config::data_page_cache_stale_sweep_time_sec, num_shards) {}
};

class IndexPageCache : public LRUCachePolicy {
class IndexPageCache : public LRUCachePolicyTrackingAllocator {
public:
IndexPageCache(size_t capacity, uint32_t num_shards)
: LRUCachePolicy(CachePolicy::CacheType::INDEXPAGE_CACHE, capacity,
LRUCacheType::SIZE, config::index_page_cache_stale_sweep_time_sec,
num_shards) {
init_mem_tracker_by_allocator(lru_cache_type_string(LRUCacheType::SIZE));
}
: LRUCachePolicyTrackingAllocator(
CachePolicy::CacheType::INDEXPAGE_CACHE, capacity, LRUCacheType::SIZE,
config::index_page_cache_stale_sweep_time_sec, num_shards) {}
};

class PKIndexPageCache : public LRUCachePolicy {
class PKIndexPageCache : public LRUCachePolicyTrackingAllocator {
public:
PKIndexPageCache(size_t capacity, uint32_t num_shards)
: LRUCachePolicy(CachePolicy::CacheType::PK_INDEX_PAGE_CACHE, capacity,
LRUCacheType::SIZE,
config::pk_index_page_cache_stale_sweep_time_sec, num_shards) {
init_mem_tracker_by_allocator(lru_cache_type_string(LRUCacheType::SIZE));
}
: LRUCachePolicyTrackingAllocator(
CachePolicy::CacheType::PK_INDEX_PAGE_CACHE, capacity, LRUCacheType::SIZE,
config::pk_index_page_cache_stale_sweep_time_sec, num_shards) {}
};

static constexpr uint32_t kDefaultNumShards = 16;
Expand Down Expand Up @@ -169,7 +163,7 @@ class StoragePageCache {
segment_v2::PageTypePB page_type, bool in_memory = false);

std::shared_ptr<MemTrackerLimiter> mem_tracker(segment_v2::PageTypePB page_type) {
return _get_page_cache(page_type)->mem_tracker_by_allocator();
return _get_page_cache(page_type)->mem_tracker();
}

private:
Expand All @@ -183,7 +177,7 @@ class StoragePageCache {
// delete bitmap in unique key with mow
std::unique_ptr<PKIndexPageCache> _pk_index_page_cache;

LRUCachePolicy* _get_page_cache(segment_v2::PageTypePB page_type) {
LRUCachePolicyTrackingAllocator* _get_page_cache(segment_v2::PageTypePB page_type) {
switch (page_type) {
case segment_v2::DATA_PAGE: {
return _data_page_cache.get();
Expand Down
10 changes: 3 additions & 7 deletions be/src/olap/rowset/segment_v2/inverted_index_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,10 @@ void InvertedIndexQueryCache::insert(const CacheKey& key, std::shared_ptr<roarin
return;
}

auto* lru_handle = LRUCachePolicy::insert(key.encode(), (void*)cache_value_ptr.release(),
bitmap->getSizeInBytes(), bitmap->getSizeInBytes(),
CachePriority::NORMAL);
auto* lru_handle = LRUCachePolicyTrackingManual::insert(
key.encode(), (void*)cache_value_ptr.release(), bitmap->getSizeInBytes(),
bitmap->getSizeInBytes(), CachePriority::NORMAL);
*handle = InvertedIndexQueryCacheHandle(this, lru_handle);
}

int64_t InvertedIndexQueryCache::mem_consumption() {
return LRUCachePolicy::mem_consumption();
}

} // namespace doris::segment_v2
42 changes: 20 additions & 22 deletions be/src/olap/rowset/segment_v2/inverted_index_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ class InvertedIndexSearcherCache {
size_t size = 0;
int64_t last_visit_time;

CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE) {}
CacheValue() = default;
explicit CacheValue(IndexSearcherPtr searcher, size_t mem_size, int64_t visit_time)
: LRUCacheValueBase(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE),
index_searcher(std::move(searcher)) {
: index_searcher(std::move(searcher)) {
size = mem_size;
last_visit_time = visit_time;
}
Expand Down Expand Up @@ -100,23 +99,23 @@ class InvertedIndexSearcherCache {
private:
InvertedIndexSearcherCache() = default;

class InvertedIndexSearcherCachePolicy : public LRUCachePolicy {
class InvertedIndexSearcherCachePolicy : public LRUCachePolicyTrackingManual {
public:
InvertedIndexSearcherCachePolicy(size_t capacity, uint32_t num_shards,
uint32_t element_count_capacity)
: LRUCachePolicy(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE, capacity,
LRUCacheType::SIZE,
config::inverted_index_cache_stale_sweep_time_sec, num_shards,
element_count_capacity, true) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE,
capacity, LRUCacheType::SIZE,
config::inverted_index_cache_stale_sweep_time_sec,
num_shards, element_count_capacity, true) {}
InvertedIndexSearcherCachePolicy(size_t capacity, uint32_t num_shards,
uint32_t element_count_capacity,
CacheValueTimeExtractor cache_value_time_extractor,
bool cache_value_check_timestamp)
: LRUCachePolicy(CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE, capacity,
LRUCacheType::SIZE,
config::inverted_index_cache_stale_sweep_time_sec, num_shards,
element_count_capacity, cache_value_time_extractor,
cache_value_check_timestamp, true) {}
: LRUCachePolicyTrackingManual(
CachePolicy::CacheType::INVERTEDINDEX_SEARCHER_CACHE, capacity,
LRUCacheType::SIZE, config::inverted_index_cache_stale_sweep_time_sec,
num_shards, element_count_capacity, cache_value_time_extractor,
cache_value_check_timestamp, true) {}
};
// Insert a cache entry by key.
// And the cache entry will be returned in handle.
Expand Down Expand Up @@ -180,8 +179,10 @@ class InvertedIndexCacheHandle {

class InvertedIndexQueryCacheHandle;

class InvertedIndexQueryCache : public LRUCachePolicy {
class InvertedIndexQueryCache : public LRUCachePolicyTrackingManual {
public:
using LRUCachePolicyTrackingManual::insert;

// cache key
struct CacheKey {
io::Path index_path; // index file path
Expand All @@ -208,14 +209,12 @@ class InvertedIndexQueryCache : public LRUCachePolicy {

class CacheValue : public LRUCacheValueBase {
public:
CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::INVERTEDINDEX_QUERY_CACHE) {}

std::shared_ptr<roaring::Roaring> bitmap;
};

// Create global instance of this class
static InvertedIndexQueryCache* create_global_cache(size_t capacity, uint32_t num_shards = 16) {
InvertedIndexQueryCache* res = new InvertedIndexQueryCache(capacity, num_shards);
auto* res = new InvertedIndexQueryCache(capacity, num_shards);
return res;
}

Expand All @@ -228,16 +227,15 @@ class InvertedIndexQueryCache : public LRUCachePolicy {
InvertedIndexQueryCache() = delete;

InvertedIndexQueryCache(size_t capacity, uint32_t num_shards)
: LRUCachePolicy(CachePolicy::CacheType::INVERTEDINDEX_QUERY_CACHE, capacity,
LRUCacheType::SIZE, config::inverted_index_cache_stale_sweep_time_sec,
num_shards) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::INVERTEDINDEX_QUERY_CACHE,
capacity, LRUCacheType::SIZE,
config::inverted_index_cache_stale_sweep_time_sec,
num_shards) {}

bool lookup(const CacheKey& key, InvertedIndexQueryCacheHandle* handle);

void insert(const CacheKey& key, std::shared_ptr<roaring::Roaring> bitmap,
InvertedIndexQueryCacheHandle* handle);

int64_t mem_consumption();
};

class InvertedIndexQueryCacheHandle {
Expand Down
9 changes: 4 additions & 5 deletions be/src/olap/schema_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using SegmentIteratorUPtr = std::unique_ptr<SegmentIterator>;
// eliminating the need for frequent allocation and deallocation during usage.
// This caching mechanism proves immensely advantageous, particularly in scenarios
// with high concurrency, where queries are executed simultaneously.
class SchemaCache : public LRUCachePolicy {
class SchemaCache : public LRUCachePolicyTrackingManual {
public:
enum class Type { TABLET_SCHEMA = 0, SCHEMA = 1 };

Expand Down Expand Up @@ -104,17 +104,16 @@ class SchemaCache : public LRUCachePolicy {

class CacheValue : public LRUCacheValueBase {
public:
CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::SCHEMA_CACHE) {}

Type type;
// either tablet_schema or schema
TabletSchemaSPtr tablet_schema = nullptr;
SchemaSPtr schema = nullptr;
};

SchemaCache(size_t capacity)
: LRUCachePolicy(CachePolicy::CacheType::SCHEMA_CACHE, capacity, LRUCacheType::NUMBER,
config::schema_cache_sweep_time_sec) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::SCHEMA_CACHE, capacity,
LRUCacheType::NUMBER,
config::schema_cache_sweep_time_sec) {}

private:
static constexpr char SCHEMA_DELIMITER = '-';
Expand Down
6 changes: 3 additions & 3 deletions be/src/olap/segment_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle*

void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
SegmentCacheHandle* handle) {
auto* lru_handle =
LRUCachePolicy::insert(key.encode(), &value, value.segment->meta_mem_usage(),
value.segment->meta_mem_usage(), CachePriority::NORMAL);
auto* lru_handle = LRUCachePolicyTrackingManual::insert(
key.encode(), &value, value.segment->meta_mem_usage(), value.segment->meta_mem_usage(),
CachePriority::NORMAL);
handle->push_segment(this, lru_handle);
}

Expand Down
9 changes: 5 additions & 4 deletions be/src/olap/segment_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class BetaRowset;
// Make sure that cache_handle is valid during the segment usage period.
using BetaRowsetSharedPtr = std::shared_ptr<BetaRowset>;

class SegmentCache : public LRUCachePolicy {
class SegmentCache : public LRUCachePolicyTrackingManual {
public:
using LRUCachePolicyTrackingManual::insert;
// The cache key or segment lru cache
struct CacheKey {
CacheKey(RowsetId rowset_id_, int64_t segment_id_)
Expand All @@ -74,15 +75,15 @@ class SegmentCache : public LRUCachePolicy {
// Holding all opened segments of a rowset.
class CacheValue : public LRUCacheValueBase {
public:
CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::SEGMENT_CACHE) {}
~CacheValue() override { segment.reset(); }

segment_v2::SegmentSharedPtr segment;
};

SegmentCache(size_t capacity)
: LRUCachePolicy(CachePolicy::CacheType::SEGMENT_CACHE, capacity, LRUCacheType::SIZE,
config::tablet_rowset_stale_sweep_time_sec) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::SEGMENT_CACHE, capacity,
LRUCacheType::SIZE,
config::tablet_rowset_stale_sweep_time_sec) {}

// Lookup the given segment in the cache.
// If the segment is found, the cache entry will be written into handle.
Expand Down
10 changes: 4 additions & 6 deletions be/src/olap/storage_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class StorageEngine final : public BaseStorageEngine {
// lru cache for create tabelt round robin in disks
// key: partitionId_medium
// value: index
class CreateTabletIdxCache : public LRUCachePolicy {
class CreateTabletIdxCache : public LRUCachePolicyTrackingManual {
public:
// get key, delimiter with DELIMITER '-'
static std::string get_key(int64_t partition_id, TStorageMedium::type medium) {
Expand All @@ -520,15 +520,13 @@ class CreateTabletIdxCache : public LRUCachePolicy {

class CacheValue : public LRUCacheValueBase {
public:
CacheValue() : LRUCacheValueBase(CachePolicy::CacheType::CREATE_TABLET_RR_IDX_CACHE) {}

int idx = 0;
};

CreateTabletIdxCache(size_t capacity)
: LRUCachePolicy(CachePolicy::CacheType::CREATE_TABLET_RR_IDX_CACHE, capacity,
LRUCacheType::NUMBER,
/*stale_sweep_time_s*/ 30 * 60) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::CREATE_TABLET_RR_IDX_CACHE,
capacity, LRUCacheType::NUMBER,
/*stale_sweep_time_s*/ 30 * 60) {}
};

struct DirInfo {
Expand Down
11 changes: 5 additions & 6 deletions be/src/olap/tablet_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,20 +516,19 @@ class DeleteBitmap {
*/
std::shared_ptr<roaring::Roaring> get_agg(const BitmapKey& bmk) const;

class AggCachePolicy : public LRUCachePolicy {
class AggCachePolicy : public LRUCachePolicyTrackingManual {
public:
AggCachePolicy(size_t capacity)
: LRUCachePolicy(CachePolicy::CacheType::DELETE_BITMAP_AGG_CACHE, capacity,
LRUCacheType::SIZE,
config::delete_bitmap_agg_cache_stale_sweep_time_sec, 256) {}
: LRUCachePolicyTrackingManual(CachePolicy::CacheType::DELETE_BITMAP_AGG_CACHE,
capacity, LRUCacheType::SIZE,
config::delete_bitmap_agg_cache_stale_sweep_time_sec,
256) {}
};

class AggCache {
public:
class Value : public LRUCacheValueBase {
public:
Value() : LRUCacheValueBase(CachePolicy::CacheType::DELETE_BITMAP_AGG_CACHE) {}

roaring::Roaring bitmap;
};

Expand Down
4 changes: 2 additions & 2 deletions be/src/olap/tablet_schema_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std:
pb.ParseFromString(key);
tablet_schema_ptr->init_from_pb(pb);
value->tablet_schema = tablet_schema_ptr;
lru_handle = LRUCachePolicy::insert(key, value, tablet_schema_ptr->num_columns(), 0,
CachePriority::NORMAL);
lru_handle = LRUCachePolicyTrackingManual::insert(
key, value, tablet_schema_ptr->num_columns(), 0, CachePriority::NORMAL);
g_tablet_schema_cache_count << 1;
g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns();
}
Expand Down
Loading

0 comments on commit 1bb4f14

Please sign in to comment.