Coverage Report

Created: 2024-11-21 12:31

/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 "util/trace.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, RowsetMetaSharedPtr rowset_meta,
33
               std::string tablet_path)
34
        : _rowset_meta(std::move(rowset_meta)),
35
          _tablet_path(std::move(tablet_path)),
36
1.69k
          _refs_by_reader(0) {
37
#ifndef BE_TEST
38
    DCHECK(!is_local() || !_tablet_path.empty()); // local rowset MUST has tablet path
39
#endif
40
41
1.69k
    _is_pending = true;
42
43
    // Generally speaking, as long as a rowset has a version, it can be considered not to be in a pending state.
44
    // However, if the rowset was created through ingesting binlogs, it will have a version but should still be
45
    // considered in a pending state because the ingesting txn has not yet been committed.
46
1.69k
    if (_rowset_meta->has_version() && _rowset_meta->start_version() > 0 &&
47
1.69k
        _rowset_meta->rowset_state() != COMMITTED) {
48
1.18k
        _is_pending = false;
49
1.18k
    }
50
51
1.69k
    if (_is_pending) {
52
509
        _is_cumulative = false;
53
1.18k
    } else {
54
1.18k
        Version version = _rowset_meta->version();
55
1.18k
        _is_cumulative = version.first != version.second;
56
1.18k
    }
57
    // build schema from RowsetMeta.tablet_schema or Tablet.tablet_schema
58
1.69k
    _schema = _rowset_meta->tablet_schema() ? _rowset_meta->tablet_schema() : schema;
59
1.69k
    g_total_rowset_num << 1;
60
1.69k
}
61
62
1.69k
Rowset::~Rowset() {
63
1.69k
    g_total_rowset_num << -1;
64
1.69k
}
65
66
534
Status Rowset::load(bool use_cache) {
67
    // if the state is ROWSET_UNLOADING it means close() is called
68
    // and the rowset is already loaded, and the resource is not closed yet.
69
534
    if (_rowset_state_machine.rowset_state() == ROWSET_LOADED) {
70
136
        return Status::OK();
71
136
    }
72
398
    {
73
        // before lock, if rowset state is ROWSET_UNLOADING, maybe it is doing do_close in release
74
398
        std::lock_guard load_lock(_lock);
75
        // after lock, if rowset state is ROWSET_UNLOADING, it is ok to return
76
398
        if (_rowset_state_machine.rowset_state() == ROWSET_UNLOADED) {
77
            // first do load, then change the state
78
398
            RETURN_IF_ERROR(do_load(use_cache));
79
398
            RETURN_IF_ERROR(_rowset_state_machine.on_load());
80
398
        }
81
398
    }
82
    // load is done
83
398
    VLOG_CRITICAL << "rowset is loaded. " << rowset_id()
84
0
                  << ", rowset version:" << rowset_meta()->version()
85
0
                  << ", state from ROWSET_UNLOADED to ROWSET_LOADED. tabletid:"
86
0
                  << _rowset_meta->tablet_id();
87
398
    return Status::OK();
88
398
}
89
90
12
void Rowset::make_visible(Version version) {
91
12
    _is_pending = false;
92
12
    _rowset_meta->set_version(version);
93
12
    _rowset_meta->set_rowset_state(VISIBLE);
94
    // update create time to the visible time,
95
    // it's used to skip recently published version during compaction
96
12
    _rowset_meta->set_creation_time(UnixSeconds());
97
98
12
    if (_rowset_meta->has_delete_predicate()) {
99
0
        _rowset_meta->mutable_delete_predicate()->set_version(version.first);
100
0
    }
101
12
}
102
103
0
void Rowset::set_version(Version version) {
104
0
    _rowset_meta->set_version(version);
105
0
}
106
107
0
bool Rowset::check_rowset_segment() {
108
0
    std::lock_guard load_lock(_lock);
109
0
    return check_current_rowset_segment();
110
0
}
111
112
0
std::string Rowset::get_rowset_info_str() {
113
0
    std::string disk_size = PrettyPrinter::print(
114
0
            static_cast<uint64_t>(_rowset_meta->total_disk_size()), TUnit::BYTES);
115
0
    return fmt::format("[{}-{}] {} {} {} {} {}", start_version(), end_version(), num_segments(),
116
0
                       _rowset_meta->has_delete_predicate() ? "DELETE" : "DATA",
117
0
                       SegmentsOverlapPB_Name(_rowset_meta->segments_overlap()),
118
0
                       rowset_id().to_string(), disk_size);
119
0
}
120
121
634
void Rowset::clear_cache() {
122
634
    {
123
634
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
124
634
        SegmentLoader::instance()->erase_segments(rowset_id(), num_segments());
125
634
    }
126
634
    {
127
634
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(std::chrono::seconds(1));
128
634
        clear_inverted_index_cache();
129
634
    }
130
634
}
131
132
5.01k
Result<std::string> Rowset::segment_path(int64_t seg_id) {
133
5.01k
    if (is_local()) {
134
5.00k
        return local_segment_path(_tablet_path, _rowset_meta->rowset_id().to_string(), seg_id);
135
5.00k
    }
136
137
4
    return _rowset_meta->remote_storage_resource().transform([=, this](auto&& storage_resource) {
138
4
        return storage_resource->remote_segment_path(_rowset_meta->tablet_id(),
139
4
                                                     _rowset_meta->rowset_id().to_string(), seg_id);
140
4
    });
141
5.01k
}
142
143
1
Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets) {
144
1
    if (rowsets.size() < 2) {
145
0
        return Status::OK();
146
0
    }
147
1
    auto prev = rowsets.begin();
148
24
    for (auto it = rowsets.begin() + 1; it != rowsets.end(); ++it) {
149
23
        if ((*prev)->end_version() + 1 != (*it)->start_version()) {
150
0
            return Status::InternalError("versions are not continuity: prev={} cur={}",
151
0
                                         (*prev)->version().to_string(),
152
0
                                         (*it)->version().to_string());
153
0
        }
154
23
        prev = it;
155
23
    }
156
1
    return Status::OK();
157
1
}
158
159
0
void Rowset::merge_rowset_meta(const RowsetMeta& other) {
160
0
    _rowset_meta->merge_rowset_meta(other);
161
    // rowset->meta_meta()->tablet_schema() maybe updated so make sure _schema is
162
    // consistent with rowset meta
163
0
    _schema = _rowset_meta->tablet_schema();
164
0
}
165
166
} // namespace doris