Coverage Report

Created: 2026-03-13 03:47

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.02M
SegmentLoader* SegmentLoader::instance() {
31
2.02M
    return ExecEnv::GetInstance()->segment_loader();
32
2.02M
}
33
34
1.77M
bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) {
35
1.77M
    auto* lru_handle = LRUCachePolicy::lookup(key.encode());
36
1.77M
    if (lru_handle == nullptr) {
37
79.1k
        return false;
38
79.1k
    }
39
1.69M
    handle->push_segment(this, lru_handle);
40
1.69M
    return true;
41
1.77M
}
42
43
void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
44
63.4k
                          SegmentCacheHandle* handle) {
45
63.4k
    auto* lru_handle =
46
63.4k
            LRUCachePolicy::insert(key.encode(), &value, value.segment->meta_mem_usage(),
47
63.4k
                                   value.segment->meta_mem_usage(), CachePriority::NORMAL);
48
63.4k
    handle->push_segment(this, lru_handle);
49
63.4k
}
50
51
55.9k
void SegmentCache::erase(const SegmentCache::CacheKey& key) {
52
55.9k
    LRUCachePolicy::erase(key.encode());
53
55.9k
}
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
1.77M
                                   OlapReaderStatistics* index_load_stats) {
59
1.77M
    SegmentCache::CacheKey cache_key(rowset->rowset_id(), segment_id);
60
1.77M
    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
1.70M
        if (cache_handle->pop_unhealthy_segment() == nullptr) {
65
1.70M
            return Status::OK();
66
1.70M
        }
67
1.70M
    }
68
    // If the segment is not healthy, then will create a new segment and will replace the unhealthy one in SegmentCache.
69
74.5k
    segment_v2::SegmentSharedPtr segment;
70
74.5k
    RETURN_IF_ERROR(rowset->load_segment(segment_id, index_load_stats, &segment));
71
74.5k
    if (need_load_pk_index_and_bf) {
72
9.89k
        RETURN_IF_ERROR(segment->load_pk_index_and_bf(index_load_stats));
73
9.89k
    }
74
74.5k
    if (use_cache && !config::disable_segment_cache) {
75
        // memory of SegmentCache::CacheValue will be handled by SegmentCache
76
63.5k
        auto* cache_value = new SegmentCache::CacheValue(segment);
77
63.5k
        _cache_mem_usage += segment->meta_mem_usage();
78
63.5k
        _segment_cache->insert(cache_key, *cache_value, cache_handle);
79
63.5k
    } else {
80
11.0k
        cache_handle->push_segment(std::move(segment));
81
11.0k
    }
82
83
74.5k
    return Status::OK();
84
74.5k
}
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
221k
                                    OlapReaderStatistics* index_load_stats) {
90
221k
    if (cache_handle->is_inited()) {
91
0
        return Status::OK();
92
0
    }
93
386k
    for (int64_t i = 0; i < rowset->num_segments(); i++) {
94
165k
        RETURN_IF_ERROR(load_segment(rowset, i, cache_handle, use_cache, need_load_pk_index_and_bf,
95
165k
                                     index_load_stats));
96
165k
    }
97
221k
    cache_handle->set_inited();
98
221k
    return Status::OK();
99
221k
}
100
101
55.9k
void SegmentLoader::erase_segment(const SegmentCache::CacheKey& key) {
102
55.9k
    _segment_cache->erase(key);
103
55.9k
}
104
105
193k
void SegmentLoader::erase_segments(const RowsetId& rowset_id, int64_t num_segments) {
106
249k
    for (int64_t i = 0; i < num_segments; i++) {
107
55.9k
        erase_segment(SegmentCache::CacheKey(rowset_id, i));
108
55.9k
    }
109
193k
}
110
111
} // namespace doris