Coverage Report

Created: 2025-09-11 19:59

/root/doris/be/src/olap/segment_loader.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
#pragma once
19
20
#include <butil/macros.h>
21
#include <glog/logging.h>
22
#include <stddef.h>
23
#include <stdint.h>
24
25
#include <atomic>
26
#include <memory>
27
#include <ostream>
28
#include <string>
29
#include <utility>
30
#include <vector>
31
32
#include "common/status.h"
33
#include "olap/lru_cache.h"
34
#include "olap/olap_common.h" // for rowset id
35
#include "olap/rowset/segment_v2/segment.h"
36
#include "runtime/memory/lru_cache_policy.h"
37
#include "util/time.h"
38
39
namespace doris {
40
41
class SegmentCacheHandle;
42
class BetaRowset;
43
44
// SegmentLoader is used to load the Segment of BetaRowset.
45
// An LRUCache is encapsulated inside it, which is used to cache the opened segments.
46
// The caller should use the following method to load and obtain
47
// the segments of a specified rowset:
48
//
49
//  SegmentCacheHandle cache_handle;
50
//  RETURN_IF_ERROR(SegmentCache::instance()->load_segments(_rowset, &cache_handle));
51
//  for (auto& seg_ptr : cache_handle.value()->segments) {
52
//      ... visit segment ...
53
//  }
54
//
55
// Make sure that cache_handle is valid during the segment usage period.
56
using BetaRowsetSharedPtr = std::shared_ptr<BetaRowset>;
57
58
class SegmentCache : public LRUCachePolicy {
59
public:
60
    using LRUCachePolicy::insert;
61
    // The cache key or segment lru cache
62
    struct CacheKey {
63
        CacheKey(RowsetId rowset_id_, int64_t segment_id_)
64
6.02k
                : rowset_id(rowset_id_), segment_id(segment_id_) {}
65
        RowsetId rowset_id;
66
        int64_t segment_id;
67
68
        // Encode to a flat binary which can be used as LRUCache's key
69
10.2k
        [[nodiscard]] std::string encode() const {
70
10.2k
            return rowset_id.to_string() + std::to_string(segment_id);
71
10.2k
        }
72
    };
73
74
    // The cache value of segment lru cache.
75
    // Holding all opened segments of a rowset.
76
    class CacheValue : public LRUCacheValueBase {
77
    public:
78
4.18k
        CacheValue(segment_v2::SegmentSharedPtr segment_) : segment(std::move(segment_)) {}
79
80
        const segment_v2::SegmentSharedPtr segment;
81
    };
82
83
    SegmentCache(size_t memory_bytes_limit, size_t segment_num_limit)
84
            : LRUCachePolicy(CachePolicy::CacheType::SEGMENT_CACHE, memory_bytes_limit,
85
                             LRUCacheType::SIZE, config::tablet_rowset_stale_sweep_time_sec,
86
                             DEFAULT_LRU_CACHE_NUM_SHARDS * 2, segment_num_limit,
87
1
                             config::enable_segment_cache_prune) {}
88
89
    // Lookup the given segment in the cache.
90
    // If the segment is found, the cache entry will be written into handle.
91
    // Return true if entry is found, otherwise return false.
92
    bool lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle);
93
94
    // Insert a cache entry by key.
95
    // And the cache entry will be returned in handle.
96
    // This function is thread-safe.
97
    void insert(const SegmentCache::CacheKey& key, CacheValue& value, SegmentCacheHandle* handle);
98
99
    void erase(const SegmentCache::CacheKey& key);
100
};
101
102
class SegmentLoader {
103
public:
104
    static SegmentLoader* instance();
105
106
    // Create global instance of this class.
107
    // "capacity" is the capacity of lru cache.
108
    // TODO: Currently we use the number of rowset as the cache capacity.
109
    // That is, the limit of cache is the number of rowset.
110
    // This is because currently we cannot accurately estimate the memory occupied by a segment.
111
    // After the estimation of segment memory usage is provided later, it is recommended
112
    // to use Memory as the capacity limit of the cache.
113
114
1
    SegmentLoader(size_t memory_limit_bytes, size_t segment_num_count) {
115
1
        _segment_cache = std::make_unique<SegmentCache>(memory_limit_bytes, segment_num_count);
116
1
    }
117
118
    // Load segments of "rowset", return the "cache_handle" which contains segments.
119
    // If use_cache is true, it will be loaded from _cache.
120
    Status load_segments(const BetaRowsetSharedPtr& rowset, SegmentCacheHandle* cache_handle,
121
                         bool use_cache = false, bool need_load_pk_index_and_bf = false,
122
                         OlapReaderStatistics* index_load_stats = nullptr);
123
124
    void erase_segment(const SegmentCache::CacheKey& key);
125
126
    void erase_segments(const RowsetId& rowset_id, int64_t num_segments);
127
128
2
    int64_t cache_mem_usage() const {
129
2
#ifdef BE_TEST
130
2
        return _cache_mem_usage;
131
#else
132
        return _segment_cache->value_mem_consumption();
133
#endif
134
2
    }
135
136
private:
137
    SegmentLoader();
138
    std::unique_ptr<SegmentCache> _segment_cache;
139
    // Just used for BE UT
140
    int64_t _cache_mem_usage = 0;
141
};
142
143
// A handle for a single rowset from segment lru cache.
144
// The handle can ensure that the segment is valid
145
// and will not be closed while the holder of the handle is accessing the segment.
146
// The handle will automatically release the cache entry when it is destroyed.
147
// So the caller need to make sure the handle is valid in lifecycle.
148
class SegmentCacheHandle {
149
public:
150
1.28k
    SegmentCacheHandle() = default;
151
1.28k
    ~SegmentCacheHandle() = default;
152
153
4.18k
    void push_segment(LRUCachePolicy* cache, Cache::Handle* handle) {
154
4.18k
        segments.push_back(((SegmentCache::CacheValue*)cache->value(handle))->segment);
155
4.18k
        cache->release(handle);
156
4.18k
    }
157
158
1.82k
    void push_segment(segment_v2::SegmentSharedPtr segment) {
159
1.82k
        segments.push_back(std::move(segment));
160
1.82k
    }
161
162
1.32k
    std::vector<segment_v2::SegmentSharedPtr>& get_segments() { return segments; }
163
164
1.28k
    [[nodiscard]] bool is_inited() const { return _init; }
165
166
1.28k
    void set_inited() {
167
1.28k
        DCHECK(!_init);
168
1.28k
        _init = true;
169
1.28k
    }
170
171
0
    segment_v2::SegmentSharedPtr pop_unhealthy_segment() {
172
0
        if (segments.empty()) {
  Branch (172:13): [True: 0, False: 0]
173
0
            return nullptr;
174
0
        }
175
0
        segment_v2::SegmentSharedPtr last_segment = segments.back();
176
0
        if (last_segment->healthy_status().ok()) {
  Branch (176:13): [True: 0, False: 0]
177
0
            return nullptr;
178
0
        }
179
0
        segments.pop_back();
180
0
        return last_segment;
181
0
    }
182
183
private:
184
    std::vector<segment_v2::SegmentSharedPtr> segments;
185
    bool _init {false};
186
187
    // Don't allow copy and assign
188
    DISALLOW_COPY_AND_ASSIGN(SegmentCacheHandle);
189
};
190
191
} // namespace doris