Coverage Report

Created: 2025-04-24 12:23

/root/doris/be/src/olap/rowset/segcompaction.h
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
#pragma once
19
20
#include <memory>
21
#include <vector>
22
23
#include "common/status.h"
24
#include "io/fs/file_reader_writer_fwd.h"
25
#include "olap/merger.h"
26
#include "olap/simple_rowid_conversion.h"
27
#include "olap/tablet.h"
28
#include "segment_v2/segment.h"
29
30
namespace doris {
31
class Schema;
32
33
namespace segment_v2 {
34
class SegmentWriter;
35
} // namespace segment_v2
36
namespace vectorized {
37
class RowSourcesBuffer;
38
class VerticalBlockReader;
39
} // namespace vectorized
40
struct OlapReaderStatistics;
41
42
using SegCompactionCandidates = std::vector<segment_v2::SegmentSharedPtr>;
43
using SegCompactionCandidatesSharedPtr = std::shared_ptr<SegCompactionCandidates>;
44
45
class BetaRowsetWriter;
46
47
class SegcompactionWorker {
48
    friend class BetaRowsetWriter;
49
50
public:
51
    explicit SegcompactionWorker(BetaRowsetWriter* writer);
52
53
703
    ~SegcompactionWorker() {
54
703
        DCHECK(_seg_compact_mem_tracker != nullptr);
55
703
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_seg_compact_mem_tracker);
56
703
        if (_rowid_conversion) {
57
3
            _rowid_conversion.reset();
58
3
        }
59
703
    }
60
61
    void compact_segments(SegCompactionCandidatesSharedPtr segments);
62
63
    bool need_convert_delete_bitmap();
64
65
    void convert_segment_delete_bitmap(DeleteBitmapPtr src_delete_bitmap, uint32_t src_seg_id,
66
                                       uint32_t dest_seg_id);
67
    void convert_segment_delete_bitmap(DeleteBitmapPtr src_delete_bitmap, uint32_t src_begin,
68
                                       uint32_t src_end, uint32_t dest_seg_id);
69
4
    DeleteBitmapPtr get_converted_delete_bitmap() { return _converted_delete_bitmap; }
70
71
714
    io::FileWriterPtr& get_file_writer() { return _file_writer; }
72
73
    // set the cancel flag, tasks already started will not be cancelled.
74
    bool cancel();
75
76
    void init_mem_tracker(int64_t txn_id);
77
78
private:
79
    Status _create_segment_writer_for_segcompaction(
80
            std::unique_ptr<segment_v2::SegmentWriter>* writer, uint32_t begin, uint32_t end);
81
    Status _get_segcompaction_reader(SegCompactionCandidatesSharedPtr segments,
82
                                     TabletSharedPtr tablet, std::shared_ptr<Schema> schema,
83
                                     OlapReaderStatistics* stat,
84
                                     vectorized::RowSourcesBuffer& row_sources_buf, bool is_key,
85
                                     std::vector<uint32_t>& return_columns,
86
                                     std::unique_ptr<vectorized::VerticalBlockReader>* reader);
87
    std::unique_ptr<segment_v2::SegmentWriter> _create_segcompaction_writer(uint32_t begin,
88
                                                                            uint32_t end);
89
    Status _delete_original_segments(uint32_t begin, uint32_t end);
90
    Status _check_correctness(OlapReaderStatistics& reader_stat, Merger::Statistics& merger_stat,
91
                              uint32_t begin, uint32_t end);
92
    Status _do_compact_segments(SegCompactionCandidatesSharedPtr segments);
93
94
private:
95
    //TODO(zhengyu): current impl depends heavily on the access to feilds of BetaRowsetWriter
96
    // Currently cloud storage engine doesn't need segcompaction
97
    BetaRowsetWriter* _writer = nullptr;
98
    io::FileWriterPtr _file_writer;
99
100
    // for unique key mow table
101
    std::unique_ptr<SimpleRowIdConversion> _rowid_conversion = nullptr;
102
    DeleteBitmapPtr _converted_delete_bitmap;
103
    std::shared_ptr<MemTrackerLimiter> _seg_compact_mem_tracker = nullptr;
104
105
    // the state is not mutable when 1)actual compaction operation started or 2) cancelled
106
    std::atomic<bool> _is_compacting_state_mutable = true;
107
};
108
} // namespace doris