Coverage Report

Created: 2025-07-25 10:29

/root/doris/be/src/olap/segment_loader.cpp
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 "olap/segment_loader.h"
19
20
#include "common/config.h"
21
#include "common/status.h"
22
#include "olap/olap_define.h"
23
#include "olap/rowset/beta_rowset.h"
24
#include "util/stopwatch.hpp"
25
26
namespace doris {
27
28
21.4k
SegmentLoader* SegmentLoader::instance() {
29
21.4k
    return ExecEnv::GetInstance()->segment_loader();
30
21.4k
}
31
32
4.83k
bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) {
33
4.83k
    auto* lru_handle = LRUCachePolicy::lookup(key.encode());
34
4.83k
    if (lru_handle == nullptr) {
35
4.83k
        return false;
36
4.83k
    }
37
0
    handle->push_segment(this, lru_handle);
38
0
    return true;
39
4.83k
}
40
41
void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
42
4.18k
                          SegmentCacheHandle* handle) {
43
4.18k
    auto* lru_handle = LRUCachePolicyTrackingManual::insert(
44
4.18k
            key.encode(), &value, value.segment->meta_mem_usage(), value.segment->meta_mem_usage(),
45
4.18k
            CachePriority::NORMAL);
46
4.18k
    handle->push_segment(this, lru_handle);
47
4.18k
}
48
49
5
void SegmentCache::erase(const SegmentCache::CacheKey& key) {
50
5
    LRUCachePolicy::erase(key.encode());
51
5
}
52
53
Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
54
                                    SegmentCacheHandle* cache_handle, bool use_cache,
55
540
                                    bool need_load_pk_index_and_bf) {
56
540
    if (cache_handle->is_inited()) {
57
0
        return Status::OK();
58
0
    }
59
5.37k
    for (int64_t i = 0; i < rowset->num_segments(); i++) {
60
4.83k
        SegmentCache::CacheKey cache_key(rowset->rowset_id(), i);
61
4.83k
        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
0
            if (cache_handle->pop_unhealthy_segment() == nullptr) {
66
0
                continue;
67
0
            }
68
0
        }
69
        // If the segment is not healthy, then will create a new segment and will replace the unhealthy one in SegmentCache.
70
4.83k
        segment_v2::SegmentSharedPtr segment;
71
4.83k
        RETURN_IF_ERROR(rowset->load_segment(i, &segment));
72
4.83k
        if (need_load_pk_index_and_bf) {
73
1
            RETURN_IF_ERROR(segment->load_pk_index_and_bf());
74
1
        }
75
4.83k
        if (use_cache && !config::disable_segment_cache) {
76
            // memory of SegmentCache::CacheValue will be handled by SegmentCache
77
4.18k
            auto* cache_value = new SegmentCache::CacheValue();
78
4.18k
            _cache_mem_usage += segment->meta_mem_usage();
79
4.18k
            cache_value->segment = std::move(segment);
80
4.18k
            _segment_cache->insert(cache_key, *cache_value, cache_handle);
81
4.18k
        } else {
82
652
            cache_handle->push_segment(std::move(segment));
83
652
        }
84
4.83k
    }
85
540
    cache_handle->set_inited();
86
540
    return Status::OK();
87
540
}
88
89
5
void SegmentLoader::erase_segment(const SegmentCache::CacheKey& key) {
90
5
    _segment_cache->erase(key);
91
5
}
92
93
20.9k
void SegmentLoader::erase_segments(const RowsetId& rowset_id, int64_t num_segments) {
94
20.9k
    for (int64_t i = 0; i < num_segments; i++) {
95
5
        erase_segment(SegmentCache::CacheKey(rowset_id, i));
96
5
    }
97
20.9k
}
98
99
} // namespace doris