Coverage Report

Created: 2024-11-18 12:21

/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 LRUCachePolicyTrackingManual {
48
public:
49
    static SchemaCache* 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 (!instance() || schema_key.empty()) {
59
0
            return {};
60
0
        }
61
0
        auto* lru_handle = lookup(schema_key);
62
0
        if (lru_handle) {
63
0
            Defer release([cache = this, lru_handle] { cache->release(lru_handle); });
64
0
            auto* value = (CacheValue*)LRUCachePolicy::value(lru_handle);
65
0
            VLOG_DEBUG << "use cache schema";
66
0
            return value->tablet_schema;
67
0
        }
68
0
        return {};
69
0
    }
70
71
    // Insert a shared Schema into cache, schema_key is full column unique ids
72
0
    void insert_schema(const std::string& key, TabletSchemaSPtr schema) {
73
0
        if (!instance() || key.empty()) {
74
0
            return;
75
0
        }
76
0
        auto* value = new CacheValue;
77
0
        value->tablet_schema = schema;
78
79
0
        auto* lru_handle = insert(key, value, 1, schema->mem_size(), CachePriority::NORMAL);
80
0
        release(lru_handle);
81
0
    }
82
83
    class CacheValue : public LRUCacheValueBase {
84
    public:
85
        TabletSchemaSPtr tablet_schema = nullptr;
86
    };
87
88
    SchemaCache(size_t capacity)
89
            : LRUCachePolicyTrackingManual(CachePolicy::CacheType::SCHEMA_CACHE, capacity,
90
                                           LRUCacheType::NUMBER,
91
0
                                           config::schema_cache_sweep_time_sec) {}
92
93
private:
94
    static constexpr char SCHEMA_DELIMITER = '-';
95
};
96
97
} // namespace doris