Coverage Report

Created: 2026-08-14 13:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/channel/tablets_channel.cpp
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
#include "load/channel/tablets_channel.h"
19
20
#include <bvar/bvar.h>
21
#include <fmt/format.h>
22
#include <gen_cpp/internal_service.pb.h>
23
#include <gen_cpp/types.pb.h>
24
25
#include <ctime>
26
27
#include "common/compiler_util.h" // IWYU pragma: keep
28
#include "common/status.h"
29
// IWYU pragma: no_include <bits/chrono.h>
30
#include <chrono> // IWYU pragma: keep
31
#include <initializer_list>
32
#include <optional>
33
#include <set>
34
#include <thread>
35
#include <utility>
36
37
#ifdef DEBUG
38
#include <unordered_set>
39
#endif
40
41
#include "common/logging.h"
42
#include "common/metrics/doris_metrics.h"
43
#include "common/metrics/metrics.h"
44
#include "core/block/block.h"
45
#include "load/channel/load_channel.h"
46
#include "load/delta_writer/delta_writer.h"
47
#include "storage/storage_engine.h"
48
#include "storage/tablet/tablet_manager.h"
49
#include "storage/tablet_info.h"
50
#include "storage/txn/txn_manager.h"
51
#include "util/defer_op.h"
52
53
namespace doris {
54
class SlotDescriptor;
55
56
bvar::Adder<int64_t> g_tablets_channel_send_data_allocated_size(
57
        "tablets_channel_send_data_allocated_size");
58
59
DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(tablet_writer_count, MetricUnit::NOUNIT);
60
61
std::atomic<uint64_t> BaseTabletsChannel::_s_tablet_writer_count;
62
63
BaseTabletsChannel::BaseTabletsChannel(const TabletsChannelKey& key, const UniqueId& load_id,
64
                                       bool is_high_priority, RuntimeProfile* profile)
65
33.1k
        : _key(key),
66
33.1k
          _state(kInitialized),
67
33.1k
          _load_id(load_id),
68
33.1k
          _closed_senders(64),
69
33.1k
          _is_high_priority(is_high_priority) {
70
33.1k
    static std::once_flag once_flag;
71
33.1k
    if (profile != nullptr) {
72
8
        _init_profile(profile);
73
8
    }
74
33.1k
    std::call_once(once_flag, [] {
75
5
        REGISTER_HOOK_METRIC(tablet_writer_count, [&]() { return _s_tablet_writer_count.load(); });
76
5
    });
77
33.1k
}
78
79
TabletsChannel::TabletsChannel(StorageEngine& engine, const TabletsChannelKey& key,
80
                               const UniqueId& load_id, bool is_high_priority,
81
                               RuntimeProfile* profile)
82
3.17k
        : BaseTabletsChannel(key, load_id, is_high_priority, profile), _engine(engine) {}
83
84
33.1k
BaseTabletsChannel::~BaseTabletsChannel() {
85
33.1k
    _s_tablet_writer_count -= _tablet_writers.size();
86
33.1k
}
87
88
TabletsChannel::~TabletsChannel() = default;
89
90
Status BaseTabletsChannel::_get_current_seq(int64_t& cur_seq,
91
35.6k
                                            const PTabletWriterAddBlockRequest& request) {
92
35.6k
    std::lock_guard<std::mutex> l(_lock);
93
35.6k
    if (_state != kOpened) {
94
0
        return _state == kFinished ? _close_status
95
0
                                   : Status::InternalError("TabletsChannel {} state: {}",
96
0
                                                           _key.to_string(), _state);
97
0
    }
98
35.6k
    cur_seq = _next_seqs[request.sender_id()];
99
    // check packet
100
35.6k
    if (request.packet_seq() > cur_seq) {
101
0
        LOG(WARNING) << "lost data packet, expect_seq=" << cur_seq
102
0
                     << ", recept_seq=" << request.packet_seq();
103
0
        return Status::InternalError("lost data packet");
104
0
    }
105
35.6k
    return Status::OK();
106
35.6k
}
107
108
8
void BaseTabletsChannel::_init_profile(RuntimeProfile* profile) {
109
8
    DCHECK(profile != nullptr);
110
8
    _profile =
111
8
            profile->create_child(fmt::format("TabletsChannel {}", _key.to_string()), true, true);
112
8
    _add_batch_number_counter = ADD_COUNTER(_profile, "NumberBatchAdded", TUnit::UNIT);
113
114
8
    auto* memory_usage = _profile->create_child("PeakMemoryUsage", true, true);
115
8
    _add_batch_timer = ADD_TIMER(_profile, "AddBatchTime");
116
8
    _write_block_timer = ADD_TIMER(_profile, "WriteBlockTime");
117
8
    _incremental_open_timer = ADD_TIMER(_profile, "IncrementalOpenTabletTime");
118
8
    _memory_usage_counter = memory_usage->AddHighWaterMarkCounter("Total", TUnit::BYTES);
119
8
    _write_memory_usage_counter = memory_usage->AddHighWaterMarkCounter("Write", TUnit::BYTES);
120
8
    _flush_memory_usage_counter = memory_usage->AddHighWaterMarkCounter("Flush", TUnit::BYTES);
121
8
    _max_tablet_memory_usage_counter =
122
8
            memory_usage->AddHighWaterMarkCounter("MaxTablet", TUnit::BYTES);
123
8
    _max_tablet_write_memory_usage_counter =
124
8
            memory_usage->AddHighWaterMarkCounter("MaxTabletWrite", TUnit::BYTES);
125
8
    _max_tablet_flush_memory_usage_counter =
126
8
            memory_usage->AddHighWaterMarkCounter("MaxTabletFlush", TUnit::BYTES);
127
8
}
128
129
48.9k
Status BaseTabletsChannel::open(const PTabletWriterOpenRequest& request) {
130
48.9k
    std::lock_guard<std::mutex> l(_lock);
131
    // if _state is kOpened, it's a normal case, already open by other sender
132
    // if _state is kFinished, already cancelled by other sender
133
48.9k
    if (_state == kOpened) {
134
15.7k
        RETURN_IF_ERROR(_init_adaptive_random_bucket_state(request));
135
15.7k
        return Status::OK();
136
15.7k
    }
137
33.1k
    if (_state == kFinished) {
138
0
        return Status::OK();
139
0
    }
140
33.1k
    _txn_id = request.txn_id();
141
33.1k
    _index_id = request.index_id();
142
33.1k
    _schema = std::make_shared<OlapTableSchemaParam>();
143
33.1k
    RETURN_IF_ERROR(_schema->init(request.schema()));
144
33.1k
    _tuple_desc = _schema->tuple_desc();
145
33.1k
    int max_sender = request.num_senders();
146
    /*
147
     * a tablets channel in reciever is related to a bulk of VNodeChannel of sender. each instance one or none.
148
     * there are two possibilities:
149
     *  1. there's partitions originally broadcasted by FE. so all sender(instance) know it at start. and open() will be 
150
     *     called directly, not by incremental_open(). and after _state changes to kOpened. _open_by_incremental will never 
151
     *     be true. in this case, _num_remaining_senders will keep same with senders number. when all sender sent close rpc,
152
     *     the tablets channel will close. and if for auto partition table, these channel's closing will hang on reciever and
153
     *     return together to avoid close-then-incremental-open problem.
154
     *  2. this tablets channel is opened by incremental_open of sender's sink node. so only this sender will know this partition
155
     *     (this TabletsChannel) at that time. and we are not sure how many sender will know in the end. it depends on data
156
     *     distribution. in this situation open() is called by incremental_open() at first time. so _open_by_incremental is true.
157
     *     then _num_remaining_senders will not be set here. but inc every time when incremental_open() called. so it's dynamic
158
     *     and also need same number of senders' close to close. but will not hang.
159
     */
160
33.1k
    if (_open_by_incremental) {
161
0
        DCHECK(_num_remaining_senders == 0) << _num_remaining_senders;
162
33.1k
    } else {
163
33.1k
        _num_remaining_senders = max_sender;
164
33.1k
    }
165
33.1k
    LOG(INFO) << fmt::format(
166
33.1k
            "open tablets channel {}, tablets num: {} timeout(s): {}, init senders {} with "
167
33.1k
            "incremental {}",
168
33.1k
            _key.to_string(), request.tablets().size(), request.load_channel_timeout_s(),
169
33.1k
            _num_remaining_senders, _open_by_incremental ? "on" : "off");
170
    // just use max_sender no matter incremental or not cuz we dont know how many senders will open.
171
33.1k
    _next_seqs.resize(max_sender, 0);
172
33.1k
    _closed_senders.Reset(max_sender);
173
174
33.1k
    RETURN_IF_ERROR(_open_all_writers(request));
175
33.1k
    RETURN_IF_ERROR(_init_adaptive_random_bucket_state(request));
176
177
33.1k
    _state = kOpened;
178
33.1k
    return Status::OK();
179
33.1k
}
180
181
186
Status BaseTabletsChannel::incremental_open(const PTabletWriterOpenRequest& params) {
182
186
    SCOPED_TIMER(_incremental_open_timer);
183
184
    // current node first opened by incremental open
185
186
    if (_state == kInitialized) {
186
0
        _open_by_incremental = true;
187
0
        RETURN_IF_ERROR(open(params));
188
0
    }
189
190
186
    std::lock_guard<std::mutex> l(_lock);
191
192
    // one sender may incremental_open many times. but only close one time. so dont count duplicately.
193
186
    if (_open_by_incremental) {
194
0
        if (params.has_sender_id() && !_recieved_senders.contains(params.sender_id())) {
195
0
            _recieved_senders.insert(params.sender_id());
196
0
            _num_remaining_senders++;
197
0
        } else if (!params.has_sender_id()) { // for compatible
198
0
            _num_remaining_senders++;
199
0
        }
200
0
        VLOG_DEBUG << fmt::format("txn {}: TabletsChannel {} inc senders to {}", _txn_id, _index_id,
201
0
                                  _num_remaining_senders);
202
0
    }
203
204
186
    std::vector<SlotDescriptor*>* index_slots = nullptr;
205
186
    int32_t schema_hash = 0;
206
207
186
    for (const auto& index : _schema->indexes()) {
208
186
        if (index->index_id == _index_id) {
209
186
            index_slots = &index->slots;
210
186
            schema_hash = index->schema_hash;
211
186
            break;
212
186
        }
213
186
    }
214
186
    if (index_slots == nullptr) {
215
0
        return Status::InternalError("unknown index id, key={}", _key.to_string());
216
0
    }
217
    // update tablets
218
186
    size_t incremental_tablet_num = 0;
219
186
    std::stringstream ss;
220
186
    ss << "LocalTabletsChannel txn_id: " << _txn_id << " load_id: " << print_id(params.id())
221
186
       << " incremental open delta writer: ";
222
223
    // every change will hold _lock. this find in under _lock too. so no need _tablet_writers_lock again.
224
6.66k
    for (const auto& tablet : params.tablets()) {
225
6.66k
        if (_tablet_writers.find(tablet.tablet_id()) != _tablet_writers.end()) {
226
96
            continue;
227
96
        }
228
6.56k
        incremental_tablet_num++;
229
230
6.56k
        WriteRequest wrequest;
231
6.56k
        wrequest.index_id = params.index_id();
232
6.56k
        wrequest.tablet_id = tablet.tablet_id();
233
6.56k
        wrequest.schema_hash = schema_hash;
234
6.56k
        wrequest.txn_id = _txn_id;
235
6.56k
        wrequest.partition_id = tablet.partition_id();
236
6.56k
        wrequest.load_id = params.id();
237
6.56k
        wrequest.tuple_desc = _tuple_desc;
238
6.56k
        wrequest.slots = index_slots;
239
6.56k
        wrequest.is_high_priority = _is_high_priority;
240
6.56k
        wrequest.table_schema_param = _schema;
241
6.56k
        wrequest.txn_expiration = params.txn_expiration(); // Required by CLOUD.
242
6.56k
        wrequest.write_file_cache = params.write_file_cache();
243
6.56k
        wrequest.storage_vault_id = params.storage_vault_id();
244
6.56k
        wrequest.enable_table_memtable_backpressure = params.is_adaptive_random_bucket();
245
6.56k
        if (tablet.has_binlog_tablet_id()) {
246
0
            wrequest.binlog_tablet_id = tablet.binlog_tablet_id();
247
0
        }
248
249
6.56k
        auto delta_writer = create_delta_writer(wrequest);
250
6.56k
        {
251
            // here we modify _tablet_writers. so need lock.
252
6.56k
            std::lock_guard<std::mutex> lt(_tablet_writers_lock);
253
6.56k
            _tablet_writers.emplace(tablet.tablet_id(), std::move(delta_writer));
254
6.56k
        }
255
256
6.56k
        ss << "[" << tablet.tablet_id() << "]";
257
6.56k
    }
258
259
186
    _s_tablet_writer_count += incremental_tablet_num;
260
186
    LOG(INFO) << ss.str();
261
186
    RETURN_IF_ERROR(_init_adaptive_random_bucket_state(params));
262
263
186
    _state = kOpened;
264
186
    return Status::OK();
265
186
}
266
267
Status BaseTabletsChannel::_init_adaptive_random_bucket_state(
268
49.1k
        const PTabletWriterOpenRequest& request) {
269
49.1k
    if (!request.is_adaptive_random_bucket() || request.random_bucket_partitions_size() == 0) {
270
39.5k
        return Status::OK();
271
39.5k
    }
272
9.57k
    if (_adaptive_random_bucket_state == nullptr) {
273
2.66k
        _adaptive_random_bucket_state = std::make_shared<AdaptiveRandomBucketState>(_load_id);
274
2.66k
    }
275
9.73k
    for (const auto& partition : request.random_bucket_partitions()) {
276
9.73k
        if (partition.ordered_tablet_ids_size() == 0) {
277
0
            return Status::InternalError(
278
0
                    "ordered_tablet_ids is empty for adaptive random bucket, load_id={}, "
279
0
                    "sender_id={}, partition_id={}",
280
0
                    print_id(_load_id), request.sender_id(), partition.partition_id());
281
0
        }
282
9.73k
        std::vector<int32_t> ordered_positions;
283
9.73k
        ordered_positions.reserve(partition.ordered_tablet_ids_size());
284
42.5k
        for (int i = 0; i < partition.ordered_tablet_ids_size(); ++i) {
285
32.7k
            ordered_positions.push_back(cast_set<int32_t>(i));
286
32.7k
        }
287
9.73k
        std::vector<int64_t> ordered_tablet_ids;
288
9.73k
        ordered_tablet_ids.reserve(partition.ordered_tablet_ids_size());
289
32.7k
        for (auto tablet_id : partition.ordered_tablet_ids()) {
290
32.7k
            ordered_tablet_ids.push_back(tablet_id);
291
32.7k
        }
292
9.73k
        RETURN_IF_ERROR(_adaptive_random_bucket_state->init_partition(
293
9.73k
                partition.partition_id(), ordered_tablet_ids, ordered_positions, 0));
294
9.73k
    }
295
9.57k
    return Status::OK();
296
9.57k
}
297
298
29.4k
std::unique_ptr<BaseDeltaWriter> TabletsChannel::create_delta_writer(const WriteRequest& request) {
299
29.4k
    DCHECK(request.write_req_type == WriteRequestType::DATA);
300
29.4k
    DCHECK(request.table_schema_param != nullptr);
301
302
    // whether to write binlog is decided by binlog_tablet_id: it is set only when this backend
303
    // also owns the binlog tablet that is paired with the base tablet.
304
29.4k
    if (request.binlog_tablet_id <= 0) {
305
29.4k
        return std::make_unique<DeltaWriter>(_engine, request, _profile, _load_id);
306
29.4k
    }
307
308
0
    int64_t row_binlog_index_id = 0;
309
0
    for (const auto* index_schema : request.table_schema_param->indexes()) {
310
0
        if (index_schema->index_id == request.index_id) {
311
0
            row_binlog_index_id = index_schema->row_binlog_id;
312
0
            break;
313
0
        }
314
0
    }
315
0
    DCHECK(row_binlog_index_id > 0);
316
317
0
    const auto* row_binlog_index_schema =
318
0
            request.table_schema_param->row_binlog_index_schema(row_binlog_index_id);
319
0
    DCHECK(row_binlog_index_schema != nullptr);
320
321
    // group_build_req is only for the group wrapper itself. It provides the group semantics and
322
    // metadata used by BaseDeltaWriter/GroupRowsetBuilder to expose tablet_id, txn_id,
323
    // partition_id, load_id and profile information, while concrete rowset builders use the
324
    // sub requests below.
325
0
    WriteRequest group_build_req = request;
326
0
    group_build_req.write_req_type = WriteRequestType::GROUP;
327
328
0
    WriteRequest sub_data_req = request;
329
0
    sub_data_req.write_req_type = WriteRequestType::DATA;
330
331
0
    WriteRequest sub_row_binlog_req = request;
332
0
    sub_row_binlog_req.write_req_type = WriteRequestType::ROW_BINLOG;
333
0
    sub_row_binlog_req.tablet_id = request.binlog_tablet_id;
334
0
    sub_row_binlog_req.index_id = row_binlog_index_schema->index_id;
335
0
    sub_row_binlog_req.schema_hash = row_binlog_index_schema->schema_hash;
336
337
0
    return std::make_unique<DeltaWriter>(_engine, group_build_req, sub_data_req, sub_row_binlog_req,
338
0
                                         _profile, _load_id);
339
29.4k
}
340
341
Status TabletsChannel::close(LoadChannel* parent, const PTabletWriterAddBlockRequest& req,
342
4.60k
                             PTabletWriterAddBlockResult* res, bool* finished) {
343
4.60k
    int sender_id = req.sender_id();
344
4.60k
    int64_t backend_id = req.backend_id();
345
4.60k
    const auto& partition_ids = req.partition_ids();
346
4.60k
    auto* tablet_errors = res->mutable_tablet_errors();
347
4.60k
    std::lock_guard<std::mutex> l(_lock);
348
4.60k
    if (_state == kFinished) {
349
0
        return _close_status;
350
0
    }
351
4.60k
    if (_closed_senders.Get(sender_id)) {
352
        // Double close from one sender, just return OK
353
0
        *finished = (_num_remaining_senders == 0);
354
0
        return _close_status;
355
0
    }
356
357
186k
    for (auto pid : partition_ids) {
358
186k
        _partition_ids.emplace(pid);
359
186k
    }
360
4.60k
    _closed_senders.Set(sender_id, true);
361
4.60k
    _num_remaining_senders--;
362
4.60k
    *finished = (_num_remaining_senders == 0);
363
364
4.60k
    LOG(INFO) << fmt::format(
365
4.60k
            "txn {}: close tablets channel of index {} , sender id: {}, backend {}, remain "
366
4.60k
            "senders: {}",
367
4.60k
            _txn_id, _index_id, sender_id, backend_id, _num_remaining_senders);
368
369
4.60k
    if (!*finished) {
370
1.42k
        return Status::OK();
371
1.42k
    }
372
373
3.17k
    _state = kFinished;
374
    // All senders are closed
375
    // 1. close all delta writers
376
3.17k
    std::set<DeltaWriter*> need_wait_writers;
377
    // under _lock. no need _tablet_writers_lock again.
378
29.4k
    for (auto&& [tablet_id, writer] : _tablet_writers) {
379
29.4k
        if (_partition_ids.contains(writer->partition_id())) {
380
27.8k
            auto st = writer->close();
381
27.8k
            if (!st.ok()) {
382
0
                auto err_msg = fmt::format(
383
0
                        "close tablet writer failed, tablet_id={}, "
384
0
                        "transaction_id={}, err={}",
385
0
                        tablet_id, _txn_id, st.to_string());
386
0
                LOG(WARNING) << err_msg;
387
0
                PTabletError* tablet_error = tablet_errors->Add();
388
0
                tablet_error->set_tablet_id(tablet_id);
389
0
                tablet_error->set_msg(st.to_string());
390
                // just skip this tablet(writer) and continue to close others
391
0
                continue;
392
0
            }
393
            // tablet writer in `_broken_tablets` should not call `build_rowset` and
394
            // `commit_txn` method, after that, the publish-version task will success,
395
            // which can cause the replica inconsistency.
396
27.8k
            if (_is_broken_tablet(writer->tablet_id())) {
397
0
                LOG(WARNING) << "SHOULD NOT HAPPEN, tablet writer is broken but not cancelled"
398
0
                             << ", tablet_id=" << tablet_id << ", transaction_id=" << _txn_id;
399
0
                continue;
400
0
            }
401
27.8k
            need_wait_writers.insert(static_cast<DeltaWriter*>(writer.get()));
402
27.8k
        } else {
403
1.51k
            auto st = writer->cancel();
404
1.51k
            if (!st.ok()) {
405
0
                LOG(WARNING) << "cancel tablet writer failed, tablet_id=" << tablet_id
406
0
                             << ", transaction_id=" << _txn_id;
407
                // just skip this tablet(writer) and continue to close others
408
0
                continue;
409
0
            }
410
1.51k
            VLOG_PROGRESS << "cancel tablet writer successfully, tablet_id=" << tablet_id
411
0
                          << ", transaction_id=" << _txn_id;
412
1.51k
        }
413
29.4k
    }
414
415
    // 2. wait all writer finished flush.
416
27.8k
    for (auto* writer : need_wait_writers) {
417
27.8k
        RETURN_IF_ERROR((writer->wait_flush()));
418
27.8k
    }
419
420
    // 3. build rowset
421
31.0k
    for (auto it = need_wait_writers.begin(); it != need_wait_writers.end();) {
422
27.8k
        Status st = (*it)->build_rowset();
423
27.8k
        if (!st.ok()) {
424
0
            _add_error_tablet(tablet_errors, (*it)->tablet_id(), st);
425
0
            it = need_wait_writers.erase(it);
426
0
            continue;
427
0
        }
428
        // 3.1 calculate delete bitmap for Unique Key MoW tables
429
27.8k
        st = (*it)->submit_calc_delete_bitmap_task();
430
27.8k
        if (!st.ok()) {
431
0
            _add_error_tablet(tablet_errors, (*it)->tablet_id(), st);
432
0
            it = need_wait_writers.erase(it);
433
0
            continue;
434
0
        }
435
27.8k
        it++;
436
27.8k
    }
437
438
    // 4. wait for delete bitmap calculation complete if necessary
439
31.0k
    for (auto it = need_wait_writers.begin(); it != need_wait_writers.end();) {
440
27.8k
        Status st = (*it)->wait_calc_delete_bitmap();
441
27.8k
        if (!st.ok()) {
442
0
            _add_error_tablet(tablet_errors, (*it)->tablet_id(), st);
443
0
            it = need_wait_writers.erase(it);
444
0
            continue;
445
0
        }
446
27.8k
        it++;
447
27.8k
    }
448
449
    // 5. commit all writers
450
451
27.8k
    for (auto* writer : need_wait_writers) {
452
        // close may return failed, but no need to handle it here.
453
        // tablet_vec will only contains success tablet, and then let FE judge it.
454
27.8k
        _commit_txn(writer, res);
455
27.8k
    }
456
457
3.17k
    return Status::OK();
458
3.17k
}
459
460
27.8k
void TabletsChannel::_commit_txn(DeltaWriter* writer, PTabletWriterAddBlockResult* res) {
461
27.8k
    Status st = writer->commit_txn();
462
27.8k
    if (st.ok()) [[likely]] {
463
27.8k
        auto* tablet_vec = res->mutable_tablet_vec();
464
27.8k
        PTabletInfo* tablet_info = tablet_vec->Add();
465
27.8k
        tablet_info->set_tablet_id(writer->tablet_id());
466
        // unused required field.
467
27.8k
        tablet_info->set_schema_hash(0);
468
27.8k
        tablet_info->set_received_rows(writer->total_received_rows());
469
27.8k
        tablet_info->set_num_rows_filtered(writer->num_rows_filtered());
470
        // report the row binlog tablet as a normal tablet so FE advances its version.
471
27.8k
        if (writer->binlog_tablet_id() > 0) {
472
0
            PTabletInfo* binlog_tablet_info = tablet_vec->Add();
473
0
            binlog_tablet_info->set_tablet_id(writer->binlog_tablet_id());
474
0
            binlog_tablet_info->set_schema_hash(0);
475
0
            binlog_tablet_info->set_received_rows(writer->total_received_rows());
476
0
            binlog_tablet_info->set_num_rows_filtered(writer->num_rows_filtered());
477
0
        }
478
27.8k
        _total_received_rows += writer->total_received_rows();
479
27.8k
        _num_rows_filtered += writer->num_rows_filtered();
480
18.4E
    } else {
481
18.4E
        _add_error_tablet(res->mutable_tablet_errors(), writer->tablet_id(), st);
482
18.4E
    }
483
27.8k
}
484
485
void BaseTabletsChannel::_add_error_tablet(
486
        google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors, int64_t tablet_id,
487
0
        Status error) const {
488
0
    PTabletError* tablet_error = tablet_errors->Add();
489
0
    tablet_error->set_tablet_id(tablet_id);
490
0
    tablet_error->set_msg(error.to_string());
491
0
    VLOG_PROGRESS << "close wait failed tablet " << tablet_id << " transaction_id " << _txn_id
492
0
                  << "err msg " << error;
493
0
}
494
495
18
void BaseTabletsChannel::refresh_profile() {
496
18
    int64_t write_mem_usage = 0;
497
18
    int64_t flush_mem_usage = 0;
498
18
    int64_t max_tablet_mem_usage = 0;
499
18
    int64_t max_tablet_write_mem_usage = 0;
500
18
    int64_t max_tablet_flush_mem_usage = 0;
501
18
    {
502
18
        std::lock_guard<std::mutex> l(_tablet_writers_lock);
503
69
        for (auto&& [tablet_id, writer] : _tablet_writers) {
504
69
            int64_t write_mem = writer->mem_consumption(MemType::WRITE_FINISHED);
505
69
            write_mem_usage += write_mem;
506
69
            int64_t flush_mem = writer->mem_consumption(MemType::FLUSH);
507
69
            flush_mem_usage += flush_mem;
508
69
            if (write_mem > max_tablet_write_mem_usage) {
509
0
                max_tablet_write_mem_usage = write_mem;
510
0
            }
511
69
            if (flush_mem > max_tablet_flush_mem_usage) {
512
0
                max_tablet_flush_mem_usage = flush_mem;
513
0
            }
514
69
            if (write_mem + flush_mem > max_tablet_mem_usage) {
515
0
                max_tablet_mem_usage = write_mem + flush_mem;
516
0
            }
517
69
        }
518
18
    }
519
18
    COUNTER_SET(_memory_usage_counter, write_mem_usage + flush_mem_usage);
520
18
    COUNTER_SET(_write_memory_usage_counter, write_mem_usage);
521
18
    COUNTER_SET(_flush_memory_usage_counter, flush_mem_usage);
522
18
    COUNTER_SET(_max_tablet_memory_usage_counter, max_tablet_mem_usage);
523
18
    COUNTER_SET(_max_tablet_write_memory_usage_counter, max_tablet_write_mem_usage);
524
18
    COUNTER_SET(_max_tablet_flush_memory_usage_counter, max_tablet_flush_mem_usage);
525
18
}
526
527
33.1k
Status BaseTabletsChannel::_open_all_writers(const PTabletWriterOpenRequest& request) {
528
33.1k
    std::vector<SlotDescriptor*>* index_slots = nullptr;
529
33.1k
    int32_t schema_hash = 0;
530
37.1k
    for (const auto& index : _schema->indexes()) {
531
37.1k
        if (index->index_id == _index_id) {
532
33.1k
            index_slots = &index->slots;
533
33.1k
            schema_hash = index->schema_hash;
534
33.1k
            break;
535
33.1k
        }
536
37.1k
    }
537
33.1k
    if (index_slots == nullptr) {
538
0
        return Status::InternalError("unknown index id, key={}", _key.to_string());
539
0
    }
540
541
#ifdef DEBUG
542
    // check: tablet ids should be unique
543
    {
544
        std::unordered_set<int64_t> tablet_ids;
545
        for (const auto& tablet : request.tablets()) {
546
            CHECK(tablet_ids.count(tablet.tablet_id()) == 0)
547
                    << "found duplicate tablet id: " << tablet.tablet_id();
548
            tablet_ids.insert(tablet.tablet_id());
549
        }
550
    }
551
#endif
552
553
33.1k
    int tablet_cnt = 0;
554
    // under _lock. no need _tablet_writers_lock again.
555
294k
    for (const auto& tablet : request.tablets()) {
556
294k
        if (_tablet_writers.find(tablet.tablet_id()) != _tablet_writers.end()) {
557
0
            continue;
558
0
        }
559
294k
        tablet_cnt++;
560
294k
        WriteRequest wrequest {
561
294k
                .tablet_id = tablet.tablet_id(),
562
294k
                .schema_hash = schema_hash,
563
294k
                .txn_id = _txn_id,
564
294k
                .txn_expiration = request.txn_expiration(), // Required by CLOUD.
565
294k
                .index_id = request.index_id(),
566
294k
                .partition_id = tablet.partition_id(),
567
294k
                .load_id = request.id(),
568
294k
                .tuple_desc = _tuple_desc,
569
294k
                .slots = index_slots,
570
294k
                .table_schema_param = _schema,
571
294k
                .is_high_priority = _is_high_priority,
572
294k
                .write_file_cache = request.write_file_cache(),
573
294k
                .storage_vault_id = request.storage_vault_id(),
574
294k
                .enable_table_memtable_backpressure = request.is_adaptive_random_bucket(),
575
294k
        };
576
294k
        if (tablet.has_binlog_tablet_id()) {
577
0
            wrequest.binlog_tablet_id = tablet.binlog_tablet_id();
578
0
        }
579
580
294k
        auto delta_writer = create_delta_writer(wrequest);
581
294k
        {
582
294k
            std::lock_guard<std::mutex> l(_tablet_writers_lock);
583
294k
            _tablet_writers.emplace(tablet.tablet_id(), std::move(delta_writer));
584
294k
        }
585
294k
    }
586
33.1k
    _s_tablet_writer_count += _tablet_writers.size();
587
33.1k
    DCHECK_EQ(_tablet_writers.size(), tablet_cnt);
588
33.1k
    return Status::OK();
589
33.1k
}
590
591
118
Status BaseTabletsChannel::cancel() {
592
118
    std::lock_guard<std::mutex> l(_lock);
593
118
    if (_state == kFinished) {
594
84
        return _close_status;
595
84
    }
596
1.42k
    for (auto& it : _tablet_writers) {
597
1.42k
        static_cast<void>(it.second->cancel());
598
1.42k
    }
599
34
    _state = kFinished;
600
601
34
    return Status::OK();
602
118
}
603
604
33.1k
std::string TabletsChannelKey::to_string() const {
605
33.1k
    std::stringstream ss;
606
33.1k
    ss << *this;
607
33.1k
    return ss.str();
608
33.1k
}
609
610
77.4k
std::ostream& operator<<(std::ostream& os, const TabletsChannelKey& key) {
611
77.4k
    os << "(load_id=" << key.id << ", index_id=" << key.index_id << ")";
612
77.4k
    return os;
613
77.4k
}
614
615
Status BaseTabletsChannel::_write_block_data(
616
        const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
617
        std::unordered_map<int64_t, TabletAddRowsPayload>& tablet_to_rows,
618
31.3k
        PTabletWriterAddBlockResult* response) {
619
31.3k
    Block send_data;
620
31.3k
    [[maybe_unused]] size_t uncompressed_size = 0;
621
31.3k
    [[maybe_unused]] int64_t uncompressed_time = 0;
622
31.3k
    RETURN_IF_ERROR(send_data.deserialize(request.block(), &uncompressed_size, &uncompressed_time));
623
31.3k
    if (send_data.rows() != request.tablet_ids_size()) {
624
0
        return Status::InternalError(
625
0
                "invalid add block request row count, load_id={}, index_id={}, packet_seq={}, "
626
0
                "block_rows={}, tablet_ids_size={}",
627
0
                print_id(_load_id), _index_id, request.packet_seq(), send_data.rows(),
628
0
                request.tablet_ids_size());
629
0
    }
630
31.3k
    bool has_row_binlog_lsn = request.row_binlog_lsns_size() > 0;
631
31.3k
    if (has_row_binlog_lsn) {
632
0
        if (send_data.rows() != request.row_binlog_lsns_size()) {
633
0
            return Status::InternalError(
634
0
                    "invalid add block request row-binlog lsn count, load_id={}, index_id={}, "
635
0
                    "packet_seq={}, block_rows={}, row_binlog_lsns_size={}",
636
0
                    print_id(_load_id), _index_id, request.packet_seq(), send_data.rows(),
637
0
                    request.row_binlog_lsns_size());
638
0
        }
639
0
    }
640
641
31.3k
    g_tablets_channel_send_data_allocated_size << send_data.allocated_bytes();
642
31.3k
    Defer defer {
643
31.3k
            [&]() { g_tablets_channel_send_data_allocated_size << -send_data.allocated_bytes(); }};
644
645
31.3k
    auto write_tablet_data = [&](int64_t tablet_id,
646
125k
                                 std::function<Status(BaseDeltaWriter * writer)> write_func) {
647
125k
        google::protobuf::RepeatedPtrField<PTabletError>* tablet_errors =
648
125k
                response->mutable_tablet_errors();
649
650
        // add_batch may concurrency with inc_open but not under _lock.
651
        // so need to protect it with _tablet_writers_lock.
652
125k
        BaseDeltaWriter* tablet_writer = nullptr;
653
125k
        {
654
125k
            std::lock_guard<std::mutex> l(_tablet_writers_lock);
655
125k
            auto tablet_writer_it = _tablet_writers.find(tablet_id);
656
125k
            if (tablet_writer_it == _tablet_writers.end()) {
657
0
                return Status::InternalError("unknown tablet to append data, tablet={}", tablet_id);
658
0
            }
659
125k
            tablet_writer = tablet_writer_it->second.get();
660
125k
        }
661
662
0
        Status st = write_func(tablet_writer);
663
125k
        if (!st.ok()) {
664
0
            auto err_msg =
665
0
                    fmt::format("tablet writer write failed, tablet_id={}, txn_id={}, err={}",
666
0
                                tablet_id, _txn_id, st.to_string());
667
0
            LOG(WARNING) << err_msg;
668
0
            PTabletError* error = tablet_errors->Add();
669
0
            error->set_tablet_id(tablet_id);
670
0
            error->set_msg(err_msg);
671
0
            static_cast<void>(tablet_writer->cancel_with_status(st));
672
0
            _add_broken_tablet(tablet_id);
673
            // continue write to other tablet.
674
            // the error will return back to sender.
675
0
        }
676
125k
        return Status::OK();
677
125k
    };
678
679
31.3k
    SCOPED_TIMER(_write_block_timer);
680
31.3k
    auto* tablet_load_infos = response->mutable_tablet_load_rowset_num_infos();
681
125k
    for (const auto& tablet_to_rows_it : tablet_to_rows) {
682
125k
        bool memtable_flushed = false;
683
125k
        RETURN_IF_ERROR(write_tablet_data(tablet_to_rows_it.first, [&](BaseDeltaWriter* writer) {
684
125k
            return writer->write(&send_data, tablet_to_rows_it.second, &memtable_flushed);
685
125k
        }));
686
687
125k
        BaseDeltaWriter* tablet_writer = nullptr;
688
125k
        {
689
125k
            std::lock_guard<std::mutex> l(_tablet_writers_lock);
690
125k
            auto tablet_writer_it = _tablet_writers.find(tablet_to_rows_it.first);
691
125k
            if (tablet_writer_it != _tablet_writers.end()) {
692
125k
                tablet_writer = tablet_writer_it->second.get();
693
125k
            }
694
125k
        }
695
125k
        if (tablet_writer != nullptr) {
696
125k
            tablet_writer->set_tablet_load_rowset_num_info(tablet_load_infos);
697
125k
        }
698
125k
    }
699
700
31.3k
    {
701
31.3k
        std::lock_guard<std::mutex> l(_lock);
702
31.3k
        _next_seqs[request.sender_id()] = cur_seq + 1;
703
31.3k
    }
704
31.3k
    return Status::OK();
705
31.3k
}
706
707
4.39k
std::shared_ptr<std::mutex> BaseTabletsChannel::_get_partition_route_lock(int64_t partition_id) {
708
4.39k
    std::lock_guard<std::mutex> l(_partition_route_locks_lock);
709
4.39k
    auto& lock = _partition_route_locks[partition_id];
710
4.39k
    if (lock == nullptr) {
711
2.71k
        lock = std::make_shared<std::mutex>();
712
2.71k
    }
713
4.39k
    return lock;
714
4.39k
}
715
716
Status BaseTabletsChannel::_write_block_data_for_adaptive_random_bucket(
717
        const PTabletWriterAddBlockRequest& request, int64_t cur_seq,
718
        std::unordered_map<int64_t, DorisVector<uint32_t>>& partition_to_rowidxs,
719
4.28k
        PTabletWriterAddBlockResult* response) {
720
4.28k
    Block send_data;
721
4.28k
    [[maybe_unused]] size_t uncompressed_size = 0;
722
4.28k
    [[maybe_unused]] int64_t uncompressed_time = 0;
723
4.28k
    RETURN_IF_ERROR(send_data.deserialize(request.block(), &uncompressed_size, &uncompressed_time));
724
4.28k
    if (send_data.rows() != request.partition_ids_size()) {
725
0
        return Status::InternalError(
726
0
                "invalid adaptive random bucket add block request row count, load_id={}, "
727
0
                "index_id={}, packet_seq={}, block_rows={}, partition_ids_size={}",
728
0
                print_id(_load_id), _index_id, request.packet_seq(), send_data.rows(),
729
0
                request.partition_ids_size());
730
0
    }
731
732
4.28k
    {
733
4.28k
        std::lock_guard<std::mutex> l(_lock);
734
4.39k
        for (const auto& [partition_id, _] : partition_to_rowidxs) {
735
4.39k
            _partition_ids.emplace(partition_id);
736
4.39k
        }
737
4.28k
    }
738
739
4.28k
    g_tablets_channel_send_data_allocated_size << send_data.allocated_bytes();
740
4.28k
    Defer defer {
741
4.29k
            [&]() { g_tablets_channel_send_data_allocated_size << -send_data.allocated_bytes(); }};
742
743
4.28k
    auto* tablet_errors = response->mutable_tablet_errors();
744
4.28k
    auto* tablet_load_infos = response->mutable_tablet_load_rowset_num_infos();
745
746
4.28k
    auto write_partition_data = [&](int64_t partition_id,
747
4.39k
                                    const DorisVector<uint32_t>& row_idxs) -> Status {
748
4.39k
        auto partition_lock = _get_partition_route_lock(partition_id);
749
4.39k
        std::lock_guard<std::mutex> partition_guard(*partition_lock);
750
4.39k
        int64_t tablet_id = -1;
751
4.39k
        if (_adaptive_random_bucket_state == nullptr) {
752
0
            return Status::InternalError(
753
0
                    "adaptive random bucket state is not initialized, load_id={}, "
754
0
                    "index_id={}, packet_seq={}, partition_id={}",
755
0
                    print_id(_load_id), _index_id, request.packet_seq(), partition_id);
756
0
        }
757
4.39k
        tablet_id = _adaptive_random_bucket_state->current_tablet(partition_id);
758
4.39k
        if (tablet_id < 0) {
759
0
            return Status::InternalError(
760
0
                    "invalid current tablet for adaptive random bucket, load_id={}, "
761
0
                    "index_id={}, sender_id={}, packet_seq={}, partition_id={}",
762
0
                    print_id(_load_id), _index_id, request.sender_id(), request.packet_seq(),
763
0
                    partition_id);
764
0
        }
765
4.39k
        VLOG_DEBUG << "FIND_TABLET_RANDOM_BUCKET: route+write begin"
766
0
                   << ", load_id=" << _load_id << ", index_id=" << _index_id
767
0
                   << ", sender_id=" << request.sender_id()
768
0
                   << ", packet_seq=" << request.packet_seq() << ", partition_id=" << partition_id
769
0
                   << ", tablet_id=" << tablet_id << ", row_count=" << row_idxs.size();
770
771
4.39k
        {
772
4.39k
            std::shared_lock<std::shared_mutex> broken_rlock(_broken_tablets_lock);
773
4.39k
            if (_is_broken_tablet(tablet_id)) {
774
0
                return Status::InternalError(
775
0
                        "current tablet is broken for adaptive random bucket, load_id={}, "
776
0
                        "index_id={}, sender_id={}, packet_seq={}, partition_id={}, tablet_id={}",
777
0
                        print_id(_load_id), _index_id, request.sender_id(), request.packet_seq(),
778
0
                        partition_id, tablet_id);
779
0
            }
780
4.39k
        }
781
782
4.39k
        BaseDeltaWriter* tablet_writer = nullptr;
783
4.39k
        {
784
4.39k
            std::lock_guard<std::mutex> l(_tablet_writers_lock);
785
4.39k
            auto tablet_writer_it = _tablet_writers.find(tablet_id);
786
4.39k
            if (tablet_writer_it == _tablet_writers.end()) {
787
0
                return Status::InternalError("unknown tablet to append data, tablet={}", tablet_id);
788
0
            }
789
4.39k
            tablet_writer = tablet_writer_it->second.get();
790
4.39k
        }
791
4.39k
        RETURN_IF_ERROR(_prepare_adaptive_random_bucket_writer(tablet_writer));
792
793
4.39k
        TabletAddRowsPayload rows {.row_idxs = row_idxs};
794
4.39k
        if (request.row_binlog_lsns_size() > 0) {
795
0
            rows.row_binlog_lsns.reserve(row_idxs.size());
796
0
            for (auto row_idx : row_idxs) {
797
0
                rows.row_binlog_lsns.emplace_back(request.row_binlog_lsns(row_idx));
798
0
            }
799
0
        }
800
4.39k
        bool memtable_flushed = false;
801
4.39k
        Status st = tablet_writer->write(&send_data, rows, &memtable_flushed);
802
4.39k
        if (!st.ok()) {
803
0
            auto err_msg =
804
0
                    fmt::format("tablet writer write failed, tablet_id={}, txn_id={}, err={}",
805
0
                                tablet_id, _txn_id, st.to_string());
806
0
            LOG(WARNING) << err_msg;
807
0
            PTabletError* error = tablet_errors->Add();
808
0
            error->set_tablet_id(tablet_id);
809
0
            error->set_msg(err_msg);
810
0
            static_cast<void>(tablet_writer->cancel_with_status(st));
811
0
            _add_broken_tablet(tablet_id);
812
0
            return Status::OK();
813
0
        }
814
815
4.39k
        VLOG_DEBUG << "FIND_TABLET_RANDOM_BUCKET: route+write done"
816
0
                   << ", load_id=" << _load_id << ", index_id=" << _index_id
817
0
                   << ", sender_id=" << request.sender_id()
818
0
                   << ", packet_seq=" << request.packet_seq() << ", partition_id=" << partition_id
819
0
                   << ", tablet_id=" << tablet_id << ", row_count=" << row_idxs.size()
820
0
                   << ", memtable_flushed=" << memtable_flushed;
821
4.39k
        if (memtable_flushed) {
822
0
            _adaptive_random_bucket_state->rotate_by_tablet(partition_id, tablet_id);
823
0
        }
824
4.39k
        tablet_writer->set_tablet_load_rowset_num_info(tablet_load_infos);
825
4.39k
        return Status::OK();
826
4.39k
    };
827
828
4.28k
    SCOPED_TIMER(_write_block_timer);
829
4.39k
    for (const auto& [partition_id, row_idxs] : partition_to_rowidxs) {
830
4.39k
        RETURN_IF_ERROR(write_partition_data(partition_id, row_idxs));
831
4.39k
    }
832
833
4.28k
    {
834
4.28k
        std::lock_guard<std::mutex> l(_lock);
835
4.28k
        _next_seqs[request.sender_id()] = cur_seq + 1;
836
4.28k
    }
837
4.28k
    return Status::OK();
838
4.28k
}
839
840
0
Status BaseTabletsChannel::_prepare_adaptive_random_bucket_writer(BaseDeltaWriter*) {
841
0
    return Status::OK();
842
0
}
843
844
Status BaseTabletsChannel::_build_partition_to_rowidxs_for_adaptive_random_bucket(
845
        const PTabletWriterAddBlockRequest& request,
846
4.29k
        std::unordered_map<int64_t, DorisVector<uint32_t>>* partition_to_rowidxs) {
847
4.29k
    if (_adaptive_random_bucket_state == nullptr) {
848
0
        return Status::InternalError(
849
0
                "adaptive random bucket state is not initialized, load_id={}, index_id={}, "
850
0
                "packet_seq={}",
851
0
                print_id(_load_id), _index_id, request.packet_seq());
852
0
    }
853
4.29k
    if (request.partition_ids_size() == 0) {
854
0
        return Status::InternalError(
855
0
                "empty partition ids for adaptive random bucket add block, load_id={}, "
856
0
                "index_id={}, packet_seq={}",
857
0
                print_id(_load_id), _index_id, request.packet_seq());
858
0
    }
859
2.06M
    for (uint32_t i = 0; i < request.partition_ids_size(); ++i) {
860
2.06M
        int64_t partition_id = request.partition_ids(i);
861
2.06M
        auto it = partition_to_rowidxs->find(partition_id);
862
2.06M
        if (it == partition_to_rowidxs->end()) {
863
4.39k
            partition_to_rowidxs->emplace(partition_id, std::initializer_list<uint32_t> {i});
864
2.05M
        } else {
865
2.05M
            it->second.emplace_back(i);
866
2.05M
        }
867
2.06M
    }
868
4.29k
    return Status::OK();
869
4.29k
}
870
871
Status TabletsChannel::add_batch(const PTabletWriterAddBlockRequest& request,
872
3.05k
                                 PTabletWriterAddBlockResult* response) {
873
3.05k
    SCOPED_TIMER(_add_batch_timer);
874
3.05k
    int64_t cur_seq = 0;
875
3.05k
    if (_add_batch_number_counter) {
876
0
        _add_batch_number_counter->update(1);
877
0
    }
878
879
3.05k
    auto status = _get_current_seq(cur_seq, request);
880
3.05k
    if (UNLIKELY(!status.ok())) {
881
0
        return status;
882
0
    }
883
884
3.05k
    if (request.packet_seq() < cur_seq) {
885
0
        LOG(INFO) << "packet has already recept before, expect_seq=" << cur_seq
886
0
                  << ", recept_seq=" << request.packet_seq();
887
0
        return Status::OK();
888
0
    }
889
890
    // Adaptive random bucket add-block RPCs carry partition ids. The receiver maps rows to
891
    // the current tablet and advances its selected-tablet state after memtable flush.
892
3.05k
    if (request.is_adaptive_random_bucket()) {
893
0
        std::unordered_map<int64_t /* partition_id */, DorisVector<uint32_t> /* row index */>
894
0
                partition_to_rowidxs;
895
0
        RETURN_IF_ERROR(_build_partition_to_rowidxs_for_adaptive_random_bucket(
896
0
                request, &partition_to_rowidxs));
897
0
        return _write_block_data_for_adaptive_random_bucket(request, cur_seq, partition_to_rowidxs,
898
0
                                                            response);
899
0
    }
900
901
3.05k
    std::unordered_map<int64_t /* tablet_id */, TabletAddRowsPayload> tablet_to_rows;
902
3.05k
    _build_tablet_to_rows(request, &tablet_to_rows);
903
904
3.05k
    return _write_block_data(request, cur_seq, tablet_to_rows, response);
905
3.05k
}
906
907
0
void BaseTabletsChannel::_add_broken_tablet(int64_t tablet_id) {
908
0
    std::unique_lock<std::shared_mutex> wlock(_broken_tablets_lock);
909
0
    _broken_tablets.insert(tablet_id);
910
0
}
911
912
33.9M
bool BaseTabletsChannel::_is_broken_tablet(int64_t tablet_id) const {
913
33.9M
    return _broken_tablets.find(tablet_id) != _broken_tablets.end();
914
33.9M
}
915
916
void BaseTabletsChannel::_build_tablet_to_rows(
917
        const PTabletWriterAddBlockRequest& request,
918
31.3k
        std::unordered_map<int64_t, TabletAddRowsPayload>* tablet_to_rows) {
919
    // just add a coarse-grained read lock here rather than each time when visiting _broken_tablets
920
    // tests show that a relatively coarse-grained read lock here performs better under multicore scenario
921
    // see: https://github.com/apache/doris/pull/28552
922
31.3k
    std::shared_lock<std::shared_mutex> rlock(_broken_tablets_lock);
923
31.3k
    bool has_row_binlog_lsn = request.row_binlog_lsns_size() > 0;
924
31.3k
    if (request.is_single_tablet_block()) {
925
        // The cloud mode need the tablet ids to prepare rowsets.
926
0
        int64_t tablet_id = request.tablet_ids(0);
927
0
        auto& rows = (*tablet_to_rows)[tablet_id];
928
0
        rows.row_idxs.emplace_back(0);
929
0
        if (has_row_binlog_lsn) {
930
0
            rows.row_binlog_lsns.emplace_back(request.row_binlog_lsns(0));
931
0
        }
932
0
        return;
933
0
    }
934
33.8M
    for (uint32_t i = 0; i < request.tablet_ids_size(); ++i) {
935
33.8M
        int64_t tablet_id = request.tablet_ids(i);
936
33.8M
        if (_is_broken_tablet(tablet_id)) {
937
            // skip broken tablets
938
0
            VLOG_PROGRESS << "skip broken tablet tablet=" << tablet_id;
939
0
            continue;
940
0
        }
941
33.8M
        auto& rows = (*tablet_to_rows)[tablet_id];
942
33.8M
        rows.row_idxs.emplace_back(i);
943
33.8M
        if (has_row_binlog_lsn) {
944
0
            rows.row_binlog_lsns.emplace_back(request.row_binlog_lsns(i));
945
0
        }
946
33.8M
    }
947
31.3k
}
948
949
} // namespace doris