Coverage Report

Created: 2026-06-25 00:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/channel/tablets_channel.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 <glog/logging.h>
21
22
#include <atomic>
23
#include <cstdint>
24
#include <mutex>
25
#include <ostream>
26
#include <shared_mutex>
27
#include <string>
28
#include <unordered_map>
29
#include <unordered_set>
30
#include <vector>
31
32
#include "common/status.h"
33
#include "core/custom_allocator.h"
34
#include "load/delta_writer/delta_writer_context.h"
35
#include "runtime/runtime_profile.h"
36
#include "util/bitmap.h"
37
#include "util/uid_util.h"
38
39
namespace google::protobuf {
40
template <typename Element>
41
class RepeatedField;
42
template <typename Key, typename T>
43
class Map;
44
template <typename T>
45
class RepeatedPtrField;
46
} // namespace google::protobuf
47
48
namespace doris {
49
class PSlaveTabletNodes;
50
class PSuccessSlaveTabletNodeIds;
51
class PTabletError;
52
class PTabletInfo;
53
class PTabletWriterOpenRequest;
54
class PTabletWriterOpenResult;
55
class PTabletWriterAddBlockRequest;
56
class PTabletWriterAddBlockResult;
57
class PUniqueId;
58
class TupleDescriptor;
59
class OpenPartitionRequest;
60
class StorageEngine;
61
62
struct TabletsChannelKey {
63
    UniqueId id;
64
    int64_t index_id;
65
66
0
    TabletsChannelKey(const PUniqueId& pid, int64_t index_id_) : id(pid), index_id(index_id_) {}
67
68
0
    ~TabletsChannelKey() noexcept = default;
69
70
0
    bool operator==(const TabletsChannelKey& rhs) const noexcept {
71
0
        return index_id == rhs.index_id && id == rhs.id;
72
0
    }
73
74
    std::string to_string() const;
75
};
76
77
std::ostream& operator<<(std::ostream& os, const TabletsChannelKey& key);
78
79
class BaseDeltaWriter;
80
class MemTableWriter;
81
class OlapTableSchemaParam;
82
class LoadChannel;
83
struct WriteRequest;
84
85
// Write channel for a particular (load, index).
86
class BaseTabletsChannel {
87
public:
88
    BaseTabletsChannel(const TabletsChannelKey& key, const UniqueId& load_id, bool is_high_priority,
89
                       RuntimeProfile* profile);
90
91
    virtual ~BaseTabletsChannel();
92
93
    Status open(const PTabletWriterOpenRequest& request);
94
    // open + open writers
95
    Status incremental_open(const PTabletWriterOpenRequest& params);
96
97
    virtual std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) = 0;
98
99
    // no-op when this channel has been closed or cancelled
100
    virtual Status add_batch(const PTabletWriterAddBlockRequest& request,
101
                             PTabletWriterAddBlockResult* response) = 0;
102
103
    // Mark sender with 'sender_id' as closed.
104
    // If all senders are closed, close this channel, set '*finished' to true, update 'tablet_vec'
105
    // to include all tablets written in this channel.
106
    // no-op when this channel has been closed or cancelled
107
    virtual Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
108
                         PTabletWriterAddBlockResult* res, bool* finished) = 0;
109
110
    // no-op when this channel has been closed or cancelled
111
    virtual Status cancel();
112
113
    void refresh_profile();
114
115
0
    size_t total_received_rows() const { return _total_received_rows; }
116
117
0
    size_t num_rows_filtered() const { return _num_rows_filtered; }
118
119
    // means this tablets in this BE is incremental opened partitions.
120
0
    bool is_incremental_channel() const { return _open_by_incremental; }
121
122
0
    bool is_finished() const { return _state == kFinished; }
123
124
protected:
125
    Status _write_block_data(const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
126
                             std::unordered_map<int64_t, TabletAddRowsPayload>& tablet_to_rows,
127
                             PTabletWriterAddBlockResult* response);
128
129
    Status _get_current_seq(int64_t& cur_seq, const PTabletWriterAddBlockRequest& request);
130
131
    // open all writer
132
    Status _open_all_writers(const PTabletWriterOpenRequest& request);
133
134
    void _add_broken_tablet(int64_t tablet_id);
135
    // thread-unsafe, add a shared lock for `_tablet_writers_lock` if needed
136
    bool _is_broken_tablet(int64_t tablet_id) const;
