Coverage Report

Created: 2026-04-14 20:14

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