be/src/storage/tablet/tablet_schema_cache.cpp
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 | | #include "storage/tablet/tablet_schema_cache.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <glog/logging.h> |
22 | | #include <json2pb/pb_to_json.h> |
23 | | |
24 | | #include <cstdint> |
25 | | #include <type_traits> |
26 | | #include <utility> |
27 | | |
28 | | #include "bvar/bvar.h" |
29 | | #include "storage/tablet/tablet_schema.h" |
30 | | #include "storage/tablet_info.h" |
31 | | #include "util/sha.h" |
32 | | |
33 | | bvar::Adder<int64_t> g_tablet_schema_cache_count("tablet_schema_cache_count"); |
34 | | bvar::Adder<int64_t> g_tablet_schema_cache_columns_count("tablet_schema_cache_columns_count"); |
35 | | bvar::Adder<int64_t> g_tablet_schema_cache_hit_count("tablet_schema_cache_hit_count"); |
36 | | |
37 | | namespace doris { |
38 | | |
39 | | // to reduce the memory consumption of the serialized TabletSchema as key. |
40 | | // use sha256 to prevent from hash collision |
41 | 6.35k | static std::string get_key_signature(const std::string& origin) { |
42 | 6.35k | SHA256Digest digest; |
43 | 6.35k | digest.reset(origin.data(), origin.length()); |
44 | 6.35k | return std::string {digest.digest().data(), digest.digest().length()}; |
45 | 6.35k | } |
46 | | |
47 | | template <typename T> |
48 | 263 | static void append_cache_key_value(std::string* key, T value) { |
49 | 263 | static_assert(std::is_integral_v<T>); |
50 | 263 | key->append(reinterpret_cast<const char*>(&value), sizeof(value)); |
51 | 263 | } tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueIlEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_ Line | Count | Source | 48 | 140 | static void append_cache_key_value(std::string* key, T value) { | 49 | 140 | static_assert(std::is_integral_v<T>); | 50 | 140 | key->append(reinterpret_cast<const char*>(&value), sizeof(value)); | 51 | 140 | } |
tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueImEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_ Line | Count | Source | 48 | 88 | static void append_cache_key_value(std::string* key, T value) { | 49 | 88 | static_assert(std::is_integral_v<T>); | 50 | 88 | key->append(reinterpret_cast<const char*>(&value), sizeof(value)); | 51 | 88 | } |
tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueIbEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_ Line | Count | Source | 48 | 35 | static void append_cache_key_value(std::string* key, T value) { | 49 | 35 | static_assert(std::is_integral_v<T>); | 50 | 35 | key->append(reinterpret_cast<const char*>(&value), sizeof(value)); | 51 | 35 | } |
|
52 | | |
53 | 88 | static void append_cache_key_string(std::string* key, const std::string& value) { |
54 | 88 | append_cache_key_value(key, static_cast<uint64_t>(value.size())); |
55 | 88 | key->append(value); |
56 | 88 | } |
57 | | |
58 | 6.32k | std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std::string& key) { |
59 | 6.32k | std::string key_signature = get_key_signature(key); |
60 | 6.32k | auto* lru_handle = lookup(key_signature); |
61 | 6.32k | TabletSchemaSPtr tablet_schema_ptr; |
62 | 6.32k | if (lru_handle) { |
63 | 6.23k | auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle); |
64 | 6.23k | tablet_schema_ptr = value->tablet_schema; |
65 | 6.23k | g_tablet_schema_cache_hit_count << 1; |
66 | 6.23k | } else { |
67 | 92 | auto* value = new CacheValue; |
68 | 92 | tablet_schema_ptr = std::make_shared<TabletSchema>(); |
69 | 92 | TabletSchemaPB pb; |
70 | 92 | pb.ParseFromString(key); |
71 | | // We should reuse the memory of the same TabletColumn/TabletIndex object, set reuse_cached_column to true |
72 | 92 | tablet_schema_ptr->init_from_pb(pb, false, true); |
73 | 92 | value->tablet_schema = tablet_schema_ptr; |
74 | 92 | lru_handle = LRUCachePolicy::insert(key_signature, value, tablet_schema_ptr->num_columns(), |
75 | 92 | tablet_schema_ptr->mem_size(), CachePriority::NORMAL); |
76 | 92 | g_tablet_schema_cache_count << 1; |
77 | 92 | g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns(); |
78 | 92 | } |
79 | 6.32k | DCHECK(lru_handle != nullptr); |
80 | 6.32k | return std::make_pair(lru_handle, tablet_schema_ptr); |
81 | 6.32k | } |
82 | | |
83 | | std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert( |
84 | 8 | const std::string& key, TabletSchemaSPtr tablet_schema) { |
85 | 8 | auto* lru_handle = lookup(key); |
86 | 8 | TabletSchemaSPtr tablet_schema_ptr; |
87 | 8 | if (lru_handle) { |
88 | 0 | auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle); |
89 | 0 | tablet_schema_ptr = value->tablet_schema; |
90 | 0 | g_tablet_schema_cache_hit_count << 1; |
91 | 8 | } else { |
92 | 8 | DCHECK(tablet_schema != nullptr); |
93 | 8 | auto* value = new CacheValue; |
94 | 8 | tablet_schema_ptr = std::move(tablet_schema); |
95 | 8 | value->tablet_schema = tablet_schema_ptr; |
96 | 8 | lru_handle = LRUCachePolicy::insert(key, value, tablet_schema_ptr->num_columns(), |
97 | 8 | tablet_schema_ptr->mem_size(), CachePriority::NORMAL); |
98 | 8 | g_tablet_schema_cache_count << 1; |
99 | 8 | g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns(); |
100 | 8 | } |
101 | 8 | DCHECK(lru_handle != nullptr); |
102 | 8 | return std::make_pair(lru_handle, tablet_schema_ptr); |
103 | 8 | } |
104 | | |
105 | | std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::lookup_schema( |
106 | 29 | const std::string& key) { |
107 | 29 | auto* lru_handle = lookup(key); |
108 | 29 | if (lru_handle == nullptr) { |
109 | 7 | return {nullptr, nullptr}; |
110 | 7 | } |
111 | 22 | auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle); |
112 | 22 | g_tablet_schema_cache_hit_count << 1; |
113 | 22 | return {lru_handle, value->tablet_schema}; |
114 | 29 | } |
115 | | |
116 | | std::string TabletSchemaCache::build_load_schema_cache_key( |
117 | | int64_t index_id, const OlapTableSchemaParam* table_schema_param, |
118 | 35 | const TabletSchema& ori_tablet_schema, const OlapTableIndexSchema* index_schema) { |
119 | 35 | DCHECK(table_schema_param != nullptr); |
120 | 35 | std::string cache_key; |
121 | 35 | cache_key.append("load_schema_v2"); |
122 | 35 | append_cache_key_value(&cache_key, index_id); |
123 | 35 | append_cache_key_value(&cache_key, table_schema_param->table_id()); |
124 | 35 | append_cache_key_value(&cache_key, table_schema_param->db_id()); |
125 | 35 | append_cache_key_value(&cache_key, table_schema_param->version()); |
126 | | |
127 | 35 | TabletSchemaPB ori_schema_pb; |
128 | 35 | ori_tablet_schema.to_schema_pb(&ori_schema_pb); |
129 | 35 | append_cache_key_string(&cache_key, |
130 | 35 | TabletSchema::deterministic_string_serialize(ori_schema_pb)); |
131 | 35 | if (ori_tablet_schema.num_variant_columns() > 0) { |
132 | | // Variant schemas carry path set info outside TabletSchemaPB, so do not share them |
133 | | // across different source TabletSchema objects unless that metadata is serialized. |
134 | 0 | append_cache_key_value(&cache_key, reinterpret_cast<uintptr_t>(&ori_tablet_schema)); |
135 | 0 | } |
136 | | |
137 | 35 | std::string auto_increment_column; |
138 | 35 | if (table_schema_param->is_partial_update()) { |
139 | 0 | auto_increment_column = table_schema_param->auto_increment_coulumn(); |
140 | 0 | } |
141 | 35 | append_cache_key_string(&cache_key, auto_increment_column); |
142 | | |
143 | 35 | const bool has_current_schema = index_schema != nullptr && !index_schema->columns.empty() && |
144 | 35 | index_schema->columns[0]->unique_id() >= 0; |
145 | 35 | append_cache_key_value(&cache_key, has_current_schema); |
146 | 35 | if (index_schema != nullptr) { |
147 | 18 | POlapTableIndexSchema index_schema_pb; |
148 | 18 | index_schema->to_protobuf(&index_schema_pb); |
149 | 18 | append_cache_key_string(&cache_key, |
150 | 18 | TabletSchema::deterministic_string_serialize(index_schema_pb)); |
151 | 18 | } |
152 | 35 | return get_key_signature(cache_key); |
153 | 35 | } |
154 | | |
155 | 6.35k | void TabletSchemaCache::release(Cache::Handle* lru_handle) { |
156 | 6.35k | LRUCachePolicy::release(lru_handle); |
157 | 6.35k | } |
158 | | |
159 | 0 | TabletSchemaCache::CacheValue::~CacheValue() { |
160 | 0 | g_tablet_schema_cache_count << -1; |
161 | 0 | g_tablet_schema_cache_columns_count << -tablet_schema->num_columns(); |
162 | 0 | } |
163 | | |
164 | | } // namespace doris |