Coverage Report

Created: 2024-11-20 15:53

/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 <butil/macros.h>
21
#include <stdint.h>
22
23
#include <memory>
24
#include <string>
25
#include <vector>
26
27
#include "common/status.h"
28
#include "io/io_common.h"
29
#include "olap/merger.h"
30
#include "olap/olap_common.h"
31
#include "olap/rowid_conversion.h"
32
#include "olap/rowset/pending_rowset_helper.h"
33
#include "olap/rowset/rowset.h"
34
#include "olap/rowset/rowset_reader.h"
35
#include "olap/tablet.h"
36
#include "olap/tablet_schema.h"
37
38
namespace doris {
39
40
class MemTrackerLimiter;
41
class RowsetWriter;
42
43
// This class is a base class for compaction.
44
// The entrance of this class is compact()
45
// Any compaction should go through four procedures.
46
//  1. pick rowsets satisfied to compact
47
//  2. do compaction
48
//  3. modify rowsets
49
//  4. gc output rowset if failed
50
class Compaction {
51
public:
52
    Compaction(const TabletSharedPtr& tablet, const std::string& label);
53
    virtual ~Compaction();
54
55
    // This is only for http CompactionAction
56
    Status compact();
57
58
    virtual Status prepare_compact() = 0;
59
    Status execute_compact();
60
    virtual Status execute_compact_impl() = 0;
61
62
10
    const std::vector<RowsetSharedPtr>& input_rowsets() { return _input_rowsets; }
63
#ifdef BE_TEST
64
    void set_input_rowset(const std::vector<RowsetSharedPtr>& rowsets);
65
    RowsetSharedPtr output_rowset();
66
#endif
67
68
0
    RuntimeProfile* runtime_profile() const { return _profile.get(); }
69
70
    virtual ReaderType compaction_type() const = 0;
71
    virtual std::string compaction_name() const = 0;
72
73
protected:
74
    virtual Status pick_rowsets_to_compact() = 0;
75
76
    Status do_compaction(int64_t permits);
77
    Status do_compaction_impl(int64_t permits);
78
79
    virtual Status modify_rowsets(const Merger::Statistics* stats = nullptr);
80
    void gc_output_rowset();
81
82
    Status construct_output_rowset_writer(RowsetWriterContext& ctx, bool is_vertical = false);
83
    Status construct_input_rowset_readers();
84
85
    Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets);
86
    Status check_correctness(const Merger::Statistics& stats);
87
    Status find_longest_consecutive_version(std::vector<RowsetSharedPtr>* rowsets,
88
                                            std::vector<Version>* missing_version);
89
    int64_t get_compaction_permits();
90
91
    bool should_vertical_compaction();
92
    int64_t get_avg_segment_rows();
93
94
    // Return true if do ordered data compaction successfully
95
    bool handle_ordered_data_compaction();
96
    Status do_compact_ordered_rowsets();
97
    bool is_rowset_tidy(std::string& pre_max_key, const RowsetSharedPtr& rhs);
98
    void build_basic_info();
99
100
    void init_profile(const std::string& label);
101
11
    [[nodiscard]] bool allow_delete_in_cumu_compaction() const {
102
11
        return _allow_delete_in_cumu_compaction;
103
11
    }
104
105
private:
106
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
107
    void _load_segment_to_cache();
108
    int64_t merge_way_num();
109
110
protected:
111
    // the root tracker for this compaction
112
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
113
114
    TabletSharedPtr _tablet;
115
116
    std::vector<RowsetSharedPtr> _input_rowsets;
117
    std::vector<RowsetReaderSharedPtr> _input_rs_readers;
118
    int64_t _input_rowsets_size;
119
    int64_t _input_row_num;
120
    int64_t _input_num_segments;
121
    int64_t _input_index_size;
122
123
    RowsetSharedPtr _output_rowset;
124
    PendingRowsetGuard _pending_rs_guard;
125
    std::unique_ptr<RowsetWriter> _output_rs_writer;
126
127
    enum CompactionState { INITED = 0, SUCCESS = 1 };
128
    CompactionState _state;
129
130
    Version _output_version;
131
132
    int64_t _newest_write_timestamp;
133
    RowIdConversion _rowid_conversion;
134
    TabletSchemaSPtr _cur_tablet_schema;
135
136
    std::unique_ptr<RuntimeProfile> _profile;
137
    bool _allow_delete_in_cumu_compaction = false;
138
139
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
140
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
141
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
142
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
143
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
144
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
145
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
146
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
147
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
148
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
149
150
    DISALLOW_COPY_AND_ASSIGN(Compaction);
151
};
152
153
} // namespace doris