Coverage Report

Created: 2026-08-13 04:14

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 "exec/sink/vtablet_finder.h"
35
#include "load/channel/adaptive_random_bucket_state.h"
36
#include "load/delta_writer/delta_writer_context.h"
37
#include "runtime/runtime_profile.h"
38
#include "util/bitmap.h"
39
#include "util/uid_util.h"
40
41
namespace google::protobuf {
42
template <typename Element>
43
class RepeatedField;
44
template <typename Key, typename T>
45
class Map;
46
template <typename T>
47
class RepeatedPtrField;
48
} // namespace google::protobuf
49
50
namespace doris {
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
33.3k
    TabletsChannelKey(const PUniqueId& pid, int64_t index_id_) : id(pid), index_id(index_id_) {}
67
68
66.6k
    ~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
33.3k
    size_t total_received_rows() const { return _total_received_rows; }
116
117
33.3k
    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 _init_adaptive_random_bucket_state(const PTabletWriterOpenRequest& request);
126
    Status _write_block_data(const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
127
                             std::unordered_map<int64_t, TabletAddRowsPayload>& tablet_to_rows,
128
                             PTabletWriterAddBlockResult* response);
129
    Status _write_block_data_for_adaptive_random_bucket(
130
            const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
131
            std::unordered_map<int64_t, DorisVector<uint32_t>>& partition_to_rowidxs,
132
            PTabletWriterAddBlockResult* response);
133
    virtual Status _prepare_adaptive_random_bucket_writer(BaseDeltaWriter* writer);
134
    Status _build_partition_to_rowidxs_for_adaptive_random_bucket(
135
            const PTabletWriterAddBlockRequest& request,
136
            std::unordered_map<int64_t, DorisVector<uint32_t>>* partition_to_rowidxs);
137
    std::shared_ptr<std::mutex> _get_partition_route_lock(int64_t partition_id);
138
139
    Status _get_current_seq(int64_t& cur_seq, const PTabletWriterAddBlockRequest& request);
140
141
    // open all writer
142
    Status _open_all_writers(const PTabletWriterOpenRequest& request);
143
144
    void _add_broken_tablet(int64_t tablet_id);
145
    // thread-unsafe, add a shared lock for `_tablet_writers_lock` if needed
146
    bool _is_broken_tablet(int64_t tablet_id) const;
147
    void _add_error_tablet(google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors,
148
                           int64_t tablet_id, Status error) const;
149
    void _build_tablet_to_rows(
150
            const PTabletWriterAddBlockRequest& request,
151
            std::unordered_map<int64_t /* tablet_id */, TabletAddRowsPayload>* tablet_to_rows);
152
    virtual void _init_profile(RuntimeProfile* profile);
153
154
    // id of this load channel
155
    TabletsChannelKey _key;
156
157
    // protect _state change. open and close. when add_batch finished, lock to change _next_seqs also
158
    std::mutex _lock;
159
    enum State {
160
        kInitialized,
161
        kOpened,
162
        kFinished // closed or cancelled
163
    };
164
    State _state;
165
166
    UniqueId _load_id;
167
168
    // initialized in open function
169
    int64_t _txn_id = -1;
170
    int64_t _index_id = -1;
171
    std::shared_ptr<OlapTableSchemaParam> _schema;
172
    TupleDescriptor* _tuple_desc = nullptr;
173
    bool _open_by_incremental = false;
174
175
    // next sequence we expect
176
    std::set<int32_t> _recieved_senders;
177
    int _num_remaining_senders = 0;
178
    std::vector<int64_t> _next_seqs;
179
    Bitmap _closed_senders;
180
    // status to return when operate on an already closed/cancelled channel
181
    // currently it's OK.
182
    Status _close_status;
183
184
    // tablet_id -> TabletChannel. it will only be changed in open() or inc_open()
185
    std::unordered_map<int64_t, std::unique_ptr<BaseDeltaWriter>> _tablet_writers;
186
    // protect _tablet_writers
187
    std::mutex _tablet_writers_lock;
188
    // broken tablet ids.
189
    // If a tablet write fails, it's id will be added to this set.
190
    // So that following batch will not handle this tablet anymore.
191
    std::unordered_set<int64_t> _broken_tablets;
192
193
    std::shared_mutex _broken_tablets_lock;
194
195
    std::unordered_set<int64_t> _reducing_tablets;
196
197
    std::unordered_set<int64_t> _partition_ids;
198
    std::shared_ptr<AdaptiveRandomBucketState> _adaptive_random_bucket_state;
199
    // Protects the route-lock map. Each entry serializes current-tablet selection, write,
200
    // and rotation for one partition so all senders on this BE share one current bucket.
201
    std::mutex _partition_route_locks_lock;
202
    std::unordered_map<int64_t, std::shared_ptr<std::mutex>> _partition_route_locks;
203
204
    static std::atomic<uint64_t> _s_tablet_writer_count;
205
206
    bool _is_high_priority = false;
207
208
    RuntimeProfile* _profile = nullptr;
209
    RuntimeProfile::Counter* _add_batch_number_counter = nullptr;
210
    RuntimeProfile::HighWaterMarkCounter* _memory_usage_counter = nullptr;
211
    RuntimeProfile::HighWaterMarkCounter* _write_memory_usage_counter = nullptr;
212
    RuntimeProfile::HighWaterMarkCounter* _flush_memory_usage_counter = nullptr;
213
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_memory_usage_counter = nullptr;
214
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_write_memory_usage_counter = nullptr;
215
    RuntimeProfile::HighWaterMarkCounter* _max_tablet_flush_memory_usage_counter = nullptr;
216
    RuntimeProfile::Counter* _add_batch_timer = nullptr;
217
    RuntimeProfile::Counter* _write_block_timer = nullptr;
218
    RuntimeProfile::Counter* _incremental_open_timer = nullptr;
219
220
    // record rows received and filtered
221
    size_t _total_received_rows = 0;
222
    size_t _num_rows_filtered = 0;
223
};
224
225
class DeltaWriter;
226
227
// `StorageEngine` mixin for `BaseTabletsChannel`
228
class TabletsChannel final : public BaseTabletsChannel {
229
public:
230
    TabletsChannel(StorageEngine& engine, const TabletsChannelKey& key, const UniqueId& load_id,
231
                   bool is_high_priority, RuntimeProfile* profile);
232
233
    ~TabletsChannel() override;
234
235
    std::unique_ptr<BaseDeltaWriter> create_delta_writer(const WriteRequest& request) override;
236
237
    Status add_batch(const PTabletWriterAddBlockRequest& request,
238
                     PTabletWriterAddBlockResult* response) override;
239
240
    Status close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
241
                 PTabletWriterAddBlockResult* res, bool* finished) override;
242
243
private:
244
    // deal with DeltaWriter commit_txn(), add tablet to list for return.
245
    void _commit_txn(DeltaWriter* writer, PTabletWriterAddBlockResult* res);
246
247
    StorageEngine& _engine;
248
};
249
250
} // namespace doris