be/src/storage/segment/condition_cache.h
Line | Count | Source |
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 | | #pragma once |
19 | | |
20 | | #include <butil/macros.h> |
21 | | #include <glog/logging.h> |
22 | | #include <stddef.h> |
23 | | #include <stdint.h> |
24 | | |
25 | | #include <atomic> |
26 | | #include <memory> |
27 | | #include <roaring/roaring.hh> |
28 | | #include <string> |
29 | | |
30 | | #include "common/config.h" |
31 | | #include "common/status.h" |
32 | | #include "io/fs/file_system.h" |
33 | | #include "io/fs/path.h" |
34 | | #include "runtime/exec_env.h" |
35 | | #include "runtime/memory/lru_cache_policy.h" |
36 | | #include "runtime/memory/mem_tracker.h" |
37 | | #include "util/lru_cache.h" |
38 | | #include "util/slice.h" |
39 | | #include "util/time.h" |
40 | | |
41 | | namespace doris::segment_v2 { |
42 | | |
43 | | class ConditionCacheHandle; |
44 | | |
45 | | class ConditionCache : public LRUCachePolicy { |
46 | | public: |
47 | | using LRUCachePolicy::insert; |
48 | | |
49 | | // The cache key or segment lru cache |
50 | | struct CacheKey { |
51 | | CacheKey(RowsetId rowset_id_, int64_t segment_id_, uint64_t digest_) |
52 | 2.27M | : rowset_id(rowset_id_), segment_id(segment_id_), digest(digest_) {} |
53 | | RowsetId rowset_id; |
54 | | int64_t segment_id; |
55 | | uint64_t digest; |
56 | | |
57 | | // Encode to a flat binary which can be used as LRUCache's key |
58 | 4.54M | [[nodiscard]] std::string encode() const { |
59 | 4.54M | char buf[16]; |
60 | 4.54M | memcpy(buf, &segment_id, 8); |
61 | 4.54M | memcpy(buf + 8, &digest, 8); |
62 | | |
63 | 4.54M | return rowset_id.to_string() + std::string(buf, 16); |
64 | 4.54M | } |
65 | | }; |
66 | | |
67 | | class CacheValue : public LRUCacheValueBase { |
68 | | public: |
69 | | std::shared_ptr<std::vector<bool>> filter_result; |
70 | | }; |
71 | | |
72 | | // Create global instance of this class |
73 | 7 | static ConditionCache* create_global_cache(size_t capacity, uint32_t num_shards = 16) { |
74 | 7 | auto* res = new ConditionCache(capacity, num_shards); |
75 | 7 | return res; |
76 | 7 | } |
77 | | |
78 | | // Return global instance. |
79 | | // Client should call create_global_cache before. |
80 | 2.27M | static ConditionCache* instance() { return ExecEnv::GetInstance()->get_condition_cache(); } |
81 | | |
82 | | ConditionCache() = delete; |
83 | | |
84 | | ConditionCache(size_t capacity, uint32_t num_shards) |
85 | 7 | : LRUCachePolicy(CachePolicy::CacheType::CONDITION_CACHE, capacity, LRUCacheType::SIZE, |
86 | 7 | config::inverted_index_cache_stale_sweep_time_sec, num_shards, |
87 | 7 | /*element_count_capacity*/ 0, /*enable_prune*/ true, |
88 | 7 | /*is_lru_k*/ true) {} |
89 | | |
90 | | bool lookup(const CacheKey& key, ConditionCacheHandle* handle); |
91 | | |
92 | | void insert(const CacheKey& key, std::shared_ptr<std::vector<bool>> filter_result); |
93 | | }; |
94 | | |
95 | | class ConditionCacheHandle { |
96 | | public: |
97 | 1.18M | ConditionCacheHandle() = default; |
98 | | |
99 | | ConditionCacheHandle(LRUCachePolicy* cache, Cache::Handle* handle) |
100 | 1.16M | : _cache(cache), _handle(handle) {} |
101 | | |
102 | 2.34M | ~ConditionCacheHandle() { |
103 | 2.34M | if (_handle != nullptr) { |
104 | 1.17M | _cache->release(_handle); |
105 | 1.17M | } |
106 | 2.34M | } |
107 | | |
108 | 0 | ConditionCacheHandle(ConditionCacheHandle&& other) noexcept { |
109 | 0 | // we can use std::exchange if we switch c++14 on |
110 | 0 | std::swap(_cache, other._cache); |
111 | 0 | std::swap(_handle, other._handle); |
112 | 0 | } |
113 | | |
114 | 69.2k | ConditionCacheHandle& operator=(ConditionCacheHandle&& other) noexcept { |
115 | 69.2k | std::swap(_cache, other._cache); |
116 | 69.2k | std::swap(_handle, other._handle); |
117 | 69.2k | return *this; |
118 | 69.2k | } |
119 | | |
120 | 0 | LRUCachePolicy* cache() const { return _cache; } |
121 | | |
122 | 69.0k | std::shared_ptr<std::vector<bool>> get_filter_result() const { |
123 | 69.0k | if (!_cache) { |
124 | 0 | return nullptr; |
125 | 0 | } |
126 | 69.0k | return ((ConditionCache::CacheValue*)_cache->value(_handle))->filter_result; |
127 | 69.0k | } |
128 | | |
129 | | private: |
130 | | LRUCachePolicy* _cache = nullptr; |
131 | | Cache::Handle* _handle = nullptr; |
132 | | |
133 | | // Don't allow copy and assign |
134 | | DISALLOW_COPY_AND_ASSIGN(ConditionCacheHandle); |
135 | | }; |
136 | | |
137 | | } // namespace doris::segment_v2 |