/root/doris/be/src/olap/page_cache.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "olap/page_cache.h" |
19 | | |
20 | | #include <gen_cpp/segment_v2.pb.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include <ostream> |
24 | | |
25 | | #include "runtime/exec_env.h" |
26 | | |
27 | | namespace doris { |
28 | | |
29 | | template <typename T> |
30 | | MemoryTrackedPageBase<T>::MemoryTrackedPageBase(size_t size, bool use_cache, |
31 | | segment_v2::PageTypePB page_type) |
32 | 45.5k | : _size(size) { |
33 | 45.5k | if (use_cache) { |
34 | 10.7k | _mem_tracker_by_allocator = StoragePageCache::instance()->mem_tracker(page_type); |
35 | 34.7k | } else { |
36 | 34.7k | _mem_tracker_by_allocator = thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker(); |
37 | 34.7k | } |
38 | 45.5k | } _ZN5doris21MemoryTrackedPageBaseIPcEC2EmbNS_10segment_v210PageTypePBE Line | Count | Source | 32 | 40.7k | : _size(size) { | 33 | 40.7k | if (use_cache) { | 34 | 6.00k | _mem_tracker_by_allocator = StoragePageCache::instance()->mem_tracker(page_type); | 35 | 34.7k | } else { | 36 | 34.7k | _mem_tracker_by_allocator = thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker(); | 37 | 34.7k | } | 38 | 40.7k | } |
_ZN5doris21MemoryTrackedPageBaseISt10shared_ptrINS_10segment_v215SegmentFooterPBEEEC2EmbNS2_10PageTypePBE Line | Count | Source | 32 | 4.78k | : _size(size) { | 33 | 4.78k | if (use_cache) { | 34 | 4.78k | _mem_tracker_by_allocator = StoragePageCache::instance()->mem_tracker(page_type); | 35 | 4.78k | } else { | 36 | 0 | _mem_tracker_by_allocator = thread_context()->thread_mem_tracker_mgr->limiter_mem_tracker(); | 37 | 0 | } | 38 | 4.78k | } |
|
39 | | |
40 | | MemoryTrackedPageWithPageEntity::MemoryTrackedPageWithPageEntity(size_t size, bool use_cache, |
41 | | segment_v2::PageTypePB page_type) |
42 | 40.7k | : MemoryTrackedPageBase<char*>(size, use_cache, page_type), _capacity(size) { |
43 | 40.7k | { |
44 | 40.7k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(this->_mem_tracker_by_allocator); |
45 | 40.7k | this->_data = reinterpret_cast<char*>( |
46 | 40.7k | Allocator<false>::alloc(this->_capacity, ALLOCATOR_ALIGNMENT_16)); |
47 | 40.7k | } |
48 | 40.7k | } |
49 | | |
50 | 36.0k | MemoryTrackedPageWithPageEntity::~MemoryTrackedPageWithPageEntity() { |
51 | 36.0k | if (this->_data != nullptr) { |
52 | 36.0k | DCHECK(this->_capacity != 0 && this->_size != 0); |
53 | 36.0k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(this->_mem_tracker_by_allocator); |
54 | 36.0k | Allocator<false>::free(this->_data, this->_capacity); |
55 | 36.0k | } |
56 | 36.0k | } |
57 | | |
58 | | template <typename T> |
59 | | MemoryTrackedPageWithPagePtr<T>::MemoryTrackedPageWithPagePtr(size_t size, |
60 | | segment_v2::PageTypePB page_type) |
61 | 4.78k | : MemoryTrackedPageBase<std::shared_ptr<T>>(size, true, page_type) { |
62 | 4.78k | DCHECK(this->_size > 0); |
63 | 4.78k | this->_size = size; |
64 | 4.78k | this->_mem_tracker_by_allocator->consume(this->_size); |
65 | 4.78k | } |
66 | | |
67 | | template <typename T> |
68 | 2 | MemoryTrackedPageWithPagePtr<T>::~MemoryTrackedPageWithPagePtr() { |
69 | 2 | DCHECK(this->_size > 0); |
70 | 2 | this->_mem_tracker_by_allocator->release(this->_size); |
71 | 2 | } |
72 | | |
73 | | StoragePageCache* StoragePageCache::create_global_cache(size_t capacity, |
74 | | int32_t index_cache_percentage, |
75 | | int64_t pk_index_cache_capacity, |
76 | 1 | uint32_t num_shards) { |
77 | 1 | return new StoragePageCache(capacity, index_cache_percentage, pk_index_cache_capacity, |
78 | 1 | num_shards); |
79 | 1 | } |
80 | | |
81 | | StoragePageCache::StoragePageCache(size_t capacity, int32_t index_cache_percentage, |
82 | | int64_t pk_index_cache_capacity, uint32_t num_shards) |
83 | 5 | : _index_cache_percentage(index_cache_percentage) { |
84 | 5 | if (index_cache_percentage == 0) { |
85 | 2 | _data_page_cache = std::make_unique<DataPageCache>(capacity, num_shards); |
86 | 3 | } else if (index_cache_percentage == 100) { |
87 | 1 | _index_page_cache = std::make_unique<IndexPageCache>(capacity, num_shards); |
88 | 2 | } else if (index_cache_percentage > 0 && index_cache_percentage < 100) { |
89 | 2 | _data_page_cache = std::make_unique<DataPageCache>( |
90 | 2 | capacity * (100 - index_cache_percentage) / 100, num_shards); |
91 | 2 | _index_page_cache = std::make_unique<IndexPageCache>( |
92 | 2 | capacity * index_cache_percentage / 100, num_shards); |
93 | 2 | } else { |
94 | 0 | CHECK(false) << "invalid index page cache percentage"; |
95 | 0 | } |
96 | | |
97 | 5 | _pk_index_page_cache = std::make_unique<PKIndexPageCache>(pk_index_cache_capacity, num_shards); |
98 | 5 | } |
99 | | |
100 | | bool StoragePageCache::lookup(const CacheKey& key, PageCacheHandle* handle, |
101 | 12.5k | segment_v2::PageTypePB page_type) { |
102 | 12.5k | auto* cache = _get_page_cache(page_type); |
103 | 12.5k | auto* lru_handle = cache->lookup(key.encode()); |
104 | 12.5k | if (lru_handle == nullptr) { |
105 | 9.87k | return false; |
106 | 9.87k | } |
107 | 2.62k | *handle = PageCacheHandle(cache, lru_handle); |
108 | 2.62k | return true; |
109 | 12.5k | } |
110 | | |
111 | | void StoragePageCache::insert(const CacheKey& key, DataPage* data, PageCacheHandle* handle, |
112 | 5.88k | segment_v2::PageTypePB page_type, bool in_memory) { |
113 | 5.88k | CachePriority priority = CachePriority::NORMAL; |
114 | 5.88k | if (in_memory) { |
115 | 8 | priority = CachePriority::DURABLE; |
116 | 8 | } |
117 | | |
118 | 5.88k | auto* cache = _get_page_cache(page_type); |
119 | 5.88k | auto* lru_handle = cache->insert(key.encode(), data, data->capacity(), 0, priority); |
120 | 5.88k | DCHECK(lru_handle != nullptr); |
121 | 5.88k | *handle = PageCacheHandle(cache, lru_handle); |
122 | 5.88k | } |
123 | | |
124 | | template <typename T> |
125 | | void StoragePageCache::insert(const CacheKey& key, T data, size_t size, PageCacheHandle* handle, |
126 | 4.78k | segment_v2::PageTypePB page_type, bool in_memory) { |
127 | 4.78k | static_assert(std::is_same<typename std::remove_cv<T>::type, |
128 | 4.78k | std::shared_ptr<typename T::element_type>>::value, |
129 | 4.78k | "Second argument must be a std::shared_ptr"); |
130 | 4.78k | using ValueType = typename T::element_type; // Type that shared_ptr points to |
131 | | |
132 | 4.78k | CachePriority priority = CachePriority::NORMAL; |
133 | 4.78k | if (in_memory) { |
134 | 0 | priority = CachePriority::DURABLE; |
135 | 0 | } |
136 | | |
137 | 4.78k | auto* cache = _get_page_cache(page_type); |
138 | | // Lify cycle of page will be managed by StoragePageCache |
139 | 4.78k | auto page = std::make_unique<MemoryTrackedPageWithPagePtr<ValueType>>(size, page_type); |
140 | | // Lify cycle of data will be managed by StoragePageCache and user at the same time. |
141 | 4.78k | page->set_data(data); |
142 | | |
143 | 4.78k | auto* lru_handle = cache->insert(key.encode(), page.get(), size, 0, priority); |
144 | 4.78k | DCHECK(lru_handle != nullptr); |
145 | 4.78k | *handle = PageCacheHandle(cache, lru_handle); |
146 | | // Now page is managed by StoragePageCache. |
147 | 4.78k | page.release(); |
148 | 4.78k | } |
149 | | |
150 | 753 | Slice PageCacheHandle::data() const { |
151 | 753 | auto* cache_value = (DataPage*)_cache->value(_handle); |
152 | 753 | return {cache_value->data(), cache_value->size()}; |
153 | 753 | } |
154 | | |
155 | | template void StoragePageCache::insert(const CacheKey& key, |
156 | | std::shared_ptr<segment_v2::SegmentFooterPB> data, |
157 | | size_t size, PageCacheHandle* handle, |
158 | | segment_v2::PageTypePB page_type, bool in_memory); |
159 | | |
160 | | template class MemoryTrackedPageWithPagePtr<segment_v2::SegmentFooterPB>; |
161 | | |
162 | | } // namespace doris |