Coverage Report

Created: 2026-06-17 10:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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.39k
static std::string get_key_signature(const std::string& origin) {
42
6.39k
    SHA256Digest digest;
43
6.39k
    digest.reset(origin.data(), origin.length());
44
6.39k
    return std::string {digest.digest().data(), digest.digest().length()};
45
6.39k
}
46
47
template <typename T>
48
287
static void append_cache_key_value(std::string* key, T value) {
49
287
    static_assert(std::is_integral_v<T>);
50
287
    key->append(reinterpret_cast<const char*>(&value), sizeof(value));
51
287
}
tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueIlEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Line
Count
Source
48
152
static void append_cache_key_value(std::string* key, T value) {
49
152
    static_assert(std::is_integral_v<T>);
50
152
    key->append(reinterpret_cast<const char*>(&value), sizeof(value));
51
152
}
tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueImEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Line
Count
Source
48
97
static void append_cache_key_value(std::string* key, T value) {
49
97
    static_assert(std::is_integral_v<T>);
50
97
    key->append(reinterpret_cast<const char*>(&value), sizeof(value));
51
97
}
tablet_schema_cache.cpp:_ZN5dorisL22append_cache_key_valueIbEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Line
Count
Source
48
38
static void append_cache_key_value(std::string* key, T value) {
49
38
    static_assert(std::is_integral_v<T>);
50
38
    key->append(reinterpret_cast<const char*>(&value), sizeof(value));
51
38
}
52
53
97
static void append_cache_key_string(std::string* key, const std::string& value) {
54
97
    append_cache_key_value(key, static_cast<uint64_t>(value.size()));
55
97
    key->append(value);
56
97
}
57
58
6.35k
std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std::string& key) {
59
6.35k
    std::string key_signature = get_key_signature(key);
60
6.35k
    auto* lru_handle = lookup(key_signature);
61
6.35k
    TabletSchemaSPtr tablet_schema_ptr;
62
6.35k
    if (lru_handle) {
63
6.25k
        auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle);
64
6.25k
        tablet_schema_ptr = value->tablet_schema;
65
6.25k
        g_tablet_schema_cache_hit_count << 1;
66
6.25k
    } else {
67
94
        auto* value = new CacheValue;
68
94
        tablet_schema_ptr = std::make_shared<TabletSchema>();
69
94
        TabletSchemaPB pb;
70
94
        pb.ParseFromString(key);
71
        // We should reuse the memory of the same TabletColumn/TabletIndex object, set reuse_cached_column to true
72
94
        tablet_schema_ptr->init_from_pb(pb, false, true);
73
94
        value->tablet_schema = tablet_schema_ptr;
74
94
        lru_handle = LRUCachePolicy::insert(key_signature, value, tablet_schema_ptr->num_columns(),
75
94
                                            tablet_schema_ptr->mem_size(), CachePriority::NORMAL);
76
94
        g_tablet_schema_cache_count << 1;
77
94
        g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns();
78
94
    }
79
6.35k
    DCHECK(lru_handle != nullptr);
80
6.35k
    return std::make_pair(lru_handle, tablet_schema_ptr);
81
6.35k
}
82
83
std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(
84
11
        const std::string& key, TabletSchemaSPtr tablet_schema) {
85
11
    auto* lru_handle = lookup(key);
86
11
    TabletSchemaSPtr tablet_schema_ptr;
87
11
    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
11
    } else {
92
11
        DCHECK(tablet_schema != nullptr);
93
11
        auto* value = new CacheValue;
94
11
        tablet_schema_ptr = std::move(tablet_schema);
95
11
        value->tablet_schema = tablet_schema_ptr;
96
11
        lru_handle = LRUCachePolicy::insert(key, value, tablet_schema_ptr->num_columns(),
97
11
                                            tablet_schema_ptr->mem_size(), CachePriority::NORMAL);
98
11
        g_tablet_schema_cache_count << 1;
99
11
        g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns();
100
11
    }
101
11
    DCHECK(lru_handle != nullptr);
102
11
    return std::make_pair(lru_handle, tablet_schema_ptr);
103
11
}
104
105
std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::lookup_schema(
106
32
        const std::string& key) {
107
32
    auto* lru_handle = lookup(key);
108
32
    if (lru_handle == nullptr) {
109
10
        return {nullptr, nullptr};
110
10
    }
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
32
}
115
116
std::string TabletSchemaCache::build_load_schema_cache_key(
117
        int64_t index_id, const OlapTableSchemaParam* table_schema_param,
118
38
        const TabletSchema& ori_tablet_schema, const OlapTableIndexSchema* index_schema) {
119
38
    DCHECK(table_schema_param != nullptr);
120
38
    std::string cache_key;
121
38
    cache_key.append("load_schema_v2");
122
38
    append_cache_key_value(&cache_key, index_id);
123
38
    append_cache_key_value(&cache_key, table_schema_param->table_id());
124
38
    append_cache_key_value(&cache_key, table_schema_param->db_id());
125
38
    append_cache_key_value(&cache_key, table_schema_param->version());
126
127
38
    TabletSchemaPB ori_schema_pb;
128
38
    ori_tablet_schema.to_schema_pb(&ori_schema_pb);
129
38
    append_cache_key_string(&cache_key,
130
38
                            TabletSchema::deterministic_string_serialize(ori_schema_pb));
131
38
    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
38
    std::string auto_increment_column;
138
38
    if (table_schema_param->is_partial_update()) {
139
0
        auto_increment_column = table_schema_param->auto_increment_coulumn();
140
0
    }
141
38
    append_cache_key_string(&cache_key, auto_increment_column);
142
143
38
    const bool has_current_schema = index_schema != nullptr && !index_schema->columns.empty() &&
144
38
                                    index_schema->columns[0]->unique_id() >= 0;
145
38
    append_cache_key_value(&cache_key, has_current_schema);
146
38
    if (index_schema != nullptr) {
147
21
        POlapTableIndexSchema index_schema_pb;
148
21
        index_schema->to_protobuf(&index_schema_pb);
149
21
        append_cache_key_string(&cache_key,
150
21
                                TabletSchema::deterministic_string_serialize(index_schema_pb));
151
21
    }
152
38
    return get_key_signature(cache_key);
153
38
}
154
155
6.38k
void TabletSchemaCache::release(Cache::Handle* lru_handle) {
156
6.38k
    LRUCachePolicy::release(lru_handle);
157
6.38k
}
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