Coverage Report

Created: 2026-03-20 20:54

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 "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
7.00k
static std::string get_key_signature(const std::string& origin) {
37
7.00k
    SHA256Digest digest;
38
7.00k
    digest.reset(origin.data(), origin.length());
39
7.00k
    return std::string {digest.digest().data(), digest.digest().length()};
40
7.00k
}
41
42
7.00k
std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std::string& key) {
43
7.00k
    std::string key_signature = get_key_signature(key);
44
7.00k
    auto* lru_handle = lookup(key_signature);
45
7.00k
    TabletSchemaSPtr tablet_schema_ptr;
46
7.00k
    if (lru_handle) {
47
6.92k
        auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle);
48
6.92k
        tablet_schema_ptr = value->tablet_schema;
49
6.92k
        g_tablet_schema_cache_hit_count << 1;
50
6.92k
    } else {
51
83
        auto* value = new CacheValue;
52
83
        tablet_schema_ptr = std::make_shared<TabletSchema>();
53
83
        TabletSchemaPB pb;
54
83
        pb.ParseFromString(key);
55
        // We should reuse the memory of the same TabletColumn/TabletIndex object, set reuse_cached_column to true
56
83
        tablet_schema_ptr->init_from_pb(pb, false, true);
57
83
        value->tablet_schema = tablet_schema_ptr;
58
83
        lru_handle = LRUCachePolicy::insert(key_signature, value, tablet_schema_ptr->num_columns(),
59
83
                                            tablet_schema_ptr->mem_size(), CachePriority::NORMAL);
60
83
        g_tablet_schema_cache_count << 1;
61
83
        g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns();
62
83
    }
63
7.00k
    DCHECK(lru_handle != nullptr);
64
7.00k
    return std::make_pair(lru_handle, tablet_schema_ptr);
65
7.00k
}
66
67
7.00k
void TabletSchemaCache::release(Cache::Handle* lru_handle) {
68
7.00k
    LRUCachePolicy::release(lru_handle);
69
7.00k
}
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