Coverage Report

Created: 2026-10-14 21:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/delta_writer/delta_writer.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 <gen_cpp/Types_types.h>
21
#include <gen_cpp/internal_service.pb.h>
22
#include <gen_cpp/types.pb.h>
23
24
#include <atomic>
25
#include <memory>
26
#include <mutex>
27
#include <vector>
28
29
#include "common/status.h"
30
#include "load/delta_writer/delta_writer_context.h"
31
#include "load/memtable/memtable_writer.h"
32
#include "storage/olap_common.h"
33
#include "storage/rowset/rowset.h"
34
#include "storage/tablet/tablet.h"
35
#include "storage/tablet/tablet_meta.h"
36
#include "storage/tablet/tablet_schema.h"
37
#include "util/uid_util.h"
38
39
namespace doris {
40
41
class FlushToken;
42
class MemTable;
43
class StorageEngine;
44
class TupleDescriptor;
45
class SlotDescriptor;
46
class OlapTableSchemaParam;
47
class RowsetWriter;
48
49
class Block;
50
51
class BaseRowsetBuilder;
52
class RowsetBuilder;
53
54
// Writer for a particular (load, index, tablet).
55
// This class is NOT thread-safe, external synchronization is required.
56
class BaseDeltaWriter {
57
public:
58
    BaseDeltaWriter(const WriteRequest& req, RuntimeProfile* profile, const UniqueId& load_id);
59
60
    virtual ~BaseDeltaWriter();
61
62
    virtual Status write(const Block* block, const TabletAddRowsPayload& rows,
63
                         bool* memtable_flushed = nullptr) = 0;
64
65
    // flush the last memtable to flush queue, must call it before build_rowset()
66
    virtual Status close() = 0;
67
    // wait for all memtables to be flushed.
68
    // mem_consumption() should be 0 after this function returns.
69
    virtual Status build_rowset();
70
    Status submit_calc_delete_bitmap_task();
71
    Status wait_calc_delete_bitmap();
72
73
    // Abandon the current memtable and cancel queued flush tasks.
74
    // Wait for running tasks before releasing their resources.
75
    // mem_consumption() should be 0 after this function returns.
76
    Status cancel();
77
    virtual Status cancel_with_status(const Status& st);
78
79
    int64_t mem_consumption(MemType mem);
80
81
    // Wait all memtable in flush queue to be flushed
82
    Status wait_flush();
83
84
    virtual Status flush_memtable_async();
85
86
391k
    int64_t partition_id() const { return _req.partition_id; }
87
88
    int64_t table_id() const;
89
90
366k
    int64_t tablet_id() const { return _req.tablet_id; }
91
92
184k
    int64_t binlog_tablet_id() const { return _req.binlog_tablet_id; }
93
94
0
    int64_t txn_id() const { return _req.txn_id; }
95
96
184k
    int64_t total_received_rows() const { return _memtable_writer->total_received_rows(); }
97
98
    int64_t num_rows_filtered() const;
99
100
    static void collect_tablet_load_rowset_num_info(
101
            BaseTablet* tablet,
102
            google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_infos);
103
104
    void set_tablet_load_rowset_num_info(
105
            google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_info);
106
107
protected:
108
    Status _get_load_cancel_status() const;
109
110
    virtual void _init_profile(RuntimeProfile* profile);
111
112
    Status init();
113
114
    bool _is_init = false;
115
    bool _is_cancelled = false;
116
    WriteRequest _req;
117
    std::unique_ptr<BaseRowsetBuilder> _rowset_builder;
118
    std::shared_ptr<MemTableWriter> _memtable_writer;
119
120
    // total rows num written by DeltaWriter
121
    std::atomic<int64_t> _total_received_rows = 0;
122
123
    RuntimeProfile* _profile = nullptr;
124
    RuntimeProfile::Counter* _close_wait_timer = nullptr;
125
    RuntimeProfile::Counter* _wait_flush_limit_timer = nullptr;
126
127
    MonotonicStopWatch _lock_watch;
128
};
129
130
// `StorageEngine` mixin for `BaseDeltaWriter`
131
class DeltaWriter final : public BaseDeltaWriter {
132
public:
133
    DeltaWriter(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile,
134
                const UniqueId& load_id);
135
    DeltaWriter(StorageEngine& engine, const WriteRequest& group_build_req,
136
                const WriteRequest& sub_data_req, const WriteRequest& sub_row_binlog_req,
137
                RuntimeProfile* profile, const UniqueId& load_id);
138
139
    ~DeltaWriter() override;
140
141
    Status write(const Block* block, const TabletAddRowsPayload& rows,
142
                 bool* memtable_flushed = nullptr) override;
143
144
    Status close() override;
145
146
    Status flush_memtable_async() override;
147
148
    Status cancel_with_status(const Status& st) override;
149
150
    Status build_rowset() override;
151
152
    Status commit_txn();
153
154
private:
155
    void _init_profile(RuntimeProfile* profile) override;
156
157
    std::mutex _lock;
158
159
    StorageEngine& _engine;
160
161
    RuntimeProfile::Counter* _commit_txn_timer = nullptr;
162
};
163
164
} // namespace doris