Coverage Report

Created: 2025-03-10 19:30

/root/doris/be/src/olap/rowset/rowset.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/rowset/rowset.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
22
#include "olap/olap_define.h"
23
#include "olap/segment_loader.h"
24
#include "olap/tablet_schema.h"
25
#include "util/time.h"
26
#include "vec/common/schema_util.h"
27
28
namespace doris {
29
30
static bvar::Adder<size_t> g_total_rowset_num("doris_total_rowset_num");
31
32
Rowset::Rowset(const TabletSchemaSPtr& schema, const RowsetMetaSharedPtr& rowset_meta)
33
11.7k
        : _rowset_meta(rowset_meta), _refs_by_reader(0) {
34
11.7k
    _is_pending = true;
35
36
    // Generally speaking, as long as a rowset has a version, it can be considered not to be in a pending state.
37
    // However, if the rowset was created through ingesting binlogs, it will have a version but should still be
38
    // considered in a pending state because the ingesting txn has not yet been committed.
39
11.7k
    if (_rowset_meta->has_version() && _rowset_meta->start_version() > 0 &&
40
11.7k
        _rowset_meta->rowset_state() != COMMITTED) {
41
11.0k
        _is_pending = false;
42
11.0k
    }
43
44
11.7k
    if (_is_pending) {
45
716
        _is_cumulative = false;
46
11.0k
    } else {
47
11.0k
        Version version = _rowset_meta->version();
48
11.0k
        _is_cumulative = version.first != version.second;
49
11.0k
    }
50
    // build schema from RowsetMeta.tablet_schema or Tablet.tablet_schema
51
11.7k
    _schema = _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : schema;
52
11.7k
    g_total_rowset_num << 1;
53
11.7k
}
54
55
11.7k
Rowset::~Rowset() {
56
11.7k
    g_total_rowset_num << -1;
57
11.7k
}
58
59
532
Status Rowset::load(bool use_cache) {
60
    // if the state is ROWSET_UNLOADING it means close() is called
61
    // and the rowset is already loaded, and the resource is not closed yet.
62
532
    if (_rowset_state_machine.rowset_state() == ROWSET_LOADED) {
63
136
        return Status::OK();
64
136
    }
65
396
    {
66
        // before lock, if rowset state is ROWSET_UNLOADING, maybe it is doing do_close in release
67
396
        std::lock_guard load_lock(_lock);
68
        // after lock, if rowset state is ROWSET_UNLOADING, it is ok to return
69
396
        if (_rowset_state_machine.rowset_state() == ROWSET_UNLOADED) {
70
            // first do load, then change the state
71
396
            RETURN_IF_ERROR(do_load(use_cache));
72
396
            RETURN_IF_ERROR(_rowset_state_machine.on_load());
73
396
        }
74
396
    }
75
    // load is done
76
396
    VLOG_CRITICAL << "rowset is loaded. " << rowset_id()
77
0
                  << ", rowset version:" << rowset_meta()->version()
78
0
                  << ", state from ROWSET_UNLOADED to ROWSET_LOADED. tabletid:"
79
0
                  << _rowset_meta->tablet_id();
80
396
    return Status::OK();
81
396
}
82
83
11
void Rowset::make_visible(Version version) {
84
11
    _is_pending = false;
85
11
    _rowset_meta->set_version(version);
86
11
    _rowset_meta->set_rowset_state(VISIBLE);
87
    // update create time to the visible time,
88
    // it's used to skip recently published version during compaction
89
11
    _rowset_meta->set_creation_time(UnixSeconds());
90
91
11
    if (_rowset_meta->has_delete_predicate()) {
92
0
        _rowset_meta->mutable_delete_predicate()->set_version(version.first);
93
0
    }
94
11
}
95
96
0
bool Rowset::check_rowset_segment() {
97
0
    std::lock_guard load_lock(_lock);
98
0
    return check_current_rowset_segment();
99
0
}
100
101
0
void Rowset::merge_rowset_meta(const RowsetMetaSharedPtr& other) {
102
0
    _rowset_meta->set_num_segments(num_segments() + other->num_segments());
103
0
    _rowset_meta->set_num_rows(num_rows() + other->num_rows());
104
0
    _rowset_meta->set_data_disk_size(data_disk_size() + other->data_disk_size());
105
0
    _rowset_meta->set_total_disk_size(total_disk_size() + other->total_disk_size());
106
0
    _rowset_meta->set_index_disk_size(index_disk_size() + other->index_disk_size());
107
0
    std::vector<KeyBoundsPB> key_bounds;
108
0
    other->get_segments_key_bounds(&key_bounds);
109
0
    for (auto key_bound : key_bounds) {
110
0
        _rowset_meta->add_segment_key_bounds(key_bound);
111
0
    }
112
113
    // In partial update the rowset schema maybe updated when table contains variant type, so we need the newest schema to be updated
114
    // Otherwise the schema is stale and lead to wrong data read
115
0
    if (tablet_schema()->num_variant_columns() > 0) {
116
        // merge extracted columns
117
0
        TabletSchemaSPtr merged_schema;
118
0
        static_cast<void>(vectorized::schema_util::get_least_common_schema(
119
0
                {tablet_schema(), other->tablet_schema()}, nullptr, merged_schema));
120
0
        if (*_schema != *merged_schema) {
121
0
            _rowset_meta->set_tablet_schema(merged_schema);
122
0
        }
123
        // rowset->meta_meta()->tablet_schema() maybe updated so make sure _schema is
124
        // consistent with rowset meta
125
0
        _schema = _rowset_meta->tablet_schema();
126
0
    }
127
0
}
128
129
20.9k
void Rowset::clear_cache() {
130
20.9k
    SegmentLoader::instance()->erase_segments(rowset_id(), num_segments());
131
20.9k
    clear_inverted_index_cache();
132
20.9k
}
133
134
} // namespace doris