Coverage Report

Created: 2025-04-21 12:08

/root/doris/be/src/olap/full_compaction.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/full_compaction.h"
19
20
#include <glog/logging.h>
21
#include <time.h>
22
23
#include <memory>
24
#include <mutex>
25
#include <ostream>
26
#include <shared_mutex>
27
28
#include "common/config.h"
29
#include "common/status.h"
30
#include "olap/cumulative_compaction_policy.h"
31
#include "olap/olap_common.h"
32
#include "olap/olap_define.h"
33
#include "olap/rowset/beta_rowset.h"
34
#include "olap/rowset/rowset.h"
35
#include "olap/schema_change.h"
36
#include "olap/tablet_meta.h"
37
#include "runtime/thread_context.h"
38
#include "util/debug_points.h"
39
#include "util/thread.h"
40
#include "util/trace.h"
41
42
namespace doris {
43
using namespace ErrorCode;
44
45
FullCompaction::FullCompaction(const TabletSharedPtr& tablet)
46
0
        : Compaction(tablet, "FullCompaction:" + std::to_string(tablet->tablet_id())) {}
47
48
0
FullCompaction::~FullCompaction() {
49
0
    _tablet->set_is_full_compaction_running(false);
50
0
}
51
52
0
Status FullCompaction::prepare_compact() {
53
0
    if (!_tablet->init_succeeded()) {
54
0
        return Status::Error<INVALID_ARGUMENT, false>("Full compaction init failed");
55
0
    }
56
57
0
    std::unique_lock base_lock(_tablet->get_base_compaction_lock());
58
0
    std::unique_lock cumu_lock(_tablet->get_cumulative_compaction_lock());
59
0
    _tablet->set_is_full_compaction_running(true);
60
61
0
    DBUG_EXECUTE_IF("FullCompaction.prepare_compact.set_cumu_point", {
62
0
        _tablet->set_cumulative_layer_point(_tablet->max_version_unlocked().second + 1);
63
0
    })
64
65
    // 1. pick rowsets to compact
66
0
    RETURN_IF_ERROR(pick_rowsets_to_compact());
67
68
0
    return Status::OK();
69
0
}
70
71
0
Status FullCompaction::execute_compact_impl() {
72
0
    std::unique_lock base_lock(_tablet->get_base_compaction_lock());
73
0
    std::unique_lock cumu_lock(_tablet->get_cumulative_compaction_lock());
74
75
0
    SCOPED_ATTACH_TASK(_mem_tracker);
76
77
    // 2. do full compaction, merge rowsets
78
0
    int64_t permits = get_compaction_permits();
79
0
    RETURN_IF_ERROR(do_compaction(permits));
80
81
    // 3. set state to success
82
0
    _state = CompactionState::SUCCESS;
83
84
    // 4. set cumulative level
85
0
    _tablet->cumulative_compaction_policy()->update_compaction_level(_tablet.get(), _input_rowsets,
86
0
                                                                     _output_rowset);
87
88
    // 5. set cumulative point
89
0
    Version last_version = _input_rowsets.back()->version();
90
0
    _tablet->cumulative_compaction_policy()->update_cumulative_point(_tablet.get(), _input_rowsets,
91
0
                                                                     _output_rowset, last_version);
92
0
    VLOG_CRITICAL << "after cumulative compaction, current cumulative point is "
93
0
                  << _tablet->cumulative_layer_point() << ", tablet=" << _tablet->tablet_id();
94
95
0
    return Status::OK();
96
0
}
97
98
0
Status FullCompaction::pick_rowsets_to_compact() {
99
0
    _input_rowsets = _tablet->pick_candidate_rowsets_to_full_compaction();
100
0
    RETURN_IF_ERROR(check_version_continuity(_input_rowsets));
101
0
    RETURN_IF_ERROR(_check_all_version(_input_rowsets));
102
0
    if (_input_rowsets.size() <= 1) {
103
0
        return Status::Error<FULL_NO_SUITABLE_VERSION>("There is no suitable version");
104
0
    }
105
106
0
    if (_input_rowsets.size() == 2 && _input_rowsets[0]->end_version() == 1) {
107
        // the tablet is with rowset: [0-1], [2-y]
108
        // and [0-1] has no data. in this situation, no need to do full compaction.
109
0
        return Status::Error<FULL_NO_SUITABLE_VERSION>("There is no suitable version");
110
0
    }
111
112
0
    return Status::OK();
113
0
}
114
115
0
Status FullCompaction::modify_rowsets(const Merger::Statistics* stats) {
116
0
    std::vector<RowsetSharedPtr> output_rowsets {_output_rowset};
117
0
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
118
0
        _tablet->enable_unique_key_merge_on_write()) {
119
0
        std::vector<RowsetSharedPtr> tmp_rowsets {};
120
121
        // tablet is under alter process. The delete bitmap will be calculated after conversion.
122
0
        if (_tablet->tablet_state() == TABLET_NOTREADY) {
123
0
            LOG(INFO) << "tablet is under alter process, update delete bitmap later, tablet_id="
124
0
                      << _tablet->tablet_id();
125
0
            return Status::OK();
126
0
        }
127
128
0
        int64_t max_version = _tablet->max_version().second;
129
0
        DCHECK(max_version >= _output_rowset->version().second);
130
0
        if (max_version > _output_rowset->version().second) {
131
0
            RETURN_IF_ERROR(_tablet->capture_consistent_rowsets(
132
0
                    {_output_rowset->version().second + 1, max_version}, &tmp_rowsets));
133
0
        }
134
135
0
        for (const auto& it : tmp_rowsets) {
136
0
            const int64_t& cur_version = it->rowset_meta()->start_version();
137
0
            RETURN_IF_ERROR(_full_compaction_calc_delete_bitmap(it, _output_rowset, cur_version,
138
0
                                                                _output_rs_writer.get()));
139
0
        }
140
0
        DBUG_EXECUTE_IF("FullCompaction.modify_rowsets.before.block", DBUG_BLOCK);
141
0
        std::lock_guard rowset_update_lock(_tablet->get_rowset_update_lock());
142
0
        std::lock_guard header_lock(_tablet->get_header_lock());
143
0
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
144
0
        for (const auto& it : _tablet->rowset_map()) {
145
0
            const int64_t& cur_version = it.first.first;
146
0
            const RowsetSharedPtr& published_rowset = it.second;
147
0
            if (cur_version > max_version) {
148
0
                RETURN_IF_ERROR(_full_compaction_calc_delete_bitmap(
149
0
                        published_rowset, _output_rowset, cur_version, _output_rs_writer.get()));
150
0
            }
151
0
        }
152
0
        RETURN_IF_ERROR(_tablet->modify_rowsets(output_rowsets, _input_rowsets, true));
153
0
        DBUG_EXECUTE_IF("FullCompaction.modify_rowsets.sleep", { sleep(5); })
154
0
        _tablet->save_meta();
155
0
    } else {
156
0
        std::lock_guard<std::mutex> rowset_update_wlock(_tablet->get_rowset_update_lock());
157
0
        std::lock_guard<std::shared_mutex> meta_wlock(_tablet->get_header_lock());
158
0
        RETURN_IF_ERROR(_tablet->modify_rowsets(output_rowsets, _input_rowsets, true));
159
0
        DBUG_EXECUTE_IF("FullCompaction.modify_rowsets.sleep", { sleep(5); })
160
0
        _tablet->save_meta();
161
0
    }
162
0
    return Status::OK();
163
0
}
164
165
0
Status FullCompaction::_check_all_version(const std::vector<RowsetSharedPtr>& rowsets) {
166
0
    if (rowsets.empty()) {
167
0
        return Status::Error<FULL_MISS_VERSION, false>(
168
0
                "There is no input rowset when do full compaction");
169
0
    }
170
0
    const RowsetSharedPtr& last_rowset = rowsets.back();
171
0
    const RowsetSharedPtr& first_rowset = rowsets.front();
172
0
    if (last_rowset->version() != _tablet->max_version() || first_rowset->version().first != 0) {
173
0
        return Status::Error<FULL_MISS_VERSION, false>(
174
0
                "Full compaction rowsets' versions not equal to all exist rowsets' versions. "
175
0
                "full compaction rowsets max version={}-{}"
176
0
                ", current rowsets max version={}-{}"
177
0
                ", full compaction rowsets min version={}-{}, current rowsets min version=0-1",
178
0
                last_rowset->start_version(), last_rowset->end_version(),
179
0
                _tablet->max_version().first, _tablet->max_version().second,
180
0
                first_rowset->start_version(), first_rowset->end_version());
181
0
    }
182
0
    return Status::OK();
183
0
}
184
185
Status FullCompaction::_full_compaction_calc_delete_bitmap(const RowsetSharedPtr& published_rowset,
186
                                                           const RowsetSharedPtr& rowset,
187
                                                           int64_t cur_version,
188
0
                                                           RowsetWriter* rowset_writer) {
189
0
    std::vector<segment_v2::SegmentSharedPtr> segments;
190
0
    RETURN_IF_ERROR(
191
0
            std::static_pointer_cast<BetaRowset>(published_rowset)->load_segments(&segments));
192
0
    DeleteBitmapPtr delete_bitmap =
193
0
            std::make_shared<DeleteBitmap>(_tablet->tablet_meta()->tablet_id());
194
0
    std::vector<RowsetSharedPtr> specified_rowsets {rowset};
195
196
0
    OlapStopWatch watch;
197
0
    RETURN_IF_ERROR(_tablet->calc_delete_bitmap(published_rowset, segments, specified_rowsets,
198
0
                                                delete_bitmap, cur_version, nullptr,
199
0
                                                rowset_writer));
200
0
    size_t total_rows = std::accumulate(
201
0
            segments.begin(), segments.end(), 0,
202
0
            [](size_t sum, const segment_v2::SegmentSharedPtr& s) { return sum += s->num_rows(); });
203
0
    VLOG_DEBUG << "[Full compaction] construct delete bitmap tablet: " << _tablet->tablet_id()
204
0
               << ", published rowset version: [" << published_rowset->version().first << "-"
205
0
               << published_rowset->version().second << "]"
206
0
               << ", full compaction rowset version: [" << rowset->version().first << "-"
207
0
               << rowset->version().second << "]"
208
0
               << ", cost: " << watch.get_elapse_time_us() << "(us), total rows: " << total_rows;
209
210
0
    for (const auto& [k, v] : delete_bitmap->delete_bitmap) {
211
0
        if (std::get<1>(k) != DeleteBitmap::INVALID_SEGMENT_ID) {
212
0
            _tablet->tablet_meta()->delete_bitmap().merge(
213
0
                    {std::get<0>(k), std::get<1>(k), cur_version}, v);
214
0
        }
215
0
    }
216
0
    return Status::OK();
217
0
}
218
219
} // namespace doris