Coverage Report

Created: 2026-03-14 20:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/information_schema/schema_tablets_scanner.cpp
Line
Count
Source
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 "information_schema/schema_tablets_scanner.h"
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/olap_common.pb.h>
22
23
#include <algorithm>
24
#include <cstddef>
25
#include <cstdint>
26
#include <memory>
27
#include <numeric>
28
#include <string>
29
#include <utility>
30
31
#include "cloud/cloud_storage_engine.h"
32
#include "cloud/cloud_tablet.h"
33
#include "cloud/cloud_tablet_mgr.h"
34
#include "cloud/config.h"
35
#include "common/status.h"
36
#include "core/data_type/define_primitive_type.h"
37
#include "core/string_ref.h"
38
#include "information_schema/schema_scanner.h"
39
#include "information_schema/schema_scanner_helper.h"
40
#include "runtime/exec_env.h"
41
#include "runtime/runtime_state.h"
42
#include "storage/storage_engine.h"
43
#include "storage/tablet/tablet_fwd.h"
44
#include "storage/tablet/tablet_manager.h"
45
46
namespace doris {
47
class Block;
48
49
#include "common/compile_check_begin.h"
50
51
std::vector<SchemaScanner::ColumnDesc> SchemaTabletsScanner::_s_tbls_columns = {
52
        //   name,       type,          size,     is_null
53
        {"BE_ID", TYPE_BIGINT, sizeof(int64_t), true},
54
        {"TABLET_ID", TYPE_BIGINT, sizeof(int64_t), true},
55
        {"REPLICA_ID", TYPE_BIGINT, sizeof(int64_t), true},
56
        {"PARTITION_ID", TYPE_BIGINT, sizeof(int64_t), true},
57
        {"TABLET_PATH", TYPE_STRING, sizeof(StringRef), true},
58
        {"TABLET_LOCAL_SIZE", TYPE_BIGINT, sizeof(int64_t), true},
59
        {"TABLET_REMOTE_SIZE", TYPE_BIGINT, sizeof(int64_t), true},
60
        {"VERSION_COUNT", TYPE_BIGINT, sizeof(int64_t), true},
61
        {"SEGMENT_COUNT", TYPE_BIGINT, sizeof(int64_t), true},
62
        {"NUM_COLUMNS", TYPE_BIGINT, sizeof(int64_t), true},
63
        {"ROW_SIZE", TYPE_BIGINT, sizeof(int64_t), true},
64
        {"COMPACTION_SCORE", TYPE_INT, sizeof(int32_t), true},
65
        {"COMPRESS_KIND", TYPE_STRING, sizeof(StringRef), true},
66
        {"IS_USED", TYPE_BOOLEAN, sizeof(bool), true},
67
        {"IS_ALTER_FAILED", TYPE_BOOLEAN, sizeof(bool), true},
68
        {"CREATE_TIME", TYPE_DATETIME, sizeof(int64_t), true},
69
        {"UPDATE_TIME", TYPE_DATETIME, sizeof(int64_t), true},
70
        {"IS_OVERLAP", TYPE_BOOLEAN, sizeof(bool), true},
71
};
72
73
SchemaTabletsScanner::SchemaTabletsScanner()
74
0
        : SchemaScanner(_s_tbls_columns, TSchemaTableType::SCH_BACKEND_TABLETS) {};
75
76
0
Status SchemaTabletsScanner::start(RuntimeState* state) {
77
0
    if (!_is_init) {
78
0
        return Status::InternalError("used before initialized.");
79
0
    }
80
0
    _backend_id = state->backend_id();
81
0
    RETURN_IF_ERROR(_get_all_tablets());
82
0
    return Status::OK();
83
0
}
84
85
0
Status SchemaTabletsScanner::_get_all_tablets() {
86
0
    if (config::is_cloud_mode()) {
87
0
        auto tablets =
88
0
                ExecEnv::GetInstance()->storage_engine().to_cloud().tablet_mgr().get_all_tablet();
89
0
        std::ranges::for_each(tablets, [&](auto& tablet) {
90
0
            _tablets.push_back(std::static_pointer_cast<BaseTablet>(tablet));
91
0
        });
92
0
    } else {
93
0
        auto tablets = ExecEnv::GetInstance()
94
0
                               ->storage_engine()
95
0
                               .to_local()
96
0
                               .tablet_manager()
97
0
                               ->get_all_tablet();
98
0
        std::ranges::for_each(tablets, [&](auto& tablet) {
99
0
            _tablets.push_back(std::static_pointer_cast<BaseTablet>(tablet));
100
0
        });
101
0
    }
102
0
    return Status::OK();
103
0
}
104
105
0
Status SchemaTabletsScanner::get_next_block_internal(Block* block, bool* eos) {
106
0
    if (!_is_init) {
107
0
        return Status::InternalError("Used before initialized.");
108
0
    }
109
0
    if (nullptr == block || nullptr == eos) {
110
0
        return Status::InternalError("input pointer is nullptr.");
111
0
    }
112
0
    *eos = true;
113
0
    return _fill_block_impl(block);
114
0
}
115
116
0
Status SchemaTabletsScanner::_fill_block_impl(Block* block) {
117
0
    SCOPED_TIMER(_fill_block_timer);
118
119
0
    size_t row_num = _tablets.size();
120
0
    if (row_num == 0) {
121
0
        return Status::OK();
122
0
    }
123
124
0
    size_t fill_tablets_num = _tablets.size();
125
0
    std::vector<void*> datas(fill_tablets_num);
126
127
0
    for (int i = 0; i < _tablets.size(); i++) {
128
0
        BaseTabletSPtr tablet = _tablets[i];
129
        // BE_ID
130
0
        SchemaScannerHelper::insert_int64_value(0, _backend_id, block);
131
132
        // TABLET_ID
133
0
        SchemaScannerHelper::insert_int64_value(1, tablet->tablet_meta()->tablet_id(), block);
134
135
        // REPLICA_ID
136
0
        SchemaScannerHelper::insert_int64_value(2, tablet->tablet_meta()->replica_id(), block);
137
138
        // PARTITION_ID
139
0
        SchemaScannerHelper::insert_int64_value(3, tablet->tablet_meta()->partition_id(), block);
140
141
        // TABLET_PATH
142
0
        SchemaScannerHelper::insert_string_value(4, tablet->tablet_path(), block);
143
144
        // TABLET_LOCAL_SIZE
145
0
        SchemaScannerHelper::insert_int64_value(5, tablet->tablet_meta()->tablet_local_size(),
146
0
                                                block);
147
148
        // TABLET_REMOTE_SIZE
149
0
        SchemaScannerHelper::insert_int64_value(6, tablet->tablet_meta()->tablet_remote_size(),
150
0
                                                block);
151
152
        // VERSION_COUNT
153
0
        SchemaScannerHelper::insert_int64_value(
154
0
                7, static_cast<int64_t>(tablet->tablet_meta()->version_count()), block);
155
156
        // SEGMENT_COUNT
157
0
        SchemaScannerHelper::insert_int64_value(
158
0
                8,
159
0
                [&tablet]() {
160
0
                    auto rs_metas = tablet->tablet_meta()->all_rs_metas();
161
0
                    return std::accumulate(rs_metas.begin(), rs_metas.end(), 0,
162
0
                                           [](int64_t val, const auto& it) {
163
0
                                               return val + it.second->num_segments();
164
0
                                           });
165
0
                }(),
166
0
                block);
167
168
        // NUM_COLUMNS
169
0
        SchemaScannerHelper::insert_int64_value(9, tablet->tablet_meta()->tablet_columns_num(),
170
0
                                                block);
171
172
        // ROW_SIZE
173
0
        SchemaScannerHelper::insert_int64_value(10, static_cast<int64_t>(tablet->row_size()),
174
0
                                                block);
175
176
        // COMPACTION_SCORE
177
0
        SchemaScannerHelper::insert_int32_value(11, tablet->get_real_compaction_score(), block);
178
179
        // COMPRESS_KIND
180
0
        SchemaScannerHelper::insert_string_value(12, CompressKind_Name(tablet->compress_kind()),
181
0
                                                 block);
182
183
        // IS_USED
184
0
        SchemaScannerHelper::insert_bool_value(
185
0
                13,
186
0
                [&tablet]() {
187
0
                    if (config::is_cloud_mode()) {
188
0
                        return true;
189
0
                    }
190
0
                    return std::static_pointer_cast<Tablet>(tablet)->is_used();
191
0
                }(),
192
0
                block);
193
194
        // IS_ALTER_FAILED
195
0
        SchemaScannerHelper::insert_bool_value(14, tablet->is_alter_failed(), block);
196
197
        // CREATE_TIME
198
0
        SchemaScannerHelper::insert_datetime_value(15, tablet->tablet_meta()->creation_time(),
199
0
                                                   _timezone_obj, block);
200
201
        // UPDATE_TIME
202
0
        SchemaScannerHelper::insert_datetime_value(
203
0
                16,
204
0
                [&tablet]() {
205
0
                    auto rowset = tablet->get_rowset_with_max_version();
206
0
                    return rowset == nullptr ? 0 : rowset->newest_write_timestamp();
207
0
                }(),
208
0
                _timezone_obj, block);
209
210
        // IS_OVERLAP
211
0
        SchemaScannerHelper::insert_bool_value(
212
0
                17,
213
0
                [&tablet]() {
214
0
                    const auto& rs_metas = tablet->tablet_meta()->all_rs_metas();
215
0
                    return std::any_of(rs_metas.begin(), rs_metas.end(), [](const auto& it) {
216
0
                        return it.second->is_segments_overlapping();
217
0
                    });
218
0
                }(),
219
0
                block);
220
0
    }
221
222
0
    return Status::OK();
223
0
}
224
} // namespace doris