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 "bvar/bvar.h" |
25 | | #include "storage/tablet/tablet_schema.h" |
26 | | #include "util/sha.h" |
27 | | |
28 | | bvar::Adder<int64_t> g_tablet_schema_cache_count("tablet_schema_cache_count"); |
29 | | bvar::Adder<int64_t> g_tablet_schema_cache_columns_count("tablet_schema_cache_columns_count"); |
30 | | bvar::Adder<int64_t> g_tablet_schema_cache_hit_count("tablet_schema_cache_hit_count"); |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | // to reduce the memory consumption of the serialized TabletSchema as key. |
35 | | // use sha256 to prevent from hash collision |
36 | 6.97k | static std::string get_key_signature(const std::string& origin) { |
37 | 6.97k | SHA256Digest digest; |
38 | 6.97k | digest.reset(origin.data(), origin.length()); |
39 | 6.97k | return std::string {digest.digest().data(), digest.digest().length()}; |
40 | 6.97k | } |
41 | | |
42 | 6.97k | std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std::string& key) { |
43 | 6.97k | std::string key_signature = get_key_signature(key); |
44 | 6.97k | auto* lru_handle = lookup(key_signature); |
45 | 6.97k | TabletSchemaSPtr tablet_schema_ptr; |
46 | 6.97k | if (lru_handle) { |
47 | 6.89k | auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle); |
48 | 6.89k | tablet_schema_ptr = value->tablet_schema; |
49 | 6.89k | g_tablet_schema_cache_hit_count << 1; |
50 | 6.89k | } else { |
51 | 80 | auto* value = new CacheValue; |
52 | 80 | tablet_schema_ptr = std::make_shared<TabletSchema>(); |
53 | 80 | TabletSchemaPB pb; |
54 | 80 | pb.ParseFromString(key); |
55 | | // We should reuse the memory of the same TabletColumn/TabletIndex object, set reuse_cached_column to true |
56 | 80 | tablet_schema_ptr->init_from_pb(pb, false, true); |
57 | 80 | value->tablet_schema = tablet_schema_ptr; |
58 | 80 | lru_handle = LRUCachePolicy::insert(key_signature, value, tablet_schema_ptr->num_columns(), |
59 | 80 | tablet_schema_ptr->mem_size(), CachePriority::NORMAL); |
60 | 80 | g_tablet_schema_cache_count << 1; |
61 | 80 | g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns(); |
62 | 80 | } |
63 | 6.97k | DCHECK(lru_handle != nullptr); |
64 | 6.97k | return std::make_pair(lru_handle, tablet_schema_ptr); |
65 | 6.97k | } |
66 | | |
67 | 6.97k | void TabletSchemaCache::release(Cache::Handle* lru_handle) { |
68 | 6.97k | LRUCachePolicy::release(lru_handle); |
69 | 6.97k | } |
70 | | |
71 | 0 | TabletSchemaCache::CacheValue::~CacheValue() { |
72 | 0 | g_tablet_schema_cache_count << -1; |
73 | 0 | g_tablet_schema_cache_columns_count << -tablet_schema->num_columns(); |
74 | 0 | } |
75 | | |
76 | | } // namespace doris |