Coverage Report

Created: 2025-03-10 22:58

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