Coverage Report

Created: 2024-11-22 21:49

/root/doris/be/src/olap/compaction.h
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
#pragma once
19
20
#include <cstdint>
21
#include <memory>
22
#include <string>
23
#include <vector>
24
25
#include "cloud/cloud_tablet.h"
26
#include "common/status.h"
27
#include "io/io_common.h"
28
#include "olap/merger.h"
29
#include "olap/olap_common.h"
30
#include "olap/rowid_conversion.h"
31
#include "olap/rowset/pending_rowset_helper.h"
32
#include "olap/rowset/rowset_fwd.h"
33
#include "olap/tablet_fwd.h"
34
#include "util/runtime_profile.h"
35
36
namespace doris {
37
38
class MemTrackerLimiter;
39
class RowsetWriter;
40
struct RowsetWriterContext;
41
class StorageEngine;
42
class CloudStorageEngine;
43
44
// This class is a base class for compaction.
45
// The entrance of this class is compact()
46
// Any compaction should go through four procedures.
47
//  1. pick rowsets satisfied to compact
48
//  2. do compaction
49
//  3. modify rowsets
50
//  4. gc output rowset if failed
51
class Compaction {
52
public:
53
    Compaction(BaseTabletSPtr tablet, const std::string& label);
54
    virtual ~Compaction();
55
56
    // Pick input rowsets satisfied to compact
57
    virtual Status prepare_compact() = 0;
58
59
    // Merge input rowsets to output rowset and modify tablet meta
60
    virtual Status execute_compact() = 0;
61
62
0
    RuntimeProfile* runtime_profile() const { return _profile.get(); }
63
64
    virtual ReaderType compaction_type() const = 0;
65
    virtual std::string_view compaction_name() const = 0;
66
67
protected:
68
    Status merge_input_rowsets();
69
70
    // merge inverted index files
71
    Status do_inverted_index_compaction();
72
73
    void construct_index_compaction_columns(RowsetWriterContext& ctx);
74
75
    virtual Status construct_output_rowset_writer(RowsetWriterContext& ctx) = 0;
76
77
    Status check_correctness();
78
79
    int64_t get_avg_segment_rows();
80
81
    void init_profile(const std::string& label);
82
83
    void _load_segment_to_cache();
84
85
    int64_t merge_way_num();
86
87
    // the root tracker for this compaction
88
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
89
90
    BaseTabletSPtr _tablet;
91
92
    std::vector<RowsetSharedPtr> _input_rowsets;
93
    int64_t _input_rowsets_data_size {0};
94
    int64_t _input_rowsets_index_size {0};
95
    int64_t _input_rowsets_total_size {0};
96
    int64_t _input_row_num {0};
97
    int64_t _input_num_segments {0};
98
99
    Merger::Statistics _stats;
100
101
    RowsetSharedPtr _output_rowset;
102
    std::unique_ptr<RowsetWriter> _output_rs_writer;
103
104
    enum CompactionState : uint8_t { INITED = 0, SUCCESS = 1 };
105
    CompactionState _state {CompactionState::INITED};
106
107
    bool _is_vertical;
108
    bool _allow_delete_in_cumu_compaction;
109
110
    Version _output_version;
111
112
    int64_t _newest_write_timestamp {-1};
113
    std::unique_ptr<RowIdConversion> _rowid_conversion = nullptr;
114
    TabletSchemaSPtr _cur_tablet_schema;
115
116
    std::unique_ptr<RuntimeProfile> _profile;
117
118
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
119
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
120
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
121
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
122
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
123
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
124
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
125
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
126
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
127
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
128
};
129
130
// `StorageEngine` mixin for `Compaction`
131
class CompactionMixin : public Compaction {
132
public:
133
    CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, const std::string& label);
134
135
    ~CompactionMixin() override;
136
137
    Status execute_compact() override;
138
139
    int64_t get_compaction_permits();
140
141
protected:
142
    // Convert `_tablet` from `BaseTablet` to `Tablet`
143
    Tablet* tablet();
144
145
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
146
147
    virtual Status modify_rowsets();
148
149
    StorageEngine& _engine;
150
151
private:
152
    Status execute_compact_impl(int64_t permits);
153
154
    void build_basic_info();
155
156
    // Return true if do ordered data compaction successfully
157
    bool handle_ordered_data_compaction();
158
159
    Status do_compact_ordered_rowsets();
160
161
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
162
163
    PendingRowsetGuard _pending_rs_guard;
164
};
165
166
class CloudCompactionMixin : public Compaction {
167
public:
168
    CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
169
                         const std::string& label);
170
171
0
    ~CloudCompactionMixin() override = default;
172
173
    Status execute_compact() override;
174
175
protected:
176
0
    CloudTablet* cloud_tablet() { return static_cast<CloudTablet*>(_tablet.get()); }
177
178
    virtual void garbage_collection();
179
180
    CloudStorageEngine& _engine;
181
182
    int64_t _expiration = 0;
183
184
private:
185
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
186
187
    Status execute_compact_impl(int64_t permits);
188
189
    void build_basic_info();
190
191
    virtual Status modify_rowsets();
192
193
    int64_t get_compaction_permits();
194
};
195
196
} // namespace doris