/root/doris/be/src/olap/schema_cache.h
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 <fmt/core.h> |
19 | | #include <parallel_hashmap/phmap.h> |
20 | | |
21 | | #include <algorithm> |
22 | | #include <memory> |
23 | | #include <mutex> |
24 | | #include <type_traits> |
25 | | #include <vector> |
26 | | |
27 | | #include "common/logging.h" |
28 | | #include "olap/iterators.h" |
29 | | #include "olap/olap_common.h" |
30 | | #include "olap/schema.h" |
31 | | #include "olap/tablet.h" |
32 | | #include "olap/tablet_schema.h" |
33 | | #include "util/time.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | namespace segment_v2 { |
38 | | class Segment; |
39 | | class SegmentIterator; |
40 | | using SegmentIteratorUPtr = std::unique_ptr<SegmentIterator>; |
41 | | } // namespace segment_v2 |
42 | | |
43 | | // The SchemaCache is utilized to cache pre-allocated data structures, |
44 | | // eliminating the need for frequent allocation and deallocation during usage. |
45 | | // This caching mechanism proves immensely advantageous, particularly in scenarios |
46 | | // with high concurrency, where queries are executed simultaneously. |
47 | | class SchemaCache : public LRUCachePolicy { |
48 | | public: |
49 | 0 | static SchemaCache* instance() { return _s_instance; } |
50 | | |
51 | | static void create_global_instance(size_t capacity); |
52 | | |
53 | | static std::string get_schema_key(int64_t tablet_id, const std::vector<TColumn>& columns, |
54 | | int32_t version); |
55 | | |
56 | | // Get a shared cached schema from cache, schema_key is a subset of column unique ids |
57 | 0 | TabletSchemaSPtr get_schema(const std::string& schema_key) { |
58 | 0 | if (!_s_instance || schema_key.empty()) { |
59 | 0 | return {}; |
60 | 0 | } |
61 | 0 | auto lru_handle = _cache->lookup(schema_key); |
62 | 0 | if (lru_handle) { |
63 | 0 | Defer release([cache = _cache.get(), lru_handle] { cache->release(lru_handle); }); |
64 | 0 | auto value = (CacheValue*)_cache->value(lru_handle); |
65 | 0 | value->last_visit_time = UnixMillis(); |
66 | 0 | VLOG_DEBUG << "use cache schema"; |
67 | 0 | return value->tablet_schema; |
68 | 0 | } |
69 | 0 | return {}; |
70 | 0 | } |
71 | | |
72 | | // Insert a shared Schema into cache, schema_key is full column unique ids |
73 | 0 | void insert_schema(const std::string& key, TabletSchemaSPtr schema) { |
74 | 0 | if (!_s_instance || key.empty()) { |
75 | 0 | return; |
76 | 0 | } |
77 | 0 | CacheValue* value = new CacheValue; |
78 | 0 | value->last_visit_time = UnixMillis(); |
79 | 0 | value->tablet_schema = schema; |
80 | 0 | auto deleter = [](const doris::CacheKey& key, void* value) { |
81 | 0 | CacheValue* cache_value = (CacheValue*)value; |
82 | 0 | delete cache_value; |
83 | 0 | }; |
84 | 0 | auto lru_handle = _cache->insert(key, value, sizeof(CacheValue), deleter, |
85 | 0 | CachePriority::NORMAL, schema->mem_size()); |
86 | 0 | _cache->release(lru_handle); |
87 | 0 | } |
88 | | |
89 | | // Try to prune the cache if expired. |
90 | | Status prune(); |
91 | | |
92 | | struct CacheValue : public LRUCacheValueBase { |
93 | | TabletSchemaSPtr tablet_schema = nullptr; |
94 | | }; |
95 | | |
96 | | private: |
97 | | SchemaCache(size_t capacity) |
98 | | : LRUCachePolicy("SchemaCache", capacity, LRUCacheType::NUMBER, |
99 | 0 | config::schema_cache_sweep_time_sec) {} |
100 | | static constexpr char SCHEMA_DELIMITER = '-'; |
101 | | static SchemaCache* _s_instance; |
102 | | }; |
103 | | |
104 | | } // namespace doris |