Coverage Report

Created: 2026-06-01 13:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/segment_loader.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/segment/segment_loader.h"
19
20
#include <butil/time.h>
21
22
#include "common/config.h"
23
#include "common/status.h"
24
#include "storage/olap_define.h"
25
#include "storage/rowset/beta_rowset.h"
26
#include "util/stopwatch.hpp"
27
28
namespace doris {
29
30
764k
SegmentLoader* SegmentLoader::instance() {
31
764k
    return ExecEnv::GetInstance()->segment_loader();
32
764k
}
33
34
653k
bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) {
35
653k
    auto* lru_handle = LRUCachePolicy::lookup(key.encode());
36
653k
    if (lru_handle == nullptr) {
37
36.1k
        return false;
38
36.1k
    }
39
617k
    handle->push_segment(this, lru_handle);
40
617k
    return true;
41
653k
}
42
43
void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
44
18.8k
                          SegmentCacheHandle* handle) {
45
18.8k
    auto* lru_handle =
46
18.8k
            LRUCachePolicy::insert(key.encode(), &value, value.segment->meta_mem_usage(),
47
18.8k
                                   value.segment->meta_mem_usage(), CachePriority::NORMAL);
48
18.8k
    handle->push_segment(this, lru_handle);
49
18.8k
}
50
51
34.1k
void SegmentCache::erase(const SegmentCache::CacheKey& key) {
52
34.1k
    LRUCachePolicy::erase(key.encode());
53
34.1k
}
54
55
Status SegmentLoader::load_segment(const BetaRowsetSharedPtr& rowset, int64_t segment_id,
56
                                   SegmentCacheHandle* cache_handle, bool use_cache,
57
                                   bool need_load_pk_index_and_bf,
58
653k
                                   OlapReaderStatistics* index_load_stats) {
59
653k
    auto start = MonotonicMicros();
60
653k
    SegmentCache::CacheKey cache_key(rowset->rowset_id(), segment_id);
61
653k
    if (_segment_cache->lookup(cache_key, cache_handle)) {
62
        // Has to check the segment status here, because the segment in cache may has something wrong during
63
        // load index or create column reader.
64
        // Not merge this if logic with previous to make the logic more clear.
65
618k
        if (cache_handle->pop_unhealthy_segment() == nullptr) {
66
618k
            if (index_load_stats != nullptr) {
67
613k
                index_load_stats->rowset_reader_load_segments_timer_ns +=
68
613k
                        (MonotonicMicros() - start) * 1000;
69
613k
            }
70
618k
            return Status::OK();
71
618k
        }
72
618k
    }
73
    // If the segment is not healthy, then will create a new segment and will replace the unhealthy one in SegmentCache.
74
35.2k
    segment_v2::SegmentSharedPtr segment;
75
35.2k
    RETURN_IF_ERROR(rowset->load_segment(segment_id, index_load_stats, &segment));
76
35.2k
    if (need_load_pk_index_and_bf) {
77
1.68k
        RETURN_IF_ERROR(segment->load_pk_index_and_bf(index_load_stats));
78
1.68k
    }
79
35.2k
    if (use_cache && !config::disable_segment_cache) {
80
        // memory of SegmentCache::CacheValue will be handled by SegmentCache
81
18.8k
        auto* cache_value = new SegmentCache::CacheValue(segment);
82
18.8k
        _cache_mem_usage += segment->meta_mem_usage();
83
18.8k
        _segment_cache->insert(cache_key, *cache_value, cache_handle);
84
18.8k
    } else {
85
16.3k
        cache_handle->push_segment(std::move(segment));
86
16.3k
    }
87
35.2k
    if (index_load_stats != nullptr) {
88
31.4k
        index_load_stats->rowset_reader_load_segments_timer_ns +=
89
31.4k
                (MonotonicMicros() - start) * 1000;
90
31.4k
    }
91
92
35.2k
    return Status::OK();
93
35.2k
}
94
95
Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
96
                                    SegmentCacheHandle* cache_handle, bool use_cache,
97
                                    bool need_load_pk_index_and_bf,
98
10.8k
                                    OlapReaderStatistics* index_load_stats) {
99
10.8k
    if (cache_handle->is_inited()) {
100
0
        return Status::OK();
101
0
    }
102
21.2k
    for (int64_t i = 0; i < rowset->num_segments(); i++) {
103
10.3k
        RETURN_IF_ERROR(load_segment(rowset, i, cache_handle, use_cache, need_load_pk_index_and_bf,
104
10.3k
                                     index_load_stats));
105
10.3k
    }
106
10.8k
    cache_handle->set_inited();
107
10.8k
    return Status::OK();
108
10.8k
}
109
110
34.1k
void SegmentLoader::erase_segment(const SegmentCache::CacheKey& key) {
111
34.1k
    _segment_cache->erase(key);
112
34.1k
}
113
114
109k
void SegmentLoader::erase_segments(const RowsetId& rowset_id, int64_t num_segments) {
115
143k
    for (int64_t i = 0; i < num_segments; i++) {
116
34.1k
        erase_segment(SegmentCache::CacheKey(rowset_id, i));
117
34.1k
    }
118
109k
}
119
120
} // namespace doris