137
    void _add_error_tablet(google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors,
138
                           int64_t tablet_id, Status error) const;
139
    void _build_tablet_to_rows(
140
            const PTabletWriterAddBlockRequest& request,
141
            std::unordered_map<int64_t /* tablet_id */, TabletAddRowsPayload>* tablet_to_rows);
142
    virtual void _init_profile(RuntimeProfile* profile);
143
144
    // id of this load channel
145
    TabletsChannelKey _key;
146
147
    // protect _state change. open and close. when add_batch finished, lock to change _next_seqs also
148
    std::mutex _lock;
149
    enum State {
150
        kInitialized,
151
        kOpened,
152
        kFinished // closed or cancelled
153
    };
154
    State _state;
155
156
    UniqueId _load_id;
157
158
    // initialized in open function
159
    int64_t _txn_id = -1;
160
    int64_t _index_id = -1;
161
    std::shared_ptr<OlapTableSchemaParam> _schema;
162
    TupleDescriptor* _tuple_desc = nullptr;
163
    bool _open_by_incremental = false;
164
165
    // next sequence we expect
166
    std::set<int32_t> _recieved_senders;
167
    int _num_remaining_senders = 0;
168
    std::vector<int64_t> _next_seqs;
169
    Bitmap _closed_senders;
170
    // status to return when operate on an already closed/cancelled channel
171
    // currently it's OK.
172
    Status _close_status;
173
174
    // tablet_id -> TabletChannel. it will only be changed in open() or inc_open()
175
    std::unordered_map<int64_t, std::unique_ptr<BaseDeltaWriter>> _tablet_writers;
176
    // protect _tablet_writers
177
    std::mutex _tablet_writers_lock;
178
    // broken tablet ids.
179
    // If a tablet write fails, it's id will be added to this set.
180
    // So that following batch will not handle this tablet anymore.
181
    std::unordered_set<int64_t> _broken_tablets;
182
183
    std::shared_mutex _broken_tablets_lock;
184
185
    std::unordered_set<int64_t> _reducing_tablets;
186
187
    std::unordered_set<int64_t> _partition_ids;
188
189
    static std::atomic<uint64_t> _s_tablet_writer_count;
190
191
    bool _is_high_priority = false;
192
193
    RuntimeProfile* _profile = nullptr;
194
    RuntimeProfile::Counter* _add_batch_number_counter = nullptr;
195
    RuntimeProfile::HighWaterMarkCounter* _memory_usage_counter = nullptr;
196
    RuntimeProfile::HighWaterMarkCounter* _write_memory_usage_counter = nullptr;
197
    RuntimeProfile::HighWaterMarkCounter* _flush_memory_usage_counter = nullptr;
198
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_memory_usage_counter = nullptr;
199
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_write_memory_usage_counter = nullptr;
200
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_flush_memory_usage_counter = nullptr;
201
    RuntimeProfile::Counter* _add_batch_timer = nullptr;
202
    RuntimeProfile::Counter* _write_block_timer = nullptr;
203
    RuntimeProfile::Counter* _incremental_open_timer = nullptr;
204
205
    // record rows received and filtered
206
    size_t _total_received_rows = 0;
207
    size_t _num_rows_filtered = 0;
208
};
209
210
class DeltaWriter;
211
212
// `StorageEngine` mixin for `BaseTabletsChannel`
213
class TabletsChannel final : public BaseTabletsChannel {
214
public:
215
    TabletsChannel(StorageEngine& engine, const TabletsChannelKey& key, const UniqueId& load_id,
216
                   bool is_high_priority, RuntimeProfile* profile);
217
218
    ~TabletsChannel() override;
219
220
    std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) override;
221
222
    Status add_batch(const PTabletWriterAddBlockRequest& request,
223
                     PTabletWriterAddBlockResult* response) override;
224
225
    Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
226
                 PTabletWriterAddBlockResult* res, bool* finished) override;
227
228
    Status cancel() override;
229
230
private:
231
    void _init_profile(RuntimeProfile* profile) override;
232
233
    // deal with DeltaWriter commit_txn(), add tablet to list for return.
234
    void _commit_txn(DeltaWriter* writer, const PTabletWriterAddBlockRequest& req,
235
                     PTabletWriterAddBlockResult* res);
236
237
    StorageEngine& _engine;
238
    bool _write_single_replica = false;
239
    RuntimeProfile::Counter* _slave_replica_timer = nullptr;
240
};
241
242
} // namespace doris