Coverage Report

Created: 2025-07-25 10:29

/root/doris/be/src/olap/base_tablet.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/base_tablet.h"
19
20
#include <fmt/format.h>
21
22
#include <algorithm>
23
24
#include "olap/cumulative_compaction_time_series_policy.h"
25
#include "olap/tablet_fwd.h"
26
#include "olap/tablet_schema_cache.h"
27
#include "util/doris_metrics.h"
28
#include "vec/common/schema_util.h"
29
30
namespace doris {
31
using namespace ErrorCode;
32
33
extern MetricPrototype METRIC_query_scan_bytes;
34
extern MetricPrototype METRIC_query_scan_rows;
35
extern MetricPrototype METRIC_query_scan_count;
36
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(flush_bytes, MetricUnit::BYTES);
37
DEFINE_COUNTER_METRIC_PROTOTYPE_2ARG(flush_finish_count, MetricUnit::OPERATIONS);
38
39
static bvar::Adder<size_t> g_total_tablet_num("doris_total_tablet_num");
40
41
502
BaseTablet::BaseTablet(TabletMetaSharedPtr tablet_meta) : _tablet_meta(std::move(tablet_meta)) {
42
502
    _metric_entity = DorisMetrics::instance()->metric_registry()->register_entity(
43
502
            fmt::format("Tablet.{}", tablet_id()), {{"tablet_id", std::to_string(tablet_id())}},
44
502
            MetricEntityType::kTablet);
45
502
    INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_bytes);
46
502
    INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_rows);
47
502
    INT_COUNTER_METRIC_REGISTER(_metric_entity, query_scan_count);
48
502
    INT_COUNTER_METRIC_REGISTER(_metric_entity, flush_bytes);
49
502
    INT_COUNTER_METRIC_REGISTER(_metric_entity, flush_finish_count);
50
502
    g_total_tablet_num << 1;
51
502
}
52
53
501
BaseTablet::~BaseTablet() {
54
501
    DorisMetrics::instance()->metric_registry()->deregister_entity(_metric_entity);
55
501
    g_total_tablet_num << -1;
56
501
}
57
58
251
Status BaseTablet::set_tablet_state(TabletState state) {
59
251
    if (_tablet_meta->tablet_state() == TABLET_SHUTDOWN && state != TABLET_SHUTDOWN) {
60
0
        return Status::Error<META_INVALID_ARGUMENT>(
61
0
                "could not change tablet state from shutdown to {}", state);
62
0
    }
63
251
    _tablet_meta->set_tablet_state(state);
64
251
    return Status::OK();
65
251
}
66
67
0
void BaseTablet::update_max_version_schema(const TabletSchemaSPtr& tablet_schema) {
68
0
    std::lock_guard wrlock(_meta_lock);
69
    // Double Check for concurrent update
70
0
    if (!_max_version_schema ||
71
0
        tablet_schema->schema_version() > _max_version_schema->schema_version()) {
72
0
        _max_version_schema = tablet_schema;
73
0
    }
74
0
}
75
76
0
Status BaseTablet::update_by_least_common_schema(const TabletSchemaSPtr& update_schema) {
77
0
    std::lock_guard wrlock(_meta_lock);
78
0
    CHECK(_max_version_schema->schema_version() >= update_schema->schema_version());
79
0
    TabletSchemaSPtr final_schema;
80
0
    bool check_column_size = true;
81
0
    RETURN_IF_ERROR(vectorized::schema_util::get_least_common_schema(
82
0
            {_max_version_schema, update_schema}, _max_version_schema, final_schema,
83
0
            check_column_size));
84
0
    _max_version_schema = final_schema;
85
0
    VLOG_DEBUG << "dump updated tablet schema: " << final_schema->dump_full_schema();
86
0
    return Status::OK();
87
0
}
88
89
0
uint32_t BaseTablet::get_real_compaction_score() const {
90
0
    const auto& rs_metas = _tablet_meta->all_rs_metas();
91
0
    return std::accumulate(rs_metas.begin(), rs_metas.end(), 0,
92
0
                           [](uint32_t score, const RowsetMetaSharedPtr& rs_meta) {
93
0
                               return score + rs_meta->get_compaction_score();
94
0
                           });
95
0
}
96
97
25
int32_t BaseTablet::max_version_config() {
98
25
    int32_t max_version = tablet_meta()->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY
99
25
                                  ? std::max(config::time_series_max_tablet_version_num,
100
0
                                             config::max_tablet_version_num)
101
25
                                  : config::max_tablet_version_num;
102
25
    return max_version;
103
25
}
104
105
} // namespace doris