Coverage Report

Created: 2026-03-16 12:03

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
2.59M
SegmentLoader* SegmentLoader::instance() {
31
2.59M
    return ExecEnv::GetInstance()->segment_loader();
32
2.59M
}
33
34
2.32M
bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) {
35
2.32M
    auto* lru_handle = LRUCachePolicy::lookup(key.encode());
36
2.32M
    if (lru_handle == nullptr) {
37
86.3k
        return false;
38
86.3k
    }
39
2.23M
    handle->push_segment(this, lru_handle);
40
2.23M
    return true;
41
2.32M
}
42
43
void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
44
68.2k
                          SegmentCacheHandle* handle) {
45
68.2k
    auto* lru_handle =
46
68.2k
            LRUCachePolicy::insert(key.encode(), &value, value.segment->meta_mem_usage(),
47
68.2k
                                   value.segment->meta_mem_usage(), CachePriority::NORMAL);
48
68.2k
    handle->push_segment(this, lru_handle);
49
68.2k
}
50
51
58.0k
void SegmentCache::erase(const SegmentCache::CacheKey& key) {
52
58.0k
    LRUCachePolicy::erase(key.encode());
53
58.0k
}
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
2.32M
                                   OlapReaderStatistics* index_load_stats) {
59
2.32M
    SegmentCache::CacheKey cache_key(rowset->rowset_id(), segment_id);
60
2.32M
    if (_segment_cache->lookup(cache_key, cache_handle)) {
61
        // Has to check the segment status here, because the segment in cache may has something wrong during
62
        // load index or create column reader.
63
        // Not merge this if logic with previous to make the logic more clear.
64
2.25M
        if (cache_handle->pop_unhealthy_segment() == nullptr) {
65
2.24M
            return Status::OK();
66
2.24M
        }
67
2.25M
    }
68
    // If the segment is not healthy, then will create a new segment and will replace the unhealthy one in SegmentCache.
69
76.0k
    segment_v2::SegmentSharedPtr segment;
70
76.0k
    RETURN_IF_ERROR(rowset->load_segment(segment_id, index_load_stats, &segment));
71
76.0k
    if (need_load_pk_index_and_bf) {
72
10.7k
        RETURN_IF_ERROR(segment->load_pk_index_and_bf(index_load_stats));
73
10.7k
    }
74
76.0k
    if (use_cache && !config::disable_segment_cache) {
75
        // memory of SegmentCache::CacheValue will be handled by SegmentCache
76
68.5k
        auto* cache_value = new SegmentCache::CacheValue(segment);
77
68.5k
        _cache_mem_usage += segment->meta_mem_usage();
78
68.5k
        _segment_cache->insert(cache_key, *cache_value, cache_handle);
79
68.5k
    } else {
80
7.56k
        cache_handle->push_segment(std::move(segment));
81
7.56k
    }
82
83
76.0k
    return Status::OK();
84
76.0k
}
85
86
Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
87
                                    SegmentCacheHandle* cache_handle, bool use_cache,
88
                                    bool need_load_pk_index_and_bf,
89
236k
                                    OlapReaderStatistics* index_load_stats) {
90
236k
    if (cache_handle->is_inited()) {
91
0
        return Status::OK();
92
0
    }
93
413k
    for (int64_t i = 0; i < rowset->num_segments(); i++) {
94
176k
        RETURN_IF_ERROR(load_segment(rowset, i, cache_handle, use_cache, need_load_pk_index_and_bf,
95
176k
                                     index_load_stats));
96
176k
    }
97
236k
    cache_handle->set_inited();
98
236k
    return Status::OK();
99
236k
}
100
101
58.0k
void SegmentLoader::erase_segment(const SegmentCache::CacheKey& key) {
102
58.0k
    _segment_cache->erase(key);
103
58.0k
}
104
105
208k
void SegmentLoader::erase_segments(const RowsetId& rowset_id, int64_t num_segments) {
106
266k
    for (int64_t i = 0; i < num_segments; i++) {
107
58.1k
        erase_segment(SegmentCache::CacheKey(rowset_id, i));
108
58.1k
    }
109
208k
}
110
111
} // namespace doris