/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.6k | SegmentLoader* SegmentLoader::instance() { | 
| 31 | 22.6k |     return ExecEnv::GetInstance()->segment_loader(); | 
| 32 | 22.6k | } | 
| 33 |  |  | 
| 34 | 6.01k | bool SegmentCache::lookup(const SegmentCache::CacheKey& key, SegmentCacheHandle* handle) { | 
| 35 | 6.01k |     auto* lru_handle = LRUCachePolicy::lookup(key.encode()); | 
| 36 | 6.01k |     if (lru_handle == nullptr) {  Branch (36:9): [True: 6.01k, False: 0]
 | 
| 37 | 6.01k |         return false; | 
| 38 | 6.01k |     } | 
| 39 | 0 |     handle->push_segment(this, lru_handle); | 
| 40 | 0 |     return true; | 
| 41 | 6.01k | } | 
| 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.28k |                                     OlapReaderStatistics* index_load_stats) { | 
| 59 | 1.28k |     if (cache_handle->is_inited()) {  Branch (59:9): [True: 0, False: 1.28k]
 | 
| 60 | 0 |         return Status::OK(); | 
| 61 | 0 |     } | 
| 62 | 7.29k |     for (int64_t i = 0; i < rowset->num_segments(); i++) {  Branch (62:25): [True: 6.01k, False: 1.28k]
 | 
| 63 | 6.01k |         SegmentCache::CacheKey cache_key(rowset->rowset_id(), i); | 
| 64 | 6.01k |         if (_segment_cache->lookup(cache_key, cache_handle)) {  Branch (64:13): [True: 0, False: 6.01k]
 | 
| 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) {  Branch (68:17): [True: 0, False: 0]
 | 
| 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 | 6.01k |         segment_v2::SegmentSharedPtr segment; | 
| 74 | 6.01k |         RETURN_IF_ERROR(rowset->load_segment(i, &segment, index_load_stats)); | Line | Count | Source |  | 637 | 6.01k |     do {                                \ |  | 638 | 6.01k |         Status _status_ = (stmt);       \ |  | 639 | 6.01k |         if (UNLIKELY(!_status_.ok())) { \| Line | Count | Source |  | 36 | 6.01k | #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)   Branch (36:24): [True: 1, False: 6.01k]
 | 
 |  | 640 | 1 |             return _status_;            \ |  | 641 | 1 |         }                               \ |  | 642 | 6.01k |     } while (false)   Branch (642:14): [Folded - Ignored]
 | 
 | 
| 75 | 6.01k |         if (need_load_pk_index_and_bf) {  Branch (75:13): [True: 2, False: 6.01k]
 | 
| 76 | 2 |             RETURN_IF_ERROR(segment->load_pk_index_and_bf(index_load_stats)); | Line | Count | Source |  | 637 | 2 |     do {                                \ |  | 638 | 2 |         Status _status_ = (stmt);       \ |  | 639 | 2 |         if (UNLIKELY(!_status_.ok())) { \| Line | Count | Source |  | 36 | 2 | #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)   Branch (36:24): [True: 0, False: 2]
 | 
 |  | 640 | 0 |             return _status_;            \ |  | 641 | 0 |         }                               \ |  | 642 | 2 |     } while (false)   Branch (642:14): [Folded - Ignored]
 | 
 | 
| 77 | 2 |         } | 
| 78 | 6.01k |         if (use_cache && !config::disable_segment_cache) {  Branch (78:13): [True: 4.18k, False: 1.82k]
  Branch (78:26): [True: 4.18k, False: 0]
 | 
| 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.82k |             cache_handle->push_segment(std::move(segment)); | 
| 85 | 1.82k |         } | 
| 86 | 6.01k |     } | 
| 87 | 1.28k |     cache_handle->set_inited(); | 
| 88 | 1.28k |     return Status::OK(); | 
| 89 | 1.28k | } | 
| 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++) {  Branch (96:25): [True: 9, False: 21.3k]
 | 
| 97 | 9 |         erase_segment(SegmentCache::CacheKey(rowset_id, i)); | 
| 98 | 9 |     } | 
| 99 | 21.3k | } | 
| 100 |  |  | 
| 101 |  | } // namespace doris |