Coverage Report

Created: 2025-04-16 14:10

/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 <butil/time.h>
21
22
#include "common/config.h"
23
#include "common/status.h"
24
#include "olap/olap_define.h"
25
#include "olap/rowset/beta_rowset.h"
26
#include "util/stopwatch.hpp"
27
28
namespace doris {
29
30
22.5k
SegmentLoader* SegmentLoader::instance() {
31
22.5k
    return ExecEnv::GetInstance()->segment_loader();
32
22.5k
}
33
34
5.47k
bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) {
35
5.47k
    auto* lru_handle = LRUCachePolicy::lookup(key.encode());
36
5.47k
    if (lru_handle == nullptr) {
37
5.47k
        return false;
38
5.47k
    }
39
0
    handle->push_segment(this, lru_handle);
40
0
    return true;
41
5.47k
}
42
43
void SegmentCache::insert(const SegmentCache::CacheKey& key, SegmentCache::CacheValue& value,
44
4.18k
                          SegmentCacheHandle* handle) {
45
4.18k
    auto* lru_handle =
46
4.18k
            LRUCachePolicy::insert(key.encode(), &value, value.segment->meta_mem_usage(),
47
4.18k
                                   value.segment->meta_mem_usage(), CachePriority::NORMAL);
48
4.18k
    handle->push_segment(this, lru_handle);
49
4.18k
}
50
51
9
void SegmentCache::erase(const SegmentCache::CacheKey& key) {
52
9
    LRUCachePolicy::erase(key.encode());
53
9
}
54
55
Status SegmentLoader::load_segments(const BetaRowsetSharedPtr& rowset,
56
                                    SegmentCacheHandle* cache_handle, bool use_cache,
57
                                    bool need_load_pk_index_and_bf,
58
1.17k
                                    OlapReaderStatistics* index_load_stats) {
59
1.17k
    if (cache_handle->is_inited()) {
60
0
        return Status::OK();
61
0
    }
62
6.64k
    for (int64_t i = 0; i < rowset->num_segments(); i++) {
63
5.47k
        SegmentCache::CacheKey cache_key(rowset->rowset_id(), i);
64
5.47k
        if (_segment_cache->lookup(cache_key, cache_handle)) {
65
            // Has to check the segment status here, because the segment in cache may has something wrong during
66
            // load index or create column reader.
67
            // Not merge this if logic with previous to make the logic more clear.
68
0
            if (cache_handle->pop_unhealthy_segment() == nullptr) {
69
0
                continue;
70
0
            }
71
0
        }
72
        // If the segment is not healthy, then will create a new segment and will replace the unhealthy one in SegmentCache.
73
5.47k
        segment_v2::SegmentSharedPtr segment;
74
5.47k
        RETURN_IF_ERROR(rowset->load_segment(i, &segment));
75
5.47k
        if (need_load_pk_index_and_bf) {
76
2
            RETURN_IF_ERROR(segment->load_pk_index_and_bf(index_load_stats));
77
2
        }
78
5.47k
        if (use_cache && !config::disable_segment_cache) {
79
            // memory of SegmentCache::CacheValue will be handled by SegmentCache
80
4.18k
            auto* cache_value = new SegmentCache::CacheValue(segment);
81
4.18k
            _cache_mem_usage += segment->meta_mem_usage();
82
4.18k
            _segment_cache->insert(cache_key, *cache_value, cache_handle);
83
4.18k
        } else {
84
1.29k
            cache_handle->push_segment(std::move(segment));
85
1.29k
        }
86
5.47k
    }
87
1.17k
    cache_handle->set_inited();
88
1.17k
    return Status::OK();
89
1.17k
}
90
91
9
void SegmentLoader::erase_segment(const SegmentCache::CacheKey& key) {
92
9
    _segment_cache->erase(key);
93
9
}
94
95
21.3k
void SegmentLoader::erase_segments(const RowsetId& rowset_id, int64_t num_segments) {
96
21.3k
    for (int64_t i = 0; i < num_segments; i++) {
97
9
        erase_segment(SegmentCache::CacheKey(rowset_id, i));
98
9
    }
99
21.3k
}
100
101
} // namespace doris